Question 1:
Positive or Negative number
Write a C program to find Number is a positive or Negative
The following concept will test weather a number is positive or negative. It is done by
checking where the number lies on the number line. The following algorithm will help to
check this condition.
If the input number is greater than zero then it is a positive number.
If the input number is less than zero it is a negative number.
If the number is zero then it is neither positive nor negative.
Same logic we have followed in the below C program.
#include<stdio.h>
int main()
{
int num;
printf(“Insert a number: “);
scanf(“%d”, &num);
//Condition to check if the number is negative or positive
if (num <= 0)
{
if (num == 0)
printf(“The number is 0.”);
else
printf(“The number is negative”);
}
else
printf(“The number is positive”);
return 0;
}
Question 2:
Number is Even or Odd
We can determine whether a number is even or odd. This can be tested using different
methods. The test can be done using simple methods such as testing the number’s
divisibility by 2. If the remainder is zero, the number is even. If the remainder is not zero,
then the number is odd. The following algorithm describes how a C program can test if a
number is even or odd.
Example :
Number is 24
It is an even number because it is exactly divisible by 2
Number is 15
It is odd number because it is not divisible by 2
#include<stdio.h>
int main()
{
int number;
printf(“Insert a number \n“);
scanf(“%d”,&number);
//Checking if the number is divisible by 2
if (number%2 == 0)
printf(“The number is even\n“);
else
printf(“The number is odd\n“);
return 0;
}
Question 3:
Find the Sum of first N Natural Numbers
A Natural number is a same as Counting [Link] are used to Count the numbers of
real physical object. Natural number is start from 1 and go on infinite. The positive
numbers 1, 2, 3… are known as natural numbers.
Example:
Natural number={1,2,,4,5,6,…….}.
Formula for Sum of First N natural numbers is : n(n+1)/2.
If you want to add first 5 Natural number then we find the Sum of 1+2+3+4+5 =15.
#include<stdio.h>
int main()
{
int sum = 0, n;
printf(“Enter the first N Natural Number\n“);
scanf(“%d”,&n);
sum=(n*(n+1))/2;
printf(“sum is %d”,sum);
return 0;
}
Question 4:
Find Sum of N Natural Numbers
In the C programming language, the user is allowed to insert any integer value. With the
help of For loop, this C program can calculate the sum of N natural numbers. Within this
program, first printf statement will request the user to insert a number or value then the
scanf statement will allocate the user inserted value to integer variable. The sum is
calculated in the For loop.
To perform the arithmetic operation of addition of n numbers we use this conditions
Example –
Enter Number 3
N natural numbers 1,2,3,4,5,6,7,8…….
Where first 3 number is 1,2,3
Then we will return sum of number = 6
/* C Program to find Sum of N Numbers using For Loop */
#include<stdio.h>
int main()
{
//for initialize variable
int Number, i, Sum = 0;
//to take user input
printf (“\n Kindly Insert an Integer Variable\n“);
scanf (“%d”, &Number);
//use for loop for these condition
for(i = 1; i <= Number; i++)
{
Sum = Sum + i;
}
//display
printf (“Sum of Natural Numbers = %d”, Sum);
return 0;
}
Question 5:
Find Sum of Numbers in a Given Range
The program given below accepts a range of values and calculates their sum. The program
uses a loop to calculate the sum of the values provided by the user. The following section
presents an algorithm followed by a C program to calculate this sum.
Example:-
Enter first and last range 4 and 8.
To use for loop start at 4 and end 8 and sum of inside the [Link] this range.
Answer is 30(i.e 4+5+6+7+8=30).
#include <stdio.h>
int main()
{
//for initialization of variable
int firstrange,lastrange, i=0, total= 0;
//to use user input first range & last range
printf(“Enter the value first range and last range\n“);
scanf(“%d\n%d”,&firstrange, &lastrange);
//use for loop for total [Link] the range
for(i = firstrange; i <= lastrange; i++){
//total+=i;
total = total + i;
}
//print the sum of number
printf(“Sum of number firstrang %d to lastrange %d is: %d”,firstrange, lastrange, total);
}
Question 6:
Find Greatest of Two Numbers
In C programming language, the greatest of numbers can be identified with the help of IF-
ELSE statement. The user is asked to insert two integers. The numbers inserted are then
calculated using a set of program to get the correct output. It will find the highest number
among them using IF-ELSE Statement and start checking which one is larger to display the
largest number.
Example – If the given numbers are 12 and 9 then greater number is 12
12, 9= 12>9
#include<stdio.h>
int main()
{
int no1, no2;
printf(“Insert two numbers:”);
scanf(“%d %d”,&no1, &no2);
//Condition to check which of the two number is greater
//it will compare of number where number 1 is greater
if(no1 > no2)
printf(“%d is greatest”,no1);
//where number 2 is greater
else if(no2 > no1)
printf(“%d is greatest”,no2);
//for both are equal
else
printf(“%d and %d are equal”, no1, no2);
return 0;
}
Question 7:
Find Greatest of Three Numbers
The C program to find the greatest of three numbers requires the user to insert three
integers. Flow chart is also used in C programming to find the greatest number among
three integers inserted by the user.
A simple if-else block is used to identify the greatest number.
#include<stdio.h>
int main()
{
int no1,no2,no3;
//Prompt user to insert any three integer variables
printf(“\nInsert value of no1, no2 and no3:”);
scanf(“%d %d %d”, &no1, &no2, &no3);
//for check of number 1 is greater
if((no1 > no2) && (no1 > no3))
printf(“\n Number1 is greatest”);
//weather number 2 is grater
else if((no2 > no3) && (no2 > no1))
printf(“\n Number2 is greatest”);
//other conditions are false than number 3 is greater
else
printf(“\n Number3 is greatest”);
return 0;
}
Question 8
Year is a Leap Year or Not
In this program we have to find the year is a leap year or not. Generally we assume that
year is exactly divisible by 4 is a leap year. But it is not only in this case 1900 is divisible by
4. But it is not a leap so it that case we follows these conditions
It is exactly divisible by 100
If it is divisible by 100, then it should also exactly divisible by 4
And it is divisible by 400
These all conditions are true year is a leap year.
#include<stdio.h>
int main()
{
//initialization of Year
int year;
//to take user input
printf(“Enter Year for find leap year or not : “);
scanf(“%d”,&year);
//we use this statement for check leap year
if(((year%4==0)&&(year%100!=0)) || (year%400==0))
printf(“%d is a Leap Year”,year);
//not leap year
else
printf(“%d is not a Leap Year”,year);
return 0;
Question 9:
Check Whether a Number is Prime Number or Not.
A number is considered as prime number when it satisfies the below conditions.
Prime number is a number which can be divided by 1 and itself
A number which can not be divided by any other number other than 1 or itself is a prime
number.
It should have only 2 factors. They are, 1 and the number itself.
#include<stdio.h>
int main()
{
//initializing variables
int c,number,div=0;
//user input
printf(“Enter number: “);
scanf(“%d”,&number);
//checking for number of divisor
for(c=1;c<=number;c++)
{
if(number%c==0)
{
div++;
}
}
//no divisors other than 1 and itself
if(div==2)
{
//display
printf(“%d is a prime number”,number);
}
else
{
//display
printf(“%d is not a prime number”,number);
}
return 0;
}
Question 10:
Prime Numbers in a Given Range
A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).
The C program reduces the number of iteration within the for loop. It is made to identify
or calculate the prime numbers within a given range of numbers inserted by the user.
Ex:- if user enter a range as 40-50
In that range 41,43,47 these three number are prime number.
#include<stdio.h>
#include<stdlib.h>
void main()
{
//To initialize variables
int num1, num2, i, j, flag, temp, count = 0;
//for taking user input
printf(“Insert the value of num1 and num2 \n“);
scanf(“%d %d”, &num1, &num2);
//check condition first range is less than 2
if (num2 < 2)
{
printf(“No prime nums found up-to %d\n“, num2);
exit(0);
}
//to display prime numbers
printf(“Prime nums are \n“);
temp = num1;
//if num1 modules 2 is equal to zero
if( num1 % 2 == 0)
{
//increment on that number.
num1++;
}
//use for loop with first rang and second rang
for (i = num1; i <= num2; i = i + 2)
{
flag = 0;
for (j = 2; j <= i / 2; j++)
{
if ((i % j) == 0)
{
flag = 1;
break;
}
}
//check if flag equal to zero
if (flag == 0)
{
//display
printf(“%d\n“, i);
count++;
}
}
//display total prime number b/w lie on given range
printf(“Num of primes between %d & %d = %d\n“, temp, num2, count);
}
Question 11:
Write a C program to find the Factorial of a number
In this program we will find the factorial of a number where the number should be
entered by the user. Factorial is sequence of a number whose multiply by all previous
number.
Ex:- No is 5.
5x4x3x2x1=120
Factorial of a 5=120
Note:-Factorial of n number is 1*2*3*…n. You will learn to calculate the factorial of a
number using for loop in this example.
#include <stdio.h>
int main()
{
//initialize of variable
int i, number, fact = 1;
//to take user input.
printf("Enter a number to calculate its factorial\n");
scanf("%d", &number);
//use this loop of following statement
for (i = 1; i<= number;i++)
fact = fact * i;
//display of factorial of a given number
printf("Factorial of a number %d is = %d\n", number, fact);
return 0;
}
Question 12:
C Program to Find Sum of Digits of a Number .
This program in C programming calculates the sum of number inserted by the user or in an
inserted integer. The program is taken as an input and stored in the variable number,
denoted as no. Initially, the sum of the variable is zero, and then it is divided by 10 to
obtain the result or output.
In this C program to allow the user enter any number and then it will divide the number
into individual digits and adding those individuals (Sum=sum+digit) digits using While
Loop.
Ex:- number is 231456
2+3+1+4+5+6=21
sum of digit of a given number is 21
/* C program to take a number & calculate the sum of its numbers */
#include<stdio.h>
int main()
{
int no, temp, digit, sum = 0;
printf ("Insert a number \n");
scanf ("%d", &no);
temp = no;
while (no > 0)
{
digit = no % 10;
sum = sum + digit;
no /= 10;
}
printf("Given number = %d\n", temp);
printf("Sum of the numbers %d = %d\n", temp, sum);
return 0;
}
Question 13:
Write a C program reverse a given number.
In this program reverses a number entered by a user and then print it. For example, if a
user will enter 6577756 as input then 6577756 will be printed as output.
This C program accepts an integer and reverse it.
#include<stdio.h>
int main()
{
//Initialization of variables where rev='reverse=0'
int number, rev = 0,store, left;
//input a numbers for user
printf("Enter the number\n");
scanf("%d", &number);
store= number;
//use this loop for check true condition
while (number > 0)
{
//left is for remider are left
left= number%10;
//for reverse of no.
rev = rev * 10 + left;
//number /= 10;
number=number/10;
}
//To show the user value
printf("Given number = %d\n",store);
//after reverse show numbers
printf("Its reverse is = %d\n", rev);
return 0;
}
Question 14:
Write a C program to find number is Palindrome or not
A palindrome number is a number that is given the same number after reverse. In C
programs to check if the input number is palindrome or not. We are using while loop and
else if statement in C Program.
Ex:-
A number is 123321 .If you read number “123321” from reverse order, it is same as
“123321”.
In that number is a palindrome.
A number is 12121. If we read number “12121” from reverse order ,it is same as 12121. It
is also a palindrome number
#include<stdio.h>
int main()
{
//Initialization of variables where rev='reverse=0'
int number, rev = 0,store, n1,left;
//input a numbers for user
printf("Enter the number\n");
scanf("%d", &number);
//for duplicacy of number
n1=number;
store= number;
//use this loop for check true condition
while (number > 0)
{
//left is for remider are left
left= number%10;
//for reverse of no.
rev = rev * 10 + left;
//number /= 10;
number=number/10;
}
//To check reverse no is a Palindrome
if(n1==rev)
printf("Number %d is Palindrome number",n1);
else
printf("it is not a Palindrome number");
return 0;
}
Question 15:
Write a program to find number is Armstrong or not.
In this program we will find the number is Armstrong or not where the number should be
entered by the user. Basically the sum of cube of its digits is equal to the number itself is
called Armstrong number.
Ex:- Enter any number 153.
1**3 + 5**3 + 3**3 = 153
Number is Armstrong
#include<stdio.h>
int main()
{
int num ,n,n1,c=0,mul=1,sum=0,r,f,i;
printf("enter any num: \n");
scanf("%d",&num);
n=num;
n1=num;
while(n!=0)
{
r=n%10;
c++;
n=n/10;
}
while (num!=0)
{
f=num%10;
mul=1;
for(i=1;i<=c;i++)
{
mul=mul*f;
}
sum=sum+mul;
num=num/10;
}
if(n1==sum)
printf("Armstrong Number");
else
printf("Not an Armstrong Number");
return 0;
}
Question 16:
Armstrong numbers between two intervals:-
To identify the Armstrong number between two intervals in C programming, the user is
required to insert integer numbers. A n digit number is known as an Armstrong number,
when the sum of the values of the digits raised to nth power is equal to the number itself.
For example: 153 = 13+53+33=153
Ex:- basically we know that Armstrong number in given range 0 to 999 are 1 2 3 4 5 6 7 8 9
153 370 371 407.
#include<stdio.h>
int main()
{
//For initializing variables
int start, end, i, temp1, temp2, rem, n = 0, result = 0;
//user give start and end point of a number
printf("Insert the start value and end value :");
scanf("%d %d", &start, &end);
//to display pint of range
printf("\n Armstrong nums between %d an %d are: ", start, end);
//for use this loop to store all number in given range
for(i = start + 1; i < end; ++i)
{
//store a duplicity value of given range
temp2 = i;
temp1 = i;
while (temp1 != 0)
{
//temp1 /= 10;
temp1=temp1/10;
++n;
}
while (temp2 != 0)
{
rem = temp2 % 10;
//result += pow(rem, n);
result=result+pow(rem,n);
//temp2 /= 10;
temp2=temp2/10;
}
//check true condition if result is equal to i
if (result == i)
{
//display
printf("%d ", i);
}
n = 0;
result = 0;
}
printf("\n");
return 0;
}
Question 17:
Write a C program to find Fibonacci series up to n
The sequence is a Fibonacci series where the next number is the sum of the previous two
numbers. The first two terms of the Fibonacci sequence is started from 0,1,…
Example: limit is Fibonacci series 8
Sequence is 0,1,1,2,3,5,8,13
Its followed on addition operation. Next number is the addition of before the first two
numbers.
#include<stdio.h>
int main()
{
//To initialize variables
int n1=0,n2=1,n3,limit,i;
//To take user input
printf("enter a limit of series \n");
scanf( "%d",&limit);
printf("Fibonacci series %d %d ",n1,n2);
//To use this loop for given length
for(i=2;i<limit;i++)
{
//n1 and n2 sum store in new variable n3
n3=n1+n2;
n1=n2;
n2=n3;
//display serious
printf("%d ",n3);
}
return 0;
}
Question 18:
Write a C program to find Power of a number.
In this program we will calculate the power of a number using C programming. We want
to calculate the power of any given number so you need to multiply that particular
number power of time.
Ex:-
1. Let suppose number is 24 so we need to multiply with 4 times of 2. That is
2*2*2*2=16.
2. Number is 53 so we need to multiply with 3 time of 5. That is 5*5*5=125
#include<stdio.h>
int main()
{
//To initialize variables
int number, expo,temp = 1;
//To take user input
printf("Enter a base number: ");
scanf("%d", &number);
//To display Exponent
printf("Enter an exponent: ");
scanf("%d", &expo);
//use while loop when power is not equal to zero
while (expo != 0)
{
//temp*=number
temp = temp * number;
--expo;
}
printf("power of a %d is %d",number, temp);
return 0;
}
Question 19:
Write a C program to find factors of a number.
In this Program we will calculate the factors of any numbers using C programming.
The factors of a number are defined as the number we multiply two numbers and get the
original number. The factor of a number is a real number which divides
the original completely with zero remainder.
Ex- no is 16,5.
16= 2 x 2 x 2 x 2
5= 1 x 5
#include<stdio.h>
int main()
{
//To initialize variable
int number, u;
//to take user input
printf("Enter an any number: ");
scanf("%d",&number);
printf("Factors of a number %d are: ", number);
//Use for loop this condition
for(u=1; u<= number; u++)
{
//now we check for true condition of this
if (number%u == 0)
//display factor
printf("%d ",u);
}
return 0;
}
Question 20:
Write a C program to find number is Strong number or not.
In this program we will find number is strong number of not using C programming. Where
the number should enter by a user. We will use the While Loop ,for loop and else if
statement in this program. In that program we use user define function for find factorial
of number t + 5!=1 + 24 +120=145
hat will use on find strong number.
Basically A strong number is a number whose sum of factorials of digits is equal to the
same number.
Ex:- number is 145
1! + 4!
So it is a strong number.
#include<stdio.h>
//find factorial of a number.
int factorial(int number)
{
//to initialize of factorial
int i,fact=1;
//use for loop with this condition
for(i=1;i<=number;i++)
{
//fact*=1;
fact=fact*i;
}
return fact;
}
//to main function
int main()
{
//to initialize variables
int number,digit,sum=0,temp;
//To take user input
printf("Enter a number:");
scanf("%d",&number);
//To store a duplicity value of a given number
temp=number;
//use this whenever number is not equal to 0
while(temp!=0)
{
//for last digit
digit=temp%10;
//now we call of factorial function
digit = factorial(digit);
//to improve of a sum on digit
sum=sum+digit;
temp=temp/10;
}
//we check sum is equal to number its true
if(sum==number)
{
//display
printf("It is a Strong Number");
}
//false condition
else
{
//display
printf("It is not Strong Number");
}
return 0;
}
Question 21:
Write a C program to find number is Perfect number or not.
In this program we will find number is a perfect number or not using C programming. so
we will use of while loop and if else statement. Basically perfect number is a positive
number which is equal to the sum of all its divisors excluding itself. we have to find all
divisors of that number and find their sum, if sum of divisors is equal to number it means
number is Perfect Number. Else sum is not equal to number it mean number is not a
perfect number.
Ex:- Enter any number 6
6 is a perfect number as 1 + 2 + 3 = 6.
Number is 15
15 is not a perfect number because 1+3+5=9
#include<stdio.h>
int main()
{
// Initialization of variables
int number,i=1,total=0;
// To take user input
printf("Enter a number: ");
scanf("%d",&number);
while(i<number)
{
if(number%i==0)
{
total=total+i;
i++;
}
}
//to condition is true
if(total==number)
//display
printf("%d is a perfect number",number);
//to condition is false
else
//display
printf("%d is not a perfect number",number);
return 0;
}
Question 22:
Write a C program to find number is Automorphic number or not.
In this program we have to find the number is Automorphic number or not using C
programming. Basically automorphic number is a number whose square ends with the
same digits as number itself.
Automorphic Number in C Programming
Example:
5=(5)2=25
6=(6)2=36
25=(25)2=625
76=(76)2=5776
376=(376)2=141376
These numbers are automorphic number.
#include<stdio.h>
int checkAutomorphic(int num)
{
int square = num * num;
while (num > 0)
{
if (num % 10 != square % 10)
return 0;
// Reduce N and square
num = num / 10;
square = square / 10;
}
return 1;
}
int main()
{
//enter value
int num;
scanf("%d",&num);
//checking condition
if(checkAutomorphic(num))
printf("Automorphic");
else
printf("Not Automorphic");
return 0;
}
Question 23:
Write a C Program to find number is Harshad number or not.
In this program we will discuss of number is harshad number or not in C programming. In
mathematics, a Harshad number is a number that is divisible by the sum of its digits. We
use while loop statement with following condition. Input consists of 1 integer.
Ex– Number is 21
it is divisible by own sum (1+2) of its digit(2,1)
So it is harshad number
Some other harshad number are 156,54,120 etc.
#include<stdio.h>
int main()
{
//To initialize of variable
int number,temp,sum = 0, digit, res;
//To take user input
printf("enter any number : ");
scanf("%d",&number);
//store in temporary variable
temp = number;
//use while loop with this condition
while(temp!=0)
{
//to find last digit
digit=temp % 10;
//sum+=digit
sum = sum + digit;
//temp/=10
temp = temp / 10;
}
res = number % sum;
//check result is equal is to 0
if(res == 0)
//display
printf("%d is Harshad Number",number);
else
//display
printf("%d is not Harshad Number",number);
return 0;
}
Question 24:
Write a C program to find number is Abundant number or not.
In this program to find number is Abundant number or not. A number n is said to be
Abundant Number to follow these condition
the sum of its proper divisors is greater than the number itself.
And the difference between these two values is called the abundance.
Ex:- Abundant number 12 having a proper divisor is 1,2,3,4,6 the sum of these factor is 16
it is greater than 12 so it is a Abundant number.
Some other abundant numbers
18, 20, 24, 30, 36, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100, 102, 104, 108, 112, 114, 120..
#include<stdio.h>
int main()
{
//initialization variables
int number,sum=0,c;
//input from user
printf("Enter a number : ");
scanf("%d",&number);
//declare a variable to store sum of factors of the number
for(c = 1 ; c < number ; c++)
{
if(number % c == 0)
//sum+=c;
sum = sum + c;
}
if(sum > number)
//display the result
printf("Abundant Number");
else
//display
printf("Not an Abundant Number");
return 0;
}
Question 25:
Write a C program to find number is Friendly Pair or Not
Two numbers are said to be friendly pairs if they have common abundancy index. Or, the
ratio between the sum of divisors of a number and the number itself. These numbers are
also known as Amicable numbers.
We can also say that two numbers n and m are friendly numbers if
?(n)/n = ?(m)/m
Where ?(n) is the sum of divisors of n.
For instance, for numbers 6 and 28,
Divisors of 6 are- 1, 2, 3, and 6.
Divisors of 28 are- 1, 2, 4, 7, 14, and 28.
Sum of the divisors of 6 and 28 are 12 and 56 respectively.
Also, the abundant index of 6 and 28 is 2.
Therefore, 6 and 28 is a friendly pair.
#include<stdio.h>
int main()
{
//1 Create two variables to use in first and second numbers
int i;
int f_Num,s_Num;
//2 two more variables created to store the sum of the divisors
int f_DivisorSum = 0;
int s_DivisorSum = 0;
//3 Asking user to enter the two numbers
printf("Enter two numbers to check if Amicable or not : ");
scanf("%d %d",&f_Num,&s_Num);
//4 Using one variable for loop and second to check for each number
for(int i=1;i<f_Num;i++)
{
//5 Condition check
if(f_Num % i == 0)
f_DivisorSum = f_DivisorSum + i;
}
//6 Calculating the sum of all divisors
for(int i=1;i<s_Num;i++)
{
if(s_Num % i == 0)
s_DivisorSum = s_DivisorSum + i;
}
//7 Check condition for friendly numbers
if((f_Num == s_DivisorSum) && (s_Num == f_DivisorSum))
else
{
printf("%d and %d are not Amicable numbers\n",f_Num,s_Num);
}
return 0;
}