Loop programs
1. Display first 10 natural numbers
#include <stdio.h>
void main() {
int i;
printf("The first 10 natural numbers are:\n");
for (i=1; i<=10; i++)
{
printf("%d ", i);
}getch();
}
2. :Display n natural numbers and their sum
#include <stdio.h>
void main() {
int i, n, sum = 0;
printf("Input Value of terms : ");
scanf("%d", &n);
printf("\nThe first %d natural numbers are:\n", n);
for (i = 1; i <= n; i++)
{
printf("%d ", i);
sum += i;
getch();}
printf("\nThe Sum of natural numbers upto %d terms : %d \n", n,
sum);
getch();}
3. Read 10 numbers and find their sum and average
#include <stdio.h>
void main() {
int i, n, sum = 0;
float avg;
printf("Input the 10 numbers : \n");
for (i = 1; i <= 10; i++) {
printf("Number-%d :", i);
scanf("%d", &n);
sum += n;
}
avg = sum / 10.0;
printf("The sum of 10 no is : %d\nThe Average is : %f\n", sum, avg);
getch();
}
4.Find cube of the number upto a given integer
#include <stdio.h>
int main() {
int n,i;
printf("enter\n");
scanf("%d",&n);
for(i=0; i<=n; i=i+1)
{
printf("number is :%d,cube is:%d\n",i, (i*i*i)) ;
}
getch();
}
5.Compute multiplication table of a given integer using for loop
#include <stdio.h>
void main() {
int i, n;
printf("Input the number (Table to be calculated) : ");
scanf("%d", &n);
printf("\n");
for (i = 1; i <= 10; i++) {
printf("%d X %d = %d \n", n, i, n *i);
}getch();}
6.To calculate factorial of any number(simple c program)
#include<stdio.h>
void main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
getch();}
7.To calculate factorial of any number using for loop
/*Program to calculate factorial of given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,f=1;
printf("\n Enter The Number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n The Factorial of %d is %d",n,f);
getch();
}
8.Display the pattern like right angle triangle using a
number
#include <stdio.h>
void main() {
int i, j, rows;
printf("Input number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++)
printf("%d", j);
printf("\n");
}getch();}
9Display first 10 natural numbersWhile Loop
#include <stdio.h>
void main() {
int i = 1;
while (i <= 10) {
printf("%d\n", i);
i++;
}
getch();
}
10.Display n natural numbers and their sum using While Loop
#include <stdio.h>
void main()
{
int n sum=0,i;
printf("Enter number:");
scanf("%d",&n);
i=1;
while(i<=n)
sum += i++;
printf("sum of first %d numbers is: %d",n,sum);
getch();
}
11.Compute multiplication table of a given integer
12.Find cube of the number up to a given integer While Loop
13.Program to print numbers from 1 to 10 do...while loop in C
#include <stdio.h>
viod main()
{
int i = 0;
do {
printf("%d\n", i+1);
i++;
}
while (i < 10);
getch();
}