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

Sambath comment

 Sambath

#include <stdio.h>
#include <conio.h>

int main()
{

int x;
int sum=0;
int i=0;
while(i<5){
printf("Enter a number:");
scanf("&d",x);
sum+=x;
i++;
}

printf("%d",sum);
system("PAUSE");
return EXIT_SUCCESS;
}


2016-10-30
rahul comment

 rahul

write a programme to accept a 5value using while loop & display the total value...........
help out with this......


2016-10-25
Umid comment

 Umid

#include <stdio.h>
int main(){
int i;
int j;
int k;
for (i=1; i<=7; i++){
for(j=1; j<=i; j++) j;
for(k=1; k<=i; k++) printf("%d", k); printf("\n");
}
}


2016-07-11
PCQ comment

 PCQ

#include <stdio.h>
main()
{

int x;
char arr[8] = {'1','*','*','*','*','*','*','\0'};


for(x=1;x<=7;x++){

arr[x] = x+49;

printf("\n%s", &arr);



}


2016-04-04
Sol comment

 Sol

Solution to Exc. 1 gives only 6 stars and down, not 7...


2016-01-30
Owais comment

 Owais

1
12
123
1234
12345
please give this pattern program.....


2016-01-20
shreya comment

 shreya

thank....
its really helpful for me..


2015-08-02
seo gador comment

 seo gador

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


2013-10-02
CAPTCHA image



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.