0% found this document useful (0 votes)
10 views46 pages

Java Loops

Uploaded by

yoganeyatk.cs23
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)
10 views46 pages

Java Loops

Uploaded by

yoganeyatk.cs23
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/ 46

JAVA LOOPS

[Link] the first 10 natural no’s:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i;
[Link]("The first 10 natural no's: ");
for(i=1;i<=10;i++)
{
[Link](i);
}
}
}

2. Sum of n natural no’s:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,n,sum=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the number: ");
n = [Link]();
for(i=1;i<n;i++)
{
sum = sum + i;
}
[Link]("The sum of n natural no's are: " + sum);
}
}

Sum of n even numbers:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int n,i,sum=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
i=0;
while(i<=n)
{
if(i%2==0)
{
sum += i;
}
i++;
}
[Link]("The sum of n even no's : %d",sum);

}
}

3. Input n numbers and find their sum and avg:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,n,sum=0,num;
double avg;
Scanner inp = new Scanner([Link]);
[Link]("Enter the no of numbers: ");
n = [Link]();
for(i=0;i<n;i++)
{
num = [Link]();
sum = sum + num;
}
avg = sum/n;
[Link]("The sum of no's are " + sum + " and the avg is "
+ avg);
}
}

4. Input n numbers and find cube of each num:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,n,cube=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the no of numbers: ");
n = [Link]();
for(i=0;i<n;i++)
{
int num = [Link]();
cube = num*num*num;
[Link]("The number is %d and cube of %d is:
%d\n",num,num,cube);
}

}
}
5. Display the multiplication table for given integer upto n terms:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,multi;
Scanner inp = new Scanner([Link]);
[Link]("Enter the n: ");
n = [Link]();
for(i=0;i<=n;i++)
{
multi = n * i;
[Link]("%d X %d = %d\n",n,i,multi);
}
}
}

6. Sum of n odd terms:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,n,sum=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;n>0;i+=2)
{
[Link]("%d\n",i);
sum = sum + i;
n--;
}
[Link]("\nThe sum of odd Natural numbers upto %d
terms is: %d",n,sum);
}
}

7. Right triangle with numb increment with row:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
[Link]("%d ",j);
}
[Link]("\n");
}

}
}
o/p:
1
12
123
12345

8. Number which repeats a number in a row.


import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
[Link]("%d ",i);
}
[Link]("\n");
}
}
}

o/p:
1
22
333
4444

9. Right angle triangle with the number increased by 1 (or) Floyd’s


triangle:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,k=1;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
[Link]("%d ",k++);
}
[Link]("\n");
}
}
}

o/p:
1
23
456
7 8 9 10

PASCAL Triangle:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,c=1,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
for(i=0;i<n;i++)
{
for(j=i;j<=n;j++)
{
[Link](" ");
}
for(j=0;j<=i;j++)
{
if(j == 0 || i == 0)
{
c = 1;
}
else
{
c = c * (i-j+1)/j;
}
[Link]("%d ",c);
}
[Link]("\n");
}
}
}

10. A Pyramid with a number that repeats in the same row:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,k=1;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
[Link](" ");
}
for(j=1;j<=i;j++)
{
[Link]("%d ",i);
}
[Link]("\n");
}
}
}

o/p:
1
22
333
4444
55555

11. Diamond:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,k=1;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<n;i++)
{
for(j=i;j<=n;j++)
{
[Link](" ");
}
for(j=1;j<i;j++)
{
[Link]("*");
}
for(j=1;j<=i;j++)
{
[Link]("*");
}
[Link]("\n");
}
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
[Link](" ");
}
for(j=i;j<n;j++)
{
[Link]("*");
}
for(j=i;j<=n;j++)
{
[Link]("*");
}
[Link]("\n");

}
}
}
o/p:
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*

[Link] full pyramid:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
[Link](" ");
}
for(j=i;j<=n;j++)
{
[Link]("* ");
}
[Link]("\n");
}
}
}
o/p:
*****
****
***
**
*
13. Left inverted pyramid:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
[Link]("* ");
}
for(j=1;j<=i;j++)
{
[Link](" ");
}
[Link]("\n");
}
}
}
o/p:
*****
****
***
**
*
14. Right Inverted pyramid:
import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
[Link](" ");
}
for(j=i;j<=n;j++)
{
[Link]("*");
}
[Link]("\n");
}
}
}
o/p:
******
*****
****
***
**
*
15. Right angle triangle(right):
import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
[Link](" ");
}
for(j=1;j<=i;j++)
{
[Link]("*");
}
[Link]("\n");
}
}
}
o/p:
*
**
***
****
*****

[Link] angle triangle(left):

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
[Link]("* ");
}
[Link]("\n");
}
}
}
o/p:
*
**
***
****
*****

17. Full Pyramid:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
[Link](" ");
}
for(j=1;j<=i;j++)
{
[Link]("* ");
}
[Link]("\n");
}
}
}
o/p:
*
**
***
****
*****

18. W3 @ triangle :

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
[Link](" ");
}
for(j=1;j<=i;j++)
{
[Link]("@");
}
[Link]("\n");
}
}
}
o/p:
@
@@
@@@
@@@@
@@@@@

[Link] diamond:
import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,k=1;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
[Link](" ");
}
for(j=1;j<i;j++)
{
[Link]("%d",k--);
}
for(j=1;j<=i;j++)
{
[Link]("%d",k++);
}
[Link]("\n");
}

for(i=1;i<=n;i++)
{
for(j=0;j<=i;j++)
{
[Link](" ");
}
for(j=n-i;j>=1;j--)
{
[Link]("%d",j);
}
for(j=2;j<=n-i;j++)
{
[Link]("%d",j);
}
[Link]("\n");
}
}
}
o/p:
1
212
32123
4321234
543212345
4321234
32123
212
1

20. Alphabet diamond:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n;
Scanner inp = new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
for(i=1;i<n;i++)
{
char p ='A';
for(j=i;j<=n;j++)
{
[Link](" ");
}
for(j=1;j<i;j++)
{
[Link]("%c",p++);
}
for(j=1;j<=i;j++)
{
[Link]("%c",p--);
}
[Link]("\n");
}

for(i=1;i<=n;i++)
{
char p='A';
for(j=1;j<=i;j++)
{
[Link](" ");
}
for(j=i;j<n;j++)
{
[Link]("%c",p++);
}
for(j=i;j<=n;j++)
{
[Link]("%c",p--);
}
[Link]("\n");
}
}
}
o/p:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A

21. Palindrome or not if it multiply else sum:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,gno,count=0;
Scanner inp= new Scanner([Link]);
[Link]("Enter n: ");
n = [Link]();
gno = n;
int rem,rev=0;
while(n>0)
{
count++;
n=n/10;
}
int a = count;
while(n>0)
{
rem = n%10;
rev = (rev*10) + rem;
n = n/10;
}
int mul=1,sum=0,r=0;
if(gno==rev)
{
for(i=0;i<a;i++)
{
r = gno%10;
mul = mul*r;
gno=gno/10;
}
[Link]("%d",mul);
}
else if(gno!=rev)
{
for(j=0;j<a;j++)
{
r = gno%10;
sum = sum + r;
gno = gno/10;
}
[Link]("%d",sum);
}
}
}
(or)

import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner([Link]);
int n = [Link]();
int temp=n;
int rem,rev=0,sum=0,mul=1;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n/=10;
sum+=rem;
mul*=rem;
}
if(temp==rev)
{
[Link](mul);
}
else{
[Link](sum);
}
}
}

22. Sum of first and last digit:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int n,i,j,first,last,sum=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
last = n % 10;
int a=0;
while(n!=0)
{
a = n % 10;
n = n/10;
}
first = a;
sum = first + last;
[Link]("The sum of 1st and last digit is: %d",sum);

}
}

23. Automorphic number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int n,square,gno,flag=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
square = n * n;
gno = n;
while(gno>0)
{
if(gno % 10 != square % 10)
{
flag=1;
break;
}
gno = gno/10;
square = square/10;
}
if(flag==0)
{
[Link]("%d is Automorphic number.",n);
}
else
{
[Link]("%d is not an automorphic number.",n);
}

}
}

24. Perfect number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,n,sum=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
for(i=1;i<n;i++)
{
if(n%i==0)
{
[Link]("%d ",i);
sum = sum + i;
}
}
[Link]("\nThe sum is %d",sum);
if(sum == n)
{
[Link]("\n%d is perfect number.",n);
}
else
{
[Link]("\n%d is not a perfect number.",n);
}
}
}

25. Happy number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int n,gno,sum=0,dig;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
gno = n;
while(n != 1 && n != 4)
{
sum=0;
while(n>0)
{
dig = n % 10;
sum = sum + dig*dig;
n = n/10;

}
n = sum;
}
if(n == 1)
{
[Link]("%d is a Happy number.",gno);
}
else
{
[Link]("%d is not a happy number.",gno);
}
}
}

26. Armstrong number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,n,sum=0,count=0,gno,rem=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
gno = n;
while(n!=0)
{
count++;
n = n/10;
}
n = gno;
while(n!=0)
{
rem = n%10;
sum = sum + (int)[Link](rem,count);
n = n/10;
}
if(sum == gno)
{
[Link]("%d is an Armstrong num.",gno);
}
else
{
[Link]("%d is not Armstrong num.",gno);
}
}
}

27. Prime number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,n,flag=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n= [Link]();
if(n<2)
{
flag=1;
}
else
{
for(i=2;i*i<=n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
}
if(flag==0)
{
[Link]("%d is a prime number.",n);
}
else
{
[Link]("%d is not a prime number",n);
}
}
}

28. Perfect square number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,flag=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
for(i=0;i*i<=n;i++)
{
if(i*i == n)
{
flag=1;
break;
}
}
if(flag==1)
{
[Link]("%d is a perfect square number",n);
}
else
{
[Link]("%d is not perfect square number",n);
}
}
}

29. Harshad number:


import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,flag=0,gno,sum=0,rem=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
gno=n;
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
if(gno%sum==0)
{
[Link]("%d is a harshad number.",gno);
}
else
{
[Link]("%d is not a harshad number.",gno);
}
}
}

30. Kaprekar number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,square,gno,count=0,part,right,left;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
gno=n;
square = n * n;
while(gno>0)
{
count++;
gno = gno/10;
}
part = (int)[Link](10, count);
right = square%part;
left = square/part;
if(right+left==n)
{
[Link]("%d is a Kaprekar number",n);
}
else
{
[Link]("%d is not a kaprekar number",n);
}

}
}
31. N prime numbers:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i=2,j,n,count=0,flag=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
while(count<n)
{
flag=0;
for(j=2;j*j<=i;j++)
{
if(i%j==0)
{
flag=1;
break;

}
}
if(flag==0)
{
[Link]("%d ",i);
count++;
}
i++;
}

}
}

32. 1 to N prime number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,count=0,flag=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
for(i=2;i<=n;i++)
{
flag=0;
for(j=2;j*j<=i;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
[Link]("%d ",i);
}
}

}
}

33. Twin prime number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,count=0,flag=0,flag2=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
for(i=2;i<=n-2;i++)
{
flag=0;
flag2=0;
for(j=2;j*j<=i;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
for(j=2;j*j<=i+2;j++)
{
if((i+2)%j==0)
{
flag2=1;
break;
}
}
if(flag==0 && flag2==0)
{
[Link]("(%d, %d)\n",i,i+2);
}
}

}
}

34. Abundant number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,n,sum=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
for(i=1;i<n;i++)
{
if(n%i==0)
{
[Link]("%d ",i);
sum = sum + i;
}
}
[Link]("\nThe sum is %d",sum);
if(sum > n)
{
[Link]("\n%d is Abundant number.",n);
}
else
{
[Link]("\n%d is not a Abundant number.",n);
}
}
}

35. Strong number:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int i,j,n,gno,fact,rem,sum=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
gno = n;
while(n!=0)
{
rem = n%10;
fact=1;
for(i=1;i<=rem;i++)
{
fact = fact*i;
}
sum = sum + fact;
n = n/10;
}
if(sum == gno)
{
[Link]("%d is a Strong number.",gno);
}
else
{
[Link]("%d is not a Strong number.",gno);
}

}
}

36. Sum of middle terms:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int n,b,c,sum=0;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n = [Link]();
c = n/10;
while(c>10)
{
b = c %10;
sum = sum + b;
c = c/10;
}
[Link]("%d",sum);

}
}

37. GCD and LCM:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int n1,n2,gcd=0,lcm,i=1,small;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n1 = [Link]();
n2 = [Link]();
if(n1<n2)
{
small = n1;
}
else
{
small = n2;
}
while(i<=small)
{
if(n1%i==0 && n2%i==0)
{
gcd = i;
}
i++;
}
lcm = (n1*n2)/gcd;
[Link]("GCD is %d\n",gcd);
[Link]("LCM is %d",lcm);

}
}
38. LCM of 2 no’s:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int n1,n2,max,flag=1;
Scanner inp = new Scanner([Link]);
[Link]("Enter the num: ");
n1 = [Link]();
n2 = [Link]();
if(n1>n2)
{
max = n1;
}
else
{
max = n2;
}
while(flag==1)
{
if(max%n1==0 && max%n2==0)
{
[Link]("%d",max);
break;
}
++max;
}

}
}

39. The sum of n terms in AP:


import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int a,d,n,sum=0,i,term;
Scanner inp = new Scanner([Link]);
[Link]("Enter the first term(a): ");
a = [Link]();
[Link]("Enter the diffrence(d): ");
d = [Link]();
[Link]("Enter the no of terms(n): ");
n = [Link]();
for(i=0;i<n;i++)
{
term = a + (i*d);
sum = sum + term;
}
[Link]("The sum of %d terms in AP: %d",n,sum);

}
}
40. The sum of n terms in GP:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int a,r,n,sum=0,i,term;
Scanner inp = new Scanner([Link]);
[Link]("Enter the first term(a): ");
a = [Link]();
[Link]("Enter the ratio(r): ");
r = [Link]();
[Link]("Enter the no of terms(n): ");
n = [Link]();
term = a;
for(i=0;i<n;i++)
{
sum = sum + term;
term = term * r;
}
[Link]("The sum of %d terms in GP: %d",n,sum);
}
}

41. The sum of n terms in HP:

import [Link];
import [Link];
public class Main
{
public static void main(String[] args)
{
int a,d,n,i;
Scanner inp = new Scanner([Link]);
[Link]("Enter the first term(a): ");
a = [Link]();
[Link]("Enter the diffrence(d): ");
d = [Link]();
[Link]("Enter the no of terms(n): ");
n = [Link]();
double sum = 0.0,term;
for(i=0;i<n;i++)
{
term = a + (i*d);
sum = sum + 1.0 / term;
}
[Link]("The sum of %d terms in AP: %f",n,sum);
}
}

42. Each number incremented by one

import [Link];

public class Main {


public static void main(String[] args) {
Int n,rem,rev=0, i;
Scanner inp = new Scanner([Link]);
n = [Link]();
int gno = n;

while(gno > 0) {
rem = gno % 10;
rev = rev * 10 + rem;
gno /= 10;
}
while(rev > 0) {
rev2 = rev % 10;
rev /= 10;

if(rev2 == 9) {
rev2 = 0;
} else {
rev2 += 1;
}
[Link](rev2);
}
}
}

43. Palindrome between range:

import [Link];
public class Main
{
public static void main(String[] args)
{
int i,start,end;
Scanner inp = new Scanner([Link]);
start = [Link]();
end = [Link]();
for(i=start;i<=end;i++)
{
int num = i;
int rev = 0,rem=0;
int gno = num;
while(num>0)
{
rem = num % 10;
rev= rev*10 + rem;
num = num/10;
}
if(gno == rev)
{
[Link]("%d ",gno);
}
}
}
}

44. Triangular number:

import [Link];
public class Main
{
public static void main(String[] args)
{
int i=1,trianglenum=0,n;
Scanner inp = new Scanner([Link]);
n = [Link]();
while(trianglenum<n)
{
trianglenum += i;
i++;
}
if(trianglenum == n)
{
[Link]("%d is a triangle number.",n);
}
else
{
[Link]("%d is not a triangular number.",n);
}
}
}

45. Advanced Prime Number:

import [Link];
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner([Link]);
int n = [Link]();
int j,flag=1,num;
for(j=2;j*j<=n;j++)
{
if(n%j==0)
{
flag=0;
break;
}
}
if(flag==1)
{
num = (n*2)+1;
flag=1;
for(j=2;j*j<=num;j++)
{
if(num%j==0)
{
flag=0;
break;
}
}
}
if(flag==1)
{
[Link]("%d is Advanced prime number.",n);
}
else
{
[Link]("%d is not a Advanced prime number.",n);
}

}
}

46. Count the number of prime numbers less than n

import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Scanner inp = new Scanner([Link]);
int num = [Link]();
int i, j, flag, count = 0;
if (num > 2) {
for (i = 2; i < num; i++) {
flag = 0;
for (j = 2; j * j <= i; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
count++;
}
}
}
[Link]("%d", count);
}
}

47. Elevator question [Link] attempts :

import [Link];
import [Link];
public class Main
{
​ public static void main(String[] args) {
​ ​ Scanner inp = new Scanner([Link]);
​ ​ int a = [Link]();
​ ​ int b = [Link]();
​ ​ int c = [Link]();
​ ​ int d = [Link]();
​ ​ int e = [Link]();
​ ​ int f=0,i,j,k,l,m=0;
​ ​ for(i=1;i<a;i++)
​ ​ {
​ ​ f=i;
​ ​ }
​ ​ i = [Link](b-a);
​ ​ j = [Link](c-b);
​ ​ k = [Link](d-c);
​ ​ l = [Link](e-d);
​ ​ m = f+i+j+k+l;
​ ​ [Link]("%d",m);
​ }
}

48. Next 5 palindromes:

import [Link];
public class Main
{
public static void main(String[] args)
{
int i,n;
Scanner inp = new Scanner([Link]);
n = [Link]();
int count=0;
for(i=n;count<5;i++)
{
int num = i;
int rev=0,rem=0;
int gno = num;
while(num!=0)
{
rem = num%10;
rev=rev*10 + rem;
num/=10;
}
if(rev==gno)
{
[Link]("%d ",gno);
count++;
}
}
}
}

You might also like