0% found this document useful (0 votes)
109 views33 pages

For Loop Solution

Uploaded by

khushibhasker01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views33 pages

For Loop Solution

Uploaded by

khushibhasker01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Programming For loop Program

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


Expected Output :
1 2 3 4 5 6 7 8 9 10
#include <stdio.h>
void main()
{
int i;
printf("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
{
printf("%d ",i);
}
printf("\n");
}
2. Write a C program to find the sum of first 10 natural numbers. .
Expected Output :
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10
The Sum is : 55
#include <stdio.h>
void main()
{
int j, sum = 0;

printf("The first 10 natural number is :\n");

for (j = 1; j <= 10; j++)


{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}
3. Write a program in C to display n terms of natural number and their sum..
Test Data : 7
Expected Output :
The first 7 natural number is :
1234567
The Sum of Natural Number upto 7 terms : 28
#include <stdio.h>
void main()
{
int i,n,sum=0;
printf("Input Value of terms : ");
scanf("%d",&n);
printf("\nThe first %d natural numbers are:\n",n);
for(i=1;i<=n;i++)
{
printf("%d ",i);
sum+=i;
}
printf("\nThe Sum of natural numbers upto %d terms : %d \n",n,sum);
}
.
4. Write a program in C to read 10 numbers from keyboard and find their sum and average. .
Test Data :
Input the 10 numbers :
Number-1 :2
...
Number-10 :2
Expected Output :
The sum of 10 no is : 55
The Average is : 5.500000

#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));
}
}

6. Write a program in C to display the multiplication table of a given integer. .


Test Data :
Input the number (Table to be calculated) : 15
Expected Output :
15 X 1 = 15
...
...
15 X 10 = 150
#include <stdio.h>
void main()
{
int j,n;
printf("Input the number (Table to be calculated) : ");
scanf("%d",&n);
printf("\n");
for(j=1;j<=10;j++)
{
printf("%d X %d = %d \n",n,j,n*j);
}
}
.
7. Write a program in C to display the multipliaction table vertically from 1 to n. .
Test Data :
Input upto the table number starting from 1 : 8
Expected Output :
Multiplication table from 1 to 8
1x1 = 1, 2x1 = 2, 3x1 = 3, 4x1 = 4, 5x1 = 5, 6x1 = 6, 7x1 = 7, 8x1 = 8
...
1x10 = 10, 2x10 = 20, 3x10 = 30, 4x10 = 40, 5x10 = 50, 6x10 = 60, 7x10 = 70, 8x10 = 80
#include <stdio.h>
void main()
{
int j,i,n;
printf("Input upto the table number starting from 1 : ");
scanf("%d",&n);
printf("Multiplication table from 1 to %d \n",n);
for(i=1;i<=10;i++)
{
for(j=1;j<=n;j++)
{
if (j<=n-1)
printf("%dx%d = %d, ",j,i,i*j);
else
printf("%dx%d = %d",j,i,i*j);

}
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;

printf("Input number of terms : ");


scanf("%d",&n);
printf("\nThe 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",n,sum);
}
.
9. Write a program in C to display the pattern like right angle triangle using an asterisk. .
The pattern like :
*
**
***
****
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
}
.
10. Write a program in C to display the pattern like right angle triangle with a number. .
The pattern like :
1
12
123
1234
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
}
11. Write a program in C to make such a pattern like right angle triangle with a number which
will repeat a number in a row. .
The pattern like :
1
22
333
4444
#include <stdio.h>
void main()
{
int i,j,rows;

printf("Input number of rows : ");


scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
}
}

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;

printf("Input the number : ");


scanf("%d",&num);

for(i=1;i<=num;i++)
f=f*i;

printf("The Factorial of %d is: %d\n",num,f);


}
.
16. Write a program in C to display the n terms of even natural number and their sum. .
Test Data :
Input number of terms : 5
Expected Output :
The even numbers are :2 4 6 8 10
The Sum of even Natural Number upto 5 terms : 30
#include <stdio.h>

void main()
{
int i,n,sum=0;

printf("Input number of terms : ");


scanf("%d",&n);
printf("\nThe 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",n,sum);
}
.
17. Write a program in C to make such a pattern like a pyramid with a number which will
repeat the number in the same row. .
1
2 2
3 3 3
4 4 4 4
#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("%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;

printf("Input the Value of x :");


scanf("%f",&x);
printf("Input the number of terms : ");
scanf("%d",&n);
s=1.00; t=1.00;

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;

printf("Input the number : ");


scanf("%d",&n);
sum = 0;
printf("The positive divisor : ");
for (i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
printf("%d ",i);
}
}
printf("\nThe sum of the divisor is : %d",sum);
if(sum==n)
printf("\nSo, the number is perfect.");
else
printf("\nSo, the number is not perfect.");
printf("\n");
}
.
28. Write a c program to find the perfect numbers within a given number of range. .
Test Data :
Input the starting range or number : 1
Input the ending range of number : 50
Expected Output :
The Perfect numbers within the given range : 6 28
/*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;
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);

for(temp=num; num!=0; num=num/10)


{
r=num % 10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number.\n",temp);
else
printf("%d is not an Armstrong number.\n",temp);

}
.
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;

printf("Input starting number of range: ");


scanf("%d",&stno);

printf("Input ending number of range : ");


scanf("%d",&enno);

printf("Armstrong numbers in given range are: ");


for(num=stno;num<=enno;num++){
temp=num;
sum = 0;

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;

printf("Input starting number of range: ");


scanf("%d",&stno);

printf("Input ending number of range : ");


scanf("%d",&enno);
printf("The prime numbers between %d and %d are : \n",stno,enno);

for(num = stno;num<=enno;num++)
{
ctr = 0;

for(i=2;i<=num/2;i++)
{
if(num%i==0){
ctr++;
break;
}
}

if(ctr==0 && num!= 1)


printf("%d ",num);
}
printf("\n");
}
.
35. Write a program in C to display the first n terms of Fibonacci series. .
Fibonacci series 0 1 2 3 5 8 13 .....
Test Data :
Input number of terms to display : 10
Expected Output :
Here is the Fibonacci series upto to 10 terms :
0 1 1 2 3 5 8 13 21 34
#include <stdio.h>

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);

/* Display number in reverse order after middle */


for(j=i-1;j>=1;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;

printf("Input a number: ");


scanf("%d",&num);

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;

printf("Input a number: ");


scanf("%d",&num);

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);

for (i = 1; i <= n; i++)


{
for(blk=1;blk<=n-i;blk++)

printf(" ");
for (j = 0; j <= (ctr / 2); j++) {
printf("%c ", alph++);
}

alph = alph - 2;

for (j = 0; j < (ctr / 2); j++) {


printf("%c ", alph--);
}
ctr = ctr + 2;
alph = 'A';
printf("\n");
}
}
.
43. Write a program in C to convert a decimal number into binary without using an array. .
Test Data :
Input a decimal number: 25
Binary number equivalent to said decimal number is: 0000000000000000000000000001 1001
#include <stdio.h>
#include <stdlib.h>
char *decimal_to_binary(int);
char *decimal_to_binary(int dn)
{
int i, j, temp;
char *ptr;
temp = 0;
ptr = (char*)malloc(32+1);
for (i = 31 ; i >= 0 ; i--)
{
j = dn >> i;
if (j & 1)
*(ptr+temp) = 1 + '0';
else
*(ptr+temp) = 0 + '0';
temp++;
}
*(ptr+temp) = '\0';
return ptr;
}
int main()
{
int dn;
char *ptr;
printf("Input a decimal number: ");
scanf("%d", &dn);
ptr = decimal_to_binary(dn);
printf("Binary number equivalent to said decimal number is: %s", ptr);
free(ptr);
return 0;
}

.
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;

printf("\n\n Convert Binary to Decimal:\n ");


printf("-------------------------\n");

printf("Input a binary number :");


scanf("%d",&n);
n1=n;
for (j=n;j>0;j=j/10)
{
d = j % 10;
if(i==1)
p=p*1;
else
p=p*2;
dec=dec+(d*p);
i++;
}
printf("\nThe Binary Number : %d\nThe equivalent Decimal Number : %d \n\n",n1,dec);
}
.
45. Write a C program to find HCF (Highest Common Factor) of two numbers. .
Test Data :
Input 1st number for HCF: 24
Input 2nd number for HCF: 28
Expected Output :
HCF of 24 and 28 is : 4
#include <stdio.h>

void main()
{
int i, n1, n2, j, hcf=1;

printf("\n\n HCF of two numbers:\n ");


printf("----------------------\n");

printf("Input 1st number for HCF: ");


scanf("%d", &n1);
printf("Input 2nd number for HCF: ");
scanf("%d", &n2);

j = (n1<n2) ? n1 : n2;

for(i=1; i<=j; i++)


{

if(n1%i==0 && n2%i==0)


{
hcf = i;
}
}

printf("\nHCF of %d and %d is : %d\n\n", n1, n2, hcf);


}
.
46. Write a program in C to find LCM of any two numbers using HCF. .
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, j, hcf=1,lcm;
printf("\n\n LCM of two numbers:\n ");
printf("----------------------\n");

printf("Input 1st number for LCM: ");


scanf("%d", &n1);
printf("Input 2nd number for LCM: ");
scanf("%d", &n2);

j = (n1<n2) ? n1 : n2;

for(i=1; i<=j; i++)


{

if(n1%i==0 && n2%i==0)


{
hcf = i;
}
}
/* We know the multiplication of HCF and LCM is equivalant
to the multiplication of these two numbers.*/
lcm=(n1*n2)/hcf;

printf("\nThe LCM of %d and %d is : %d\n\n", n1, n2, lcm);

}
.
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;

printf("\n\n LCM of two numbers:\n ");


printf("----------------------\n");

printf("Input 1st number for LCM: ");


scanf("%d", &n1);
printf("Input 2nd number for LCM: ");
scanf("%d", &n2);

max = (n1>n2) ? n1 : n2;

for(i=max; ; i+=max)
{

if(i%n1==0 && i%n2==0)


{
lcm = i;
break;
}
}

printf("\nLCM of %d and %d = %d\n\n", n1, n2, lcm);

}
.
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;

printf("\n\nConvert Binary to Decimal:\n ");


printf("-------------------------\n");

printf("Input the binary number :");


scanf("%d",&n);
n1=n;
while(n!=0)
{ d = n % 10;
dec=dec+d*pow(2,i);
n=n/10;
i++;
}
printf("\nThe Binary Number : %d\nThe equivalent Decimal Number is : %d\n\n",n1,dec);
}
.
49. Write a C program to check whether a number is a Strong Number or not. .
Test Data :
Input a number to check whether it is Strong number: 15
Expected Output :
15 is not a Strong number.
#include <stdio.h>
void main()
{
int i, n, n1, s1=0,j;
long fact;

printf("\n\n Check whether a number is Strong Number or not:\n ");


printf("----------------------------------------------------\n");
/* If sum of factorial of digits is equal to original number then it is Strong number */

printf("Input a number to check whether it is Strong number: ");


scanf("%d", &n);

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;

printf("\n\n Find Strong Numbers within an range of numbers:\n ");


printf("------------------------------------------------------\n");

/* If sum of factorial of digits is equal to original number then it is Strong number */

printf("Input starting range of number : ");


scanf("%d", &sn);
printf("Input ending range of number: ");
scanf("%d", &en);
printf("\n\nThe Strong numbers are :\n");

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)

printf("%d ", n1);


}
printf("\n\n");
}
.
49. Write a c program to find out the sum of an A.P. series. .
Test Data :
Input the starting number of the A.P. series: 1
Input the number of items for the A.P. series: 10
Input the common difference of A.P. series: 4
Expected Output :
The Sum of the A.P. series are :
1 + 5 + 9 + 13 + 17 + 21 + 25 + 29 + 33 + 37 = 190
#include <stdio.h>
#include <math.h>

void main(){

int n1,df,n2,i,ln;
int s1=0;

printf("\n\n Find out the sum of A.P. series :\n ");


printf("----------------------------------------\n");

printf("Input the starting number of the A.P. series: ");


scanf("%d",&n1);

printf("Input the number of items for the A.P. series: ");


scanf("%d",&n2);

printf("Input the common difference of A.P. series: ");


scanf("%d",&df);

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;

printf("\n\nConvert Decimal to Octal:\n ");


printf("-------------------------\n");

printf("Enter a number to convert : ");


scanf("%d",&n);

dn=n;
i=1;

for(j=n;j>0;j=j/8)
{
ocno=ocno+(j % 8)*i;
i=i*10;
n=n/8;
}

printf("\nThe Octal of %d is %d.\n\n",dn,ocno);


}

.
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;

printf("\n\nConvert Octal to Decimal:\n ");


printf("-------------------------\n");

printf("Input an octal number (using digit 0 - 7) :");


scanf("%d",&n1);
n5=n1;

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;

printf("\n\n Find the Sum of GP series.:\n ");


printf("-------------------------\n");

printf("Input the first number of the G.P. series: ");


scanf("%f",&g1);
printf("Input the number or terms in the G.P. series: ");
scanf("%f",&ntrm);

printf("Input the common ratio of G.P. series: ");


scanf("%f",&cr);

/*-------- generate G.P. series ---------------*/


printf("\nThe numbers for the G.P. series:\n ");
printf("%f ",g1);
sum=g1;

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;

printf("\n\nConvert Binary to Octal:\n ");


printf("-------------------------\n");

printf("Input a binary number :");


scanf("%d",&n);
n1=n;
for (j=n;j>0;j=j/10)
{
d = j % 10;
if(i==1)
p=p*1;
else
p=p*2;

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;
}

printf("\nThe Binary Number : %d\nThe equivalent Octal Number : %d \n\n",n1,ocno);


}
.
54. Write a program in C to convert an octal number into binary. .
Test Data :
Input an octal number (using digit 0 - 7) :57
Expected Output :
The Octal Number : 57
The equivalent Binary Number : 101111
#include <stdio.h>
#include <math.h>

void main()
{

long int n1, n5,p=1;


long int dec=0,i=1,j,d;
long int binno=0;

printf("\n\nConvert Octal to Binary:\n ");


printf("-------------------------\n");

printf("Input an octal number (using digit 0 - 7) :");


scanf("%ld",&n1);
n5=n1;

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;
}

printf("\nThe Octal Number : %ld\nThe equivalent Binary Number : %ld \n\n",n5,binno);

}
.
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;

printf("\n\nConvert Decimal to Hexadecimal:\n ");


printf("-------------------------\n");

printf("Input any Decimal number: ");


scanf("%ld",&decn);
q = decn;
for(l=q;l>0;l=l/16)
{
tmp = l % 16;
if( tmp < 10)
tmp =tmp + 48; else
tmp = tmp + 55;
dn=dn*100+tmp;
}
printf("\nThe equivalent Hexadecimal Number : ");
for(m=dn;m>0;m=m/100)
{
s=m % 100;
printf("%c",s);
}
printf("\n\n");
}
.
56. Write a program in C to Check Whether a Number can be Express as Sum of Two Prime
Numbers. .
Test Data :
Input a positive integer: 16
Expected Output :
16 = 3 + 13
16 = 5 + 11
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int num,i,j,temp1,temp2,ctr=0;
printf("input the number:\n");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
temp1=i;
temp2=num-i;
for(j=2;j<=i/2;j++){
if(i%j==0){ctr++;break;}
}
if(ctr==0){
for(j=2;j<=(num-i)/2;j++){
if((num-i)%j==0){ctr++;break;}
}
if(ctr==0) printf("%d can be written as %d + %d.\n ",num,i,num-i);
}
ctr=0;
}
return 0;
}
.
57. Write a program in C to print a string in reverse order. .
Test Data :
Input a string to reverse : Welcome
Expected Output :
Reversed string is: emocleW
#include <stdio.h>
#include <string.h>

void main()
{
char str1[100], tmp;
int l, lind, rind,i;

printf("\n\nPrint a string in reverse order:\n ");


printf("-------------------------------------\n");

printf("Input a string to reverse : ");


scanf("%s", str1);
l = strlen(str1);

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;

printf("\n\nFind the length of a string:\n ");


printf("-------------------------------------\n");

printf("Input a string : ");


scanf("%s", str1);

for (i = 0; str1[i] != '\0'; i++)


{
l++;
}
printf("The string contains %d number of characters. \n",l);
printf("So, the length of the string %s is : %d\n\n", str1, l);
}
.
59. Write a program in C to check Armstrong number of n digits. .
Test Data :
Input an integer : 1634
Expected Output :
1634 is an Armstrong number
#include <stdio.h>
#include <math.h>

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");

printf(" Input an integer : ");


scanf("%d", &n1);

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;
}
.

You might also like