Problem:  C program to print pattern.

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

Source Code:

  1. #include<stdio.h> 
  2. int main()
  3. int i,j,n;
  4. printf("length of ball:"); 
  5. scanf("%d",&n);
  6. /*printf("Breadth of ball");
  7. scanf("%d",&j);*/

  8. // This is upper half of pattern 
  9. for (i=1; i<=n; i++) 
  10. for (j=1; j<=(2*n); j++) 
  11. // Left part of pattern 
  12. if (i>(n-j+1)) 
  13. printf(" "); 
  14. else
  15. printf("*"); 
  16. // Right part of pattern 
  17. if ((i+n)>j) 
  18. printf(" "); 
  19. else
  20. printf("*"); 
  21. printf("\n"); 
  22. // This is lower half of pattern 
  23. for (i=1; i<=n; i++) 
  24. for (j=1; j<=(2*n); j++) 
  25. // Right Part of pattern 
  26. if (i<j) 
  27. printf(" "); 
  28. else
  29. printf("*"); 
  30. // Left Part of pattern 
  31. if (i<=((2*n)-j)) 
  32. printf(" "); 
  33. else
  34. printf("*"); 
  35. printf("\n"); 
  36. }
  37. return 0; 
Output:


length of ball:5
     

0 comments:

Post a Comment