St10Assignment 2
St10Assignment 2
St10Assignment 2
Assignment Part 2
10. Input 2 numbers. Check if they are twin primes/co-primes. [Two numbers x and y are said to be
twin primes if both the numbers are primes and they differ by 2. Eg. 3 & 5 , 13 and 15 ]
System.out.println(“Enter the two numbers”);
int a=sc.nextInt(); //7
int b=sc.nextInt(); //5
if(a-b==-2 || a-b==2) //To check whether two numbers differ by 2 or not
{
int i, c=0;
for(i=1;i<=a;i++) //To check whether 1st number is prime or not
{
if(a%i==0)
c++;
}
if(c==2)
{
c=0;
for(i=1;i<=b;i++) //To check whether 1st number is prime or not
{
if(b%i==0)
c++;
}
if(c==2)
System.out.println(“Twin Prime”);
else //If 2nd number is not prime
System.out.println(“Not Twin Prime”);
}
else //If 1st number is not prime
System.out.println(“Not Twin Prime”);
}
else //If difference between two numbers is not 2
System.out.println(“Not Twin Prime”);
5
6
12. Print the product of the first n terms of the series 1,12,123,1234…….
H = + + ……………………….
int a=4;
double H=0.0;
for(int i=2;i<=10;i++)
{
long s=0,f=1;
for(int j=1;j<=i;j++)
s=s+j;
for(int j=1;j<=a;j++)
f=f*j;
H=H+s/f;
a=a+4;
}
System.out.println(“H = ”+H);
15. Print the product of all odd factors and the sum of all even factors of a number
System.out.println(“Enter number”);
int n = sc.nextInt();
int p=1,s=0;
for(int i=1;i<=n;i++)
{
if(n%i==0&&i%2==1)
6
7
p=p*i;
else if(n%i==0&&i%2==0)
s=s+i;
}
System.out.println(“Product of odd factors = ”+p);
System.out.println(“Sum of even factors = ”+s);
7
8