1. Logic to print different patterns is
first loop will decide the number of rows
Second loop will decide the number of columns
you have to manipulate the second loop logic to get the desired pattern.
public class Pattern {
public static void main(String[] args)
{
//Pattern1
for(int i=1; i<= 5 ;i++)
{
for(int j=0; j < i; j++)
{
System.out.print("*");
}
//generate a new line
System.out.println("");
}
//Pattern2
System.out.println("");
for(int i=5; i>0 ;i--){
for(int j=0; j < i; j++){
System.out.print("*");
}
//generate a new line
System.out.println("");
}
System.out.println("");
//Pattern3
for(int i=1; i<= 5 ;i++){
for(int j=0; j < i; j++){
System.out.print(j+1);
}
System.out.println("");
}
}
}
2. Ignore the syntax of below diagram - understand the logic only
3. Fibonacci Series
class Fibonacci
{
public static void main(String args[])
{
int n=20;
int j=0;
int k=1;
int num=0;
System.out.println(k);
while (num<=n)
{
num=j+k;
System.out.println(num);
j=k;
k=num;
}
}
}
first loop will decide the number of rows
Second loop will decide the number of columns
you have to manipulate the second loop logic to get the desired pattern.
public class Pattern {
public static void main(String[] args)
{
//Pattern1
for(int i=1; i<= 5 ;i++)
{
for(int j=0; j < i; j++)
{
System.out.print("*");
}
//generate a new line
System.out.println("");
}
//Pattern2
System.out.println("");
for(int i=5; i>0 ;i--){
for(int j=0; j < i; j++){
System.out.print("*");
}
//generate a new line
System.out.println("");
}
System.out.println("");
//Pattern3
for(int i=1; i<= 5 ;i++){
for(int j=0; j < i; j++){
System.out.print(j+1);
}
System.out.println("");
}
}
}
2. Ignore the syntax of below diagram - understand the logic only
3. Fibonacci Series
class Fibonacci
{
public static void main(String args[])
{
int n=20;
int j=0;
int k=1;
int num=0;
System.out.println(k);
while (num<=n)
{
num=j+k;
System.out.println(num);
j=k;
k=num;
}
}
}
No comments:
Post a Comment