Null PDF
Null PDF
Using the switch statement, write a menu driven program to perform following operations:
(i) To print the value of Z where Z = X3+0.5X− where x ranges from –10 to 10 with an increment of 2
and Y remains constant at 5.5.
(ii) To print the Floyds triangle with N rows
Example: If N = 5,
Output:
1
23
456
7 8 9 10
11 12 13 14 15
import java.util.*;
class program1
{
public static void main(String args[])
{
int rows, number = 1, counter, j,ch;
double x,z,y=5.5;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 to display value of function and 2 to print Floyd's triangle:");
ch= sc.nextInt();
switch(ch)
{
case 1:
for(x= -10; x<=10; x+=2)
{
z= Math.pow(x,3)+0.5*x -y;
System.out.println("Z="+z);
}
break;
case 2:
System.out.println("Enter the number of rows for floyd's triangle:");
rows= sc.nextInt();
System.out.println("Floyd's triangle");
System.out.println("****************");
for ( counter = 1 ; counter <= rows ; counter++ )
{
for ( j = 1 ; j <= counter ; j++ )
{
System.out.print(number+" ");
number++;
}
System.out.println();
}
break;
default: System.out.println("Wrong choice...");
}}
}
Output:
import java.util.*;
public class program2
{
public static void main(String args[])
{
String s1, st="", s2="";
char x=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
s1= sc.nextLine();
s1=s1.toUpperCase();
for(int i= s1.length()-1; i>=0; i--)
{
st= st +s1.charAt(i);
}
for(int i=0;i<st.length()-1;i++)
{
x=st.charAt(i);
if(st.indexOf(x)!=st.lastIndexOf(x))
{
for(int j=i+1;j<st.length();j++)
{
char y=st.charAt(j);
if(x==y)
st=st.substring(0,j)+st.substring(j+1,st.length());
}
}
}
for(int i= st.length()-1; i>=0; i--)
s2+= st.charAt(i);
System.out.println(s2);
}
}
Output:
A tech number has even number of digits. If the number is split in two equal halves, then the square of sum of
these
halves is equal to the number itself. Write a program to generate and print all four digits tech numbers. Example:
Consider the number 3025. Square of sum of the halves of 3025 = (30 + 25)2= (55)2= 3025 is a tech number.
import java.util.*;
public class program3
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int k=n,c=0,f=0,fs=0,ls=0,t=0,num=0;
while(k>0)
{
c=c+1;
k=k/10;
}
if(c%2==0)
{
c=c/2;
f=(int)Math.pow(10,c);
fs=n%f;
ls=n/f;
t=fs+ls;
num=(int)Math.pow(t,2);
if(num==n)
System.out.println("Tech Number");
else
System.out.println("Not a Tech Number");
}
else
{
System.out.println("Not a Tech Number");
}
}
}
Output:
import java.util.*;
public class program4 {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int copy , d = 0, sum = 0,ch,num,p,c=0;
System.out.println("Enter 1 to check Disarium number and 2 to check Duck Number");
ch= sc.nextInt();
System.out.print("Input a number : ");
num = sc.nextInt();
switch(ch)
{
case 1:String s = Integer.toString(num);
copy = num;
int len = s.length();
while(copy>0)
{
d = copy % 10;
sum = sum + (int)Math.pow(d,len);
len--;
copy = copy / 10;
}
if(sum == num)
System.out.println("Disarium Number.");
else
System.out.println("Not a Disarium Number.");
break;
case 2:
while(num>0)
{
p= num%10;
if(p==0)
c++;
num/=10;
}
if(c>0)
System.out.println("Duck Number.");
else
System.out.println("Not a Duck Number.");
break;
default:System.out.println("Wrong choice");
}
}
}
Output:
Write a Java program to accept a sentence and count the number of palindromes in it.
Enter a Sentence: Nitin Arora uses only liRIL soap
Number of Palindromes=3
import java.util.*;
class Palindrome{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.println("Enter a string:");
String str, wrd="", rev="";
int count=0,i,j;
str=sc.nextLine();
str=str+' ';
for( i=0;i < str.length();i++){
char c= str.charAt(i);
if(c!=' ')
wrd+=c;
else
{
int len= wrd.length();
for(j=len-1; j>=0; j--)
{
rev= rev+ wrd.charAt(j);
}
if(wrd.equalsIgnoreCase(rev)==true)
count++;
rev="";
wrd="";
}
}
Output:
VARIABLE DESCRIPTION TABLE:
import java.util.*;
public class toggle
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String str, str1="";
int i,l;
System.out.println("Enter a string:");
str= sc.nextLine();
l= str.length();
for(i=0; i<l; i++)
{
char c=str.charAt(i);
if(Character.isUpperCase(c))
str1+= Character.toLowerCase(c);
else
str1+= Character.toUpperCase(c);
}
System.out.println("Sentence after toggling:"+str1);
}
}
Output:
}
str1= str1+ str. substring(p+1);
System.out.println("Initials of name-"+str1);
}
}
Output:
r1, r2 =
If the roots are imaginary the program displays a message and terminates.
import java.util.*;
public class Quadratic {
import java.util.*;
public class program9 {
Design a menu driven program to calculate the amount after applying simple/compound interest on a given sum of
money. The rate of interest (for SI as well as CI) is as follows:
Time Period Rate
Upto 12 months 6.25%
More than 12 months and upto 36 months 7.25%
More than 36 months 7.00%
The time of deposit and principal is accepted from the user.
For SI, A=P 1+ For CI, A= P 1 +
import java.util.*;
public class program10
{
public static void main(String args[ ])
{
Scanner sc= new Scanner(System.in);
int ch;
double si, ci, p, t, amt;
System.out.println("Enter the principal amount and time in months:");
p= sc.nextDouble();
t=sc.nextDouble();
System.out.println("Enter 1 for Simple Interest and 2 for Compound Interest:");
ch= sc.nextInt();
switch(ch)
{
case 1: if(t<=12)
si= (p*t*6.25)/100;
else if(t>12 && t<=36)
si= (p*t*7.25)/100;
else
si= (p*t*7.00)/100;
amt= p+ si;
System.out.println("Amount after simple interest="+amt);
break;
case 2:
if(t<=12)
amt= p*(double)Math.pow((1+(6.25/100)),t);
else if(t>12 && t<=36)
amt= p*(double)Math.pow((1+(7.25/100)),t);
else
amt= p*(double)Math.pow((1+(7.00/100)),t);
System.out.println("Amount after compound interest="+amt);
break;
default:System.out.println("Wrong choice");
}
}
}
Output:
import java.util.*;
public class program11 {
Output:
import java.util.*;
public class program12 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int i, l, count=0,a1=0, a2=0, a3=0, n, max;
double avg1, avg2, avg3;
String maximum;
System.out.println("Enter age of road accident victims and type 0 to exit");
do
{
System.out.println("Enter age:");
n=sc.nextInt();
count++;
if(n<=30)
a1++;
else if(n>30 && n<=50)
a2++;
else
a3++;
}while(n!=0);
if(a1>a2 && a1> a2){
max=a1;
maximum=" age group of below 30";}
else if(a2>a1 && a2> a3){
max= a2;
maximum=" age group of 31 to 50";}
else{
max=a3;
maximum="age group of above 50";}
avg1=(double)a1/(a1+a2+a3);
avg2=(double)a2/(a1+a2+a3);
avg3=(double)a3/(a1+a2+a3);
System.out.println("No. of victims in the age group of below 30="+a1);
System.out.println("No. of victims in the age group of 31 to 50="+a2);
System.out.println("No. of victims in the age group of above 50="+a3);
System.out.println("age profile with highest number of casualties="+maximum);
System.out.println("average age of victims age group of below 30="+avg1);
System.out.println("average age of victims age group of 31 to 50="+avg2);
System.out.println("average age of victims age group of above 50="+avg3);
}
}
Output:
Write a program to accept a word (in upper case) and check if it is a unique letter word. A unique letter word is one
that does not have any repetition of letters.
Examples: QUBIT is a unique letter word
PROGRAM is not unique letter word
import java.util.*;
public class program13
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
str = str.toUpperCase();
boolean isUnique = true;
int len = str.length();
char ch = str.charAt(i);
if (!isUnique)
break;
}
if (isUnique)
System.out.println("Unique String");
else
System.out.println("Not Unique String");
}
}
Output:
VARIABLE DESCRIPTION TABLE:
Write a program to remove the repetition of words from a user input sentence entered in upper case.
INPUT: SHE SELLS SEA SHELLS ON THE SEA SHORE
OUTPUT: SHE SELLS SEA SHELLS ON THE SHORE
import java.util.*;
class program14
{
public static void main(String[] args)
{
String input="";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence: ");
input= sc.nextLine();
String[] words=input.split(" ");
for(int i=0;i<words.length;i++)
{
if(words[i]!=null)
{
for(int j=i+1;j<words.length;j++)
{
if(words[i].equals(words[j]))
{
words[j]=null;
}
}
}
}
for(int k=0;k<words.length;k++)
{
if(words[k]!=null)
{
System.out.print(words[k] + " ");
}
}
}
}
Output:
import java.util.*;
if (checkAnagram(s1, s2))
System.out.println(s1 + " and " + s2 + " are Anagrams");
else
System.out.println(s1 + " and " + s2 + " are NOT Anagrams");
}
public static boolean checkAnagram(String s1, String s2)
{
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
if (s1.length() != s2.length())
return false;
else
{
for (int i = 0; i < s1.length(); i++)
{
for (int j = 0; j < s2.length(); j++)
{
if (s1.charAt(i) == s2.charAt(j))
{
s2 = s2.substring(0, j) + s2.substring(j + 1);
break;
}
}
}
if (s2.length() == 0)
{
return true;
} else
{
return false;
}
}
}
}
Output:
Write a Java program to accept a sentence in uppercase and count the frequency of each letter present in it.
Enter a sentence in upper case: DO OR DIE
D--->2
E--->1
I--->1
O--->2
R--->1
import java.util.*;
public class program16
{
public static void main(String args[])
{
int c=0,f=0;
char x;
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.trim();
st=st.toUpperCase();
System.out.println("Character\tFrequency");
for(char i='A';i<'Z';i++)
{
f=0;c=0;
for(int j=0;j<st.length();j++)
{
x=st.charAt(j);
if(x==i)
{
f=1;
c++;
}
}
if(f==1)
System.out.print(" "+i+"\t \t"+c+"\n");
}
}
}
Output:
A strong password refers to a string of minimum length 8, having combination of characters consisting of
uppercase
letters, lower case letters, digits and other characters (excluding blank space character), otherwise it is said to be a
weak password. Write a program to accept a password string from the user and classify it as strong or weak. If the
user enters a blank space character at any position of the password string or a string of length less than 8, then
display a message as INVALID INPUT!
import java.util.*;
public class program17 {
public static final int PASSWORD_LENGTH = 8;
}}
int charCount = 0;
int numCount = 0;
int sp=0;
int l=0, u=0;
for (int i = 0; i < password.length(); i++) {
char ch = password.charAt(i);
if (is_Numeric(ch)) numCount++;
else if (is_Letter(ch)) {
if (Character.isUpperCase(ch)== true) u++;
else l++;
charCount++;}
else if (Character.isWhitespace(ch)==false) sp++;
else return false;
}
return (password.length()>=8 && charCount >= 1 && numCount >= 1 && sp>=1 && l>=1 && u>=1 );
}
public static boolean is_Letter(char ch) {
ch = Character.toUpperCase(ch);
return (ch >= 'A' && ch <= 'Z');
}
Output:
if(square.endsWith(str_num))
System.out.println("Automorphic Number.");
else
System.out.println("Not an Automorphic Number.");
}
}
Output:
import java.util.*;
class program19
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter decimal number: ");
int num, p;
String s="";
num= sc.nextInt();
if(num<0)
System.out.print("Number is invalid");
else{
while (num >0)
{
p = num % 2;
s=p+s;
num /= 2;
}
System.out.print("\nBinary representation is:"+s);
System.out.println();}
}
}
Output:
import java.util.*;
class Library
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int day,cost,d;
double fine,ch;
System.out.println("Enter the cost price of the book: ");
cost=sc.nextInt();
System.out.println("Enter number of days: ");
day=sc.nextInt();
if(day<=7)
{
ch= (cost*0.02)*day;
System.out.println("Rental Charge="+ch);}
else{
d= day-7;
if(d>=1 && d<=5)
fine=d * 2.00;
else if(d>=6 && d<=10)
fine=(5 * 2.00)+((d-5) * 3.00 );
else
fine=(5 * 2.00) + (5 * 3.00) + ((d-10) * 5.00);
ch= (cost*0.02)*7;
System.out.println("Fine for "+day+" days is Rs."+(fine+cost));
}
}
}
Output: