You are on page 1of 10

Revision Program- 1

Q1. Write a program to find the sum of the following series:


S = 2/(1x)1 - 4/(3x)2 + 6/(5x)3 - ………… n terms

import java.util.Scanner;

class Revision_1

static void main()

Scanner sc=new Scanner(System.in);// scanner object

System.out.println("Enter the total number of terms: ");

int n=sc.nextInt();

System.out.println("Enter the value of x: ");

int x=sc.nextInt();

double s=0.0;

int num=2,den=1;

for(int i=1;i<=n;i++)// n times

if(i%2!=0)// odd

s=s + num/Math.pow(den*x,i);

else// even

s=s - num/Math.pow(den*x,i);

num=num+2;
den=den+2;

System.out.println("Sum = "+s);

Revision Program- 2
Write a program to find the sum of the following series:
3 3 3
S =x2/ √𝑥 + 2 - x4/ √𝑥 + 4 + x6/ √𝑥 + 6 -………..n terms

import java.util.Scanner;
class Revision_2
{
static void main()
{
Scanner sc=new Scanner(System.in);// scanner object
System.out.println("Enter the total number of terms: ");
int n=sc.nextInt();
System.out.println("Enter the value of x: ");
int x=sc.nextInt();
double s=0.0;
int num=2;
for(int i=1;i<=n;i++)// n times
{
if(i%2!=0)//odd
s=s + Math.pow(x,num)/Math.cbrt(x+num);
else// even
s=s - Math.pow(x,num)/Math.cbrt(x+num);
num=num+2;

}
System.out.println("Sum= "+s);
}// close main
}//close class
Revision Program- 3
Write a program to find the sum of the following series:
S= 1/2a + 3/4a+ 7/8a + 15/16a + ....................n terms

import java.util.Scanner;
class Revision_3
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the total number of terms");
int n= sc.nextInt();
System.out.println("Enter the value of a");
int a= sc.nextInt();
double s=0.0;
int den=2;
for(int i=1;i<=n;i++)
{
s=s+ (Math.pow(2,i)-1)/den*a;
den=den*2;
}
System.out.println("Sum= "+s);
}
}

Revision Program- 4
Write a program to accept a number and check and display whether it is a Niven
number or not.
(Niven number is that number which is divisible by sum of its digits).
[Example: Consider the number 126. Sum of its digits is 1+2+6=9 and 126 is
divisible by 9]

import java.util.Scanner;
class Revision_4
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int num=sc.nextInt();
int temp=num;
int sum=0;
while(temp>0)
{
int dig= temp%10;// last digit got extracted
sum=sum+dig;
temp=temp/10;
}
if(num%sum==0)
System.out.println(num+" is a Niven number");
else
System.out.println(num+" is not a Niven number");
}
}

Revision Program- 5
Write a program to accept a number and check and display whether it is a spy
number or not.
(A number is spy if the sum of its digits equals to the product of its digits).
[Example: Consider the number 1124. Sum of digits= 1+1+2+4=8 and product of
digits=1*1*2*4=8]

import java.util.Scanner;
class Revision_5
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int num=sc.nextInt();
int temp=num;
int sum=0,prod=1;
while(num>0)
{
int dig= num%10;// last digit got extracted
sum=sum+dig;
prod=prod*dig;
num=num/10;
}//num=0
if(sum==prod)
System.out.println(temp+" is a Spy number");
else
System.out.println(temp+" is not a Spy number");
}
}

Revision Program- 6
Write a program to accept a number and check and display whether it is a
Munchausen number or not.
(A number is called Munchausen if sum of its each digit’s own power is equal to
the original number).
[Example: Consider the number 3435. Here 33 + 44 + 33 +55= 3435]

import java.util.Scanner;
class Revision_6
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int num=sc.nextInt();
int temp=num;
double sum=0;
while(temp>0)
{
int dig= temp%10;// last digit got extracted
sum=sum+Math.pow(dig,dig);
temp=temp/10;
}
if(num==sum)
System.out.println(num+" is a Munchausen number");
else
System.out.println(num+" is not a Munchausen number");
}
}

Revision Program- 7
Write a program to accept a number and check and display whether it is a
Disarium number or not.
(A number is called Disarium if sum of its digits powered with their respective
position is equal to the original number).
[Example: Consider the number 135. Here 11 + 32 + 53 = 135]

import java.util.Scanner;
class Revision_7
{
static void main()
{
Scanner sc=new Scanner(System.in);// scanner object
System.out.println("Enter a number: ");
int num=sc.nextInt();
int temp1=num, temp2=num, count=0;
double sum=0.0;
while(temp1>0)// counting the digits
{
count++;
temp1=temp1/10;
}// temp1=0
while(temp2>0)// extracting the digits
{
int dig=temp2%10;
sum=sum+ Math.pow(dig,count);
temp2=temp2/10;
count--;
}// temp2=0
if(num==sum)
System.out.println(num+" is a disarium number: ");
else
System.out.println(num+" is not a disarium number: ");
}
}

Revision Program- 8
Write a menu driven program using switch case to display the following series:
(i) 224 112 56 28 14 7
(ii) 27 21 16 12 9 7
(iii) 36 26 18 12 8 6

import java.util.Scanner;
class Revision_8
{
static void main()
{
Scanner sc=new Scanner(System.in);// scanner object
System.out.println("Enter 1 to print series 1, 2 to print series 2, 3 to print
series 3");
System.out.println("Enter your choice (1,2,3): ");
int choice=sc.nextInt();
int term,i,diff;
switch(choice)
{
case 1:
term=224;
for(i=1;i<=6;i++)
{
System.out.print(term+" ");
term=term/2;
}
break;

case 2:
term=27;
diff=6;
for(i=1;i<=6;i++)
{
System.out.print(term+" ");
term=term-diff;//27-6=21
diff=diff-1;
}
break;

case 3:
term=36;
diff=10;
for(i=1;i<=6;i++)
{
System.out.print(term+" ");
term=term-diff;
diff=diff-2;
}
break;

default:
System.out.println("Wrong choice");
}
}
}
Revision Program- 9
Write a program in java to find the sum of the given series:
S = 1 – 4 + 9 – 16 +…..to n terms

import java.util.Scanner;
class Revision_9
{
static void main()
{
Scanner sc=new Scanner(System.in);// scanner object
System.out.println("Enter the number of terms in the series: ");
int n=sc.nextInt();
double s=0.0;
for(int i=1;i<=n;i++)
{
if(i%2!=0)// terms at odd position
s=s+ Math.pow(i,2);
else// terms at even position
s=s- Math.pow(i,2);
}
System.out.println("Sum = "+s);
}
}

Revision Program- 10
Write a program in java to find the sum of the given series after accepting the
value of ‘a’ from the console:

(𝑎−1) (𝑎−2) (𝑎−3) (𝑎−10)


S= + + +………..
(𝑎+10) (𝑎+9) (𝑎+8) (𝑎+1)

import java.util.Scanner;
class Revision_10
{
static void main()
{
Scanner sc=new Scanner(System.in);// scanner object
System.out.println("Enter the value of a: ");
int a=sc.nextInt();
double s=0.0;
double den=10.0;
for(int i=1;i<=10;i++)
{
s=s+ (a-i)/(a+den);
den-- ;
}
System.out.println("Sum = "+s);
}
}

Revision Program- 11
Write a program to find the sum of the series:

𝑎2 𝑎2 𝑎2 𝑎2
S=1- + - +…………….
2 3 4 10

import java.util.Scanner;
class Revision_11
{
static void main()
{
Scanner sc=new Scanner(System.in);// scanner object
System.out.println("Enter the value of a: ");
int a=sc.nextInt();
double s=0.0;
for(int i=2;i<=10;i++) // to find sum of 9 terms of the series
{
if(i%2!=0)// odd
s=s+ Math.pow(a,2)/i;
else// even
s=s- Math.pow(a,2)/i;
}
System.out.println("Sum ="+(s+1));
}
}

Revision Program- 12
Write a program to input two integers and print their GCD.
[GCD of two integers is calculated by continued division method. Divide the larger
number by the smaller; the remainder then divides the previous divisor. The
process is repeated till the remainder is zero. The divisor then results the GCD.]
import java.util.Scanner;
class Revision_12
{
static void main()
{
Scanner sc=new Scanner(System.in);// scanner object
System.out.println("Enter two numbers: ");
int x=sc.nextInt();
int y=sc.nextInt();
int rem;
rem=x%y;
while(rem!=0)
{
x=y;
y=rem;
rem=x%y;
}
System.out.println("GCD ="+y);

}
}

You might also like