Here is the code to print different star formations using for loop.
#include "stdio.h" int main() { int num,i,j,k,m,n,o,p; printf("Please enter a number to print the star formations \n"); scanf("%d", &num); for(i=0;i<num;i++) { for(j=0;j<num;j++) { printf("*"); } printf("\n"); } printf("\n"); j=0,i=0,k=0; // Hollow Star formation for(i=0;i<num;i++) { for(j=0;j<num;j++) { if(i==0 || i==num-1) { printf("*"); } else if(j==0 || j==num-1) { printf("*"); } else { printf(" "); } } printf("\n"); } // downward descending formation printf("\n"); j=0,i=0,k=0; for(i=num;i>0;i--) { for(j=i;j>0;j--) { printf("*"); } printf("\n"); } printf("\n"); j=0,i=0,k=0; for(i=num;i>=0;i--) { for(k=i;k<num;k++) { printf(" "); } for(j=0;j<=i-1;j++) { printf("*"); } printf("\n"); } printf("\n"); j=0,i=0,k=0; // Star formation right angle triangle. for(i=num;i>0;i--) { for(j=0;j<i-1;j++) { printf(" "); } for(k=j;k<num;k++) { printf("*"); } printf("\n"); } printf("\n"); j=0,i=0,k=0; for(i=0;i<num;i++) { for(j=0;j<=i;j++) { printf("*"); } printf("\n"); } return 0; } |
The output of the above code is following:
Helped a lot. Thank u soo much
ReplyDelete