0% found this document useful (0 votes)
2 views5 pages

Java Questions With Patterns

The document contains a series of programming exercises that demonstrate various patterns and algorithms using loops in Java. It includes examples such as star triangles, number triangles, Fibonacci series, prime number checks, Armstrong numbers, and more. Each exercise is accompanied by code snippets that illustrate the implementation of the respective pattern or algorithm.
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)
2 views5 pages

Java Questions With Patterns

The document contains a series of programming exercises that demonstrate various patterns and algorithms using loops in Java. It includes examples such as star triangles, number triangles, Fibonacci series, prime number checks, Armstrong numbers, and more. Each exercise is accompanied by code snippets that illustrate the implementation of the respective pattern or algorithm.
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

Q1 Star Triangle

*
**
***
****
*****

for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
[Link]("*");
}
[Link]();
}

Q2 Number Triangle

1
12
123
1234
12345

for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
[Link](j);
}
[Link]();
}

Q3 Inverted Star Triangle

*****
****
***
**
*

for(int i=5;i>=1;i--){
for(int j=1;j<=i;j++){
[Link]("*");
}
[Link]();
}
Q4 Repeated Number Triangle

1
22
333
4444
55555

for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
[Link](i);
}
[Link]();
}

Q5 Pyramid Pattern

*
***
*****
*******
*********

for(int i=1;i<=5;i++){
for(int s=1;s<=5-i;s++){
[Link](" ");
}
for(int j=1;j<=2*i-1;j++){
[Link]("*");
}
[Link]();
}

Q6 First 10 Natural Numbers

for(int i=1;i<=10;i++){
[Link](i+" ");
}

Q7 Even Numbers 1-50

for(int i=2;i<=50;i+=2){
[Link](i+" ");
}
Q8 Series 1 4 7 10 ...

1 4 7 10 13 ...

int num=1;
for(int i=1;i<=15;i++){
[Link](num+" ");
num=num+3;
}

Q9 Fibonacci Series (10 Terms)

0 1 1 2 3 5 8 13 21 34

int a=0,b=1;
[Link](a+" "+b+" ");
for(int i=3;i<=10;i++){
int c=a+b;
[Link](c+" ");
a=b;
b=c;
}

Q10 Sum 1 to n

1+2+3+...+n

int n=10;
int sum=0;
for(int i=1;i<=n;i++){
sum=sum+i;
}
[Link]("Sum="+sum);

Q11 Prime Number

int num=7;
int count=0;
for(int i=1;i<=num;i++){
if(num%i==0)
count++;
}
if(count==2)
[Link]("Prime");
else
[Link]("Not Prime");

Q12 Armstrong Number

int num=153;
int original=num;
int sum=0;
while(num!=0){
int digit=num%10;
sum=sum+digit*digit*digit;
num=num/10;
}
if(sum==original)
[Link]("Armstrong");
else
[Link]("Not Armstrong");

Q13 Reverse Number

int num=1234;
int reverse=0;
while(num!=0){
int digit=num%10;
reverse=reverse*10+digit;
num=num/10;
}
[Link](reverse);

Q14 Palindrome Number

int num=121;
int original=num;
int reverse=0;
while(num!=0){
int digit=num%10;
reverse=reverse*10+digit;
num=num/10;
}
if(original==reverse)
[Link]("Palindrome");
else
[Link]("Not Palindrome");
Q15 Factorial

5! = 120

int num=5;
int fact=1;
for(int i=1;i<=num;i++){
fact=fact*i;
}
[Link]("Factorial="+fact);

Q16 Floyd's Triangle

1
2 3
4 5 6
7 8 9 10

int num=1;
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
[Link](num+" ");
num++;
}
[Link]();
}

Q17 Alphabet Pattern

A
A B
A B C
A B C D

for(int i=1;i<=4;i++){
char ch='A';
for(int j=1;j<=i;j++){
[Link](ch+" ");
ch++;
}
[Link]();
}

You might also like