Assignment 3
Assignment 3
/*
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
/* 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
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
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
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++;
}
}
/*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
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
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;
}
}
}
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");
}
}
}
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;
}
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
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
/*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");
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
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
}
}
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
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;
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
/* 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");
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
/*64*/
import java.util.Scanner;
int a,r,n,i,c;
a=sc.nextInt ();
c=0;
n=a;
while (n!=0)
r=n%10;
if (r==i)
c++;
n/=10;
}
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