1.
Print a pyramid of prime numbers up to n rows:
package college;
import java.util.Scanner;
public class PyramidofPrimeNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter number of rows in Pyramid:");
int row = input.nextInt();
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) {
//System.out.print(i + " ");
array[index]=i;
index++;
nos++;
}
i++;
}
System.out.println();
//printing pyramid
int y=0;
for(i=1;i<=row;i++) {
for(int j=row-i;j>=1;j--) {
System.out.print(" ");
}
for(int x=0;x<i;x++) {
System.out.print(array[y]+" ");
y++;
}
System.out.println();
}
input.close();
}
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 java.util.Scanner;
public class AmstrongRange {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter Range:");
int range=input.nextInt();
for(int i=100;i<range;i++) {
if(ams(i)==1) {
System.out.print(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++)
System.out.print(" ");
for(int j=0;j<=i;j++) {
System.out.print(num+" ");
num=num*(i-j)/(j+1);
}
System.out.println();
}
}
Output:
1
11
121
1331
14641
4. Print first n Fibonacci numbers and display sum of even and odd
numbers separately:
package college;
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter range:");
int range=input.nextInt();
fib(range);
input.close();
}
private static void fib(int range) {
int a=0,b=1,c=0,odd=0,even=0;
int[] array=new int[range];
System.out.print(b+" ");
for(int i=0;i<range;i++) {
c=a+b;
a=b;
b=c;
System.out.print(c+" ");
array[i]=c;
}
System.out.println();
for(int index:array) {
if(index%2==0)
even+=index;
else
odd+=index;
}
System.out.println("Sum of even numbers: "+even);
System.out.println("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<array.length;i++) {
for(int j=0;j<array.length;j++) {
if(i==j) {
sum+=array[i][j];
}
}
}
int j=array.length-1;
System.out.println("Sum of main diagonal: "+sum);
for(int i=0;i<array.length;i++) {
sec+=array[i][j];
j--;
System.out.println("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 java.util.Scanner;
public class NumberPalindrome {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter any number: ");
int number=input.nextInt();
int dup=number,result=0;
while(number>0) {
result=(result*10)+number%10;
number/=10;
}
if(result==dup)
System.out.println("The number is palindrome.");
else
System.out.println("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;
}
System.out.println("GCD = " + gcd);
System.out.println("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[s.length()];
for(int i=0;i<s.length();i++) {
if(visited[i])
continue;
int count=1;
for(int j=i+1;j<s.length();j++) {
if(s.charAt(i)==s.charAt(j)) {
count++;
visited[j]=true;
}
}
System.out.println(s.charAt(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 java.util.Scanner;
public class DoubleNumberSeries {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter range:");
int range=input.nextInt();
int a=1,b=1,sum=b;
for(int i=0;i<range;i++) {
System.out.print(b+" ");
sum+=b;
a=b;
b=a+b;
}
System.out.println("\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 java.util.Scanner;
public class Grade {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int java,python,c,ds,dbms,avg=0;
System.out.println("Enter java marks:");
java=input.nextInt();
System.out.println("Enter python marks:");
python=input.nextInt();
System.out.println("Enter c programming marks:");
c=input.nextInt();
System.out.println("Enter data structures marks:");
ds=input.nextInt();
System.out.println("Enter dbms marks:");
dbms=input.nextInt();
avg=(java+python+c+ds+dbms)/5;
if(avg>=90)
System.out.println("You secured A Grade");
else if(avg<90 && avg>=80)
System.out.println("You secured B Grade");
else if(avg<80 && avg>=60)
System.out.println("You secured C Grade");
else if(avg<60 && avg>=40)
System.out.println("You secured D Grade");
else if(avg<40)
System.out.println("You Failed!!!...");
else
System.out.println("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