0% found this document useful (0 votes)
9 views9 pages

C Loop Examples: Print, Sum, Multiplication

Uploaded by

rakeshsivan08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

C Loop Examples: Print, Sum, Multiplication

Uploaded by

rakeshsivan08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭C program problems solved using different types of loops:‬

‭1.C program for print 1 to 5.‬


‭While loop‬ ‭#include <stdio.h>‬

‭int main() {‬
‭int i=1;‬
‭while (i<=5)‬
‭{‬
‭printf("%d ",i);‬
‭i++;‬
‭}‬
‭return 0;‬
‭}‬

‭For loop‬ ‭#include <stdio.h>‬

‭int main() {‬
‭int i;‬
‭for(i=1;i<=5;i++)‬
‭{‬
‭printf("%d ",i);‬
‭}‬
‭return 0;‬
‭}‬

‭Do while loop‬ ‭#include <stdio.h>‬

‭int main() {‬
‭int i=1;‬
‭do‬
‭{‬
‭printf("%d ",i);‬
‭i++;‬
‭}while(i<=5);‬
‭return 0;‬
‭}‬

‭Output‬ ‭1 2 3 4 5‬

‭2. Print from 5 to 1.‬


‭While loop‬ ‭#include <stdio.h>‬

‭int main() {‬
‭int i=1;‬
‭while (i<=5)‬
‭{‬
‭printf("%d ",i);‬
‭i++;‬
‭C program problems solved using different types of loops:‬

}‭ ‬
‭return 0;‬
‭}‬

‭For loop‬ ‭#include <stdio.h>‬

‭int main() {‬
‭int i;‬
‭for(i=5;i>=1;i--)‬
‭{‬
‭printf("%d ",i);‬
‭}‬
‭return 0;‬
‭}‬

‭Do while loop‬ ‭#include <stdio.h>‬

‭int main() {‬
‭int i=5;‬
‭do‬
‭{‬
‭printf("%d ",i);‬
‭i--;‬
‭}while(i>=1);‬
‭return 0;‬
‭}‬

‭Output‬ ‭5 4 3 2 1‬

‭3. Print sum of 1 to 5.‬


‭While loop‬ ‭ include <stdio.h>‬
#
‭int main() {‬
‭int a[5]={1,2,3,4,5};‬
‭int i=0,sum=0;‬
‭while(i<5)‬
‭{‬
‭sum+=a[i];‬
‭i++;‬
‭}‬
‭printf("%d+%d+%d+%d+%d=%d",a[0],a[1],a[2],a[3],a[4],sum);‬
‭return 0;‬
‭}‬

‭For loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int a[5]={1,2,3,4,5};‬
‭C program problems solved using different types of loops:‬

i‭nt i=0,sum=0;‬
‭for(i=0;i<5;i++)‬
‭{‬
‭sum+=a[i];‬
‭}‬
‭printf("%d+%d+%d+%d+%d=%d",a[0],a[1],a[2],a[3],a[4],sum);‬
‭return 0;‬
‭}‬

‭Do while loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int a[5]={1,2,3,4,5};‬
‭int i=0,sum=0;‬
‭do‬
‭{‬
‭sum+=a[i];‬
‭i++;‬
‭}while(i<5);‬
‭printf("%d+%d+%d+%d+%d=%d",a[0],a[1],a[2],a[3],a[4],sum);‬
‭return 0;‬
‭}‬

‭output‬ ‭1+2+3+4+5=15‬

‭4. Print the multiplication table of 5‬


‭While loop‬ ‭ include <stdio.h>‬
#
‭int main() {‬
‭int a=5,i=1,m;‬
‭while(i<=10)‬
‭{‬
‭m=a*i;‬
‭printf("%d * %d = %d\n",i,a,m);‬
‭i++;‬
‭}‬
‭return 0;‬
‭}‬

‭For loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int a=5,i,m;‬
‭for(i=1;i<=10;i++)‬
‭{‬
‭m=a*i;‬
‭printf("%d * %d = %d\n",i,a,m);‬
‭}‬
‭return 0;‬
‭}‬
‭C program problems solved using different types of loops:‬

‭Do while loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int a=5,i=1,m;‬
‭do‬
‭{‬
‭m=a*i;‬
‭printf("%d * %d = %d\n",i,a,m);‬
‭i++;‬
‭}while(i<=10);‬
‭return 0;‬
‭}‬

‭output‬ ‭ * 5 = 5‬
1
‭2 * 5 = 10‬
‭3 * 5 = 15‬
‭4 * 5 = 20‬
‭5 * 5 = 25‬
‭6 * 5 = 30‬
‭7 * 5 = 35‬
‭8 * 5 = 40‬
‭9 * 5 = 45‬
‭10 * 5 = 50‬

‭5. Print the multiplication table of the given number‬


‭While loop‬ ‭ include <stdio.h>‬
#
‭int main() {‬
‭int a,i=1,m;‬
‭scanf("%d",&a);‬
‭while(i<=10)‬
‭{‬
‭m=a*i;‬
‭printf("%d * %d = %d\n",i,a,m);‬
‭i++;‬
‭}‬
‭return 0;‬
‭}‬

‭For loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int a,i,m;‬
‭scanf("%d",&a);‬
‭for(i=1;i<=10;i++)‬
‭{‬
‭m=a*i;‬
‭printf("%d * %d = %d\n",i,a,m);‬
‭}‬
‭return 0;‬
‭}‬
‭C program problems solved using different types of loops:‬

‭Do while loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int a,i=1,m;‬
‭scanf("%d",&a);‬
‭do‬
‭{‬
‭m=a*i;‬
‭printf("%d * %d = %d\n",i,a,m);‬
‭i++;‬
‭}while(i<=10);‬
‭return 0;‬
‭}‬

‭input‬ ‭i) 2‬

‭ii) 6‬

‭Output‬ i‭)1 * 2 = 2‬
‭2 * 2 = 4‬
‭3 * 2 = 6‬
‭4 * 2 = 8‬
‭5 * 2 = 10‬
‭6 * 2 = 12‬
‭7 * 2 = 14‬
‭8 * 2 = 16‬
‭9 * 2 = 18‬
‭10 * 2 = 20‬

‭ii)1 * 6 = 6‬
‭2 * 6 = 12‬
‭3 * 6 = 18‬
‭4 * 6 = 24‬
‭5 * 6 = 30‬
‭6 * 6 = 36‬
‭7 * 6 = 42‬
‭8 * 6 = 48‬
‭9 * 6 = 54‬
‭10 * 6 = 60‬

‭6. Print the odd number from m to n.‬

‭While loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int m,n;‬
‭scanf("%d\n%d",&m,&n);‬
‭while(m<n){‬
‭if(m%2!=0){‬
‭C program problems solved using different types of loops:‬

‭printf("%d ",m);‬
}‭ ‬
‭m++;‬
}‭ ‬
‭return 0;‬
‭}‬

‭For loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int m,n;‬
‭scanf("%d\n%d",&m,&n);‬
‭for( int i=0;m<n;m++)‬
‭if(m%2!=0){‬
‭printf("%d ",m);‬
‭}‬
‭return 0;‬
‭}‬

‭Do while‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int m,n;‬
‭scanf("%d\n%d",&m,&n);‬
‭do{‬
‭if(m%2!=0){‬
‭printf("%d ",m);‬
‭}‬
‭m++;‬
‭}while(m<n);‬
‭return 0;‬
‭}‬

‭input‬ ‭m=1 n=10‬

‭output‬ ‭1 3 5 7 9‬

‭7. Print the square number of 1 to 5.‬


‭While loop‬ ‭ include <stdio.h>‬
#
‭int main() {‬
‭int i=1;‬
‭while(i<=5){‬
‭printf("%d\n",i*i);‬
‭i++;‬
‭}‬
‭return 0;‬
‭}‬

‭For loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭C program problems solved using different types of loops:‬

i‭nt i;‬
‭for(i=1;i<=5;i++){‬
‭printf("%d\n",i*i);‬
‭}‬
‭return 0;‬
‭}‬

‭Do while loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int i=1;‬
‭do{‬
‭printf("%d\n",i*i);‬
‭i++;‬
‭}while(i<=5);‬
‭return 0;‬
‭}‬

‭output‬ ‭‬
1
‭4‬
‭9‬
‭16‬
‭25‬

‭8. Print even from 1 to 10.‬


‭While loop‬ ‭ include <stdio.h>‬
#
‭int main() {‬
‭int i=1;‬
‭while(i<=10){‬
‭if(i%2==0){‬
‭printf("%d ",i);‬
‭}‬
‭i++;‬
‭}‬
‭return 0;‬
‭}‬

‭For loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int i;‬
‭for(i=1;i<=10;i++){‬
‭if(i%2==0){‬
‭printf("%d ",i);‬
‭}‬
‭}‬
‭return 0;‬
‭}‬
‭C program problems solved using different types of loops:‬

‭Do while‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int i=1;‬
‭do{‬
‭if(i%2==0){‬
‭printf("%d ",i);‬
‭}‬
‭i++;‬
‭}while(i<=10);‬
‭return 0;‬
‭}‬

‭output‬ ‭2 4 6 8 10‬

‭9. Print power of given number.(ie..a power b(a^b))‬


‭While‬ ‭ include <stdio.h>‬
#
‭int main() {‬
‭int a,b,r=1,i=1;‬
‭scanf("%d\n%d",&a,&b);‬
‭while(i<=b){‬
‭r=r*a;‬
‭i++;‬
‭}‬
‭printf("%d\n",r);‬
‭return 0;‬
‭}‬

‭For loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int a,b,r=1,i;‬
‭scanf("%d\n%d",&a,&b);‬
‭for(i=1;i<=b;i++){‬
‭r=r*a;‬
‭}‬
‭printf("%d\n",r);‬
‭return 0;‬
‭}‬

‭Do while loop‬ ‭ include <stdio.h>‬


#
‭int main() {‬
‭int a,b,r=1,i=1;‬
‭scanf("%d\n%d",&a,&b);‬
‭do{‬
‭r=r*a;‬
‭i++;‬
‭}while(i<=b);‬
‭C program problems solved using different types of loops:‬

‭printf("%d\n",r);‬
r‭ eturn 0;‬
‭}‬

‭input‬ i‭) a=3 b=3‬


‭ii) a=2 b=3‬
‭iii) a=2 b=2‬

‭output‬ i‭) 9‬
‭ii) 8‬
‭iii) 4‬

‭By Rakesh Sivan S , ECE(I year)‬


‭Ref no.:24900385‬
‭FOC 4V3-1‬‭¶‬

You might also like