0% found this document useful (0 votes)
4 views

for loop-merged

The document contains multiple C programming examples demonstrating the use of for loops to perform various tasks such as displaying natural numbers, calculating sums, generating patterns, and checking for perfect and Armstrong numbers. Each program is structured with standard input and output functions, showcasing fundamental programming concepts in C. The examples serve as practical exercises for learning and understanding loops and basic algorithms.

Uploaded by

aryankhanx595
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)
4 views

for loop-merged

The document contains multiple C programming examples demonstrating the use of for loops to perform various tasks such as displaying natural numbers, calculating sums, generating patterns, and checking for perfect and Armstrong numbers. Each program is structured with standard input and output functions, showcasing fundamental programming concepts in C. The examples serve as practical exercises for learning and understanding loops and basic algorithms.

Uploaded by

aryankhanx595
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/ 16

FOR LOOP

Write a program in C to display the first 10 natural numbers.

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

Write a C program to compute the sum of the first 10 natural numbers.

#include<stdio.h>
int main()
{
int i,sum=0;
for(i=1;i<=10;i++)
{
sum=sum+i;
}
printf("Sum:%d",sum);
return 0;
}

Write a program in C to display n terms of natural numbers and their sum.


#include<stdio.h>
int main()
{
int i,sum=0,n;
printf("Input n:");
scanf("%d",&n);
printf("The first %d natural numbers:",n);
for(i=1;i<=n;i++)
{
printf("%d ",i);
sum=sum+i;
}
printf("\nThe Sum of Natural Number upto %d terms :%d",n,sum);
return 0;
}
Write a program in C to read 10 numbers from the keyboard and find their
sum and average

#include<stdio.h>
int main()
{
float i,sum=0;
float avg;
for(i=1;i<=10;i++)
{
sum=sum+i;
}
printf("The Sum of Natural Number upto 10 terms :%f",sum);
avg=sum/10;
printf("\nThe Average is:%f",avg);
return 0;
}

Write a program in C to display the cube of the number up to an integer.

#include<stdio.h>
int main()
{
int i,n;
printf("Input number of terms:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Number is :%d and cube of the %d is :%d \n",i,i,i*i*i);
}
return 0;
}
Write a program in C to display the multiplication table for a given integer

#include<stdio.h>
int main()
{
int i,n;
printf("Input number(Table to be calculated) :");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("%d * %d : %d \n",n,i,n*i);
}
return 0;
}

Write a program in C to display the multiplier table vertically from 1 to n

#include<stdio.h>
int main()
{
int i,n;
printf("Input upto the table number starting from 1 :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d * %d : %d ",i,1,i*1);
}
printf("\n");
for(i=1;i<=n;i++)
{
printf("%d * %d : %d ",i,10,i*10);
}
return 0;
}
Write a C program to display the n terms of odd natural numbers and their
sum.

#include<stdio.h>
int main()
{
int i,n,sum=0;
printf("Input number of terms :");
scanf("%d",&n);
printf("The Odd numbers are:");
for(i=1; i<=n; i++)
{
printf("%d ",2*i-1);
sum+=2*i-1;
}
printf("\nThe Sum of odd Natural Number upto %d terms :%d",n,sum);
return 0;
}

Write a program in C to display a pattern like a right angle triangle using


an asterisk.

#include<stdio.h>
int main()
{
int row,column,n;
printf("Input Pattern Size :");
scanf("%d",&n);
for(row=1; row<=n; row++)
{
for(column=1;column<=row;column++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Write a C program to display a pattern like a right angle triangle with a
number.

#include<stdio.h>
int main()
{
int row,column,n;
printf("Input Pattern Size :");
scanf("%d",&n);
for(row=1; row<=n; row++)
{
for(column=1;column<=row;column++)
{
printf("%d",column);
}
printf("\n");
}
return 0;
}

Write a program in C to make such a pattern like a right angle triangle with
a number which will repeat a number in a row.

#include<stdio.h>
int main()
{
int row,column,n;
printf("Input Pattern Size :");
scanf("%d",&n);
for(row=1; row<=n; row++)
{
for(column=1;column<=row;column++)
{
printf("%d",row);
}
printf("\n");
}
return 0;
}
Write a program in C to make such a pattern like a right angle triangle with
the number increased by 1.

#include<stdio.h>
int main()
{
int row,column,n,k=1;//k for incrementing Numbers
printf("Input Pattern Size :");
scanf("%d",&n);
for(row=1; row<=n; row++)
{
for(column=1;column<=row;column++)
{
printf("%d ",k++);
}
printf("\n");
}
return 0;
}

Write a program in C to make a pyramid pattern with numbers increased by


1.

#include<stdio.h>
int main()
{
int row,column,n,k=1;//k for incrementing Numbers
printf("Input Pattern Size :");
scanf("%d",&n);
int space=n-row;
for(row=1; row<=n; row++)
{
for(column=1;column<=space;column++)
{
printf(" ");
}
for(column=1;column<=row;column++)
{
printf("%d ",k++);
}
printf("\n");
space--;
}
return 0;
}
Write a C program to make such a pattern as a pyramid with an asterisk
#include<stdio.h>
int main()
{
int row,column,n;
printf("Input Pattern Size :");
scanf("%d",&n);
int space=n-row;
for(row=1; row<=n; row++)
{
for(column=1;column<=space;column++)
{
printf(" ");
}
for(column=1;column<=row;column++)
{
printf("* ");
}
printf("\n");
space--;
}
return 0;
}

Write a C program to calculate the factorial of a given number

#include<stdio.h>
int main()
{
int i,n,fact=1;
printf("Input the Number:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
fact=fact*i;
}
printf("The Factorial of %d is: %d",n,fact);
return 0;
}
Write a C program to display the sum of n terms of even natural numbers

#include<stdio.h>
int main()
{
int i,n,sum=0;
printf("Input the Number:");
scanf("%d",&n);
printf("The Even Numbers are: ");
for(i=1; i<=n; i++)
{
printf("%d ",2*i);
sum+=2*i;
}
printf("\nThe Sum of even Natural Number upto %d terms: %d",n,sum);
return 0;
}

Write a C program to make such a pattern like a pyramid with a number


which will repeat the number in the same row.

#include<stdio.h>
int main()
{
int row,column,n,k; //k for loop counter
printf("Input the Size of Pattern:");
scanf("%d",&n);
int space=n+4-1;
for(row=1; row<=n; row++)
{
for(k=space; k>=1; k--)
{
printf(" ");
}
for(column=1; column<=row; column++)
{
printf("%d ",row);
}
printf("\n");
space--;
}
return 0;
}
Write a program in C to display the n terms of a harmonic series and their
sum. 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
#include<stdio.h>
int main()
{
int i,n;
float sum=0;
printf("Input the number of terms:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
if(i<n)
{
printf("1/%d + ", i); // Print the term with a plus sign.
sum += 1 / (float)i; // Calculate and add the term to the sum.
}
if (i == n)
{
printf("1/%d ", i); // Print the last term without a plus sign.
sum += 1 / (float)i; // Calculate and add the term to the sum.
}
}
printf("\nSum of Series upto %d terms : %f \n", n, sum);
return 0;
}

Write a C program to display the pattern as a pyramid using asterisks, with


each row containing an odd number of asterisks.
#include<stdio.h>
int main()
{
int row,column,n;
printf("Input the number of terms:");
scanf("%d",&n);
for(row=0; row<n; row++)
{
for(column=1;column<=n-row;column++)
{
printf(" ");
}
for(column=1;column<=2*row-1;column++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........].

#include<stdio.h>
void main()
{
int n,i;
float sum,x,term,deno;
printf("Input the number of terms:");
scanf("%d",&n);
printf("Input the value of x:");
scanf("%f",&x);
sum=1;
term=1;
for(i=1; i<n; i++)
{
deno=(2*i)*(2*i-1);
term=-term*x*x/deno;
sum=sum+term;
}
printf("\nThe sum = %f\nNumber of terms = %d\nValue of x = %f\n",
sum, n, x);
}

Write a program in C to display the sum of the series [ 9 + 99 + 999 +


9999 ...].

#include<stdio.h>
void main()
{
long int n,i,term=9;
int sum=0;
printf("Input the number of terms:");
scanf("%ld",&n);
for(i=1; i<=n; i++)
{
sum+=term;
printf("%ld ",term);
term=term*10+9;
}
printf("\nThe sum of the series = %d \n", sum);
}
Write a program in C to print Floyd's Triangle.

#include <stdio.h>
void main()
{
int row, column, n, p, q;
printf("Input number of rows : ");
scanf("%d", &n);
for (row = 1; row <= n; row++)
{
if (row % 2 == 0) // Check if 'row' is even.
{
p = 1;
q = 0;
}
else // If 'row' is odd.
{
p = 0;
q = 1;
}
for (column = 1; column <= row; column++)
{
if (column % 2 == 0) // Check if 'column' is even.
printf("%d", p); // Print 'p' if 'column' is even.
else
printf("%d", q); // Print 'q' if 'column' is odd.
}
printf("\n");
}
}
Write a program in C to find the sum of the series [1+x+x^2/2!+x^3/3!+....].

#include<stdio.h>
void main()
{
int n,i;
float sum,x,term;
printf("Input the number of terms:");
scanf("%d",&n);
printf("Input the value of x:");
scanf("%f",&x);
sum=1;
term=1;
for(i=1; i<n; i++)
{
term=term*x/(float)i;// Calculate the next term using the given formula.
sum=sum+term;
}
printf("\nThe sum = %f", sum);
}

Write a C program that displays the n terms of square natural numbers and
their sum

#include<stdio.h>
void main()
{
int n,i,sum=0;
printf("Input the number of terms:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d ",i*i);
sum=sum+i*i;
}
printf("\nThe Sum:%d",sum);
}
Write a program in C to find the sum of the series [ x - x^3 + x^5 + ......]

#include<stdio.h>
#include<math.h>
void main()
{
int x,n,sum,vx,ctp,m,vm,i;
printf("Input value of x:");
scanf("%d",&x);
printf("Input value of term:");
scanf("%d",&n);
sum=x;
m=-1;
printf("The Value of series:\n");
printf("%d\n",x);
for(i=1;i<n;i++)
{
ctp=(2*i+1);// Calculate the power of 'x' using the given formula.
vx=pow(x,ctp);// Calculate the value of 'x' raised to the power 'ctp'.
vm=vx*m;
printf("%d\n",vm);
sum=sum+vm; // Add the term to the running sum.
m=m*(-1);// Toggle the value of 'm' between -1 and 1.
}
printf("\nThe sum = %d\n", sum);
}

Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n
terms

#include<stdio.h>
void main()
{
long int n,i,term=1;
int sum=0;
printf("Input the number of terms:");
scanf("%ld",&n);
for(i=1; i<=n; i++)
{
sum+=term;
printf("%ld ",term);
term=term*10+1;
}
printf("\nThe sum of the series = %d \n", sum);
}
Write a C program to check whether a given number is a 'Perfect' number or
not.
#include<stdio.h>
void main()
{
int n,i,sum=0;
printf("Input Number:");
scanf("%d",&n);
for(i=1; i<n; i++) {
if(n%i==0) {
sum+=i;
}
}
if(sum==n) {
printf("%d is a Perfect Number",n);
}
else {
printf("%d is Not a Perfect Number",n);
}
}

Write a C program to find the 'Perfect' numbers within a given number of


ranges.
#include <stdio.h>
void main(){
int n, i, sum;
int mn, mx;
printf("Input the starting range or number : ");
scanf("%d", &mn);
printf("Input the ending range of number : ");
scanf("%d", &mx);
printf("The Perfect numbers within the given range : ");
for(n = mn; n <= mx; n++)
{
i = 1; // Initialize the divisor.
sum = 0; // Initialize the sum of divisors.
while(i < n){
if(n % i == 0) // If 'i' is a divisor of 'n'.
sum = sum + i;
i++;
}
if(sum == n)
printf("%d ", n);
}
printf("\n");
}
Write a C program to check whether a given number is an Armstrong number
or not.

#include<stdio.h>
#include<math.h>
int main()
{
int num,temp,originalnum,remainder,sum=0,n=0;
printf("Input a Number:");
scanf("%d",&num);
originalnum=num;
// Calculate the number of digits in the input number
for(temp=num;temp!=0;n++)
{
temp/=10;
}
// Calculate the sum of the nth powers of each digit
for(temp=num;temp!=0;temp/=10)
{
remainder=temp%10;
sum+=pow(remainder,n);
}
if (sum == originalnum) {
printf("%d is an Armstrong number.\n", originalnum);
} else {
printf("%d is not an Armstrong number.\n", originalnum);
}
}
Write a C program to find the Armstrong number for a given range of
number.

#include<stdio.h>
#include<math.h>
int main()
{
int sn,en;
int num,temp,remainder,sum;
printf("Input Starting Number:");
scanf("%d",&sn);
printf("Input Ending Number:");
scanf("%d",&en);
printf("Armstrong numbers in given range are: ");
for(num=sn;num<=en;num++)
{
temp=num;
sum=0;
while(temp!=0)
{
remainder=temp%10;// Get the last digit of 'temp'.
temp/=10;// Remove the last digit from 'temp'
sum=sum+remainder*remainder*remainder;// Calculate the sum of
cubes of each digit.
}
if(sum==num)
printf("%d ",num);
}
printf("\n");
}

You might also like