Monday, November 12, 2012

Java Inverted Star C

Output:

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

Code:
class JavaStar {
    public static void main(String star[]) {
            int n = Integer.parseInt(star[0]);
        int i,j;
        System.out.println();
        for(i=0;i<n;i++) {
                for(j=0;j<i*2;j++) {
                System.out.print(" ");                   
                }
                for(j=0;j<(((n-i)*2)-1);j++) { //if input is 4 then middle star like 7 5 3 1
                        System.out.print("*");
                }
            System.out.println(); //for line termination
            }
                for(i=0;i<n;i++) { //we need to print 1 line less
                for(j=(n-i-1)*2;j>0;j--) {
                System.out.print(" ");                   
                  }
                for(j=0;j<(i*2)+1;j++) { // need to print starts like 1 3 5 7
                       System.out.print("*");
                }
                 System.out.println();
        }
    }
}

c-x Sequence using stars

Output :
* *********** *
** ********* **
*** ******* ***
**** ***** ****
***** *** *****
****** * ******
***** *** *****
**** ***** ****
*** ******* ***
** ********* **
* *********** *

Code :
#include<stdio.h>
void main() {
    int x,n,i,j;
    printf("Enter No of lines to be printed");
    scanf("%d",&n);
    printf("\n");
    for(i=0;i<n;i++) {
        for(j=0;j<=i;j++) {
            printf("*");
        }
        printf(" "); // for X gap
        for(j=0;j<(((n-i)*2)-1);j++) { //if input is 4 then middle star like 7 5 3 1
            printf("*");
        }
        printf(" "); // for X gap
          for(j=0;j<=i;j++) {
            printf("*");
        }
    printf("\n"); //for line termination
    }
    for(i=1;i<n;i++) { //we need to print 1 line less
        for(j=n-(i);j>0;j--) {
            printf("*");
        }
        printf(" ");
        for(j=(i*2)+1;j>0;j--) { // need to print starts like 3 5 7 as i is 1 then j = 3
            printf("*");
        }
        printf(" ");
          for(j=n;j>i;j--) {
            printf("*");
        }
    printf("\n");
    }
    printf("\n");
}

Sunday, October 21, 2012

Java Star Sequence for upper and lower Right angle sequence


class Star {
    public static String printStar(int n) {
        int  i= 0;
        int  j= 0;
        String starSequence = "";       
        for(i=0;i<n;i++) {
            for(j=0;j<i+1;j++) {
                starSequence += "*";
            }       
            starSequence += "\n";
        }       
        for(i=0;i<n-1;i++) {
            for(j=(n-(i+1));j>0;j--) {
                starSequence += "*";
            }       
            starSequence += "\n";
        }       

        return starSequence;   
    }
    public static void main(String star[]) {
        System.out.println(printStar(Integer.parseInt(star[0])));
    }
}

Thursday, October 18, 2012

Square shape using * and _

OutPut:-

______*______
_____*_*_____
____*_*_*____
___*_*_*_*___
__*_*_*_*_*__
_*_*_*_*_*_*_
Code :-
#include <stdio.h>
main() {
    int i,j,n;
    printf("Enter No of lines to be printed");
        scanf("%d",&n);
        printf("\n");   
    for(i=0;i<n;i++) {
        for(j=n-i;j>0;j--) {
            printf("_");
        }
        for(j=0;j<((2*i)+1);j++) {
            if(j%2 == 0) printf("*"); else printf("_");
        }
        for(j=n-i;j>0;j--) {
            printf("_");
        }
        printf("\n");
    }
        printf("\n");
}

* and _ right angle triangle shape With loops and conditions

OutPut:-
*
_*
*_*
_*_*
*_*_*
_*_*
*_*
_*
*
 
Code :-
 
#include<stdio.h>
void main() {
    int x,n,i,j;
    printf("Enter No of lines to be printed");
    scanf("%d",&n);
    printf("\n");
    for(i=0;i<n;i++) {
          for(j=0;j<=i;j++) {
            if((j+i)%2 == 0)
            printf("*");
            else
            printf("_");
        }
        printf("\n");
    }
    for(i=0;i<n;i++) {
          for(j=n-1;j>i;j--) {
            if((j+i)%2 == 1)
            printf("*");
            else
            printf("_");      
        }
        printf("\n");
    }
    printf("\n");
}

Wednesday, August 15, 2012

Right Angle Triangle Program's concept using * and only Single loop

OutPut :-

*
* *
* * *
* * * *

Code:-

#include<stdio.h>
void main() {
    int x,n,i,j,count;
    printf("Enter No of lines to be printed");
    scanf("%d",&n);
    printf("\n");
    count = 0;
    for(i=0;i<n;i++) {
        printf("*");
        if(i == count) {
             printf("\n");
            if(i==(n-1)) break;
            i=0;           
            i--;
            count++;
        }
    }
    printf("\n");
}