0% found this document useful (0 votes)
31 views

Assignment 3

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Assignment 3

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 149

Assignment 3(1-20)

/*
1. Write a program to print the natural numbers upto 15 using for() loop.
*/

import java.util.*;
public class p1
{
public static void main(String args[])
{
for(int i=1;i<=15;i++)
{
System.out.println(i);
}
}
}
VARIABLE LIST:
Variable Type Reason
i int For loop

OUTPUT:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* 2. Write a program to print the natural numbers from 15 to 1 using
while() loop.*/
public class p2
{
public static void main(String args[])
{
int i=15;
while(i>0)
{
System.out.println(i);
i--;
}
}
}
VARIABLE LIST:
Variable Type Reason
i Int To take the starting
point of the series.
OUTPUT:
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
/* 3.Write a program to print only even numbers between 1 to 20 using
do-while( ) loop.
*/
public class p3
{
public static void main(String args[])
{
int i=2;
do
{
System.out.println(i);
i+=2;
}
while(i<=20);
}
}
VARIABLE LIST:
Variable Type Reason
i Int To take the starting
point of series.
OUTPUT:
0
2
4
6
8
10
12
14
16
18
20

/* 4.Write a program to print the fractional numbers from 2.0 to 30.0 by


the step 0.5 (e.g. 2.0, 2.5, 3.0...). */
public class p4
{
public static void main(String args[])
{
for(double i=2.0;i<=30.0;i+=0.5)
{
System.out.println(i);
}
}
}
VARIABLE LIST:
Variable Type Reason
i int For loop
OUTPUT:
2.0
2.5
3.0
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
10.5
11.0
11.5
12.0
12.5
13.0
13.5
14.0
14.5
15.0
15.5
16.0
16.5
17.0
17.5
18.0
18.5
19.0
19.5
20.0
20.5
21.0
21.5
22.0
22.5
23.0
23.5
24.0
24.5
25.0
25.5
26.0
26.5
27.0
27.5
28.0
28.5
29.0
29.5
30.0

/* 5. Write a program to print the sum of even numbers and sum of odd
numbers from the natural numbers upto 30.*/
public class p5
{
public static void main(String args[])
{
int sum=0,sum_1=0;
for(int i=1;i<=30;i+=2)
{
sum=sum+i;
}
for(int i=2;i<=30;i+=2)
{
sum_1=sum_1+i;
}
System.out.println("Sum of even numbers ranging from 1-
30="+sum_1);
System.out.println("Sum of odd numbers ranging from 1-
30="+sum);
}
}
VARIABLE LIST:
Variable Type Reason
sum int To calculate the sum
of odd numbers.
sum_1 int To calculate the sum
of even numbers.
i int For loop
OUTPUT:
Sum of even numbers ranging from 1-30=240
Sum of odd numbers ranging from 1-30=225
/* 6.Write a program to print alphabets in lowercase (small letters) Trom
a to z using for ) loop*/
public class p6
{
public static void main(String args[])
{
for(char i='a';i<='z';i++)
{
System.out.println(i);
}
}
}
VARIABLE LIST:
Variable Type Reason
I char For loop

OUTPUT:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z

/* 7.Write a program to print alphabets in uppercase (capital letters) in


reverse from Z to A.*/
public class p7
{
public static void main(String args[])
{
for(char i='Z';i>='A';i--)
{
System.out.println(i);
}
}
}
VARIABLE LIST:
Variable Type Reason
I Char For loop

OUTPUT:
Z
Y
X
W
V
U
T
S
R
Q
P
O
N
M
L
K
J
I
H
G
F
E
D
C
B
A

/* 8.Write a program to compute and print the sum of even integers and
product of odd integers from the list of
integers input by the user. The, process of input numbers terminates
when • (zero) is entered */
import java.util.*;
public class p8
{
public static void main(String args[])
{
int sum = 0, product = 1;
Scanner br = new Scanner(System.in);
System.out.println("Input a list of numbers.
Process of input numbers terminates when entered 0");
for(int i = 0;;i++)
{
int a = br.nextInt();
if(a == 0)
{
break;
}
else if(a % 2 == 0)
{
sum = sum + a;
}
else if(a % 2 != 0)
{
product = product*a;
}

}
System.out.println("Sum of Even Integers="+sum);
System.out.println("Product of Odd Integers="+product);

}
}
VARIABLE LIST:
Variable Type Reason
sum int To store the sum of
even integers
product int To store the product
of odd integers
i int For loop
a int To get input and
check whether the
number is even or odd

OUTPUT:
Input a list of numbers. To terminate list enter 0.
1
2
3
4
5
6
0
Sum of Even Integers=12
Product of Odd Integers=15
/* 9.Write a program to find and print count of double digit integers,
count of triple digit integers and count of
other integers from the list of integers. The process should terminate
when 0 is entered.*/
import java.util.*;
public class p9
{
public static void main(String args[])
{
int count=0;
int count1=0;
int count2=0;
Scanner in=new Scanner(System.in);
System.out.println("Input a list of numbers.Process of input
numbers terminates when entered 0");

for(int i=0;;i++) {
int a=in.nextInt();
if(a>=10&&a<100)
{ count++; }
else if(a>=100&&a<1000)
{ count1++; }
else
{ count2++; }
if(a==0)
{ break; }

}
System.out.println("No. of double digits=" +count);
System.out.println("No. of triple digits=" +count1);
System.out.println("No. of other digits=" +count2);
}
}
VARIABLE LIST:
Variable Type Reason
count Int To count double
digits number.
count1 Int To count triple digits
number.
count2 Int To count other digits
number.
i Int For loop

OUTPUT:
Input a list of numbers. To terminate list enter 0.
1
11
111
1111
0
No. of double digits=1
No. of triple digits=1
No. of other digits=2
/* 10.Write a program to input an integer N and print whether it is a
prime number or not. A number is said to be
prime if the number has only two factors i.e. the number is divisible by I
and itself OR a number is divisible
by I and itself and not divisible by any other factor of it.*/
import java.util.*;
public class p10
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Input a Number");
int x=in.nextInt();
int count=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
{
count++;
}
}
if(count==2)
{
System.out.println(x+" is a prime number");
}
else
{
System.out.println(x+" is not a prime number");
}
}
}
VARIABLE LIST:
Variable Type Reason
x int To take input from
user
i int To check whether
number is prime or
not
count int To use it in condition
to print correct
answer

OUTPUT:
i)Input a Number
13
13 is a prime number
ii)Input a Number
4
4 is not a prime number

/*11. Write a program to input an integer N and print whether it is a


composite number or not. If a number has more than two factors then it
is a composite mumber ie, a non-prime mumber is a composite
number.*/
import java.util.*;
public class p12
{
public void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input a number");
int n=in.nextInt();
int count=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count>1)
{
System.out.println(n+" is a composite number");
}
else
{
System.out.println(n+" not a composite number");
}
}
}

VARIABLE LIST:
Variable Type Reason
N int To take input from
user
I int To check whether
number is composite
or not
count int To use it in condition
to print correct
answer

OUTPUT:
i)Input a number
13
13 not a composite number
ii) Input a number
4
4 not a composite number
/*12. Write a program to input two integers N and M. Find and print
whether N and M are Twin-prime numbers or not. If the difference
between the two numbers is '2' then they are twin-primes. For example:
N=3, M=5 and M-N=5-3=2, so N and M are twin-primes but 4, 6 are
not.*/
import java.util.*;
public class p12
{
public void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input a number to check whether it is a twin-
prime or not");
int M=in.nextInt();
System.out.println("Input a number to check whether it is a twin-
prime or not");
int N=in.nextInt();
int count=0,count1=0;
for(int i=1;i<=M;i++)
{
if(M%i==0)
{
count++;
}
}
for(int j=1;j<=N;j++)
{
if(N%j==0)
{
count1++;
}
}
if(count==2 && count1==2 && (M-N==2 || N-M==2))
{
System.out.println("They are twin-prime numbers");
}
else
{
System.out.println("They are not twin-prime");
}
}
}
VARIABLE LIST:
Variable Type Reason
M int To take input from
user for second
number
N int To take input from
user for first number
I int To check whether
number M is prime
or not
J int To check whether
number N is prime or
not
count int To use it in condition
to print correct
answer for M
count1 int To use it in condition
to print correct
answer for N

OUTPUT:
i)Input a number to check whether it is a twin-prime or not
13
Input a number to check whether it is a twin-prime or not
11
They are twin-prime numbers
ii)Input a number to check whether it is a twin-prime or not
13
Input a number to check whether it is a twin-prime or not
10
They are not twin-prime
/*13. Write a program to input an integer N and compute its factorial.
Print the number and the factorial. The factorial is calculated by
multiplying all the integers from the number upto 1. For example:
factorial of 4 (4!) is : 4x3 x2 x1 = 24 or 4! =1x2x3 x4 =24.*/
import java.util.*;
Public class p13
{
public void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input a number to find its factorial");
int N=in.nextInt();
int fact=1;
for(int i=1;i<=N;i++)
{
fact=fact*i;
}
System.out.println("Factorial of "+N+" is="+fact);
}
}
VARIABLE LIST:
Variable Type Reason
N int To take input from
user
fact int To calculate the
factorial of the
number
I int To create a loop and
multiply

OUTPUT:
Input a number to find its factorial
3
Factorial of 3 is=6
/* 14. Write a program to input an integer N and print whether it is a
perfect or not. A number is said to be perfect if the sum of all the factors
excluding the number itself is equal to the original number. For example:
Input 6, its factors = 1, 2, 3 and its sum = 1+2+3 = 6, so 6 is a perfect
number*/.
import java.util.*;
public class p14
{
void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input a Number");
int x=in.nextInt();
int sum=0;
for(int i=1;i<x;i++)
{
if(x%i==0)
{
sum=sum+i;
}
}
if(sum==x)
{
System.out.println("It is a perfect number");
}
else
{
System.out.println("Not a perfect number");
}
}
}
VARIABLE LIST:
Variable Type Reason
X int To take input from
user
Sum int To calculate whether
thee number is
perfect or not
I int To create a loop and
add to the sum

OUTPUT:
i) Input a Number
6
It is a perfect number
ii) Input a Number
12
Not a perfect number

/*15. Write a program to input an integer N and print whether it is a


BUZZ, number or not. A BUZZ number is either divisible by 7 or ends
with 7. For example: Input 49 and 49 % 7 = 0, so 49 is a BUZZ number.
For example: Input 27 and 27 ends with 7, so 27 is a BUZZ number.*/
import java.util.*;
class p15
{
public void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input a number to check whether it is a buzz
number");
int x=in.nextInt();
if(x%10==7||x%7==0)
{
System.out.println("Buzz Number");
}
else
{
System.out.println("Not a Buzz Number");
}
}
}
VARIABLE LIST:
Variable Type Reason
x int To take input from
user

OUTPUT:
i) Input a number to check whether it is a buzz number
47
Buzz Number
ii) Input a number to check whether it is a buzz number
56
Buzz Number
iii) Input a number to check whether it is a buzz number
46
Not a Buzz Number
/*16. Write a program to print all Pythagorean triplets between 1 to 100.
A Pythagorean triplet is the set of three consecutive numbers such that
sum of square of first two numbers is equal to the square of third number
This is given by: a^2 + b^2 = c^2? (Example 3^2 + 4^2 = 5^2).*/
import java.util.*;
public class p16
{
public void main()
{
Scanner in=new Scanner(System.in);
for(int i=1;i<=100;i++)
{
for(int j=i;j<=100;j++)
{
for(int x=1;x<=100;x++)
{
if(Math.pow(i,2)+Math.pow(j,2)==Math.pow(x,2))
{
System.out.println(i+" " +j+" "+x+" ");
}
else
{
continue;
}
}
}
}
}
}
VARIABLE LIST:
Variable Type Reason
i int To create a loop for
finding first number
of triplets
j int To create a loop for
finding second
number of triplets
x int To create a loop for
finding third number
of triplets
OUTPUT:
345
5 12 13
6 8 10
7 24 25
8 15 17
9 12 15
9 40 41
10 24 26
11 60 61
12 16 20
12 35 37
13 84 85
14 48 50
15 20 25
15 36 39
16 30 34
16 63 65
18 24 30
18 80 82
20 21 29
20 48 52
21 28 35
21 72 75
24 32 40
24 45 51
24 70 74
25 60 65
27 36 45
28 45 53
28 96 100
30 40 50
30 72 78
32 60 68
33 44 55
33 56 65
35 84 91
36 48 60
36 77 85
39 52 65
39 80 89
40 42 58
40 75 85
42 56 70
45 60 75
48 55 73
48 64 80
51 68 85
54 72 90
57 76 95
60 63 87
60 80 100
65 72 97
/*17. In school election three candidates are contesting, They have been
allotted three different symbols like $, !, &. There are 1252 voters in the
school. Write a program to find:
(i) Number of valid and invalid voters.
(ii) Percentage of valid votes received by each candidate.*/
Ans=import java.util.*;
public class p17
{
public void main()
{
int count1=0,count2=0,count3=0,invalid=0;
Scanner in=new Scanner(System.in);
for(int i=1;i<=1252;i++)
{
System.out.println("Enter '$' to vote for 1st candidate, '!' to vote for
2nd candidate and '&'
to vote for 3rd candidate");
char vote=in.next().charAt(0);
if(vote=='$')
{
count1++;
}
else if(vote =='!')
{
count2++;
}
else if(vote=='&')
{
count3++;
}
else
{
invalid++;
}
}

int valid = count1+count2+count3;


double percent_candidate1 = (count1/valid)*100;
double percent_candidate2 = (count2/valid)*100;
double percent_candidate3 = (count3/valid)*100;
System.out.println("Number of Valid voters="+valid);
System.out.println("Number of Invalid voters="+invalid);
System.out.println("Percentage of Valid votes received by 1st
candidate= "
+ percent_candidate1+"%");
System.out.println("Percentage of Valid votes received by 2nd
candidate= " + percent_candidate2+"%");
System.out.println("Percentage of Valid votes received by 3rd
candidate= " + percent_candidate3+"%");
}
}
VARIABLE LIST:
Variable Type Reason
count1 int To calculate number
of vote received by
first candidate
count2 int To calculate number
of vote received by
second candidate
count3 int To calculate number
of vote received by
third candidate
invalid int To get the number of
invalid votes
i int To create a loop for
calculating votes
received by each
person
vote char To check the
symbols input by the
voters
valid int To calculate number
of valid voters
Percent_candidate1 double To calculate percent
of valid votes
received by candidate
1
Percent_candidate2 double To calculate percent
of valid votes
received by candidate
2
Percent_candidate3 double To calculate percent
of valid votes
received by candidate
3

/*18. Write a program to input two integers n and p. Find and print n
raised to the power p without using Math, pow () function.*/
import java.util.*;
public class p18
{
public void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input a number");
int n=in.nextInt();
System.out.println("Input a number to find power of n");
int p=in.nextInt();
int a=n;
for(int i=2;i<=p;++i)
{
n=n*a;
}
System.out.println(“Final ans=”n);
}
}
VARIABLE LIST:
Variable Type Reason
n int To take input of base
from user
p int To take input of
power from user
a int To store the value of
n
i int To create a loop for
finding the n^p

OUTPUT:
Input a number
4
Input a number to find power of n
3
Final ans=64
/*19. Write a program to input a character (ch). Convert the character
into its opposite case. Print the entered character and the new character.
The program should reject if the character entered is not an alphabet (A-
Z or a-z) Your program should continue as long as the user wants.*/
import java.util.*;
public class p19
{
public void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input a character");
for(int i=1;;i++)
{
char ch=in.next().charAt(0);
if(Character.isUpperCase(ch))
{
System.out.println(ch+=32);
}
else if(Character.isLowerCase(ch))
{
System.out.println(ch-=32);
}
else
{
break;
}
}
}
}
VARIABLE LIST:
Variable Type Reason
i int To create a loop for
continuous input
from user
ch char To take input of
alphabet from use

OUTPUT:
Input a character
A
a
s
S
1
/*20. Write a program to print those integers between 1 to n which are
divisible by m. Also print whether the number that is divisible by m is
even or odd. Input n as last limit and m as divisor.*/
import java.util.*;
public class p20
{
public void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input the last limit");
int n=in.nextInt();
System.out.println("Input the divisor");
int m=in.nextInt();
String odd_no="";
String even_no="";
for(int i=1;i<=n;i++)
{
if(i%m==0)
{
System.out.println(i);
if(i%2==0)
{
even_no=even_no+i+" ";
}
else
{
odd_no=odd_no+i+" ";
}
}
}
System.out.println("even numbers="+even_no);
System.out.println("odd numbers="+odd_no);
}
}
VARIABLE LIST:
Variable Type Reason
n int To take input from
user for last limit
m int To take input from
user for divisor
i int To create a loop to
print all divisible
numbers
even_no string To print all even
numbers
odd_no string To print all odd
numbers

OUTPUT:
Input the last limit
16
Input the divisor
2
2
4
6
8
10
12
14
16
even numbers=2 4 6 8 10 12 14 16
Assignment 4(21-40)
/*21. Write a program to input employee code (cd) in integer and basic
salary (basic) in double. It is required to calculate gross pay of 20
employees for the following allowances and deductions:
Dearness Allowance (DA) - 25% of basic pay
House Rent Allowance (HRA) - 15% of basic pay
Provident fund (PF) - 8.33% of basic pay
Net salary (net) - basic pay + DA + HRA
Gross salary (gross)
net salary - provident fund*/
import java.util0*.;
public class p21
{
public void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input the employee code");
int emp_code=in.nextInt();
System.out.println("Input the basic salary");
for(int i=1;i<=20;i++)
{
double salary=in.nextInt();
double da=(25.0/100.0)Salary;;
double hra=(15Salary)/100.0;
double pf=(8.33Salary)/100.0;
double net=salary+da+hra;
double gross=net-pf;
System.out.println(" Salary Slip ");
System.out.println("Employee Code:"+emp_code+"
Basic Salary:"+salary);
System.out.println("Dearness Allowance:"+da+" House
Rent allowance:"+hra);
System.out.println("Net Salary:"+net+" Gross
Salary:"+gross);
}
}
}
Variable list:
Variable Type Reason
emp_code int To get employee id
from user
I int To start a loop
Salary double To get the salary of
the user
Da double To get dearness
allowance
Hra double To get house rent
allowance
Pf Double To get providence
fund
Net Double To get the net salary
Gross Double To get gross salary
Output:
Input the employee code
1
Input the basic salary
140000
Salary Slip
Employee Code:1 Basic Salary:140000.0
Dearness Allowance:1.7857142857142857E-6 House Rent
allowance:21000.0
Net Salary:161000.00000178572 Gross
Salary:149338.00000178572

/* 22. Write a program to print the values of S from the following


equation of motion : U7 _ V2 = 2GS.
Where U ranges from 1 to 15, V ranges from O to 20 and G=9.8 m/s7
and is constant throughout the process.
The output should display U, V and S in three columns.*/
import java.util.*;
class p22
{
void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input value of U ranging from 1-15");
int U=in.nextInt();
System.out.println("Input value of V ranging from 0-20");
int V=in.nextInt();
double G=9.8;
double S=0;
if(U>=1&&U<=15)
{
if(V>=0&&V<=20)
{
S=(Math.pow(U,2)-Math.pow(V,2))/(2G);
System.out.println("Value of U:"+U);
System.out.println("Value of V:"+V);
System.out.println("Value of S:"+S);
}
}
else
{
System.out.println("Wrong Input");
}
}
}
Variable list:
Variable Type Reason
S int To store value of
speed
U int To store value of
initial speed
V int To store value of final
speed

Output:
Input value of U ranging from 1-15
14
Input value of V ranging from 0-20
20
Value of U:14
Value of V:20
Value of S:-999.6000000000001
/* 23 Write a program to prepare a frequency distribution table of the
percentage marks in Computer Studies sixty students, to be taken as
inputs, into the following categories, Category Marks (%) Fail 0 - 34
Pass Good 35 - 59 60 - 79 Very Good 80 and above The output should
display the categories and the corresponding frequencies i.e. the number
of students in each category in two columns. */
import java.util.*;
class p23
{
void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input the percentage of computer studies");
int count=0,count1=0,count2=0,count3=0;
for(int i=1;i<=60;i++)
{
double percent=in.nextDouble();
if(percent>=0&&percent<=34)
{
count++;
}
else if(percent>=35&&percent<=59)
{
count1++;
}
else if(percent>=60&&percent<=79)
{
count2++;
}
else if(percent>=80&&percent<=100)
{
count3++;
}
}
System.out.println("Category Frequency Marks");
System.out.println("Fail "+count+ " 0-34");
System.out.println("Pass "+count1+ " 35-59");
System.out.println("Good "+count2+ " 60-79");
System.out.println("Very Good "+count3+ " 80-100");
}
}
Variable List:
Variable name Data type Reason
i int For loop
percent int To store the
percentage of the
marks entered by the
student
count int To count and store the
number of students
failed
count1 int To count and store the
number of students
passed with marks 35-
59
count2 int To count and store the
number of students
passed with marks 60-
79
count3 Int To count and store the
number of students
passed with marks 80-
100

/* 24 Twin primes are prime numbers whose difference is 2. For


example: 3, S: 5, 7; 11, 13; 17, 19 etc, Write a program to print all twin-
prime numbers from 2 to 200 (both inclusive). */
import java.util.*;
class p24
{
void main()
{
Scanner in=new Scanner(System.in);
int count=1;int j=0;
System.out.println("Twin prime between 2-200");
for(int i=1;i<=200;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
break;
}
}
if(i==j)
{
if((i-count)==2)
{
System.out.println((i-2)+","+i);
}
count=i;
}
}
}
}
Variable List:
Variable type Reason
i int For loop
j int For loop
count int To find the twin
primes
Output:
Twin prime between 2-200
3,5
5,7
11,13
17,19
29,31
41,43
59,61
71,73
101,103
107,109
137,139
149,151
179,181
191,193
197,199
/* 25 Write a program to evaluate the following function:
F(x)=(x2+ 1.5x+5)/(x-3) */
class p25
{
void main()
{
for(double x=-10;x<=10;x++)
{
double i=(x*x+1.5*x+5)/(x-3);
System.out.println("f(x)="+i);
}
}
}
Variable List:
Variable name Data type Reason
x double For loop
i double To find x in the
formula given
Output:
f(x)=-6.923076923076923
f(x)=-6.041666666666667
f(x)=-5.181818181818182
f(x)=-4.35
f(x)=-3.5555555555555554
f(x)=-2.8125
f(x)=-2.142857142857143
f(x)=-1.5833333333333333
f(x)=-1.2
f(x)=-1.125
f(x)=-1.6666666666666667
f(x)=-3.75
f(x)=-12.0
f(x)=Infinity
f(x)=27.0
f(x)=18.75
f(x)=16.666666666666668
f(x)=16.125
f(x)=16.2
f(x)=16.583333333333332
f(x)=17.142857142857142
/*26 0. 1. 1, 2, 3. 5, 8, 1.3.......... ......... The fibonacci terms are generated
by adding two previous terms. Initially O, 1 are assigned as first two
terms of the series, then O+/=1 (third term), add 2nd and 3rd term =
1+1=2 (4th term)... and so on.)*/
import java.util.*;
public class p26
{
public static void main(String args[])
{
int a=0,b=1,c;
System.out.print("Fibonacci Series: "+a+" "+b+" ");
for(int i=0;i<13;i++)
{
c=a+b;
a=b;
b=c;
System.out.print(c+" ");
}
}
}
Variable List:
Variable name Data type Use
i int Used as a loop
variable
a int Used to store as first
number
b int Used to store as
second number
c int Used to store as third
number
Output:
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
/*27 The total distance traveled by a car in t seconds is given by:
distance= ut + ½a t2 where u is initial velocity (m/s), a is the acceleration
(m/s?). Write a program to evaluate the distance traveled at regular
intervals of time, given the values of u and a.*/
import java.util.*;
class p27
{
void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter no of parts");
int ab=in.nextInt();
int distance=0;
for(int i=1;i<=ab;i++)
{
System.out.println("Part "+i);
System.out.println("Enter value of A");
int a=in.nextInt();
System.out.println("Enter value of u");
int u=in.nextInt();
System.out.println("Enter value of t");
int t=in.nextInt();
distance=(u*t)+(a*t*t)/2;
}
System.out.println("Distance="+distance);
}
}
Variable List:
Variable name Data type Use
A int Used to store value of
acceleration
U int Used to store value of
initial velocity
T int Used to store value of
final velocity
Ab int Used to store number
of parts
i Int Used as a loop
variable

Output:
Enter no of parts
1
Part 1
Enter value of A
14
Enter value of u
85
Enter value of t
45
Distance=18000
/*28 Write a program to compute and print the sum of following series:
10 10 1-0 1.0 1-0 S= - 2 3 ,Take integer n as input.*/
import java.util.*;
class p28
{
void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input the nmber of last limit of following
series");
int n=in.nextInt();
double num=1.0,sum=0;
for(int i=1;i<=n;i++)
{
double cc=num/i;
sum=sum+cc;
}
System.out.println(sum);
}
}
Variable List:
Variable name Data type Use
i int Used as a loop
variable
n int Used to store the last
digit of series entered
by user
num double Used as numerator of
the fraction
sum double Used to calculate the
sum of all fractions
upto n
cc double Used to form the
fraction

Output:
Input the number of last limit of following series
15
3.3182289932289937
/*29 Write a menu driven program (using switch-case statement) to
perform the following : (a) To print the series 0, 3, 7, 15, 24 .......n terms
(value of 'n' is to be an input by user). ) To find the sum of the series
given below: S=1/2+3/4+516 + 718 .... ......... 19 / 20*/
import java.util.*;
class p29
{
void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input a choice 1.to print series
0,3,7,15,24......upto n terms 2.to find sum of 1/2+3/4...");
int a=in.nextInt();
double sum=0,num=2;
switch(a)
{
case 1:
System.out.println("Input the last limit");
int n=in.nextInt();
for(int i=1;i<=n;i++)
{
System.out.print(i*i-1+" ");
}
break;
case 2:
for(double i=1;i<=19;i+=2)
{
sum=sum+(i/(num));
num+=2;
}
System.out.println(sum);
break;
}
}
}

Variable name Data type Use


num double Used to store the
series
sum double Used to store the sum
of numbers upto n
a int Used to choose one of
the options in the
menu driven program
n int Used to store the last
limit of numbers
i int Used as a loop
variable

Output:
Input a choice 1.to print series 0,3,7,15,24......upto n terms 2.to find sum
of 1/2+3/4...
1
Input the last limit
14
0 3 8 15 24 35 48 63 80 99 120 143 168 195
Q.30 Write a program to input 'n'. Find and print the sum of the
following series upto 'n' terms : S=1+ (1 + 2) + (1+2+3) + (1+2+3+4)
+ ............ + n terms.
Program:
import java. util. *;
class p30
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in) ;
System.out.println("Input the last limit");
int n=sc.nextInt() ;
int sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i; j++)
{
sum=sum+j;
}
}
System.out.println("Sum="+sum);
}
}
Variable List:
Variable name Data type Use
i int Used as a loop
variable
j int Used a a loop variable
sum int Used to store the sum
of all numbers
n int Used to store the last
limit
Output:
Input the last limit
145
Sum=518665
/*31 Input two integers N and M as the start and end limits. Print the
factorial of all the integers between N and M, if N < M, otherwise print a
message "factorials not possible", The factorial of N is given by : N! = N
* (N-1) * (N-2) * (N-3) * * I*/

import java.util.*;
class p31
{
void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Input value of N as the start limit");
int N=in.nextInt();
System.out.println("Input value of M as the last limit");
int M=in.nextInt();
if(N<M)
{
for (int i=N;i<=M;i++)
{
long fact=1;
for (int j=1;j<=i;j++)
{
fact=fact*j;
}
System.out.println("Factorial of "+i+"="+fact);
}
}
else
{
System.out.println("Factorials not possible");
}
}
}

Variable name Data type Use


fact int Used to store factorial
i int Used as a loop
variable
j int Used as a loop
variable
n int Used as start limit
m int Used as end limit
Output:
Input value of N as the start limit
14
Input value of M as the last limit
25
Factorial of 14=87178291200
Factorial of 15=1307674368000
Factorial of 16=20922789888000
Factorial of 17=355687428096000
Factorial of 18=6402373705728000
Factorial of 19=121645100408832000
Factorial of 20=2432902008176640000
Factorial of 21=-4249290049419214848
Factorial of 22=-1250660718674968576
Factorial of 23=8128291617894825984
Factorial of 24=-7835185981329244160
Factorial of 25=7034535277573963776
/*Q.32 a) 1-2+3-4+5……… n terms*/
import java.util.*;
public class p32a
{
public static void main(String[]args)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter nth term");
int n = s.nextInt();
if(n%2 == 1)
{
System.out.println("Ans is: "+((n+1)/2));
}
else
{
System.out.println("Ans is: "+(-n/2));
}
}
}
Variable List:
Variable Type Reason
n int To store the value of
number entered by
user
Output:
Enter nth term
10
Ans is: -5
/* 32. b) 2/22+2/32+2/42 till n terms*/
import java.util.*;
class p32b
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Please enter number of terms");
double n=s.nextDouble();
double sum=0;
double k=2;
double sqr;
for (double i=2; i<=n+1; i++)
{
sqr=Math.pow(i,2);
sum+= k/sqr;
if (sqr%2!=0) {
k++;
}
}
System.out.println("Sum = " + sum);
}
}
Variable Type Reason
n double To store number of
number of terms
sum double To store the sum of
number till nth term
sqr double To store the
denominator as square
of number
k double To value of numerator

Output:
Please enter number of terms
10
Sum = 1.4719061583157906
/* 32.c) 0+1+1+2+3+5+8….nth term*/
import java.util.*;
class p32c
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter nth term");
int n , first = 0, second = 1,sum=0;
n=s.nextInt();
for (int i = 1; i <= n; ++i)
{
int next = first + second;
first = second;
second = next;
sum += first;
}
System.out.println(sum);
}
}
Variable List:
Variable type Reason
first int To store 1st number
second int To store 2nd number
next int To store the 3rd
number
sum int To store the sum of
the series
n int To store the value of
number of terms
Output:
Enter nth term
10
143
d) x/1+x2/2+x3/3+x4/4….nth term
Program:
import java.util.*;
class p32d
{
double sum;

void main()
{
Scanner z= new Scanner(System.in);
int x,s;
System.out.println("Input value for x");
x= z.nextInt();
System.out.println("Enter number of terms");
s = z.nextInt();
for (int i=1;i<=s;i++)
{
{
sum+= (Math.pow(x,i))/i;

}
}
System.out.println(+sum+" is the sum");
}
}
Variable List:
Variable Name Data type Reason
i int For loop
sum double To calculate the sum
s int To store the number
of terms
x int To store the value of x
Output:
Input value for x
1
Enter number of terms
14
3.251562326562327 is the sum
e) 5+8+12+15……n terms
import java.util.*;
public class Q32E
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Please enter number of terms");
double n = s.nextDouble();
int t = 4;
int sum = 0;
int term = 1;
for (int i = 1; i <= n; i ++)
{
term += t;
if (t == 3)
{
t = 4;
}
else if (t == 4)
{
t = 3;
}
sum += term;
}
System.out.println("Sum = " + sum);
}
}
Variable List:
Variable name Data type Use
n double To store number of
terms
t int To store the difference
sum int To store the sum
term int To change difference
from 3 to 4 or 4 to 3
OUTPUT:
Please enter number of terms
10
Sum = 205
f)13+23+33+43……nth term
Program:
import java.util.*;
public class Q32F
{
public static void main(String args[])
{
Scanner me=new Scanner(System.in);
double n;
System.out.println("Enter the number of terms");
n=me.nextDouble();
double fin=0;
double sum=0;
for(int i=1;i<=n;i++)
{
fin=Math.pow(i,3);
sum=sum+fin;
}
System.out.println("Sum= "+sum);
}
}
Variable List:
Variable List Data type Reason
n double To store number of
terms
fin double To store the cube of
number
sum double To store the sum of
series
Output:
Enter the number of terms
10
Sum= 3025.0
g)(1*22)+(2*32)…….nth term
Program:
import java.util.*;
public class Assignment3Q32G {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter nth term: ");
int n = s.nextInt();
int first=1,second=1,sum=0;
for(int i=0;i<n;i++){
int next=first+second;
first=second;
second=next;
double temp=first*Math.pow(first+1,2);
sum += temp;
}
System.out.println("Ans is "+sum);
}
Variable List:
Variable name Data type Reason
n int To store value of
number of terms
first int To store 1st number
second int To store 2nd number
next int To store power
i int For loop
temp double Used to find 1 number
in series
sum int Used to store the sum
of series
Output:
Enter nth term:
10
Ans is 948640
h) 1/1!+2/3!+2/4!......n terms
Program:
public class 32h
{
public static double pattern_sum(double val)
{
double res=0,fact=1;
for (int i = 1; i <= val; i++)
{
fact = fact * i;
res = res + (i / fact);
}
return (res);
}
public static void main(String[] args)
{
double val = 6;
System.out.println("The sum of the series is : " + pattern_sum(val));
}
}
Variable List:
Variable name Data type Reason
val double To store the number
of terms
fact double To store value of
1,used to find factorial
res double To store value of
factorial
i int For loop

Output:
The sum of the series is : 2.7166666666666663
i)x+x^2/2!+x^3/3!+x^4/4!.......n terms
Program:
import java.util.Scanner;
public class Q32I
{
public static void main(String[] args)
{
Scanner cs=new Scanner(System.in);
int n,i=1,x,j,fact;
double sum=0.0;
System.out.println("Enter the range of number:");
n=cs.nextInt();
System.out.println("Enter the value of x:");
x=cs.nextInt();

fact=1;
for(j=1;j<=n;j++)
{
fact*=j;
sum+=Math.pow(x,i)/fact;
}

System.out.println("The sum of the series = "+sum);


}
}
Variable List:
Variable Name Data type Reason
n int To store the value of
number of terms
i int To store the
denominator of the
series
fact int To store value of 1 to
find factorial
j int For loop
sum double To store the sum of
series
x int To store value of x
Output:
Enter the range of number:
10
Enter the value of x:
14
The sum of the series = 24.055945216049377
/*33 The fibonacci term is generated by adding two previous terms to
produce third term. The following are few fibonacci terms by taking 1, 1
as first two terms as; 1, 1, 2, 3, 5, 8, 13, ...................... A number is said
to be prime if the number has only two factors ie the number is divisible
by one and itself and no other factor or a number is divisible by I and
itself and not divisible by any other factor. Write a program to print only
prime fibonacci terms upto n. Input 'n' as the limit of the series.*/
import java.util.*;
class p33
{
void main()
{
Scanner sc=new Scanner(System.in);
int n1=0,n2=1,n3=0;
System.out.println("Enter the no of terms");
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
n3=n1+n2;
int count=0;

for(int j=1;j<=n1;j++)
{
if(n1%j==0)
{
count++;
}
}
if(count==2)
{
System.out.print(" "+n1+" ");
}
n1=n2;
n2=n3;
}
}
}
Variable List:
Variable List Data type Reason
n int To store the number
of terms
n1 int To store as 1st number
n2 int To store as 2nd
number
n3 int To store as 3rd
number
count int To count number of
factors
i int For loop

Output:
Enter the no of terms
14
2 3 5 13 89 233
/*34 A cloth showroom has announced the festival discount as follows
on the purchase of the items based on the total cost of the item purchased
by the customer:
Total cost Less than R$ 2000 Rs 2001 to Rs 5000 Rs 5001 to R$ 10000
Discount %age on total cost 5 % 25 % 35 % 50 % R 10001 and above
Write a program to input the total cost to compute and display the
amount to be paid by each of 30 customer availed by customer*/

import java.util.Scanner;
public class p34 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
for(int i=1;i<=30;i++)
{
System.out.println(“Enter the price”);
double cost = sc.nextDouble();
if (cost < 2000) {
System.out.println("Discount:" + (cost * 0.05));
} else if (cost >= 2001 && cost <= 5000) {
System.out.println("Discount:" + (cost * 0.25));
} else if (cost >= 5001 && cost <= 10000) {
System.out.println("Discount:" + (cost * 0.35));
} else if (cost >= 10001) {
System.out.println("Discount:" + (cost * 0.5));
} else {
System.out.println("Invalid cost");
}
}
}
}
Variable List:
Variable name Data type Reason
i int For loop(to get the
price)
cost double To get the cost from
the user

Output:
Enter the price
14
Discount:0.7000000000000001
Enter the price
154
Discount:7.7
/*35 Shasha Travels Pvt. Ltd. gives the following discount to its
customers Ticket Amount Discount Above Rs 70000 18% Rs 55001 to
70000 16% Rs 35001 to 55000 12% Rs 25001 to 35000 10% less than
Rs 25001 2% Write a program to input name and ticket amount for the
customer and calculate the discount amount and not amount to be paid.
Display the output in the following format for each customer. SI. No.
Name Ticket charges Discount Net amount (Assume total 15 customers,
first customers serial no. is (SI. No.) 1, next 2 ... an so on).*/
import java.util.*;
public class p35
{
public void Travel()
{
int discount;
Scanner s=new Scanner(System.in);
for(int i =1;i<=15;i++){
System.out.println("Please enter your name");
String name = s.nextLine();
System.out.println("Please enter your tickt amount");
int tot_amt = s.nextInt();
if(ta<25001){
discount=2;
}else if(ta>=25001 && ta<=35000){
discount=10;
}else if(ta>=35001 && ta<=55000){
discount=12;
}else if(ta>=55001 && ta<=70000){
discount=12;
}else{
discount=18;
}
double dis_amt = (discount/100)*tot_amt;
double net_amt = tot_amt - dis_amt;
System.out.println("Customer " + i + ": ");
System.out.println("Sl. No." + '\t' + "Name" + '\t' + "Ticket
Charges" + '\t' + "Discount Amount" + '\t' + "Net Amount");
System.out.println(i + '\t' + name + '\t' + ta+ '\t' + dis_amt+ '\t' +
net_amt);
}
}
}
Variable List:
Variable List Data type Reason
name String To store name of
passenger
i int For loop
discount int To store value of
discount
Tot_amt int To store the total
amount
Dis_amt double To store value of
discount
Net_amt double To store the final
amount
Output:
Please enter your name
Jainam
Please enter your tickt amount
1000
Customer 1:
Sl. No. Name Ticket Charges Discount Amount Net
Amount
10Jainam 1000 0.0 1000.0
/*36 Write program to input an integer. Compute and print the sum of all
the digits and count of digits. Example : Input : N = 2854 Output : Sum
of digits = 19 and Count of digits = 4.*/

import java.util.Scanner;
public class p36 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int sum = 0;
int digit= 0;
while (num > 0) {
sum += num % 10;
digit++;
num /= 10;
}
System.out.println("Sum of the digits: " + sum);
System.out.println("Number of digits: " +digit);
}
}
Variable List:
Variable name Data type Reason
num int To store the value of a
number
sum int To store the sum of
digits
digit int To save number of
digits
Output:
Enter a number: 14
Sum of the digits: 5
Number of digits: 2
/*37 Write program to input an integer. Find and print the sum of all
even digits and product of odd digits, Example : Input : N = 3498
Output: Sum of even digits = 12 and Product of odd digits = 27.*/
import java.util.*;
public class p37
{
public void main()
{
int sum=0,product=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number : ");
int num=sc.nextInt();
int temp=num;
while(temp > 0)
{
int digit = temp % 10;
if(digit%2==0)
{
sum=digit+sum;
}
else
{
product=digit* product;
}
temp/=10;
}
System.out.println("OUTPUT");
System.out.println("sum of even numbers ="+sum);
System.out.println("product of odd numbers ="+product);
}
}
Variable List:
Variable name Data type Reason
sum int To store the sum of
even numbers
product int To store the product
of odd numbers
num int To store a number
temp int To store a number
temporarily
digit int To divide the number
into digits

Output:
Enter a number :
145
sum of even numbers =4
product of odd numbers =5

/* 39 Write program to accept an integer and reverse it. Print the original
number and the reverse number. Example : Input : N = 2369 Output:
Reverse = 9632.*/
import java.util.*;
public class p39
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int num,num1 ,dig;
System.out.println("enter a number");
num=sc.nextInt();
num1=num;
System.out.print("In Reverse order:");
while(num>0)
{
dig=num%10;
System.out.print(dig);
num=num/10;
}
System.out.println("");
System.out.println("Original order:"+num1);
}
}
Variable name Data type Reason
Num Int To store the number
Num1 Int To store the number
temporarily
Dig int To calculate last digit
using %10
Output:
enter a number
145
In Reverse order:541
Original order:145
/*40 Write a program to accept an integer to find out and print the
difference between the greatest and the smallest digits present the
number.*/
import java.util.*;
public class pr40
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int num,dig,max=0,min=10;
System.out.println("enter a number");
num=sc.nextInt();
while(num>0)
{
dig=num%10;
if(dig>max){
max=dig;
}
if(dig<min)
{
min=dig;

num=num/10;
}
System.out.println("Maximum Digit:"+max);
System.out.println("Minimum Digit:"+min);
System.out.println("Difference:"+(max-min));
}
}
Variable List:
Variable name Data type Reason
num int To store a number
Dig int To find last digit of
the given number
max int To store and find the
maximum digit
min int To find and store the
minimum digit
Output:
enter a number
145
Maximum Digit:5
Minimum Digit:1
Difference:4

ASSIGNMENT 5(41 to 65)


/*41 Write a program to input any number n and using only for( ) loop
print sum of the digits, product of the even digits and count of the odd
digits.*/
import java.util.*;
public class p41
{
void number()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a number");
int n,dig=0,digeven=1,sum=0,count=0;
n=in.nextInt();
int i;
for(i=n;i>0;i+=0)
{
dig=i%10;
i=i/10;
sum+=dig;
if(dig%2==0)
{
digeven=digeven*dig;
}
else
{
count+=1;
}
}
System.out.println("Sum of digits="+sum);
System.out.println("Product of even digits="+digeven);
System.out.println("Count of odd digits="+count);
}
}
Variable List:
Variable name Data type Reason
n int To get the number
from the user
i int For loop
dig int To find the last digit
digeven int Used to find the
product of even digits
sum int Used to find the sum
of digits
F int Used to find the count
of odd digits
Output:
Enter a number
14
Sum of digits=5
Product of even digits=4
Count of odd digits=1
/*42 Write a program to accept an integer greater than O and print the
sum of all the digits of the number which are prime in nature. It is
assumed that the number is containing all non-zero digits.*/
import java.util.*;
public class p42
{
void sum()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter an integer");
int num,dig=0,sumF=0;
num=in.nextInt();
int sumP=0;
for(int i=num;i>0;i+=0)
{
dig=i%10;
i=i/10;
for(int j=1;j<=dig;j++)
{
if(dig%j==0)
{
sumF++;
}
if(e==2)
{
sumP=sumP+c;
}
}
}
System.out.println("Sum of prime numbers="+sumP);
}
}
Variable List:
Variable name Data type Reason
Num int To store a number
i int To store the number
temporarily
dig int To find the last digits
j int For loop
sumF int To store the sum of
factors
sumP int To calculate the sum
of prime numbers
Output:
Enter an integer
145
Sum of prime numbers=6
/*43 A number is said to be NEON number if sum of digits of square of
a number is equal to the number itself. Eg : INPUT: N= 9, Output :
Square: 81 [ where 8 + 1 = 9] so N (9) is a NEON number. Write a
program to find all such numbers between 10 to 10000.*/
import java.util.*;
class p43
{
void asgn3()
{
Scanner sc=new Scanner(System.in);
int a=0;
for(int i=10;i<=10000;i++)
{
int b=i*i;
int sum=0,c=0;
while(b!=0)
{
c=b%10;
sum+=c;
b/=10;
}
if(i==sum)
{
System.out.println(i+" is a neon no");
a=0;
}
else{
a=1;
}
}
if(a==1)
{
System.out.println("No neon number found");
}
}
}
Variable List:
Variable name Data type Reason
a int To count
b int To store the square of
the number entered
c int To store the last digits
of the number
sum int To store the sum
i int For loop

Output:
No neon number found
/*44 Write program to input integers m1 and n2. Merge n2 after n1 and
store merged number in M. Print n1, I? and merged number M (do not
simply print the numbers). Example : Input : n1 = 24, n2 = 567 Output :
M = 24567.*/

class p44
{
static int merging(int num1, int num2)
{
String n1 = Integer.toString(num1);
String n2 = Integer.toString(num2);
String s = n1 + n2;
int m = Integer.parseInt(s);
return m;
}
public static void main(String args[])
{
int num1 = 55;
int num2= 14;
System.out.println(merging(num1, num2));
}
}
Variable List:
Variable name Data type Reason
n1 String To store the first
number
n2 String To store the 2nd
number
num1 int To take value of 1st
number
num2 int To take the value of
2nd number
m int To change the the type
s String To add n1 and n2

Output:
5514
/*45 Write a program to accept an integer. Print whether the integer is a
palindrome number or not. If the original number and its reverse are
same then number is palindrome. Eg : INPUT : N = 151, Output : Its
reverse = 151, so 151 is a palindrome number, but 235 is not a
palindrome number.*/
import java.util.*;
public class p45
{
public static void PrintNo(int n)
{
int temp=n;
int rev=0;

while(temp>0)
{
int digit=temp%10;
rev=rev*10+digit;
temp=temp/10;
}
System.out.println("OUTPUT:");
if(rev==n)
{
System.out.println(n+"is a Palindrone number");
}
else
{
System.out.println(n+"is not a Palindrone number");
}
}
}
Variable List:
Variable list Data type Reason
n int To store the number
temp int To store the number
temporarily
digit int To store the last digits
of the number
rev int Tstore the reverse
number
Output:
OUTPUT:
145is not a Palindrone number
/*46 Write a program to accept an integer. Print whether the integer is a
special number or not. if the original number and sum of the factorial of
each digit are same then number is special. Eg: INPUT : N = 145, Output
: Sum of factorial of digits = 1:+4:45! = 1424+120 = 145, so 145 is
special number, but 219 is not a special number.*/
import java.util.*;
public class p46
{
public void SpecialNo()
{
int num,fact,sum=0;
Scanner br=new Scanner(System.in);
System.out.print("Input a number");
num=br.nextInt();
int N=num;
while(N!=0)
{
int digit=N%10;
fact=1;
for(int y=1;y<=digit;y++)
{
fact=fact*y;
}
sum=sum+fact;
N=N/10;
}
if(sum==num)
System.out.println(num+"is a Special number");
else
System.out.println(num+"is not a Special number");
}
}
Variable List:
Variable name Data type Use
num int Used to store the
number entered by
user
sum int Used to store the sum
of digits
N int Used to stpre the
number entered by the
user temporarily
fact int Used to find the
factorial of a number
y int Used as a loop
variable
digit int Used to find the digit
of a number
Output:
Input a number: 145
145 is a Special number
/*47 Write a program to accept an integer. Print whether the integer is an
armstrong number or not if the original number and sum of cubes of all
its digits are same then number is armstrong. Example : INPUT : N =
153, Output : Sum of cubes of digits = 1'+5+3' = 1+125+27 = 153, so
153 is an armstrong number, but 166 is not an armstrong number.*/
import java.util.*;
public class p47
{
public static void main(String args[])
{
Scanner br = new Scanner(System.in);
System.out.print("Enter the number: ");
int x = br.nextInt();
int dig = 0;
int sum = 0;
int x1 = x;
while(x>0)
{
dig = x % 10;
sum = sum + dig*dig*dig;
x = x/10;
}
if(sum == x1)
{
System.out.println(sum+ " is Armstrong number");
}
else
{
System.out.println(sum+ " is not an Armstrong number");
}
}
}
OUTPUT:
Enter the number: 153
153 is Armstrong number
Variable list:
Variable name Data type Reason
x int To get the user input
dig int To store the last digit
of the number
sum Int To store the sum of
cube of each digit
x1 int To store the value of x

/*48 Write a program to define a function void Automorphic(int num), to


check and print whether the argument 'num' is an Automorphic number
or not.A number is said to be an Automorphic if the number itself is
present on the right of the square of the original number. Example : 25 is
an Automorphic as its square is 625 and number 25 is present on the
right in 625. Example : 7 is not an Automorphic as its square is 49 and
number 7 is not present on the right in 49.*/
public class p48
{
public static void Automorphic(int num)
{
int temp = num;int c=0;
double square = Math.pow(temp,2);
while(temp>0)
{
temp=temp/10;
c++;
}
double a = Math.pow(10,c);
if(square%a==num)
{
System.out.println("the number is a Automorphic number ");
}
else
{
System.out.println("the number is not a Automorphic number");
}
}
}
Variable List:
Variable name Data type Use
num int Used to store the
number by the user
temp int Used to store the
number temporarily
square double Used to store the
square of a number
c int Used to store number
of square
a double Used to store the
power
Output:
14
the number is not a Automorphic number
/*49 Write program to input an integers X and Y. Print the GCD / HCF
and LCM of X and Y. Example: Input : X =9, Y= 6 Output :
GCD/HCF=3 and LCM=18.*/
import java.util. *;
class p49
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of X");
int X=sc.nextInt();
System.out.println("Enter the value of Y");
int Y=sc.nextInt();
int gcd=0;
for(int i=1;i<=X&&i<=Y;i++)
{
if(X%i==0&&Y%i==0)
{
gcd=i;
}
}
int lcm=(X *Y)/gcd;
System.out.println("The LCM= "+lcm+"\nThe GCD="+gcd);
}
}
Variable List:
Variable Data type Reason
X int To store 1st number
Y int To store 2nd number
i int For loop
j int For loop
gcd int To store greatest
common divisor
lcm int To store lowest
common multiple
Output
Enter the value of X
9
Enter the value of Y
6
The LCM= 18
The GCD=3:

/*50 Write a menu driven program to accept a number from user and
check whether it is a 'BUZZ' number or to
accept any two numbers and print the 'GCD' of them.
(a) A BUZZ number is the number which either ends with 7 or divisible
by 7.
(b) GCD (Greatest Common Divisor) of two integers is calculated by
continued division method.*/
import java.util.*;
class p50
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter one of the following option:\na=To find
buzz no\nb=To find the GCD of two no");
char a=sc.next().charAt(0);
int gcd=0;
switch(a)
{
case 'a':
System.out.println("Enter a no");
int num1=sc.nextInt();
if(num1%7==0&&b%num1==7)
{
System.out.println("The no is a buzz no");
}
else
{
System.out.println("The no is not a buzz no");
}
break;
case 'b':
System.out.println("Enter Two no");
num1=sc.nextInt();
int num2=sc.nextInt();
for(int i=1;i<=num1&&i<=num2;i++)
{
if(num1%i==0&&num2%i==0)
{
gcd=i;
}
}
System.out.println("The GCD of two no="+gcd);
break;
default:
System.out.println("Wrong Input");
}
}
}
Variable List:
Variable name Data type Reason
A char To store the character
entered by the user
Gcd int To store the greatest
common factor
Num1 int To store the 1st
number
Num2 int To store the 2nd
number
I int For loop

Output:
Enter one of the following option:
a=To find buzz no
b=To find the GCD of two no
a
Enter a no
15
import java.util.Scanner;
public class Q51 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Type 1 for a triangle");
System.out.println("Type 2 for an inverted triangle");

System.out.print("Enter your choice: ");


int ch = s.nextInt();

System.out.print("Enter number of terms");


int n = s.nextInt();

switch (ch) {
case 1:
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;

case 2:
for (int i = n; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
}
/*52. Write a menu driven program (using switch-case) to perform the
following :
(a) To input a number and print the reverse of the number.
Example: Input : 7854 , Output : Reverse number : 4587
(b) To input a number and print whether it is a perfect number or not.
A number is said to be perfect if sum of all the factors excluding the
number itself is equal to the
original number.*/
import java.util.*;
public class p52
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter 1 for printing number in reverse and press
2 for checking if it is perfect or not");
System.out.println("Enter your choice: ");
int ch = in.nextInt();
switch(ch)
{

case 1:
System.out.println("Enter a number");
int a=0;
a=in.nextInt();
int b=0;
int c=a;
while(c>0)
{
int d=c%10;
int m=d*10;
b=b*10+m;
c=c/10;
}
b=b/10;
System.out.println("The reverse="+b);
break;
case 2:
System.out.println("Enter a number: ");
int z = in.nextInt();
int x=0;
for(int y=1;y<z;y++)
{
if(z%y==0)
{
x+=y;
}
}
if(x==z)
{
System.out.println("It is a perfect number");
}
else
{
System.out.println("It is not a perfect number");
}
break;
default:
System.out.println("try again");
break;
}
}
}
Output:
Output:
Enter 1 for printing number in reverse and press 2 for checking if it is
perfect or not
Enter your choice:
1
Enter a number
145
The reverse=541
Enter 1 for printing number in reverse and press 2 for checking if it is
perfect or not
Enter your choice:
2
Enter a number:
14
It is not a perfect number
Variable Type Reason
a int To get the value of a
b Int To get the starting
point of for loop
c int To store the sum of c
and b
/* 53. Write a program to perform the following operations:
a) To input a number and print the product of first and the last digits
of that number.
b) To input the values of x and n and print the sum of the following
series:
S = 1+(x+2)/2! + (2x+3)/3! + (3x + 4)/4! + …………….n terms*/
public class p53a
{
void main(String args[])
{
Scanner sc = new Scanner(System.in);
int number, first_digit, last_digit;
System.out.print(" Please Enter any Number that you wish ");
number = sc.nextInt();
first_digit = number;
while(first_digit >= 10)
{
first_digit = first_digit / 10;
}
last_digit = number % 10;
System.out.println("The First Digit of a Given Number " + number + "
= " + first_digit);
System.out.println("The Last Digit of a Given Number " + number + " =
" + last_digit);
}
}
import java.util.*;
class 53b
{
static double sum(int x, int n)
{
double i, total = 0;
for (i = 1; i <= n; i++)
{
total = total + (Math.pow(x, i) / i);
}
System.out.println(total);
return total;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the number:");
int x = in.nextInt();
System.out.println("Enter the number of time for it to happen");
int n = in.nextInt();
53b ob = new 53b();
ob.sum(x,n);
}
}
/*54. Write a menu driven program to accept a number and check and
display whether it is a prime number
not OR the same number is an automorphic number or not (use switch-
case statement).
(a)
Prime number: A number is said to be a prime number if it is divisible
only 1 and itself and not by any other number
(b)
Automorphic number :
An automorphic number is the number which is contained in the last
digits) of its square. Example : 25 is an automorphic number as its square
is 625 and 25 is present as the last digits.*/
import java.util.*;
public class p54
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("1.Prime number");
System.out.println("2.Automorphic number");
System.out.println("enter your choice ");
int choice=sc.nextInt();
System.out.println("enter a number");
int num=sc.nextInt();
switch(choice)
{
case 1:
int c=0;
for(int i=1;i<=num;i++)
{
if(num%i==0)
{
c++;
}
}
if(c==2)
{
System.out.println(num+" is a Prime number");
}
else
{
System.out.println(num+" is not a Prime number");
}
break;
case 2:
int d=0;
while(num>0)
{
d++;
num/=10;
}
int sq =num % Math.pow(10,d));
if(sq==num)
{
System.out.println(num+"is a Automorphic number");
}
else
{
System.out.println(num+" is not a Automorphic number");
}
break;
default:
System.out.println("incorrect choice");
}
}
}
OUTPUT:
a)
1.Prime number
2.Automorphic number
enter your choice
1
enter a number
45
45 is not a Prime number
b)
1.Prime number
2.Automorphic number
enter your choice
2
enter a number
22
22 is not an Automorphic number
Variable Type Reason
choice int To get the user’s
choice for prime or
automorphic number
num int To get the user input
c int To check for the last
digit
i int To get a loop
D Int To get the last digit
Sq Int To check the sequence

/* 55. Write a program to accept an integer. Print whether the integer is a


special number or not if the sum of the factorial of each digit of a
number is same as the original number then the number special.
Example:145 is a special number, because 1!+4!+5!=1+24+120=145.
The symbol ! means factorial of number which is product of all integers
from 1 to that number (4! =123*4=24).*/
import java.util.*;
public class p55
{
public static void main(String args[])
{
int num,m,d,sum=0;
Scanner me= new Scanner(System.in);
System.out.print("Enter a number: ");
m=me.nextInt();
num=m;
while (m>0)
{
d=m%10;
int fact=1;
for(int i=1; i<=d; i++)
{
fact=fact*i;
}
sum=sum+fact;
m=m/10;
}

if(num==sum)
{
System.out.println(num+ " is a special number.");
}
else
{
System.out.println(num+ " is not a special number.");
}
}
}
OUTPUT:
Enter a number: 145
145 is a special number.
Variable Type Reason
num Int To store the user input
m Int To get the user input
d Int To get the last didgit
sum Int To get the sum of
factorial of the
numbers
fact Int To get the factorial of
a number

/* 56. Write a menu driven program to perform the following:


To print the series 0,3,8,15,24,……….. n terms(value of n is to be input
from the user)
To find and print the sum of the series: S = 1/2 +3/4 + 5/6 + 7/8 +
………….19/20*/
import java.util.*;
public class p56a
{
public static void main(String args[])
{
Scanner me=new Scanner(System.in);
int a,n;
System.out.println("Enter the number");
n=me.nextInt();
System.out.println("Series: ");
for(int i=1;i<=n;i++)
{
a=i*i-1;
System.out.println(a);
}

}
}
OUTPUT:
Enter the number
14
Series:
0
3
8
15
24
35
48
63
80
99
120
143
168
195
Variable Type Reason
a int To print the series
n int To get the number of
times from the user
i int For the loop

public class p56b


{
public static void main(String args[])
{
double a=1.0,b=2.0;
double s=0.0,s1=0.0;
System.out.println("the terms are");
for(int i=1;i<=10;i++)
{
s1=(a/b);
System.out.println(s1);
s=s+s1;
a+=2;
b+=2;
}
System.out.println("");
System.out.println("the sum is "+s);
}
}
OUTPUT:
the terms are
0.5
0.75
0.8333333333333334
0.875
0.9
0.9166666666666666
0.9285714285714286
0.9375
0.9444444444444444
0.95

the sum is 8.535515873015873


Variable Type Reason
a double To get the numerator
b double To get the
denominator
s double To get the sum of all
divisions
s1 double To store the value of
the division of a and b
i int For the loop
/* 57.Using switch-case statement, write a menu driven program to
perform the following :
To Generate and display the first 10 terms of the Fibonacci series 0, 1, 1,
2, 3, 5.........
The first two Fibonacci numbers are 0 and 1, and each subsequent
number is the sum of the previos
two.
Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, an appropriate error message should be
displayed.*/
import java.util.*;
public class p57
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("please enter 1 to see the fibnacci series or 2 to
compute the sum of digits");
int a = sc.nextInt();long sum =0;
switch(a)
{
case 1:
int n1 =0;int n2=1;int n3;
System.out.print(n1+" "+n2);
for(int i=0;i<10;i++)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
break;
case 2:
System.out.println("please enter the number");
long number = sc.nextLong();
long temp = number;
while(temp>0)
{
long digit = temp%10;
sum = sum + digit;
temp = temp/10;
}
System.out.println("the sum of the digits are "+sum);
break;
}
}
}
OUTPUT:
please enter 1 to see the fibnacci series or 2 to compute the sum of digits
1
0 1 1 2 3 5 8 13 21 34 55 89
please enter 1 to see the fibnacci series or 2 to compute the sum of digits
2
please enter the number
14
the sum of the digits are 5
Variable Type Reason
a char To get the user’s
choice
n1 int To print the first
number of Fibonacci
series
n2 int To print the second
number of the
Fibonacci series
n3 To print the rest of the
Fibonacci series by
the increment of the
number and their sum
i int For the for loop
number long To get the user input
temp long To store the value of
the number
digit long To get the last digit of
the number
sum long To get the sum of the
digits
/* 58. Using switch-case statement, write a menu driven program to
perform the following : To check and display whether the number input
by the user is a composite number or not.
A number is said to be a composite, if it has one or more than one factors
excluding I and the number itself. Example: 4, 6, 8, 9,.....
To find the smallest digit of an integer that is input.
Sample Input: 6524
Sample Output: Smallest digit = 2 */
import java.util.Scanner;
public class p58
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Composite Number");
System.out.println("Type 2 for Smallest Digit");
System.out.print("Enter your choice: ");
int ch = in.nextInt();

switch (ch)
{
case 1:
System.out.print("Enter Number: ");
int n = in.nextInt();
int c = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
c++;
}

if (c > 2)
System.out.println("Composite Number");
else
System.out.println("Not a Composite Number");
break;

case 2:
System.out.print("Enter Number: ");
int num = in.nextInt();
int s = 10;
while (num != 0)
{
int d = num % 10;
if (d < s)
s = d;
num /= 10;
}
System.out.println("Smallest digit is " + s);
break;

default:
System.out.println("Wrong choice");
}
}
}
OUTPUT:
Type 1 for Composite Number
Type 2 for Smallest Digit
Enter your choice: 1
Enter Number: 14
Composite Number
Type 1 for Composite Number
Type 2 for Smallest Digit
Enter your choice: 2
Enter Number: 17
Smallest digit is 1
Variable Type Reason
ch int To get the choice of
the user
n int To get the number
from the user
c int To check whether the
number is composite
or not
i int To get the starting
point for the for loop
num int To get the number
fromt the user
d int To store the last digit
of the number

/* 59. A special two-digit number is such that when the sum of its digits
is added to the product of its digits, result is equal to the original two-
digit number. Write a program to accept a two digit number. Add the
sum of the digits to the product of its digit. If the value is equal to the
number output the message “special 2 digit number” otherwise output
the message “not a special 2 digit number”.*/
import java.util.Scanner;

public class prg_59


{
public void checkNumber()
{

Scanner in = new Scanner(System.in);

System.out.print("Enter a 2 digit number: ");


int num1 = in.nextInt();
int num = num1;
int count = 0, digitSum = 0, digitProduct = 1;

while (num != 0)
{
int digit = num % 10;
num /= 10;
digitSum += digit;
digitProduct *= digit;
count++;
}

if (count != 2)
System.out.println("Invalid input, please enter a 2-digit
number");
else if ((digitSum + digitProduct) == num1)
System.out.println("Special 2-digit number");
else
System.out.println("Not a special 2-digit number");

}
}
OUTPUT:
Enter a 2 digit number: 58
Not a special 2-digit number
Enter a 2 digit number: 59
Special 2-digit number

Variable Type Reason


Num1 int To get the user input
num int To store the value of
the user input
count int To check whether the
number is special or
not
digit Int To get the digits of the
number
digitSum int To get the sum of the
digits
digitProduct int To get the product of
the digits

/* 60. Using the switch case statement, write a menu driven program to
perform the following:
To find and display the factors of a number input by the user, including 1
and excluding itself.
To find and display the factorial of a number input by the user. The
factorial of a number is the product of all integers from 1 to that
number.*/
import java.util.Scanner;
public class prg_60
{
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:
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;
}
}
}
OUTPUT:
1. Factors of number
2. Factorial of number
Enter your choice: 1
Enter number: 54
1 2 3 6 9 18 27
1. Factors of number
2. Factorial of number
Enter your choice: 2
Enter number: 15
Factorial = 2004310016
Variable Type Reason
Choice Int To get the choice of
user
num int To get the number
input from the user
f int To get the factorial
i int To start the for loop

/*61*/
import java.util.*;
public class p61
{
public void checkNiven()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
int num1 = num;

int digitSum = 0;

while (num != 0)
{
int digit = num % 10;
num /= 10;
digitSum += digit;
}
if (digitSum != 0 && num1 % digitSum == 0)
System.out.println(num1 + " is a Niven number");
else
System.out.println(num1 + " is not a Niven number");
}
}
OUTPUT:
Enter number: 14
14 is not a Niven number
Variable Type Reason
num int To get the input
number from the user
Num1 int To store the value of
num
digit int To store the digit of
the number
digitSum int To store the sum of
digits

/* 62*/
import java.util.*;
public class Question62
{
public void Flyodtriangle()
{
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Floyd's triangle");
System.out.println("Type 2 for an ICSE pattern");

System.out.print("Enter your choice: ");


int ch = in.nextInt();

switch (ch)
{
case 1:
int a = 1;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(a++ + "\t");
}
System.out.println();
}
break;
case 2:
String s = "ICSE";
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print(s.charAt(j) + " ");
}
System.out.println();
}
break;

default:
System.out.println("Incorrect Choice");
}
}
}
OUTPUT:
Type 1 for Floyd's triangle
Type 2 for an ICSE pattern
Enter your choice: 1
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Type 1 for Floyd's triangle
Type 2 for an ICSE pattern
Enter your choice: 2
I
IC
ICS
ICSE
Variable Type Reason
ch int To get the choice of
the user
a int To print the floyd’s
triangle from start
i int To get the starting
point of the for loop
j int To get the starting
point of the for loop
s String To store the value of s

/* 63. Write a program to accept a number and display whether it is a spy


number or not. (A number is called Spy if the sum of its digits equals the
product of its digits.)*/
import java.util.*;
class unit6_63
{
public static void main()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter a number to check it is a spy number or
not");
int a=s.nextInt();
int b=1,c=0;
int d=0;
for(;a>0;a/=10)
{
d=a%10;
c+=d;
b=d;
}
if(c==b)
{
System.out.println(" It is a spy number ");
}
else
{
System.out.println(" it is a not spy number");
}
}
}
OUTPUT:
Enter a number to check it is a spy number or not
54
it is a not spy number
Variable Type Reason
A Int To get the user input
B Int To store the value of d
and check for it to be
a spy number
C Int To store the sum of
each digit
d Int To store the last digit
of the number

/*64*/
import java.util.Scanner;

public class Q64


{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Choice 1 or 2");
int option=s.nextInt();
switch(option)
{
case 1:
System.out.println("Enter a number");
double x=s.nextDouble();
double sum=0;
for(int i=1;i<=20;i++)
{
if(x%2==0)
{
sum+=Math.pow(x, i);
}
if(x%2!=0)
{
sum-=Math.pow(x, i);
}
}
System.out.println("Sum= "+sum);
break;
case 2:
System.out.print("Enter the number of terms: ");
int n = s.nextInt();
int j = 0, c;
for (c = 1; c <= n; c++)
{
j = j * 10 + 1;
System.out.print(j + " ");
}
break;
default:
System.out.println("Invalid Input");
}
}
}
OUTPUT:
Choice1 or 2
1
Enter a number
24
Sum= 4.194770836007586E27
Variable Type Reason
option Int To get the choice of
the user
x Double To get the user input
sum double To store the value of
the sum
i int To get the starting
point of the for loop
n int To get the user input

/* 65. Write a program to accept an integer number and print the


frequency of each digit present in the number.*/
import java.util.*;
public class p65
{
public static void main ()

Scanner sc = new Scanner (System.in);

int a,r,n,i,c;

System.out.println ("Enter a number");

a=sc.nextInt ();

for (i=0;i <10;i++)

c=0;
n=a;

while (n!=0)

r=n%10;

if (r==i)

c++;

n/=10;

System.out.println ("Frequency of "+ i +" is :"+ c);

}
VARIABLE TYPE REASON
a int To get the user input
n int To store the value of a
r int To store the last digit
of the number
i int For loop
c int To check for the
frequency

You might also like