Professional Documents
Culture Documents
Mona Lisa
Mona Lisa
Write a program to input a sentence and convert it into uppercase and count
and display the total number of words starting with letter ‘A’.
EXAMPLE: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY
ARE EVER CHANGING.
Sample output: Total number of words starting with letter ‘A’=4
SOLUTION:
Import java.util.Scanner;
public class letter
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
String str=in.nextLine();
str = “ “ + str;
int c= 0;
int len = str.length();
str= str. toUpperCase();
for( int i= 0 ; i < len- 1 ; i++ )
if (str.charAt(i)== ‘ ’&& str .charAt( i + 1)== ‘A’)
c++;
}
System.out,println( “Total number of words starting with letter ‘A’ = ”+c)
}
}
PROGRAM NUMBER
Write a program to input a number and check whether it is a duck no. or not .
(A duck number should contain at least one zero if it is a duck no.)
SOLUTION:
import java.util.Scanner;
public class duck
{
public static void main()
{
Scanner sc = new Scanner (System.in);
System. out.println(“ Enter a number ”);
int n= sc . nextInt();
int c =0;
int d;
while(n!=0)
{
d = n%10;
if (d==0)
c++;
n=n/10;
}
If (c>0)
System.out.println(n+ “ is a duck number ”);
else
System.out.println(n+“ is not a duck number”);
}}
PROGRAM NUMBER
Write a program to print the sum of the following series
3-5+7-9+11……………n
SOLUTION:
public class series
{
public static void main( int n)
{
Int i =3;
Int s =0;
Int p=1;
while(i=<n)
{
If (p%2== 0)
s =s + i ;
else
s= s- i;
p++;
i = i+2;
}
System.out.println(“ Sum of series is ”+s);
}}
Variable Data Type Description
i int (integer) loop control variable
s int (integer) for calculating sum
p int (integer) for determining the operation performed
PROGRAM NUMBER
Write a program to input a number to display the sum of each digit raised to
the power of the number of digits. [ ARMSTRONG NUMBER ]
SOLUTION:
import java.util.Scanner;
public class no
{
public static void main()
{
Scanner w = new Scanner (System.in);
System. out.println(“ Enter a number ”);
int N= w . nextInt();
int d=0; c=0;
for(int i =N; i>0; i=i/10 )
{
d = i%10;
c++;
}
double s= 0.0;
for(int j = N ; j>0 ; j=j/10)
{
d = j%10;
s= s+ Math.pow(d,c);
}
System.out.println(“ Sum = ” +s);
}}
PROGRAM NUMBER
Write a program to check whether a number is pronic or not.
SOLUTION:
public class pronic
{
public static void main (int n)
{
int p= 0;
int c =0;
for (int i=1; i<n; i++)
{
p= i*(i +1);
if(p == n)
}
c++;
if (c >= 1)
System.out.println( n + “is a pronic number”);
else
System.out.println(n+ “ is not a pronic number”);
}}
Variable Data Type Description
n int (integer) to accept input
p int (integer) to calculate and check condition
c int (integer) to check number
i int (integer) loop control variable
PROGRAM NUMBER
Write a menu driven program in Java for the following options:
a) To print whether the number entered is even or odd
b) To print whether the entered number is divisible by 11 or not
SOLUTION:
import java.util.Scanner;
public class menu
{
public static void main()
{
Scanner w = new Scanner (System.in);
System.out.println (“ Enter a number ”);
int new = w . nextInt();
System.out.println (“Enter ‘A’ for checking odd or even \n Enter B for
checking divisibility by 11”);
char choice = w.next()charAt(0);
switch(choice)
{
Case ‘A’:
if (new%2==0)
System.out.println(n+ “ is even. ”);
else
System.out.println(n+ “ is odd. ”);
break;
case ‘B’:
if (new%11 == 0)
System.out.println(new +“ is divisible by 11”);
else
System.out.println(new + “ is not divisible by 11”);
}}}
Palindrome words are those words which read the same from left to right and vice-versa. Example:
MALYALAM, MADAM, LEVEL, ROTATOR, CIVIC.
Write a program to accept a word. Check and display whether the word is a palindrome or only a
special word or none of them.
SOLUTION:
import java.util.Scanner;
System.out.print(“Enter a word”);
str = str.toUpperCase();
isPalin = false;
break;
}}
if (isPalin) {
else {
System.out.println(“Special”);
}}
else {
}}}
PROGRAM NUMBER
Design a class to overload a function check( ) as follows: 1. void check (String str , char ch ) — to find
and print the frequency of a character in a string. Example: Input: str = “success”; ch = ‘s’
Output: number of s present is = 3
2. void check(String s1) — to display only vowels from string s1, after converting it to lower case
Example: Input: s1 =”computer” Output
:oue SOLUTION:
int count = 0;
char c = str.charAt(i);
if (ch == c) {
count++;
}}
System.out.println(“Frequency of “ + ch +” = ”; + count);
String s2 = s1.toLowerCase();
System.out.println(“Vowels”);
char ch = s2.charAt(i);
if (ch == ‘a’ ||ch == ‘e’ ||ch == ‘i’ ||ch == ‘o’ ||ch == ‘u’)
System.out.print(ch + “ ”);
}}}
class name result Data Members: Name, Roll number, sub1, sub2, su3
PROGRAM NUMBER
Member functions : result( String, int) constructor to initialize the data members
void input(int, int,int) accept marks for three subjects
double percent( ) function to return the percentage of marks in three subjects
char grade(double) returns grade ‘A’, ‘B’ or ‘C’ as per the following conditions.
Percentage <=50 grade is C, percentage 51 to 70 grade B and percentage >70 grade A.
void display() to display the name, roll number, percentage and grade.
SOLUTION:
import java.util.Scanner;
class result
{
String Name; int Rollnumber, sub1, sub2, sub3;
result( String s, int rn)
{ Name= s;
Rollnumber=rn;
}
void input(int a, int b,int c)
{ sub1=a;
sub2=b;
sub3=c;
}
double percent( )
{ double p= (sub1+sub2+sub3)/3.0;
return p;
}
char grade(double Percentage)
{
if(Percentage <=50 )
return 'C';
else if (Percentage >=51 && Percentage<= 70)
return 'B';
else
return 'A';
}
void display()
{ System.out.println(Name);
double pr=percent();
System.out.println(pr);
char gr=grade(pr);
System.out.println(gr);
}
public static void main()
{ Scanner sc= new Scanner(System.in);
System.out.println("Enter a number Name");
String NA=sc.next();
System.out.println("Enter roll no");
int roll=sc.nextInt();
System.out.println("Enter marks");
int s1=sc.nextInt();
System.out.println("Enter marks");
int s2=sc.nextInt();
System.out.println("Enter marks");
int s3=sc.nextInt()
result obj = new result(NA,roll);
obj.input(s1,s2,s3);
PROGRAM NUMBER
import java.util.Scanner;
public class WSQ198 {
public static void main(String args[]) {
Scanner read = new Scanner(System.in);
int a,b,c,n,cat,ch;
double amt;
a=b=c=0;
amt=0.0;
do
{
System.out.println("Enter Category [1] for 18 and above");
System.out.println("Enter Category [2] for below 18 to 5");
System.out.println("Enter Category [3] for below 5");
cat=read.nextInt();
System.out.println("Enter number of people: ");
n=read.nextInt();
if(cat==1)
{
a+=n;
amt+=n*5;
}
else if(cat==2)
{
b+=n;
amt+=n*3;
}
else if(cat==3)
{
c+=n;
amt+=n*0;
}
System.out.println("Press[1] to continue [0] for exit");
ch=read.nextInt();
}
while(ch==1);
System.out.println("Number of people 18 years and above : "+a);
System.out.println("Number of people 5 to 18 years : "+b);
System.out.println("Number of people below 5 years : "+c);
System.out.println("Total Amount : "+amt);
}
}
case 2:
PROGRAM NUMBER
System.out.print("Enter number: ");
num = in.nextInt();
int f = 1;
for (int i = 1; i <= num; i++)
f *= i;
System.out.println("Factorial = " + f);
break;
default : System.out.println("Incorrect Choice");
break;
}}}
PROGRAM NUMBER
Design a class to overload a function compare( ) as follows:
l. void compare(int, int) — to compare two integers values and print the greater of the two
integers.
2. compare(char, char) — to compare the numeric value of two character and print with
the higher numeric value.
3. void compare(double, double) — to compare the two decimal values and print the
greater number.
SOLUTION: import java.util.Scanner;
public class compare
{
public static void main(int a, int b){
if(a>b){
System.out.println(a);
}
else{
System.out.println(b);
}}
public void compare(char a, char b )
int x =(int)a;
int y = (int)b;
if(x>y){
System.out.println(a);
}
else{
System.out.println(b);
}}
public void compare(double l1 , double l2)
if( l1>l2 )
System.out.println(l1);
else
System.out.println(l2);
PROGRAM NUMBER
}}
public static void main(String args[])
Scanner in= new Scanner ( System.in );
compare obj= new compare();
System.out.print(“Enter first integer”);
int n1= in.nextInt();
System.out.print(“Enter second integer”);
int n2 = in.nextInt();
obj.compare (n1 , n2);
System.out.print(“Enter first character”);
char c1 = in.next().charAt(0);
System.out.print(“ Enter second character”);
char c2 = in.next().charAt();
obj compare(c1 , c2);
System.out.print(“Enter first double number”);
double s1= in.nextDouble();
System.out.print(“Enter second double number”);
obj compare(s1 , s2);
}}
PROGRAM NUMBER
Design a class to overload a function () series as follows :
a) void series (int x, int n) – To display the sum of the series given below:
x + x + x +… x terms
1 2 3 n