1.
Print a pyramid of prime numbers up to n rows:
package college;
import [Link];
public class PyramidofPrimeNumbers {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter number of rows in Pyramid:");
int row = [Link]();
int noe=row*(row+1)/2;
int[] array=new int[noe];
int index=0,nos=0,i=2;
//putting elements in array for pyramid
while(nos<noe) {
if (prime(i) == 1) {
//[Link](i + " ");
array[index]=i;
index++;
nos++;
}
i++;
}
[Link]();
//printing pyramid
int y=0;
for(i=1;i<=row;i++) {
for(int j=row-i;j>=1;j--) {
[Link](" ");
}
for(int x=0;x<i;x++) {
[Link](array[y]+" ");
y++;
}
[Link]();
}
[Link]();
}
static int prime(int n) {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
}
Output:
Enter number of rows in pyramid:
5
2
35
7 11 13
17 19 23 29
31 37 41 43 47
2. Find all Armstrong numbers between given range:
package college;
import [Link];
public class AmstrongRange {
public static void main(String[] args) {
Scanner input=new Scanner([Link]);
[Link]("Enter Range:");
int range=[Link]();
for(int i=100;i<range;i++) {
if(ams(i)==1) {
[Link](i+" ");
}
}
}
static int ams(int n) {
int x,result=0,y=n;
while(n>0) {
x=n%10;
result+=x*x*x;
n/=10;
}
if(result==y) {
return 1;
}
return 0;
}}
Output:
Enter Range:
1000
153 370 371 407
3. Print Pascal’s triangle for n rows using loops:
package college;
public class PascalTriangle {
public static void main(String[] args) {
int rows=5;
for(int i =0;i<rows;i++) {
int num=1;
for(int j=0;j<rows-i;j++)
[Link](" ");
for(int j=0;j<=i;j++) {
[Link](num+" ");
num=num*(i-j)/(j+1);
}
[Link]();
}
}
Output:
1
11
121
1331
14641
4. Print first n Fibonacci numbers and display sum of even and odd
numbers separately:
package college;
import [Link];
public class Fibonacci {
public static void main(String[] args) {
Scanner input=new Scanner([Link]);
[Link]("Enter range:");
int range=[Link]();
fib(range);
[Link]();
}
private static void fib(int range) {
int a=0,b=1,c=0,odd=0,even=0;
int[] array=new int[range];
[Link](b+" ");
for(int i=0;i<range;i++) {
c=a+b;
a=b;
b=c;
[Link](c+" ");
array[i]=c;
}
[Link]();
for(int index:array) {
if(index%2==0)
even+=index;
else
odd+=index;
}
[Link]("Sum of even numbers: "+even);
[Link]("Sum of odd numbers: "+odd);
}
}
Output:
Enter range:
10
1 1 2 3 5 8 13 21 34 55 89
Sum of even numbers: 44
Sum of odd numbers: 187
5. Find sum of main diagonal and secondary diagonal of a square
matrix:
package college;
public class SumOfDiagonal {
public static void main(String[] args) {
int[][] array= {{1,2,8},{4,5,6},{7,8,9}};
int sum=0,sec=0;
for(int i=0;i<[Link];i++) {
for(int j=0;j<[Link];j++) {
if(i==j) {
sum+=array[i][j];
}
}
}
int j=[Link]-1;
[Link]("Sum of main diagonal: "+sum);
for(int i=0;i<[Link];i++) {
sec+=array[i][j];
j--;
[Link]("Sum of secondary diagonal: "+sec);
}}
Output:
Sum of main diagonal: 15
Sum of secondary diagonal: 20
6. Reverse a number and check if it is a palindrome:
package college;
import [Link];
public class NumberPalindrome {
public static void main(String[] args) {
Scanner input=new Scanner([Link]);
[Link]("Enter any number: ");
int number=[Link]();
int dup=number,result=0;
while(number>0) {
result=(result*10)+number%10;
number/=10;
}
if(result==dup)
[Link]("The number is palindrome.");
else
[Link]("It is not a palindrome.");
}
}
Output:
Enter any number:
4554
The number is palindrome.
7. Find GCD and LCM of two integers using loops:
package college;
public class GCDLCM {
public static void main(String[] args) {
int n1 = 12, n2 = 18;
// GCD calculation using for loop
int gcd = 1;
for (int i = 1; i <= n1 && i <= n2; ++i) {
if (n1 % i == 0 && n2 % i == 0)
gcd = i;
}
// LCM calculation using while loop
int lcm = (n1 > n2) ? n1 : n2;
while (true) {
if (lcm % n1 == 0 && lcm % n2 == 0) {
break;
}
++lcm;
}
[Link]("GCD = " + gcd);
[Link]("LCM = " + lcm);
}
}
Output:
GCD = 6
LCM = 36
8. Count the frequency of each character in a given string:
package college;
public class FrequencyInString {
public static void main(String[] args) {
String s="Bharath";
boolean[] visited=new boolean[[Link]()];
for(int i=0;i<[Link]();i++) {
if(visited[i])
continue;
int count=1;
for(int j=i+1;j<[Link]();j++) {
if([Link](i)==[Link](j)) {
count++;
visited[j]=true;
}
}
[Link]([Link](i)+": "+count);
}
}
}
Output:
B: 1
h: 2
a: 2
r: 1
t: 1
9. Print a number series: 1, 2, 4, 8, 16… up to n terms and display the
sum of series:
package college;
import [Link];
public class DoubleNumberSeries {
public static void main(String[] args) {
Scanner input=new Scanner([Link]);
[Link]("Enter range:");
int range=[Link]();
int a=1,b=1,sum=b;
for(int i=0;i<range;i++) {
[Link](b+" ");
sum+=b;
a=b;
b=a+b;
}
[Link]("\nSum of the series is: "+sum);
}
}
Output:
Enter range:
10
1 2 4 8 16 32 64 128 256 512
Sum of the series is: 1024
10. Read marks for 5 subjects and assign grade based on average:
package college;
import [Link];
public class Grade {
public static void main(String[] args) {
Scanner input=new Scanner([Link]);
int java,python,c,ds,dbms,avg=0;
[Link]("Enter java marks:");
java=[Link]();
[Link]("Enter python marks:");
python=[Link]();
[Link]("Enter c programming marks:");
c=[Link]();
[Link]("Enter data structures marks:");
ds=[Link]();
[Link]("Enter dbms marks:");
dbms=[Link]();
avg=(java+python+c+ds+dbms)/5;
if(avg>=90)
[Link]("You secured A Grade");
else if(avg<90 && avg>=80)
[Link]("You secured B Grade");
else if(avg<80 && avg>=60)
[Link]("You secured C Grade");
else if(avg<60 && avg>=40)
[Link]("You secured D Grade");
else if(avg<40)
[Link]("You Failed!!!...");
else
[Link]("Invalid input");
}
}
Output:
Enter java marks: 95
Enter python marks: 86
Enter c programming marks: 87
Enter data structures marks: 75
Enter dbms marks: 90
You secured B Grade