Comp Project t2
Comp Project t2
Write a program to compute and display the sum of the following series:
S=1+2/1+2+ 1+2+3/ 1+2+3+ --------------- +1+2+3−−−−−+𝑛/1+2+3−−−−−+𝑛
A1.
import java.util.Scanner;
class q1
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n:");
int n =sc.nextInt();
int y=0, d=0,sum=0;
for(int i=1;i<=n;i++)
{
sum=sum+i+(i+1);
d=sum/sum;
y=y+d;
}
System.out.println(y);
}
}
VARIABLE NAME DATA TYPE FUNCTION
n int Stores the value of the
number entered by the
user.
i int Stores value for iterations.
sum int Calculates the sum of
numerator and
denominator.
d int Calculates the quotient of
the sum
y int Calculates the total sum
1
2
Q2. A number is called Disarium number if the sum of its power of the positions from
left to right is equal to the number. Write a program to input a number and check
whether it is a Disarium number or not. Print the message accordingly.
Example: 1 + 3*3 + 5*5*5 = 1 + 9 + 125 = 135
A2.
import java.util.Scanner;
class q2
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int num=n,c=0,d=0,i,sum=0;
while(num>0)
{
d=num%10;
num=num/10;
c++;
}
d=0;
num=n;
double x;
for(i=c;i>=1;i--)
{
d=num%10;
num=num/10;
x = Math.pow(d,i);
sum=sum+(int)x;
3
}
if(sum==n)
{
System.out.println("Disarium number");
}
}
}
4
Q3. Write a program to print the following pattern:
****
***
**
*
**
***
****
A3.
class q3
{
public static void main()
{
int x=0;
for (int i=1;i<=4;i++)
{
for(int j=4;j>=i;j--)
{
System.out.print("*");
}
System.out.println();
}
for(x=2;x<=4;x++)
{
for(int y=1;y<=x;y++)
{
System.out.print("*");
5
}
System.out.println();
}
}}
6
Q 4. Write a program to print the following pattern:
A
BC
DEF
GHIJ
KLMNO
A4.
class q4
{
public static void main()
{ int num =65;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print((char)num);
num=num+1;
}
System.out.println();
}
}
}
VARIABLE NAME DATA TYPE FUNCTION
i int Stores the value for the
iterations.
j int Stores the value for the
second iterations.
7
num int Stores value to find the
character.
8
Q 5. Using switch statement, write a menu driven program for the following:
a) To find and display the sum of the following series:
S = x^1- x^2 + x^3- x^4 + x^5- ………… - x^20; where x = 2
b) To display the series:
1 11 111 1111…..upto 10 terms
A5.
import java.util.Scanner;
class q5
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("1.display the sum of the following series:S = x1- x2 + x3- x4
+ x5- ………… - x20; where x = 2\n2.display the series:1 11 111 1111…..upto 10
terms");
int n =sc.nextInt();
double x=0;
int sum=0;
switch(n)
{
case 1:
for(int i =1;i<20;i=i+2)
{
x=Math.pow(2,i);
if(i%2==0)
sum=sum-(int)x;
else
sum=sum+(int)x;
}
9
System.out.println(sum);
break;
case 2:
for( int r =1;r<=10;r++)
{
for(int s=1;s<=r;s++)
{
System.out.print("1");
}
System.out.print((char)32);
}
break;
}
}
}
VARIABLE NAME DATA TYPE FUNCTION
n int Stores the value for
switch case.
x double Stores the power of x.
sum int Calculates the total sum.
s int Stores the value for the
iteration.
r int Stores the value for the
iteration.
10
Q 6. Write a program to print the following series:
i) 1,8,27,64,125,……n terms
ii) (1*22) , (2*32) , (3*42), ……n terms
A 6.
import java.util.Scanner;
class q6
{
public static void main()
{
Scanner sc =new Scanner(System.in);
System.out.println("Enter the value of n:");
int n = sc.nextInt();
for(int i =1;i<=n;i++)
{
System.out.println(i*i*i);
}
System.out.println();
for(int j=1;j<=n;j++)
{
System.out.println(j*((j+1)*(j+1)));
}
}
}
}
VARIABLE NAME DATA TYPE FUNCTION
n int Stores value entered by user.
i int Stores value for iteration.
j int Stores value for iteration.
11
12
Q 7. Write a program to input two numbers and check whether it is a pearre
partners. Hint: Two numbers M,N are called pearee partners, if the 1st digit of one of
them is the last digit of the 2nd and vice versa. Input two numbers and verify
whether they are pearee partners or not.
EX: (526,6735).
A 7.
import java.util.Scanner;
class q7
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers:");
int m =sc.nextInt();
int n = sc.nextInt();
int num=m;
int d =0,c=0;
while(num>0)
{
d=num%10;
num=num/10;
c++;
}
c--;
double x=Math.pow(10,c);
c=0;num=n;
while(num>0)
{
d=num%10;
13
num=num/10;
c++;
}
c--;
double y=Math.pow(10,c);
int fd1=m/(int)x;
int fd2=n/(int)y;
int ld1=m%10,ld2=n%10;
if(fd1==ld2||fd2==ld1)
System.out.println(" pearee partners");
}
}
VARIABLE NAME DATA TYPE FUNCTION
m int Stores value of first number
n int Stores value of second number
num int Takes value of m/n.
c int Counts no. of digits.
d int Finds value of digit.
x double Finds the power of 10 to the no. of digit -1 for
1st no.
y double Finds the power of 10 to the no. of digit -1 for
2nd no.
ld1 int Finds last digit of 1st no.
ld2 int Finds last digit of 2nd no.
fd1 int Finds first digit of 1st no.
fd2 int Finds first digit of 2nd no.
14
Q 8. Write a program to print the following pattern:
#5* #6* #7* #8* #9*
#5* #6* #7* #8* #9*
#5* #6* #7* #8* #9*
#5* #6* #7* #8* #9*
#5* #6* #7* #8* #9*
#5* #6* #7* #8* #9*
A 8.
class q8
{
public static void main()
{
for(int i =1;i<=6;i++)
{
for(int j=5;j<=9;j++)
{
System.out.print("#"+j+"*" +(char)32);
}
System.out.println();
}}}
VARIABLE NAME DATA TYPE FUNCTION
j int Stores value for iteration
i int Stores value for iteration.
15
Q 9. 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
the squares of the first two numbers is equal to the square of third number.
This is given by : 𝑥2 + 𝑦2 = 𝑧2
A 9.
class q9
{
public static void main()
{
double csq=0;
for(int i =1;i<=100;i++)
{
for(int j =1;j<=100;j++)
{
csq=Math.sqrt(i*i+j*j);
if(csq==Math.ceil(csq))
System.out.println(i+","+j+","+(int)csq);
}}}}
VARIBLE NAME DATA TYPE FUNCTION
i int Stores value for iterations.
j int Stores value for iterations.
Csq double Stores square root of i2+j2.
16
Q10. Write a program to perform the following operations:
To input a number and print the product of first and last digits of the
number. Example: Input : 2965 , output : Product of first and last digit
is : 10.
To input the values of x and n and print the sum of the following
series:
S= 1 + (𝑥+2)/2! + (2𝑥+3) /3! + (3𝑥+4)/4! +……………………….n terms
The symbol “!” stands for factorial and factorial of a number is the
product from 1 to that number or from the number to 1.
Example: 5! Means 1*2* 3* 4*5 or 5*4*3*2*1= 120
A 10.
import java .util.Scanner;
class q10
{
public static void main()
{
Scanner sc =new Scanner(System.in);
System.out.println("Enter a number");
int num=sc.nextInt();
int temp=num,ld=0;
while(temp>=10)
{
temp=temp/10;
}
ld=num%10;
System.out.println(temp*ld);
17
int q=0;
float term=0,nume=0,fact=1,result=1;
System.out.println("Enter the value of x:");
float x =sc.nextFloat();
System.out.println("Enter the value of n:");
int n =sc.nextInt();
for(int i=1;i<=n;i++)
{
q= i+1;
nume=(x*i)+q;
fact=1;
for(int j=1;j<=q;j++)
{
fact=fact*j;
}
term=nume/fact;
result+=term;
}
System.out.println(result);
}}
20
Q12. Write a program to input any positive integer and check whether it is a twin
prime number or not. [Hint: A twin prime is a prime number that is either 2 less or 2
more than another prime number—for example, either member of the twin prime
pair (41, 43). In other words, a twin prime is a prime that has a prime gap of two]
A 12.
import java.util.Scanner;
class q12
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int num=sc.nextInt();
int c=0;
for(int i=2;i<num-2 || i<num+2;i++)
{
if( (num-2)%i==0 && (num+2)%i==0)
c++;
}
if(c==0)
System.out.println("Twin prime number");
}}
VARIABLE NAME DATA TYPE FUNCTION
i int Stores value for iteration.
num int Stores value entered by the user.
c int Increments when the condition is true.
21
Q13. Write a program to print the following pattern:
1
212
32123
4321234
A13.
class q13
{
public static void main()
{
int i,j,k;
for(i=1;i<=4;i++)
{
for(j=i;j>=1;j--)
{
System.out.print(j+"");
}
for(k=2;k<=i;k++)
{
System.out.print(k+"");
}
System.out.println(); }}}
VARIABLE NAME DATA TYPE FUNCTION
i int Stores the value for the iteration.
j int Stores the value for the iteration.
k int Stores the value for the iteration.
22
Q14. Write a program to print the following pattern:
1
11
121
1331
A14.
class q14
{
public static void main()
{
double p=0;
for(int i=0;i<=3;i++)
{ p=Math.pow(11,i);
System.out.println((int)p);
}}}
VARIABLE NAME DATA TYPE FUNCTION
i int Stores the value for the iteration.
p double Stores the power of 11.
23
Q15. Write a program in Java to find the sum of the given series:x – x2/3! + x3/5! –
x4/7 !+ …. to n terms
A15.
import java.util.Scanner;
class q15
{
public static 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 n:");
; int n=sc.nextInt();
double p=0.0,fact=1,diff=0.0,sum=0.0;
for(int i=2;i<=n;i++)
{
p=Math.pow(x,i);
fact=1;
if(i%2==0)
{for(int j=1;j<=i+1;j++)
{
fact=j*fact;
}}
else
{
for(int j=1;j<=i+2;j++)
{
24
fact=j*fact;
}
}
diff=p/fact;
if(i%2==0)
{sum=sum-diff;}
else
{sum=sum+diff;}
}
sum=x-sum;
System.out.println(sum);
}
}
VARIABLE NAME DATA TYPE FUNCTION
n int Stores the value entered by the user.
x int Stores the value entered by the user.
p double Stores the power of x
fact double Stores the factorial.
diff double Stores the quotient of power and factorial.
sum double Calculates the total sum.
i int Stores the value for the iteration.
j int Stores the value for the iteration.
25