C program for given two numbers to perform arithmetic operations using switch statement.
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n
4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
return 0;
}
C Program to find biggest of three numbers using nested If
Else statement
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter three numbers\n");
scanf("%d %d %d", &a, &b, &c);
if(a > b)
{
if(a > c)
printf("a: %d is largest\n", a);
else
printf("b: %d is largest\n", b);
}
else if(b > c)
printf("b: %d is largest\n", b);
else
printf("c: %d is largest\n", c);
}
Output 1
Enter three numbers
89
12
65
a: 89 is largest
Output 2
Enter three numbers
45
68
34
b: 68 is largest
Output 3
Enter three numbers
12
38
73
c: 73 is largest
1) C program to find sum of all natural numbers.
Series: 1+2+3+4+..N
/*This program will print the sum of all natural numbers from 1
to N.*/
#include<stdio.h>
int main()
{
int i,N,sum;
/*read value of N*/
printf("Enter the value of N: ");
scanf("%d",&N);
/*set sum by 0*/
sum=0;
/*calculate sum of the series*/
for(i=1;i<=N;i++)
sum= sum+ i;
/*print the sum*/
printf("Sum of the series is: %d\n",sum);
return 0;
}
/*This program will print the sum of the square
of all natural numbers from 1 to N.*/
#include<stdio.h>
int main()
{
int i,N;
unsigned long sum;
/*read value of N*/
printf("Enter the value of N: ");
scanf("%d",&N);
/*set sum by 0*/
sum=0;
/*calculate sum of the series*/
for(i=1;i<=N;i++)
sum= sum+ (i*i);
/*print the sum*/
printf("Sum of the series is: %ld\n",sum);
return 0;
}
/*
C program to find sum of following series
1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
*/
#include<stdio.h>
int main()
{
int i,N;
float sum;
/*read value of N*/
printf("Enter the value of N: ");
scanf("%d",&N);
/*set sum by 0*/
sum=0.0f;
/*calculate sum of the series*/
for(i=1;i<=N;i++)
sum = sum + ((float)1/(float)i);
/*print the sum*/
printf("Sum of the series is: %f\n",sum);
return 0;
}
A prime number is a positive integer that is divisible only by 1 and itself. For
example: 2, 3, 5, 7, 11, 13, 17.
Program to Check Prime Number
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
C Program To Find Prime Numbers From 2 To N,
using For Loop
1. #include<stdio.h>
2. #include<math.h>
3.
4. int main()
5. {
6. int num, count, limit, prime, inum;
7.
8. printf("Enter the limit\n");
9. scanf("%d", &limit);
10.
11. printf("Prime Numbers from 2 To %d are\n", limit);
12.
13. for(num = 2; num <= limit; num++)
14. {
15. prime = 1;
16. inum = sqrt(num);
17. for(count = 2; count <= inum; count++)
18. {
19. if(num % count == 0)
20. {
21. prime = 0;
22. break;
23. }
24. }
25.
26. if(prime)
27. printf("%d\n", num);
28. }
29.
30. return 0;
31.}
Output 1:
Enter the limit
25
Prime Numbers from 2 To 25 are
2
3
5
7
C Program to Display Fibonacci
Sequence
In this example, you will learn to display the Fibonacci sequence of first n
numbers (entered by the user).
The Fibonacci sequence is a sequence where the next term is the sum of
the previous two terms. The first two terms of the Fibonacci sequence are 0
followed by 1.
The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
Visit this page to learn about the Fibonacci sequence.
Fibonacci Series up to n terms
#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
C Program to Find Factorial of a
Number
In this example, you will learn to calculate the factorial of a number entered
by the user.
The factorial of a positive number n is given by:
factorial of n (n!) = 1 * 2 * 3 * 4....n
The factorial of a negative number doesn't exist. And, the factorial of 0 is 1.
Factorial of a Number
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
Run Code
Output
Enter an integer: 10
Factorial of 10 = 3628800
Binary Number To Decimal Number
Try It!
a)
The idea is to extract the digits of a given binary number starting from the
rightmost digit and keep a variable dec_value. At the time of extracting digits
from the binary number, multiply the digit with the proper base (Power of 2)
and add it to the variable dec_value. In the end, the variable dec_value will
store the required decimal number.
For Example:
If the binary number is 111.
dec_value = 1*(2^2) + 1*(2^1) + 1*(2^0) = 7
The below diagram explains how to convert ( 1010 ) to equivalent decimal
value:
Below is the implementation of the above idea :
C
// C program to convert binary to decimal
#include <stdio.h>
// Function to convert binary to decimal
int binaryToDecimal(int n)
int num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
return dec_value;
// Driver program to test above function
int main()
int num = 10101001;
printf("%d", binaryToDecimal(num));
Output:
169
b)
/ convert binary to decimal
#include <stdio.h>
#include <math.h>
// function prototype
int convert(long long);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
Run Code
Output
Enter a binary number: 1101
1101 in binary = 13 in decimal
Example 2: C Program to convert decimal number to binary
// convert decimal to binary
#include <stdio.h>
#include <math.h>
long long convert(int);
int main() {
int n, bin;
printf("Enter a decimal number: ");
scanf("%d", &n);
bin = convert(n);
printf("%d in decimal = %lld in binary", n, bin);
return 0;
}
long long convert(int n) {
long long bin = 0;
int rem, i = 1;
while (n!=0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
}
Run Code
Output
Enter a decimal number: 13
13 in decimal = 1101 in binary
Suppose n = 13 . Let's see how the while loop in the convert() function
works.
n != 0 rem = n % 2 n /= 2 i bin += rem * i i * = 10
13 != 0 13 % 2 = 1 13 / 2 = 6 1 0+1*1=1 1 * 10 = 10
6 != 0 6%2=0 6/2=3 10 1 + 0 * 10 = 1 10 * 10 = 100
3 != 0 3%2=1 3/2=1 100 1 + 1 * 100 = 101 100 * 10 = 10
1 != 0 1%2=1 1/2=0 1000 101 + 1 * 1000 = 1101 1000 * 10 = 1
0 != 0 - - - Loop termina
Thus, 13 in decimal is 1101 in binary.
12 .C Program to find roots of a quadratic equation using If
Else statement
Program
#include<stdio.h>
#include<math.h>
void main()
{
int a, b, c;
float disc, root1, root2, real, imag;
printf("Enter the values of a, b and c\n");
scanf("%d %d %d", &a, &b, &c);
disc = ((b*b)-(4*a*c));
if(disc > 0)
{
root1 = (-b + sqrt(disc))/(2*a);
root2 = (-b - sqrt(disc))/(2*a);
printf("Roots are real and complex\n");
printf("Root 1:\t%f\n", root1);
printf("Root 2:\t%f\n", root2);
}
else if(disc == 0)
{
root1 = root2 = -b/(2*a);
printf("Roots are equal\n");
printf("Root:\t%f\n", root1);
}
else
{
real = -b/(2*a);
imag = sqrt(-disc)/(2*a);
printf("Roots are imaginary\n");
printf("Root 1:\t%f + i%f\n", real, imag);
printf("Root 2:\t%f - i%f\n", real, imag);
}
}
Output 1
Enter the values of a, b and c
1
2
1
Roots are equal
Root: -1.000000
Output 2
Enter the values of a, b and c
6
8
2
Roots are real and complex
Root 1: -0.333333
Root 2: -1.000000
Output 3
Enter the values of a, b and c
6
3
3
Roots are imaginary
Root 1: 0.000000 + i0.661438
Root 2: 0.000000 - i0.661438
Example C program to find the Sum
Reverse and check for Palindrome
of a number using functions.
#include<stdio.h>
#include<conio.h>
void sum(int n)
int rem,s=0;
while(n > 0)
rem=n%10;
s=s+rem;
n=n/10;
}
printf("\nSum of digits = %d",s);
void reverse(int n)
int rem,rev=0;
while(n > 0)
rem = n % 10;
rev = rev * 10 + rem;
n =n/10;
printf("\nReversed number is = %d", rev);
void palindrome(int n)
int original,rem,rev=0;
original=n;
while (n > 0) {
rem = n % 10;
rev = rev * 10 + rem;
n =n/10;
if(original==rev)
printf("\nThe number is palindrome");
else
printf("\nThe number is not palindrome");
int main() {
int n;
printf("Enter an integer number: ");
scanf("%d", &n);
sum(n);
reverse(n);
palindrome(n);
getch();
return 0;
Output 1:
Enter an integer number: 1234
Sum of digits = 10
Reversed number is = 4321
The number is not palindrome
Output 2:
Enter an integer number: 23432
Sum of digits = 14
Reversed number is = 23432
The number is palindrome
Source code to find largest and
smallest number
#include<stdio.h>
int main()
int i, n, lar,sm, elem;
printf ("Enter total number of elements n");
scanf ("%d", &elem);
printf ("Enter first number n");
scanf ("%d", &n);
lar = n;
sm=n;
for (i=1; i<= elem -1 ; i++)
printf ("n Enter another number n");
scanf ("%d",&n);
if (n>lar)
lar=n;
if (n<sm)
sm=n;
}
printf ("n The largest number is %d", lar);
printf ("n The smallest number is %d", sm);
return 0;
lgorithm
Given below is an algorithm to find the second largest and the second
smallest numbers in an array −
Step 1 − Declare and read the number of elements.
Step 2 − Declare and read the array size at runtime.
Step 3 − Input the array elements.
Step 4 − Arrange numbers in descending order.
Step 5 − Then, find the second largest and second smallest numbers by
using an index.
Step 6 − Print the second largest and the second smallest numbers.
Program
Given below is the C program to find the second largest and the
second smallest numbers in an array −
#include<stdio.h>
void main(){
int i,j,a,n,counter,ave,number[30];
printf ("Enter the value of N
");
scanf ("%d", &n);
printf ("Enter the numbers
");
for (i=0; i<n; ++i)
scanf ("%d",&number[i]);
for (i=0; i<n; ++i){
for (j=i+1; j<n; ++j){
if (number[i] < number[j]){
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf ("The numbers arranged in descending order are given below
");
for (i=0; i<n; ++i)
printf ("%10d
",number[i]);
printf ("The 2nd largest number is = %d
", number[1]);
printf ("The 2nd smallest number is = %d
", number[n-2]);
ave = (number[1] +number[n-2])/2;
counter = 0;
for (i=0; i<n; ++i){
if (ave==number[i])
++counter;
}
if (counter==0)
printf("The average of 2nd largest & 2nd smallest is not in the array
");
else
printf("The average of 2nd largest & 2nd smallest in array is %d in
numbers
", counter);
}
Output
When the above program is executed, it produces the following result −
Enter the value of N
5
Enter the numbers
10
12
17
45
80
The numbers arranged in descending order are given below
80
45
17
12
10
The 2nd largest number is = 45
The 2nd smallest number is = 12
The average of 2nd largest & 2nd smallest is not in the array
Bhanu Priya
Updated on 01-Sep-2021 13:00:26