C exercises and solutions: loops in C

Loops in C exercises



1. Write a C program that will print the following pattern:


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

Solution:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
 int i=7;
 int j=7;
 
 for(i=1;i<=7;i++){
  for(j=7-i;j>=1;--j)printf("*");
  printf("\n");
}
  
   printf("\n");
   system("PAUSE");
   return EXIT_SUCCESS;
}

2. Write a C program that will print the following pattern:


       1******
       12*****
       123****
       1234***
       12345**
       123456*
       1234567

Solution:

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
 int i;
 int j;
 int k;
 
 for(i=1;i<=7;i++){
  for(j=1;j<=i;++j)
    cout<<j;               
               
  for(k=7-i;k>=1;k--) printf("*");                
  
  printf("\n");
}
  
   printf("\n");
   system("PAUSE");
   return EXIT_SUCCESS; }



Comments

seo gador comment

 seo gador

These are very useful c loop exercises for me. thank.

10-02-2013
Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like