0% found this document useful (0 votes)
0 views6 pages

C Language For Loop (Iteration) Program-1

The document contains a series of C programming exercises focused on using for loops for various tasks, including displaying numbers, calculating sums, factorials, and checking for prime numbers. Each exercise includes a brief description followed by the corresponding C code implementation. The author of the document is Avijit Pramanik, and it appears to be a tutorial or educational resource for learning C programming concepts.

Uploaded by

swapnilpatra09
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)
0 views6 pages

C Language For Loop (Iteration) Program-1

The document contains a series of C programming exercises focused on using for loops for various tasks, including displaying numbers, calculating sums, factorials, and checking for prime numbers. Each exercise includes a brief description followed by the corresponding C code implementation. The author of the document is Avijit Pramanik, and it appears to be a tutorial or educational resource for learning C programming concepts.

Uploaded by

swapnilpatra09
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
You are on page 1/ 6

Avijit Pramanik 9038652152

C Language for loop (Iteration) program


// 1. WAP to display 1 to 6.....
#include<stdio.h>
void main()
{ Avijit Pramanik 9038652152
int i;
for(i=1;i<=6;i++)
Avijit Pramanik 9038652152
{
printf("%d\t",i);
}
}
// 2. WAP to display 1 2 3 4 5 6 7..............n
#include<stdio.h>
void main()
Avijit Pramanik 9038652152
{
int i,n;
printf("Enter value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\t",i);
}
}
// 3. WAP to display 20 to 50.....
#include<stdio.h>
void main()
{ Avijit Pramanik 9038652152
int i;
for(i=5;i<=25;i=i+5)
{
printf("%d\t",i);
}
}

Avijit Pramanik 9038652152 Page 1 of 6


Avijit Pramanik 9038652152
// 4. WAP to display 10 to 1.....
#include<stdio.h>
void main()
{ Avijit Pramanik 9038652152

int i;
for(i=10;i>=1;i--)
Avijit Pramanik 9038652152
{
printf("%d\t",i);
}
}
//5. WAP to calculate 1+2+3+4+5+.....+n
#include<stdio.h>
void main() Avijit Pramanik 9038652152
{
int i,n,sum=0; //to avoid garbage value we assign sum=0
printf("Enter value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("\nSummation is:%d",sum);
}

Avijit Pramanik 9038652152 Page 2 of 6


Avijit Pramanik 9038652152
//6. WAP to calculate n! or Find Factorial of a number
#include<stdio.h>
#include<stdlib.h>
void main() Avijit Pramanik 9038652152
{
int i,n,fact=1; //to avoid garbage value we assign fact=1
printf("Enter any number:");
scanf("%d",&n);
if(n<0)
{
printf("Factorial of -ve value is undefine");
exit(0);
}
for(i=1;i<=n;i++)
Avijit Pramanik 9038652152
{
fact=fact*i;
}
printf("Factorial value is:%d",fact);
}
// 7. WAP to find factors a given number
#include<stdio.h>
void main()
{ Avijit Pramanik 9038652152

int i,n;
printf("Enter any number:");
scanf("%d",&n);
printf("\nFactors of the Given number:");
for(i=1;i<=n/2;i++)
{
if(n%i==0)
printf("%d\t",i);
}
}

Avijit Pramanik 9038652152 Page 3 of 6


Avijit Pramanik 9038652152
// 8. WAP to check a given number is prime or not
#include<stdio.h>
void main()
Avijit Pramanik 9038652152
{
int n,i,c=0;
printf("Enter any number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if(c>2)
printf("Given number is not a prime number");
else
printf("Given number is a prime number");
}
Alternate Answer
#include<stdio.h> Avijit Pramanik 9038652152
void main()
{
int n,i,flag=0;
printf("Enter any number:");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
} Avijit Pramanik 9038652152
if(flag==0)
printf("\nNumber is prime");
else
printf("\nNumber is not prime") ;
}

Avijit Pramanik 9038652152 Page 4 of 6


Avijit Pramanik 9038652152
//9. WAP to calculate 1+x+x^2+x^3+x^4+x^5+.....+x^n
#include<stdio.h>
void main()
{ Avijit Pramanik 9038652152
int i,n;
float x, sum=1.0,term=1.0;
//to avoid garbage value we assign sum=1.0, term=1.0
printf("Enter value of x and n:");
scanf("%f %d",&x,&n);
for(i=1;i<=n;i++)
{
term=term*x;
sum=sum+term;
}
printf("\nSum of the series is:%f",sum);
}
//10. WAP to find HCF or GCD of two numbers
#include<stdio.h>
void main() Avijit Pramanik 9038652152
{
int a,b,i,hcf;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
for(i=1; i<=a&& i<=b; i++)
{
if(a%i==0 && b%i==0)
hcf=i;
}
printf("HCF or GCD is:%d",hcf);
}
Avijit Pramanik 9038652152

Avijit Pramanik 9038652152 Page 5 of 6


Avijit Pramanik 9038652152
//11. WAP to for infinite loop
#include<stdio.h>
void main()
Avijit Pramanik 9038652152
{
int i;
for(i=5;i>2;i++)
{
printf("%d\t",i);
}
}

// 12. WAP to calculate x^n using for loop


#include<stdio.h>
void main() Avijit Pramanik 9038652152
{
int i,n;
float x,prod=1.0;
printf("Enter value of x and n:");
scanf("%f %d",&x,&n);
for(i=1;i<=n;i++)
{
prod=prod*x;
}
printf("Result is:%f",prod);
}

Avijit Pramanik 9038652152 Page 6 of 6

You might also like