Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Thursday 24 October 2013

Generate numbers using loops

Generate following output using for loops

1     2   3   4   5

10   9   8   7   6

11 12 13 14 15

20 19 18 17 16




 #include<stdio.h>  
 #include<conio.h>  
 int main()  
 {  
   int i,j,k=0,flag=0,t=1;  
   clrscr();  
   for(i=1; i<=4; i++)  
   {  
     if(i%2==0)  
     {  
       t=k=k+4;  
       flag=1;  
     }  
     else  
     {  
       k++;  
       flag=0;  
     }  
     if(flag==1)  
     {  
       for(j=1; j<=5; j++)  
       {  
         printf("%d ",t);  
         t--;  
       }  
       printf("\n");  
     }  
     else  
     {  
       for(j=1; j<=5; j++)  
       {  
         printf("%d ",k);  
         k++;  
       }  
       printf("\n");  
     }  
   }  
   getch();  
   return 0;  
 }  

0 comments :

Post a Comment