Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

PROGRAM NUMBER

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”);
}}}

Variable Data Type Description


PROGRAM NUMBER
Write a program in Java to accept a string in lower case and change the
first letter of every word to upper case. Display the new string.
SAMLE INPUT: we are in cyber world
SAMPLE OUTPUT: We Are In Cyber World
SOLUTION:
public class lower
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
System.out.println ( “ Enter a sentence ” );
String str = in.nextLine();
String word = “ ”;
for( int i =0 ; I < str.length() ; i++)
{
if (i == 0 || str.charAt( i – 1 ) == ‘ ’)
{
word += Character . toUpperCase ( str . charAt(i));
}
else
{
word += str.charAt(i);
}}
System.out.println(word);
}}
PROGRAM NUMBER
Special words are those words which start and end with the same letter Example: EXISTENCE,
COMIC, WINDOW .

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;

public class special

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print(“Enter a word”);

String str = in.next();

str = str.toUpperCase();

int len = str.length();

if (str.charAt(0) == str.charAt(len - 1)) {

boolean isPalin = true;

for (int i = 1; i < len / 2; i++) {

if (str.charAt(i) != str.charAt(len - 1 - i)) {

isPalin = false;

break;

}}

if (isPalin) {

System.out.println (“Palindrome “);

else {

System.out.println(“Special”);

}}

else {

System.out.println(“Neither Special nor Palindrome “);

}}}
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:

public class Overload

void check (String str , char ch ) {

int count = 0;

int len = str.length();

for (int i = 0; i < str.length(); i++) {

char c = str.charAt(i);

if (ch == c) {

count++;

}}

System.out.println(“Frequency of “ + ch +” = ”; + count);

void check(String s1) {

String s2 = s1.toLowerCase();

int len = s2.length();

System.out.println(“Vowels”);

for (int i = 0; i < len; i++) {

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);
}
}

Define a class .Find Member variables : int a,b,c,n,m,n1; int hcf,lcm;


Member methods: Find() constructor to initialize the data members with legal initial values
void fibo() to calculate Fibonacci series for 10 terms
PROGRAM NUMBER
void factorial() to find factorial of n1
void hcflcm to find hcf and lcm of two numbers n,m
SOLUTION:
import java.util. Scanner;
public class Find
{ in
t a,b,c,n,m,n1;
int fact, hcf,lcm;
Find()
{
a=0;
b=0;
c=0;
hcf=0;
lcm=0;
fact=1;
}
void getvalue(int x ,int y,int z)
{
n=x;
m=y;
n1=z;
}
void fibo()
{
a=0; // 0 1 1 2 3 5
b=1;
System.out.println(a);
System.out.println(b);
for( int i =1 ;i;8;i++)
{
c=a+b;
System.out.println(c);
a=b;
b=c;
}}
void hcflcm()
{
int product =n*m;
PROGRAM NUMBER
for(int i=1;i=product;i++)
{
if(n%i==0 m%i==0)
hcf=i;
}
lcm=product/hcf;
System.out.println(“ hcf of “+n + “and “+m +”is” +hcf+”lcm is”+lcm);
}
void factorial()
{
fact=1;
int i;
for(i=1;i=n1;i++)
fact=fact*i;
System.out.println(”factorial of “+n1+”is” +fact);
}
public static void main()
{ Scanner sc= new Scanner(System.in);
System.out.println(“Enter a number for factorial”);
int ff=sc.nextInt();
System.out.println(“Enter two numbers for hcf lcm”);
int h1=sc.nextInt();
int h2=sc.nextInt();
Find num = new Find();
num.getvalue(h1,h2,ff);
num.fibo();
num.factorial();
num.hcflcm();
}}

Using switch statement, write a menu driven program to:


PROGRAM NUMBER
(a) find and display all the factors of a number input by the user ( including 1 and the
excluding the number itself).
Example : Sample Input : n = 15 Sample Output : 1, 3, 5
(b) find and display the factorial of a number input by the user (the factorial of a non-
negative integer n, denoted by n!, is the product of all integers less than or equal to n.)
Example: Sample Input : n = 5
Sample Output : 5! = 1*2*3*4*5 = 120
For an incorrect choice, an appropriate error message should be displayed.
SOLUTION: import java.util.Scanner;
public class facto
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Factors of number");
System.out.println("2. Factorial of number");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
int num ;
switch (choice) {
case 1:
System.out.print("Enter number: ");
num = in.nextInt();
for (int i = 1; i < num; i++) {
if (num % i == 0) {
System.out.print(i + " ");
}
}
System.out.println();
break;

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

b) void series( int p) – To display the following series:


0 , 7 , 26 , 63 ….. p terms
c) void series() - To display the sum of the series given below:
1 1 1 1
+ + +⋯ ⋯
2 3 4 10

public class seri


{
public static void series(int x , int n)
{
long sum;
for(int i = 1 ; i ; i ++ )
sum+=;
}
System.out.println(“Sum = ”+sum );
}
void series(int p)
{
for(int i = 0 ; i <=p ; i ++)
{
int term =(int) +(Math.pow(i,3)-1);
System.out.print(term+ “ ”);
}
System.out.println();
}
void seies()
{
double sum = 0.0;
for(int i=2 ; i<=10 ; i++)
{
sum+=1.0/ i;
}
System.out.println(“Sum =” +sum);
}}
PROGRAM NUMBER
Write a program to accept 10 different numbers in a single dimensional array . Display the
greatest and smallest numbers of array elements.
SOLUTION :
import java.util.Scanner;
public class maxx
(
public static void main(String args[])
{
Scanner sc= new Scanner (System.in);
int num[]= new int [10];
int min , max;
System.out.println(“Enter 10 numbers”);
for(int i=0 ; i<10 ; i++ )
num[i]= sc.nextInt();
min = num[0];
max = num[0];
for(int i=1 ; i<10 ; i++)
{
if(num[i]> max)
max=num[i];
if(num[i]< min)
min= num[i];
}
System.out.println(“The greatest of the array elements is :”+ max );
System.out.println(“The smallest of the array elements is :”+ min );
}}
PROGRAM NUMBER
Write a program in java to store the characters in word COMPUTER in a single
dimensional array . Arrange and display all the characters in descending order using
Bubble sort
SOLUTION:
public class word
{
public static void main(Strin args[])
{
char list[] = { ‘C’ , ’O’ , ’M’ , ‘P’ , ‘U’ , ‘T’ , ‘E’ , ‘R’};
int len = list.length;
for(int i=0 ; i < len-1 ; i++)
{
for(int j=0 ; j < len-i-1 ; j++)
{
if( list[j] < list[j + 1] )
{
char tmp = list[j];
list[j] = list[j + 1];
list[ j + 1] = tmp;
}}}
System.out.println(“Array in descending order is :”);
for(int i=0 ; i < len ; i++)
{
System.out.println( list[i] );
}}}
PROGRAM NUMBER
Write a program in java to input a two- dimensional array of n*m(rows = n and column = m )
and perform the following tasks:
a) Print the array in matrix form of n*m
b) Compute and print the sum of elements of each row.
SOLUTION:
import java.util. Scanner;
public class array
{
public static void main()
{
Scanner in = new Scanner(System.in);
int sum = 0;
System.out.println(“Enter number”)

You might also like