For Loop Assignment.

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

DO….

WHILE(SUM PROGRAM)
#include <stdio.h>
int main() {
double number, sum = 0;
// the body of the loop is executed at least once
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}

WHILE(SUM PROGRAM)
#include<stdio.h>
main()
{ int i,n,sum=0;
printf("ENTER THE VALUE OF i=");
scanf("%d",&i);
printf("ENTER THE VALUE OF n=");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("sum=%d",sum);
return 0;
}

WHILE WITH BREAK STATEMENT(SUM PROGRAM)


main()
{ int i,n,sum=0;
printf("ENTER THE VALUE OF i=");
scanf("%d",&i);
printf("ENTER THE VALUE OF n=");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
if(sum>=500)
break;
i++;
}
printf("sum=%d",sum);
return 0;
}

WHILE WITH CONTINUE STATEMENT(SUM PROGRAM)


#include<stdio.h>
#include<stdlib.h>
main()
{int a,b,sum=0;
printf("ENTER THE VALUE OF a= ");
scanf("%d",&a);
printf("ENTER THE VALUE OF b=");
scanf("%d",&b);
while(a<=b)
{if(a==6)
{a++;
continue;}
else
sum=sum+a;
a++;
}
printf("sum=%d\n a=%d",sum,a);
return 0;
}

FOR (SUM PROGRAM)


#include<stdio.h>
#include<stdlib.h>
main()
{int a,b,sum=0;
printf("ENTER THE VALUE OF a= ");
scanf("%d",&a);
printf("ENTER THE VALUE OF b=");
scanf("%d",&b);
for(a;a<=b;a++)
{
sum=sum+a;
}
printf("sum=%d\n a=%d",sum,a);
return 0;
}

FOR WITH CONTINUE STATEMENT(SUM PROGRAM)


#include<stdio.h>
#include<stdlib.h>
main()
{int a,b,sum=0;
printf("ENTER THE VALUE OF a= ");
scanf("%d",&a);
printf("ENTER THE VALUE OF b=");
scanf("%d",&b);
for(a;a<=b;a++)
{if(a==6)
continue;
else
sum=sum+a;
}
printf("sum=%d\n a=%d",sum,a);
return 0;}

FOR (TO PRINT MULTIPLES TABLES)


#include <stdio.h>
#include <stdlib.h>
int main()
{ int a,b,i,j;
printf("enter a:");
scanf("%d",&a);
printf("enter b:");
scanf("%d",&b);
i=1;
for(j=a;j<=b;j++)
{
for(i=1;i<=10;i++)
{
printf("%d x %d =%d\n",j,i,j*i);
}
printf("\n");
}
Return 0;}
CONVERT INTO WHILE
#include<stdio.h>
int main()
{int a,b,i;
printf("ENTER a=");
scanf("%d",&a);
printf("ENTER b=");
scanf("%d",&b);
while(a<=b)
{i=1;
while (i<=10)
{printf("%dx%d=%d\n",a,i,a*i);
i++;
}
printf("\n");
a++;
}
return 0;
}
CONVERT INTO WHILE(WITH BREAK STATEMENT)
#include<stdio.h>
int main()
{int a,b,i;
printf("ENTER a=");
scanf("%d",&a);
printf("ENTER b=");
scanf("%d",&b);
i=1;
while(a<=b)
{while (i<=10)
{printf("%dx%d=%d\n",a,i,a*i);
If(i==10)
{i=1;
Break;}
i++;
}
printf("\n");
a++;
}
return 0;
}
WHILE(to display the first 10 natural numbers)
#include<stdio.h>
main()
{int a=1;
while(a<=10)
{
printf("%d\n",a);
a++;
}
return 0;
}
FOR(to display the first 10 natural numbers)
#include<stdio.h>
main()
{int a;
For(a=1;a<=10;a++)
{
printf("%d\n",a);
}
return 0;
}
FOR(to display the cube of the number up to given an integer)
#include<stdio.h>
#include<math.h>
{int a=1,cube;
for(a=1;a<=5;a++)
{printf("the number is= %d\n",a);
cube=pow(a,3);
printf("the cube of the number= %d\n",cube);
}
return 0;
}
WHILE(to display the cube of the number up to given an integer)
#include<stdio.h>
#include<math.h>
{int a=1,cube;
while(a<=5)
{printf("the number is= %d\n",a);
cube=pow(a,3);
a++;
printf("the cube of the number= %d\n",cube);
}
return 0;
}
FOR(to display the n terms of odd natural number and their sum)
#include<stdio.h>
#include<stdlib.h>
main()
{int i,a,sum=0;
printf("Enter a number of terms=");
scanf("%d",&a);
printf("THE ODD NATURAL NUMBER IS=\n");
for(i=1;i<=a;i++)
{ printf("%d\n",2*i-1);
sum=sum + 2*i-1;
}
printf("the sum of odd natural number= %d",sum);
return 0;
}
WITH NESTED LOOP AND BREAK STATEMENT
#include<stdio.h>
#include<stdlib.h>
main()
{int i,a,j;
printf("Enter a number of terms=");
scanf("%d",&a);
printf("THE ODD NATURAL NUMBER IS=\n");
for(i=1;i<=a;i++)
{for(j=2;j<=i;j++)
{if(i%j==1)
printf("%d is an odd number\n",i);
break;}
}
return 0;
}
WHILE(to display the n terms of odd natural number and their sum)
#include<stdio.h>
#include<stdlib.h>
main()
{int i,a,sum=0;
printf("Enter a number of terms=");
scanf("%d",&a);
printf("THE ODD NATURAL NUMBER IS=\n");
while(i<=a)
{ printf("%d\n",2*i-1);
sum=sum + 2*i-1;
i++;
}
printf("the sum of odd natural number= %d",sum);
return 0;
}
FOR(to display the n terms of even natural number and their sum)
#include<stdio.h>
#include<stdlib.h>
main()
{int i,a,sum=0;
printf("Enter a number of terms=");
scanf("%d",&a);
printf("THE EVEN NATURAL NUMBER IS=\n");
for(i=1;i<=a;i++)
{ printf("%d\n",2*i);
sum=sum + 2*i;
}
printf("the sum of even natural number= %d",sum);
return 0;
}
WITH NESTED LOOP AND BREAK STATEMENT
#include<stdio.h>
#include<stdlib.h>
main()
{int i,a,j;
printf("Enter a number of terms=");
scanf("%d",&a);
printf("THE EVEN NATURAL NUMBER IS=\n");
for(i=1;i<=a;i++)
{for(j=2;j<=i;j++)
{if(i%j==0)
printf("%d is an even number\n",i);
break;}
}
return 0;
}
WHILE(to display the n terms of even natural number and their sum)
#include<stdio.h>
#include<stdlib.h>
main()
{int i,a,sum=0;
printf("Enter a number of terms=");
scanf("%d",&a);
printf("THE EVEN NATURAL NUMBER IS=\n");
while(i<=a)
{ printf("%d\n",2*i);
sum=sum + 2*i;
i++;
}
printf("the sum of even natural number= %d",sum);
return 0;
}
FOR(to display the n terms of prime natural number )
#include<stdio.h>
int main()
{int a,b,c,count;
printf("ENTER THE VALUE OF b=");
scanf("%d",&b);
for(a=1;a<=b;a++)
{count=0;
for(c=1;c<=a;c++)
{if(a%c==0)
count++;
}
if(count==2)
printf("%d\n",a);
}
return 0;
}

FOR(to calculate the factorial of a given number.)


#include<stdio.h>
main()
{int a,b,fact=1;
printf("ENTER YOUR NUMBER=");
scanf("%d",&a);
for(b=a;b>=1;b--)
{
fact=fact*b;
}
printf("THE FACTORIAL OF 5=%d",fact);
return 0;
}
//CONVERT INTO WHILE
#include<stdio.h>
main()
{int a,b,fact=1;
printf("ENTER YOUR NUMBER=");
scanf("%d",&b);
a=b;
while(a>=1)
{fact=fact*a;
a--;
}
printf("THE FACTORIAL OF 5=%d",fact);
return 0;
}
TO PRINT FACTORIAL OF MULTIPLE NUMBERS AND THEIR SUM
#include<stdio.h>
main()
{int a,b,i,j,sum=0,fact;
printf("ENTER YOUR NUMBER=");
scanf("%d",&a);
printf("ENTER THE RANGE OF FACTORIAL NUMBERS=");
scanf("%d",&j);
for(i=a;i<=j;i++)
{fact=1;
for(b=i;b>=1;b--)
{
fact=fact*b;
}
printf("THE FACTORIAL OF %d=%d\n",i,fact);
printf("\n");
sum=sum+fact;
}
printf("%d\n",sum);
return 0;
}
//CONVERT INTO WHILE
#include<stdio.h>
main()
{int a,b,i,j,sum=0,fact;
printf("ENTER YOUR NUMBER=");
scanf("%d",&a);
printf("ENTER THE RANGE OF FACTORIAL NUMBERS=");
scanf("%d",&j);
i=a;
while(i<=j)
{b=i;
fact=1;
while(b>=1)
{
fact=fact*b;
b--;
}
printf("THE FACTORIAL OF %d=%d\n",i,fact);
printf("\n");
sum=sum+fact;
i++;
}
printf("%d\n",sum);
return 0;
}
IF..ELSE(to accept two integers and check whether they are equal or not)
#include<stdio.h>
main()
{int a=15,b=15;
if(a==b)
printf("a and b are equal");
else
printf("a and b are not equal");
return 0;
}
//convert into switch
#include<stdio.h>
main()
{int a=15,b=15;
switch(a)
{
case 14:
printf("%d != %d",a,b);
break;
case 15:
printf("%d == %d",a,b);
break;
default:
printf("a and b are not equal");
}
return 0;
}
IF..ELSE(to check whether a given number is even or odd.)
#include<stdio.h>
main()
{int a=15;
if(a<20)
printf("15 is an odd");
else
printf("15 is an even");
return 0;
}
//CONVERT INTO SWITCH
#include<stdio.h>
main()
{
int a=15;
switch(a)
{case 14:
printf("15 is an even");
break;
case 15:
printf("15 is an odd");
break;
default:
printf("15 is an even");
}
return 0;
}
IF..ELSE(to find whether a given year is a leap year or not.)
#include<stdio.h>
main()
{int a=2016;
if((a%400)==0)
printf("2016 is a leap year \n");
else if((a%100)==0)
printf("2016 is not a leap year \n");
else if((a%4)==0)
printf("2016 is a leap year \n");
else
printf("2016 is not a leap year \n");
return 0;
}
//CONVERT INTO SWITCH
#include<stdio.h>
main()
{int a=2016;
switch(a)
{case (2016):
if((a%400)==0)
printf("2016 is a leap year \n");
else if((a%100)==0)
printf("2016 is not a leap year \n");
else if((a%4)==0)
printf("2016 is a leap year \n");
else
printf("2016 is not a leap year \n");
break;
default:
printf("default\n");}
return 0;
}
IF..ELSE(to read the value of an integer m and display the value of n is 1 when m is larger than
0, 0 when m is 0 and -1 when m is less than 0)
include<stdio.h>
main()
{int n,m=-5;
if(m>0)
printf("THE VALUE OF n IS 1");
else if(m==0)
printf("THE VALUE OF n IS 0");
else if(m<0)
printf("THE VALUE OF n IS -1");
else
printf("error!\n");
return 0;
}
//CONVERT INTO SWITCH
#include<stdio.h>
main()
{int n,m=-5;
switch(m<0)
{case(-5>0):
printf("THE VALUE OF n IS 1");
break;
case(-5<0):
printf("THE VALUE OF n IS -1");
break;
default:
printf("error!\n");
}
return 0;
}
SUM OF FIRST n terms in the SERIES (S=1+1/2+1/3……+1/n)
#include<stdio.h>
int main()
{int x,num;
float sum=0;
printf("ENTER NO. OF TERMS=");
scanf("%d",&num);
for(x=1;x<=num;x++)
{ sum=sum+(float)1/x;
if(x==1)
printf("1+");
else if(x==num)
printf("1/%d\n",x);
else
printf("1/%d +",x);
}
printf("sum of the series=%f\n",sum);
return 0;
}
Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........].

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy