Posts

Showing posts with the label series program

print the series 2-4+6-8+10.....n using java

 int a=2,n=10;  // n for number of terms //a is the starting value int sum=0; for(int i=1;i<=n;i++) { if(i%2==0) { sum=sum-a; } else { sum=sum+a; } a=a+2; } System.out.println(sum);

How to write program 1- 1/3+1/4+1/5......n

 double a=4.0,sum=0.0; // take third term as "a" int n=10;  for(i=1 ;i<=n-2;i++) { sum=sum + 1/a; a++ } System.out.prtinln(1- 1.0/3+sum);

How to print 1,12,123,1234...n using java

 /  common way to write  int a=1,n=10 ;  // n value you can take anything for(int i=1;i<=n;i++) { System.out.println(a+","); a=a*10+i; }

How to print 1, 11,111,1111...n using java

 /  common way to write  int a=1,n=10 ;  // n value you can take anything for(int i=1;i<=n;i++) { System.out.print(a+" ,"); a=a*10+1; }

How to print 9,99,8,89,7,79....

 /  common way to write  int a=9,n=10 ;  // n value you can take anything for(int i=1;i<=n;i++) { if(i%2==1) { System.out.print(a); } else { System.out..print(a*10+9); a--; }}

How to print 1,2,4,7,11...n using java

 // always focus on the first number and mark the difference pattern //  common way to write  int a=1,n=10 ;  // n value you can take anything for(int i=1;i<=n;i++) { System.out.println(a); a=a+i; }

How to print 1,2,3,4...n using java

 // always focus on the first number and mark the difference pattern // common way to write  int a=1,n=10 ;  // n value you can take anything for(int i=1;i<=n;i++) { System.out.println(a); a++; } // Short cut way to write int n=10 ;  // n value you can take anything for(int i=1;i<=n;i++) { System.out.println(i); a++; }

How to write series program in java

 1/a 2 + 1/a 3 + 1/a 4 -----n // always focus on first term  then see the difference in first and second term Normal way double a=3.0, b=2.0,sum=0.0;  // "a " value you can take anything int n=10  // n value you can take any thing for(int i=1;i<=n;i++) { sum=sum+ 1/ Math.pow(a,b); b++; } System.out.println(sum); Short cut way double sum=0.0,a=3.0;  // "a" value you can take anything int n=10  // n value you can take any thing for(int i=1;i<=n;i++) { sum=sum+ 1/ Math.pow(a, (i+1)); } System.out.println(sum);

How to write program 1/2+2/3+3/4------n terms

 1/2+2/3+3/4------n terms // always focus on first term  then see the difference in first and second term Normal way double a=1.0, b=2.0,sum=0.0; int n=10  // n value you can take any thing for(int i=1;i<=n;i++) { sum=sum+ a/b; a++; b++; } System.out.println(sum); Short cut way double sum=0.0; int n=10  // n value you can take any thing for(int i=1;i<=n;i++) { sum=sum+ i/(i+1); } System.out.println(sum);

series-11

1 0 1 1 0 1 0 1 0 1   class series { public static void main(String args[]) { for(int r=1;r<=4;r++) { for (int c=1 ; c<=r ; c++) { if((r+c)%2==0) System.out.print(1+ "  "); else System.out.print(0+"  "); } System.out.println() // to come to next line } } } // common way class series { public static void main(String args[]) { int d=1; // how many element in first line for(int r=1;r<=4;r++) { for (int c=1 ; c<=d ; c++) { if((r+c)%2==0) System.out.print(1+ "  "); else System.out.print(0+"  "); } d++; System.out.println() // to come to next line } } }

series-10

1 1 0 1 0 1 1 0 1 0   class series { public static void main(String args[]) { for(int r=1;r<=4;r++) { for (int c=1 ; c<=r ; c++) { if(c%2==0) System.out.print(0+ "  "); else System.out.print(1+" "); } System.out.println() // to come to next line } } } //Common way class series { public static void main(String args[]) { int d=1;  // how many element in first line for(int r=1;r<=4;r++) { for (int c=1 ; c<=d ; c++) { if(c%2==0) System.out.print(0+ "  "); else System.out.print(1+" "); } d++; System.out.println() // to come to next line } } }

series-9

 *  * * * * * * * * * class series { public static void main(String args[]) { for(int r=1;r<=4;r++) { for (int c=1 ; c<=r ; c++) { System.out.print("*"+ "  "); } System.out.println() // to come to next line } } } // common way class series { public static void main(String args[]) { int  d=1;   //    d- means how many element in first line for(int r=1;r<=4;r++) { for (int c=1 ; c<=d ; c++) { System.out.print('*'+ "  "); } d++; System.out.println() // to come to next line } } }

series-8

 1 2 3 4 5 1 2 3 4 1 1 2 3 1 2 1 2 1 2 3 class series { public static void main(String args[]) { int a,d=5; for(int r=1;r<=5;r++) { a=1; for (int c=1 ; c<=5 ; c++) { if(c<=d) System.out.print(c); else System.out.print(a++) } d--; System.out.println() // to come to next line } } }

series-7

 1 2 3 4 5  2 2 3 4 5 3 3 3 4 5 4 4 4 4 5 class series { public static void main(String args[]) { for(int r=1; r<=4; r++) { for (int c=1 ; c<=5 ; c++) { if(c<=r) System.out.print(r); else System.out.print(c); } System.out.println() // to come to next line } } }

series-6

 1 2 3 4 5 6 class series { public static void main(String args[]) { int a=1; for(int r=1 ; r<=3 ; r++) { for (int c=1 ; c<=r ; c++) { System.out.print(a+ "  "); a++; } System.out.println() // to come to next line } } } // common way class series { public static void main(String args[]) { int a=1, d=1;   //  a- means first value  d- means how many element in first line for(int r=1;r<=4;r++) { for (int c=1 ; c<=d ; c++) { System.out.print(a+ "  "); a++; } d++; System.out.println() // to come to next line } } }

series-5

1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 class series { public static void main(String args[]) { for(int r=5 ; r>=1 ; r--) { for (int c=r ; c<=5 ; c++) { System.out.print(c+ "  "); } System.out.println() // to come to next line } } } // common way class series { public static void main(String args[]) { int a=1, d=5;   //  a- means first value  d- means how many element in first line for(int r=1;r<=4;r++) { a=r; for (int c=1 ; c<=d ; c++) { System.out.print(a+ "  "); a++; } d--; System.out.println() // to come to next line } } }

series-4

1 1 1 1 2 2 2 2 3 3 3 3   class series { public static void main(String args[]) { for(int r=1;r<=3;r++) { for (int c=1 ; c<=4 ; c++) { System.out.print(r+ "  "); } System.out.println() // to come to next line } } } // common way class series { public static void main(String args[]) { int a=1, d=4;   //  a- means first value  d- means how many element in first line for(int r=1;r<=3;r++) { for (int c=1 ; c<=d ; c++) { System.out.print(a+ "  "); } a++; System.out.println() // to come to next line } } }

series-3

1 2 3 4 1 2 3 4 1 2 3 4   class series { public static void main(String args[]) { for(int r=1;r<=3;r++) { for (int c=1 ; c<=4 ; c++) { System.out.print(c+ "  "); } System.out.println() // to come to next line } } } // common way class series { public static void main(String args[]) { int a=1, d=4;   //  a- means first value  d- means how many element in first line for(int r=1;r<=4;r++) { a=1; for (int c=1 ; c<=d ; c++) { System.out.print(a+ "  "); a++; } System.out.println() // to come to next line } } }

series-2

 5 4 3 2 1 5 4 3 2 5 4 3 5 4 5 // shortcut way class series { public static void main(String args[]) { for(int r=1;r<=5;r++)  //  r- for row { for (int c=5 ; c>=r ; c--)// c- column { System.out.print(c+ "  "); } System.out.println() // to come to next line } } } // common way class series { public static void main(String args[]) { int a=5, d=5;   //  a- means first value  d- means how many element in first line for(int r=1;r<=5;r++) { a=5; for (int c=1 ; c<=d ; c++) { System.out.print(a+ "  "); a--; } d--; System.out.println() // to come to next line } } }

series-1

 1 1 2 1 2 3 1 2 3 4 //short cut way class series { public static void main(String args[]) { for(int r=1;r<=4;r++) { for (int c=1 ; c<=r ; c++) { System.out.print(c+ "  "); } System.out.println() // to come to next line } } } // common way class series { public static void main(String args[]) { int a=1, d=1;   //  a- means first value  d- means how many element in first line for(int r=1;r<=4;r++) {   a=1; for (int c=1 ; c<=d ; c++) { System.out.print(a+ "  "); a++; } d++; System.out.println() // to come to next line } } }