Question 1: Write a program in C to display the first 10 natural numbers.
Solution:
#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");
}
You can check the solution:- Check here
Question 2: Write a C program to find the sum of first 10 natural numbers.
Solution:
#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);
}
You can check the solution:- Check here.
Question 3: Write a program in C to display n terms of natural number and their sum.
Solution:
#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);
}
You can check the solution:- Check here.
Question 4: Write a program in C to read 10 numbers from the keyboard and find their sum and average.
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);
}
You can check the solution:- Check here.
Question 5: Write a program in C to display the cube of the number upto given an integer.
Solution:
#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));
}
}
You can check the solution:- Check here.
Question 6: Write a program in C to display the multiplication table of a given integer.
Solution:
#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);
}
}
You can check the solution:- Check here.
Question 7:Write a program in C to display the multipliaction table vertically from 1 to n.
Solution:
#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");
}
}
You can check the solution:- Check here.
Question 8:Write a program in C to display the n terms of odd natural number and their sum .
Solution:
#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);
}
You can check the solution:- Check here.
Question 9:Write a C program to calculate the factorial of a given number.
Solution:
#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);
}
You can check the solution:- Check here.
Question 10:Write a program in C to display the n terms of even natural number and their sum.
Solution:
#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);
}
You can check the solution:- Check here.
Question 11:Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........].
Solution:
#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);
}
You can check the solution:- Check here.
Question 12: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
Solution:
#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);
}
You can check the solution:- Check here.
Question 13:Write a program in C to display the sum of the series [ 9 + 99 + 999 + 9999 ...].
Solution:
#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);
}
You can check the solution:- Check here.
Question 14:Write a program in C to display the sum of the series [ 1+x+x^2/2!+x^3/3!+....].
Solution:
#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);
}
You can check the solution:- Check here.
Question 15:Write a program in C to find the sum of the series [ x - x^3 + x^5 + ......].
Solution:
#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);
}
You can check the solution:- Check here.
Question 16:Write a program in C to display the n terms of square natural number and their sum.
Solution:
#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);
}
You can check the solution:- Check here.
Question 17:Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n terms.
Solution:
#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);
}
You can check the solution:- Check here.
Question 18: Write a c program to check whether a given number is a perfect number or not.
Solution:
#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");
}
You can check the solution:- Check here.
Question 19: Write a program in C to check Armstrong number of n digits.
Solution:
#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 can check the solution:- Check here.
Question 20: Write a C program to find the length of a string without using the library function.
Solution:
#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);
}
You can check the solution:- Check here.
Question 21: Write a program in C to print a string in reverse order.
Solution:
#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);
}
You can check the solution:- Check here.
Question 22: Write a program in C to find the prime numbers within a range of numbers.
Solution:
#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");
}
You can check the solution:- Check here.
Question 23: Write a program in C to convert a decimal number into binary without using an array.
Solution:
#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;
}
You can check the solution:- Check here.
Question 24: Write a program in C to convert a binary number into a decimal number without using an array, function, and while loop.
Solution:
#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);
}
You can check the solution:- Check here.
Question 25: Write a program in C to convert a binary number into a decimal number using math function.
Solution:
#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);
}
You can check the solution:- Check here.
Question 26: Write a C program to find Strong Numbers within a range of numbers.
Solution:
#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 the sum of the 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");
}
You can check the solution:- Check here.
Question 27: Write a program in C to convert a decimal number into octal without using an array.
Solution:
#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);
}
You can check the solution:- Check here.
Question 28: Write a program in C to convert an octal number to a decimal without using an array.
Solution:
#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;
}
}
You can check the solution:- Check here.
Question 29: Write a program in C to convert a binary number to octal.
Solution:
#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);
}
You can check the solution:- Check here.
Question 30: Write a program in C to convert an octal number into binary.
Solution:
#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);
}
You can check the solution:- Check here.
0 Comments
If you are a good learner please leave a challenging question on comment box for us when you visited this website ( Coding with Fun).