Professional Documents
Culture Documents
int i,a=1;
int n=Integer.parseInt(args[0]);
for(i=1;i<=n;i++)
a=a*i;
Output:-
int i,n;
n=sn.nextInt();
for(i=1;i<=10;i++)
Output:-
3. Write a program to check whether the given number is an
Armstrong number or not.
int k=0,a,temp;
int n= sn.nextInt();
temp=n;
while(n>0)
a=n%10;
n=n/10;
k=k+(a*a*a);
if(temp==k)
System.out.println("Armstrong number");
else
Output:-
int n,i;
n=sc.nextInt();
boolean flag=false;
for(i=2;i<=n/2;i++)
if(n%i==0)
flag=true;
break;
if(!flag)
}
else
Output:-
(i) 1
1 2
1 2 3
public class P1
System.out.println();
Output:-
5 (ii) *
**
***
**
*
Code:-
import java.util.Scanner;
public class P3
int r = sc.nextInt();
System.out.print(" ");
System.out.println();
System.out.print(" ");
System.out.println();
Output:-
int n,a=0,b=1,s;
n=sn.nextInt();
System.out.println(a+" ");
s= a+b;
a=b;
b=s;
Output:-
7. Write a program to sort n numbers in ascending order.
Code:-
import java.util.Scanner;
public class sort1
int n;
int s = sc.nextInt();
{
System.out.print("Enter element : ");
numArray[i] = sc.nextInt();
System.out.print(numArray[i] + ",");
System.out.println(numArray[numArray.length - 1]);
n = numArray[i];
numArray[i] = numArray[j];
numArray[j] = n;
}
}
System.out.print(numArray[i] + ",");
System.out.println(numArray[numArray.length - 1]);
Output:-
Code:-
import java.util.Scanner;
n = sn.nextInt();
arr[count] = sn.nextInt();
i= sn.nextInt();
first = 0;
last = n - 1;
if ( arr[mid] < i )
first = mid + 1;
else if ( arr[mid] == i )
break;
else
last = mid - 1;
}
Output:-
9. Write a program to read ‘n’ numbers and print their sum and average.
Code:- import java.util.Scanner;
int i,n,sum=0,avg=0,count=0;
n=sn.nextInt();
for(i=1;i<=n;i++)
System.out.println(i);
sum=sum+i;
avg=sum/n;
Output:-
10. Write a program that accepts a number as input and convert them into
binary, octal and hexadecimal equivalents.
int n;
void get()
n = Integer.parseInt(Scan.nextLine());
void convert()
{
String hexa = Integer.toHexString(n);
obj.get();
obj.convert();
Output:-
11. Write a menu driven program to i) append a string ii) insert a string iii)
delete a portion of the string.
CODE:-
import java.util.Scanner;
String s = sc.nextLine();
int choice = 0;
while (choice != 4) {
String substr;
int index;
System.out.print(
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
substr = sc.nextLine();
break;
case 2:
substr = sc.nextLine();
index = sc.nextInt();
sc.nextLine();
break;
case 3:
index = sc.nextInt();
sc.nextLine();
System.out.print("Enter the position(Number of characters till the end
of the string to be deleted): ");
sc.nextLine();
break;
default:
break;
sc.close();
Output:-
12. Write a program to check whether a string is palindrome or not without
using functions.
int a,sum=0,k,n;
n=sn.nextInt();
a=n;
while(n>0)
k=n%10;
sum=(sum*10)+k;
n=n/10;
}
if(a==sum)
else
Output:-
13. Write a menu driven program to i) compare two strings ii) get the character
in the specified position iii) extract a substring iv) replace a character with the
given character v) get the position of a specified substring/character.
CODE:-
import java.util.Scanner;
int choice = 0;
while (choice != 6) {
choice = sc.nextInt();
sc.nextLine();
char ch;
switch (choice) {
case 1:
str = sc.nextLine();
System.out.print("Enter string two: ");
str1 = sc.nextLine();
if (str1.equals(str))
else
break;
case 2:
str = sc.nextLine();
index = sc.nextInt();
sc.nextLine();
break;
case 3:
str = sc.nextLine();
index = sc.nextInt();
sc.nextLine();
System.out.print("Enter the end position(starting from 1): ");
endIndex = sc.nextInt();
sc.nextLine();
break;
case 4:
str = sc.nextLine();
index = sc.nextInt();
sc.nextLine();
ch = sc.next().charAt(0);
break;
case 5:
str = sc.nextLine();
str1 = sc.nextLine();
index = str.indexOf(str1);
if (index != -1)
else
break;
default:
break;
sc.close();
14. Write a program to change the case of the letters in a string. Eg. ABCdef
abcDEF.
{
String temp = "";
int k = a.length();
char ch=a.charAt(i);
ch = (char)(97 +
(int)(ch - 'A'));
temp += ch;
return temp;
String b=Change(a);
System.out.println(b);
{
String a="ABCdef";
Transstring(a);
Output:-
15. Write a class with the following methods: wordCount: This method accepts
a String object as an argument and returns the number of words contained in
the object. arrayToString: This method accepts a char array as an argument
and converts it to a String object. mostFrequent: This method accepts a String
object as an argument and returns the character that occurs the most
frequently in the object.
CODE:-
import java.util.Scanner;
int numberOfWords;
{
int number = 0;
char ch = string1.charAt(i);
if(Character.isWhitespace(ch))
number = number + 1;
return number + 1;
return String.valueOf(charArray);
int most = 0;
int j;
int count = 0;
char ch = string1.charAt(i);
if(ch == string1.charAt(j))
count = count + 1;
most = count;
mostOccurrence = ch;
return mostOccurrence;
OUTPUT:-
16. Create a class Student (Regno, Name, Branch, Year, Semester and 5 Marks).
Add methods to read the student details, calculate the grade and print the
mark statement.
CODE:
package Pkg_1;
class Student
{
String reg;
String year;
String branch;
String name;
int sem;
double[] marks = new double[5];
}
OUTPUT:
17. Write a program that displays an invoice of several items. Create a class
called Item with members item_name, quantity, price and total_cost and
methods to get and set values for the members. Derive a new class to print the
bill using Item class.
import java.util.Scanner;
item_number = item_num;
name = nm;
quantity = quan;
price = pri;
total_cost = 0;
}
public int getitem_number()
return item_number;
return name;
return quantity;
return price;
return total_cost;
}
public void setitem_number( int item_n)
item_number = item_n;
name = n;
quantity = quan;
total();
price = pri;
total();
{
total_cost = price * quantity;
System.out.println("Name: "+name);
System.out.println("Quantity: "+quantity);
System.out.println("Price: "+price);
import java.util.Scanner;
//-----------------------------------------------------------
chk1.setitem_number(sn.nextInt());
chk1.setname(sn.next());
chk1.setquantity(sn.nextInt());
chk1.setprice(sn.nextDouble());
//-----------------------------------------------------------
//-----------------------------------------------------------
chk2.setitem_number(sn.nextInt());
chk2.setname(sn.next());
chk2.setquantity(sn.nextInt());
chk2.setprice(sn.nextDouble());
//-----------------------------------------------------------
//-----------------------------------------------------------
chk3.setitem_number(sn.nextInt());
chk3.setname(sn.next());
System.out.println("Please enter quantity: ");
chk3.setquantity(sn.nextInt());
chk3.setprice(sn.nextDouble());
//-----------------------------------------------------------
chk1.displayLine();
chk2.displayLine();
chk3.displayLine();
}
18. Create a class Telephone with two members to hold customer’s name and
phone number. The class should have appropriate constructor, input and
display methods. Derive a class TelephoneIndex with methods to change the
name or phone number.Create an array of objects and perform the following
functions. a. Search for a name when the user enters a name or the first few
characters. b. Display all of the names that match the user’s input and their
corresponding phone numbers. c. Change the name of a customer. d. Change
the phone number of a customer.
import java.io.*;
if (args.length == 0)
fileName = DEFAULT_FILENAME;
else
fileName = args[0];
readPhoneData(fileName);
/* Show user a menu of available
operations, get the user's
choice, and carry it out. Repeat until
the user selects
the "Exit from this program" operation.
Each of the other
four commands is carried out by calling
a subroutine. */
done = false;
if (directory.changed == true)
writePhoneData(fileName);
TextIO.putln("\nExiting program.");
} // end main()
19. Create an abstract class called BankAccount with members customer name,
date of birth, address, account number, balance and member functions to get
values for the members and display it. Derive a class SavingsAccount with
member functions to perform deposit and withdraw in the account. Write a
menu driven program to create a new account, perform withdraw, deposit and
delete an account.
class BankAccount
{
// instance variable
/**
* Constructs a bank account with an account number and initial balance
*
* @param acctNum the account number
* @param initialBalance the initial balance
*/
public BankAccount(String acctNum, double initialBalance)
{
accountNum = acctNum;
balance = initialBalance;
}
/**
* Deposits money into the bank account.
*
* @param amount the amount to deposit
*/
public void deposit(double amount) // note "mutator" method
{
double newBalance = balance + amount;
balance = newBalance; // modifies instance
var
}
/**
* Withdraws money from the bank account.
*
* @param amount the amount to withdraw
*/
public void withdraw(double amount) // note "mutator" method
{
double newBalance = balance - amount;
balance = newBalance; // modifies instance var
}
/**
* Gets the account number
*
* @return the account number
*/
public String getAccount() // note "accessor" method
{
return accountNum; // returns value of instance var
}
/**
* Gets the current balance
*
* @return the balance
*/
public double getBalance() // note "accessor" method
{
return balance; // returns value of instance var
}
/**
* Transfer funds from one bank account object to another
*
* @param destination the BankAccount object receiving the funds
* @param amount the amount of money to be transferred
*/
public void transferFundsA(BankAccount destination, double amount)
{
destination.balance = destination.balance + amount;
// note explicit use of this to reference instance variables of the
// object for which the method was called
this.balance = this.balance - amount;
}
/**
* A class to test the BankAccount2 class
*/
public class BankAccountTest2
{
20. Create an Interface with methods add(), sub(), multiply() and divide().
Write two classes FloatValues to perform arithmetic operations on floating
point numbers and IntegerValues on integer numbers by implementing the
interface.
f.add();
f.sub();
f.divide();
f.multiply();
i.add();
i.sub();
i.divide();
i.multiply();
interface calculator {
float a;
float b;
this.a = a;
this.b = b;
@Override
@Override
@Override
@Override
int a;
int b;
this.a = a;
this.b = b;
}
@Override
@Override
@Override
@Override
}
}