for loop-merged
for loop-merged
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("%d\n",i);
}
return 0;
}
#include<stdio.h>
int main()
{
int i,sum=0;
for(i=1;i<=10;i++)
{
sum=sum+i;
}
printf("Sum:%d",sum);
return 0;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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);
}
#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);
}
}
#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");
}