For Loop Solution
For Loop Solution
#include <stdio.h>
void main()
{
int i,n,sum=0;
float avg;
printf("Input the 10 numbers : \n");
for (i=1;i<=10;i++)
{
printf("Number-%d :",i);
scanf("%d",&n);
sum +=n;
}
avg=sum/10.0;
printf("The sum of 10 no is : %d\nThe Average is : %f\n",sum,avg);
}
.
5. Write a program in C to display the cube of the number upto given an integer. .
Test Data :
Input number of terms : 5
Expected Output :
Number is : 1 and cube of the 1 is :1
Number is : 2 and cube of the 2 is :8
Number is : 3 and cube of the 3 is :27
Number is : 4 and cube of the 4 is :64
Number is : 5 and cube of the 5 is :125
.
#include <stdio.h>
void main()
{
int i,ctr;
printf("Input number of terms : ");
scanf("%d", &ctr);
for(i=1;i<=ctr;i++)
{
printf("Number is : %d and cube of the %d is :%d \n",i,i, (i*i*i));
}
}
}
printf("\n");
}
}
.
8. Write a program in C to display the n terms of odd natural number and their sum . .
Test Data
Input number of terms : 10
Expected Output :
The odd numbers are :1 3 5 7 9 11 13 15 17 19
The Sum of odd Natural Number upto 10 terms : 100
#include <stdio.h>
void main()
{
int i,n,sum=0;
12. Write a program in C to make such a pattern like right angle triangle with number
increased by 1. .
The pattern like :
1
2 3
4 5 6
7 8 9 10
#include <stdio.h>
void main()
{
int i,j,rows,k=1;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("%d ",k++);
printf("\n");
}
}
.
13. Write a program in C to make such a pattern like a pyramid with numbers increased by 1.
.
1
2 3
4 5 6
7 8 9 10
#include <stdio.h>
void main()
{
int i,j,spc,rows,k,t=1;
printf("Input number of rows : ");
scanf("%d",&rows);
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
printf("%d ",t++);
printf("\n");
spc--;
}
}
.
14. Write a program in C to make such a pattern like a pyramid with an asterisk. .
*
* *
* * *
* * * *
#include <stdio.h>
void main()
{
int i,j,spc,rows,k;
printf("Input number of rows : ");
scanf("%d",&rows);
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
printf("* ");
printf("\n");
spc--;
}
}
.
15. Write a C program to calculate the factorial of a given number. .
Test Data :
Input the number : 5
Expected Output :
The Factorial of 5 is: 120
#include <stdio.h>
void main(){
int i,f=1,num;
for(i=1;i<=num;i++)
f=f*i;
void main()
{
int i,n,sum=0;
void main()
{
int i,j,spc,rows,k;
printf("Input number of rows : ");
scanf("%d",&rows);
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
printf("%d ",i);
printf("\n");
spc--;
}
}
.
18. Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........]. .
Test Data :
Input the Value of x :2
Input the number of terms : 5
Expected Output :
the sum = -0.415873
Number of terms = 5
value of x = 2.000000
#include <stdio.h>
void main()
{
float x,sum,t,d;
int i,n;
printf("Input the Value of x :");
scanf("%f",&x);
printf("Input the number of terms : ");
scanf("%d",&n);
sum =1; t = 1;
for (i=1;i<n;i++)
{
d = (2*i)*(2*i-1);
t = -t*x*x/d;
sum =sum+ t;
}
printf("\nthe sum = %f\nNumber of terms = %d\nvalue of x = %f\n",sum,n,x);
}
Or
#include <stdio.h>
void main()
{
float x,s,t,num=1.00,fac=1.00;
int i,n,pr,y=2,m=1;
for (i=1;i<n;i++)
{
for(pr=1;pr<=y;pr++)
{
fac=fac*pr;
num=num*x;
}
m=m*(-1);
num=num*m;
t=num/fac;
s=s+t;
y=y+2;
num=1.00;
fac=1.00;
}
printf("\nthe sum = %f\nNumber of terms = %d\nvalue of x = %f\n",s,n,x);
}
.
19. Write a program in C to display the n terms of harmonic series and their sum. .
1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Test Data :
Input the number of terms : 5
Expected Output :
1/1 + 1/2 + 1/3 + 1/4 + 1/5 +
Sum of Series upto 5 terms : 2.283334
#include <stdio.h>
void main()
{
int i,n;
float s=0.0;
printf("Input the number of terms : ");
scanf("%d",&n);
printf("\n\n");
for(i=1;i<=n; i++)
{
if(i<n)
{
printf("1/%d + ",i);
s+=1/(float)i;
}
if(i==n)
{
printf("1/%d ",i);
s+=1/(float)i;
}
}
printf("\nSum of Series upto %d terms : %f \n",n,s);
}
.
20. Write a program in C to display the pattern like a pyramid using asterisk and each row
contain an odd number of asterisks. .
*
***
*****
#include <stdio.h>
void main()
{
int i,j,n;
printf("Input number of rows for this pattern :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
}
.
21. Write a program in C to display the sum of the series [ 9 + 99 + 999 + 9999 ...]. .
Test Data :
Input the number or terms :5
Expected Output :
9 99 999 9999 99999
The sum of the saries = 111105
#include <stdio.h>
void main()
{ long int n,i,t=9;
int sum =0;
printf("Input the number or terms :");
scanf("%ld",&n);
for (i=1;i<=n;i++)
{
sum +=t;
printf("%ld ",t);
t=t*10+9;
}
printf("\nThe sum of the series = %d \n",sum);
}
.
22. Write a program in C to print the Floyd's Triangle. .
1
01
101
0101
10101
#include <stdio.h>
void main()
{
int i,j,n,p,q;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
{
p=1;
q=0;
}
else
{
p=0;
q=1;
}
for(j=1;j<=i;j++)
if(j%2==0)
printf("%d",p);
else
printf("%d",q);
printf("\n");
}
}
.
23. Write a program in C to display the sum of the series [ 1+x+x^2/2!+x^3/3!+....]. .
Test Data :
Input the value of x :3
Input number of terms : 5
Expected Output :
The sum is : 16.375000
#include <stdio.h>
void main()
{
float x,sum,no_row;
int i,n;
printf("Input the value of x :");
scanf("%f",&x);
printf("Input number of terms : ");
scanf("%d",&n);
sum =1; no_row = 1;
for (i=1;i<n;i++)
{
no_row = no_row*x/(float)i;
sum =sum+ no_row;
}
printf("\nThe sum is : %f\n",sum);
}
.
24. Write a program in C to find the sum of the series [ x - x^3 + x^5 + ......]. .
Test Data :
Input the value of x :2
Input number of terms : 5
Expected Output :
The values of the series:
2
-8
32
-128
512
The sum = 410
#include <stdio.h>
#include <math.h>
void main()
{
int x,sum,ctr;
int i,n,m,mm,nn;
printf("Input the value of x :");
scanf("%d",&x);
printf("Input number of terms : ");
scanf("%d",&n);
sum =x; m=-1;
printf("The values of the series: \n");
printf("%d\n",x);
for (i = 1; i < n; i++)
{
ctr = (2 * i + 1);
mm = pow(x, ctr);
nn = mm * m;
printf("%d \n",nn);
sum = sum + nn;
m = m * (-1);
}
printf("\nThe sum = %d\n",sum);
}
.
25. Write a program in C to display the n terms of square natural number and their sum. .
1 4 9 16 ... n Terms
Test Data :
Input the number of terms : 5
Expected Output :
The square natural upto 5 terms are :1 4 9 16 25
The Sum of Square Natural Number upto 5 terms = 55
#include <stdio.h>
void main()
{
int i,n,sum=0;
printf("Input the number of terms : ");
scanf("%d",&n);
printf("\nThe square natural upto %d terms are :",n);
for(i=1;i<=n;i++)
{
printf("%d ",i*i);
sum+=i*i;
}
printf("\nThe Sum of Square Natural Number upto %d terms = %d \n",n,sum);
}
.
26. Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms. .
Test Data :
Input the number of terms : 5
Expected Output :
1 + 11 + 111 + 1111 + 11111
The Sum is : 12345
#include <stdio.h>
void main()
{
int n,i;
long sum=0;
long int t=1;
printf("Input the number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%ld ",t);
if (i<n)
{
printf("+ ");
}
sum=sum+t;
t=(t*10)+1;
}
printf("\nThe Sum is : %ld\n",sum);
}
.
27. Write a c program to check whether a given number is a perfect number or not. .
Test Data :
Input the number : 56
Expected Output :
The positive divisor : 1 2 4 7 8 14 28
The sum of the divisor is : 64
So, the number is not perfect.
/*Perfect number is a positive number which sum of all positive divisors excluding that number is
equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3. Sum of its
divisor is 1 + 2+ 3 = 6*/
#include <stdio.h>
void main()
{
int n,i,sum;
int mn,mx;
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;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
printf("\n");
}
.
29. Write a C program to check whether a given number is an armstrong number or not. .
Test Data :
Input a number: 153
Expected Output :
153 is an Armstrong number.
#include <stdio.h>
void main(){
int num,r,sum=0,temp;
printf("Input a number: ");
scanf("%d",&num);
}
.
30. Write a C program to find the Armstrong number for a given range of number. .
Test Data :
Input starting number of range: 1
Input ending number of range : 1000
Expected Output :
Armstrong numbers in given range are: 1 153 370 371 407
/*When the sum of the cube of the individual digits of a number
is equal to that number, the number is called Armstrong number. For example 153.
Sum of its divisor is 13 + 53;+ 33; = 1+125+27 = 153*/
#include <stdio.h>
void main(){
int num,r,sum,temp;
int stno,enno;
while(temp!=0){
r=temp % 10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
printf("\n");
}
.
31. Write a program in C to display the pattern like a diamond. .
*
***
*****
*******
*********
*******
*****
***
*
#include <stdio.h>
void main()
{
int i,j,r;
printf("Input number of rows (half of the diamond) :");
scanf("%d",&r);
for(i=0;i<=r;i++)
{
for(j=1;j<=r-i;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
for(i=r-1;i>=1;i--)
{
for(j=1;j<=r-i;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
}
.
32. Write a C program to determine whether a given number is prime or not. .
Test Data :
Input a number: 13
Expected Output :
13 is a prime number.
#include <stdio.h>
void main(){
int num,i,ctr=0;
printf("Input a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num % i==0){
ctr++;
break;
}
}
if(ctr==0 && num!= 1)
printf("%d is a prime number.\n",num);
else
printf("%d is not a prime number",num);
}
.
33. Write a C program to display Pascal's triangle. .
Test Data :
Input number of rows: 5
Expected Output :
1
1 1
1 2 1
1 3 1 3
1 4 6 4 1
#include <stdio.h>
void main()
{
int no_row,c=1,blk,i,j;
printf("Input number of rows: ");
scanf("%d",&no_row);
for(i=0;i<no_row;i++)
{
for(blk=1;blk<=no_row-i;blk++)
printf(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("% 4d",c);
}
printf("\n");
}
}
.
34. Write a program in C to find the prime numbers within a range of numbers. .
Test Data :
Input starting number of range: 1
Input ending number of range : 50
Expected Output :
The prime number between 1 and 50 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
#include <stdio.h>
void main(){
int num,i,ctr,stno,enno;
for(num = stno;num<=enno;num++)
{
ctr = 0;
for(i=2;i<=num/2;i++)
{
if(num%i==0){
ctr++;
break;
}
}
void main()
{
int prv=0,pre=1,trm,i,n;
printf("Input number of terms to display : ");
scanf("%d",&n);
printf("Here is the Fibonacci series upto to %d terms : \n",n);
printf("% 5d % 5d", prv,pre);
for(i=3;i<=n;i++)
{
trm=prv+pre;
printf("% 5d",trm);
prv=pre;
pre=trm;
}
printf("\n");
}
.
36. Write a program in C to display the such a pattern for n number of rows using a number which
will start with the number 1 and the first and a last number of each row will be 1. .
1
121
12321
#include <stdio.h>
void main()
{
int i,j,n;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
/* print blank spaces */
for(j=1;j<=n-i;j++)
printf(" ");
/* Display number in ascending order upto middle*/
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
}
.
37. Write a program in C to display the number in reverse order. .
Test Data :
Input a number: 12345
Expected Output :
The number in reverse order is : 54321
#include <stdio.h>
void main(){
int num,r,sum=0,t;
for(t=num;num!=0;num=num/10){
r=num % 10;
sum=sum*10+r;
}
printf("The number in reverse order is : %d \n",sum);
}
.
38. Write a program in C to check whether a number is a palindrome or not. .
Test Data :
Input a number: 121
Expected Output :
121 is a palindrome number.
.
39. Write a program in C to find the number and sum of all integer between 100 and 200 which are
divisible by 9. .
Expected Output :
Numbers between 100 and 200, divisible by 9 :
108 117 126 135 144 153 162 171 180 189 198
The sum : 1683
#include <stdio.h>
void main(){
int num,r,sum=0,t;
for(t=num;num!=0;num=num/10){
r=num % 10;
sum=sum*10+r;
}
if(t==sum)
printf("%d is a palindrome number.\n",t);
else
printf("%d is not a palindrome number.\n",t);
}
.
40. Write a program in C to find the number and sum of all integer between 100 and 200 which are
divisible by 9.
#include <stdio.h>
void main()
{
int i, sum=0;
printf("Numbers between 100 and 200, divisible by 9 : \n");
for(i=101;i<200;i++)
{
if(i%9==0)
{
printf("% 5d",i);
sum+=i;
}
}
printf("\n\nThe sum : %d \n",sum);
}
41. Write a C Program to display the pattern like pyramid using the alphabet. .
A
A B A
A B C B A
A B C D C B A
#include <stdio.h>
void main()
{
int i, j;
char alph = 'A';
int n,blk;
int ctr = 1;
printf("Input the number of Letters (less than 26) in the Pyramid : ");
scanf("%d", &n);
printf(" ");
for (j = 0; j <= (ctr / 2); j++) {
printf("%c ", alph++);
}
alph = alph - 2;
.
44. Write a program in C to convert a binary number into a decimal number without using array,
function and while loop. .
Test Data :
Input a binary number :1010101
Expected Output :
The Binary Number : 1010101
The equivalent Decimal Number : 85
#include <stdio.h>
void main()
{ int n1, n,p=1;
int dec=0,i=1,j,d;
void main()
{
int i, n1, n2, j, hcf=1;
j = (n1<n2) ? n1 : n2;
void main()
{
int i, n1, n2, j, hcf=1,lcm;
printf("\n\n LCM of two numbers:\n ");
printf("----------------------\n");
j = (n1<n2) ? n1 : n2;
}
.
47. Write a program in C to find LCM of any two numbers. .
Test Data :
Input 1st number for LCM: 15
Input 2nd number for LCM: 20
Expected Output :
The LCM of 15 and 20 is : 60
#include <stdio.h>
void main()
{
int i, n1, n2, max, lcm=1;
for(i=max; ; i+=max)
{
}
.
48. Write a program in C to convert a binary number into a decimal number using math function. .
Test Data :
Input the binary number :1010100
Expected Output :
The Binary Number : 1010100
The equivalent Decimal Number is : 84
#include <stdio.h>
#include <math.h>
void main()
{ int n1, n;
int dec=0,i=0,j,d;
n1 = n;
for(j=n;j>0;j=j/10)
{
fact = 1;
for(i=1; i<=j % 10; i++)
{
fact = fact * i;
}
s1 = s1 + fact;
if(s1==n1)
{
printf("\n%d is Strong number.\n\n", n1);
}
else
{
printf("\n%d is not Strong number.\n\n", n1);
}
}
.
48. Write a C program to find Strong Numbers within a range of numbers. .
Test Data :
Input starting range of number : 1
Input ending range of number: 200
Expected Output :
The Strong numbers are :
1 2 145
#include <stdio.h>
void main()
{
int i, n, n1, s1=0,j,k,en,sn;
long fact;
for(k=sn;k<=en;k++)
{
n1=k;
s1=0;
for(j=k;j>0;j=j/10)
{
fact = 1;
for(i=1; i<=j % 10; i++)
{
fact = fact * i;
}
s1 = s1 + fact;
}
if(s1==n1)
void main(){
int n1,df,n2,i,ln;
int s1=0;
s1 = ( n2 * ( 2 * n1 + ( n2 -1 ) * df ) )/ 2;
ln = n1 + (n2-1) * df;
printf("\nThe Sum of the A.P. series are : \n");
for(i=n1;i<=ln; i= i + df ){
if (i != ln)
printf("%d + ",i);
else
printf("%d = %d \n\n",i,s1);
}
}
.
50. Write a program in C to convert a decimal number into octal without using an array. .
Test Data :
Enter a number to convert : 79
Expected Output :
The Octal of 79 is 117.
#include <stdio.h>
void main()
{
int n, i, j, ocno=0,dn;
dn=n;
i=1;
for(j=n;j>0;j=j/8)
{
ocno=ocno+(j % 8)*i;
i=i*10;
n=n/8;
}
.
51. Write a program in C to convert an octal number to a decimal without using an array. .
Test Data :
Input an octal number (using digit 0 - 7) :745
Expected Output :
The Octal Number : 745
The equivalent Decimal Number : 485
#include <stdio.h>
void main()
{ int n1, n5,p=1,k,ch=1;
int dec=0,i=1,j,d;
for(;n1>0;n1=n1/10)
{
k=n1 % 10;
if(k>=8)
{
ch=0;
}
}
switch(ch)
{
case 0 :
printf("\nThe number is not an octal number. \n\n");
break;
case 1:
n1=n5;
for (j=n1;j>0;j=j/10)
{
d = j % 10;
if(i==1)
p=p*1;
else
p=p*8;
dec=dec+(d*p);
i++;
}
printf("\nThe Octal Number : %d\nThe equivalent Decimal Number : %d \n\n",n5,dec);
break;
}
}
.
52. Write a program in c to find the Sum of GP series. .
Test Data :
Input the first number of the G.P. series: 3
Input the number or terms in the G.P. series: 5
Input the common ratio of G.P. series: 2
Expected Output :
The numbers for the G.P. series:
3.000000 6.000000 12.000000 24.000000 48.000000
The Sum of the G.P. series : 93.000000
#include <stdio.h>
#include <math.h>
void main()
{
float g1,cr,i,n,j;
float ntrm,gpn;
float sum=0;
for(j=1;j<ntrm;j++)
{
gpn=g1*pow(cr,j);
sum=sum+gpn;
printf("%f ",gpn);
}
/*-------- End of G.P. series generate ---------------*/
printf("\nThe Sum of the G.P. series : %f\n\n",sum);
}
.
53. Write a program in C to convert a binary number to octal. .
Test Data :
Input a binary number :1001
Expected Output :
The Binary Number : 1001
The equivalent Octal Number : 11
#include <stdio.h>
#include <math.h>
void main()
{ int n1, n,p=1;
int dec=0,i=1,j,d;
int ocno=0,dn;
dec=dec+(d*p);
i++;
}
/*--------------------------------------------*/
dn=dec;
i=1;
for(j=dec;j>0;j=j/8)
{
ocno=ocno+(j % 8)*i;
i=i*10;
n=n/8;
}
void main()
{
for (j=n1;j>0;j=j/10)
{
d = j % 10;
if(i==1)
p=p*1;
else
p=p*8;
dec=dec+(d*p);
i++;
}
/*------------------------------------------------------------------------------*/
i=1;
for(j=dec;j>0;j=j/2)
{
binno=binno+(dec % 2)*i;
i=i*10;
dec=dec/2;
}
}
.
55. Write a program in C to convert a decimal number to hexadecimal. .
Test Data :
Input any Decimal number: 79
Expected Output :
The equivalent Hexadecimal Number : 4F
#include <stdio.h>
void main()
{
long int decn,rmd,q,dn=0,m,l;
int i=1,j,tmp;
char s;
void main()
{
char str1[100], tmp;
int l, lind, rind,i;
lind = 0;
rind = l-1;
for(i=lind;i<rind;i++)
{
tmp = str1[i];
str1[i] = str1[rind];
str1[rind] = tmp;
rind--;
}
printf("Reversed string is: %s\n\n", str1);
}
.
58. Write a C program to find the length of a string without using the library function. .
Test Data :
Input a string : welcome
Expected Output :
The string contains 7 number of characters.
So, the length of the string welcome is : 7
#include <stdio.h>
#include <string.h>
void main()
{
char str1[50];
int i, l = 0;
int main()
{
int n1, onum, r, result = 0, n = 0 ;
printf("\n\n Check whether an n digits number is armstrong or not :\n");
printf("-----------------------------------------------------------\n");
onum = n1;
while (onum != 0)
{
onum /= 10;
++n;
}
onum = n1;
while (onum != 0)
{
r = onum % 10;
result += pow(r, n);
onum /= 10;
}
if(result == n1)
printf(" %d is an Armstrong number.\n\n", n1);
else
printf(" %d is not an Armstrong number.\n\n", n1);
return 0;
}
.