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

St10Assignment 2

The document contains 20 programming problems involving checking if numbers are twin primes, converting case of characters, finding products and sums of series, checking if numbers are composite, pronic or palindromes, and performing other mathematical operations on numbers. Each problem is presented with sample code to solve the problem in Java using concepts like loops, conditionals, arithmetic and mathematical operations.

Uploaded by

muhammedyafis23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

St10Assignment 2

The document contains 20 programming problems involving checking if numbers are twin primes, converting case of characters, finding products and sums of series, checking if numbers are composite, pronic or palindromes, and performing other mathematical operations on numbers. Each problem is presented with sample code to solve the problem in Java using concepts like loops, conditionals, arithmetic and mathematical operations.

Uploaded by

muhammedyafis23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

5

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”);

11. Input n alphabets. Convert it into their opposite case.


System.out.println(“Enter number of alphabets”);
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
System.out.println(“Enter alphabet”);
char c=sc.next().charAt(0);
if(c>='A'&&c<='Z')
{
c=(char)(c+32);
System.out.println("Alphabet in small letter: "+c);
}
else if(c>='a'&&c<='z')
{
c=(char)(c-32);
System.out.println("Alphabet in small letter: "+c);
}
else
System.out.println(c+" is not an alphabet");
}

5
6

12. Print the product of the first n terms of the series 1,12,123,1234…….

System.out.println(“Enter number of terms”);


int n=sc.nextInt();
long a=1,b=2,p=1;
for(int i=1;i<=n;i++)
{
p=p*a;
a=a*10+b++;
}
System.out.println(“Product: ”+p);

13. Find S where S= 1 -3X3+ 5X33-7X333……………….for 10 terms


System.out.println(“Enter value for X”);
double X=sc.nextDouble();
int a=3,b=3, sign=-1;
double S=1;
for(int i=1;i<=10;i++)
{
S=S+a*Math.pow(X,b)*sign;
a=a+2;
b=b*10+3;
sign=-sign;
}
System.out.println(“S = ”+S);

14. Find H where

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

16. Input a number . Check if it is composite or not


System.out.println(“Enter number”);
int n = sc.nextInt();
boolean flag=true;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{ flag=false; break;
}
}
if(n!=1&&flag=false)
System.out.println(“Composite”);
else
System.out.println(“Not Composite);
17. Input a number. Check if it is a pronic number or not
Using Math.sqrt() and without loop:
System.out.println(“Enter number”);
int n = sc.nextInt(); //20
int x=(int)Math.sqrt(n); //4
if(x*(x+1)==n) //20==20
System.out.println(“Pronic”);
else
System.out.println(“Not Pronic);
Using loop:
System.out.println(“Enter number”);
int n = sc.nextInt(); //25
boolean flag=false;
for(int i=1;i<=n/2;i++)
{
if(i*(i+1)==n)
{
flag=true; break;
}
}
if(flag==true)
System.out.println(“Pronic”);
else
System.out.println(“Not Pronic);
18. Print all 3-digit magical numbers
for(int n=100;n<=999;n++) //to generate 3 digit numbers
{ int i=n;
do
{ int s=0,d;
while(i>0)
{
d=i%10;
s=s+d;
i=i/10;
}
i=s;
}
while(s>=10);
if(s==1)
System.out.print(n+“\t”);
}
8
9

19. INPUT n NUMBERS:-


A) Product of the squares of the largest digit of the numbers
B) Print the square root of all numbers
C) Count the number of all three-digit positive numbers divisible by 6
D) Print the smallest and largest digit of each number
System.out.print(“Enter number of numbers”);
int n = sc.nextInt();
long p=1;
for(int i=1;i<=n;i++)
{ System.out.print(“Enter number : ”);
int num = sc.nextInt();
//A) To find product of squares of largest digit of numbers
int j=num,d,largest=0;
while(j>0) //297>0 29>0 2>0 0>0
{
d=j%10; //7 9 2
if(d>largest) //7>0 9>7 2>9
largest=d; //7 9
j=j/10; //29 2 0
}
p=p*(largest*largest); //81
//B) To print square root of numbers
System.out.println(“Square root of “+num+ “ : ”+Math.sqrt(num));
//C) To count all three-digit positive numbers divisible by 6
int c=0;
if(num>=100&&num<=999 && num>0 && num%6==0)
c++;
//D) To print the smallest and largest digit of each number
int k=num,lar=0,sm=9;
while(k>0)
{
d=k%10;
if(d>lar)
lar=d;
if(d<sm)
sm=d;
k/=10;
}
System.out.println(“Largest : ”+lar);
System.out.println(“Smallest : ”+sm);
} //for loop close
System.out.println(“Product of squares of largest digits : ”+p);
System.out.println(“Count of all 3 digit positive numbers divisible by 6 : ”+c);
20. Check if a number is an odd palindrome number.
System.out.print(“Enter number: ”);
int n = sc.nextInt();
int i=n,d,rev=0;
while(i>0)
{ d=i%10;
rev=rev*10+d;
i=i/10;
}
if(rev==n&&n%2==1)
System.out.println(“Odd Palindrome”);
else
System.out.println(“Not an odd Palindrome”);
9

You might also like