C_Programs
C_Programs
#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;
}
void main()
{
int 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
/*This program will print the sum of all natural numbers from 1
to N.*/
#include<stdio.h>
int main()
{
int i,N,sum;
return 0;
}
#include<stdio.h>
int main()
{
int i,N;
unsigned long 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;
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.
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
return 0;
}
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
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.
int i, n;
return 0;
}
Factorial of a Number
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
return 0;
}
Run Code
Output
Enter an integer: 10
Factorial of 10 = 3628800
C
// C program to convert binary to decimal
#include <stdio.h>
int binaryToDecimal(int n)
int num = n;
int dec_value = 0;
int base = 1;
while (temp) {
base = base * 2;
return dec_value;
int main()
printf("%d", binaryToDecimal(num));
Output:
169
b)
#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
#include <stdio.h>
#include <math.h>
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;
}
while (n!=0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
}
Run Code
Output
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
Program
#include<stdio.h>
#include<math.h>
void main()
{
int a, b, c;
float disc, root1, root2, real, imag;
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
#include<conio.h>
void sum(int n)
int rem,s=0;
while(n > 0)
rem=n%10;
s=s+rem;
n=n/10;
}
void reverse(int n)
int rem,rev=0;
while(n > 0)
rem = n % 10;
n =n/10;
void palindrome(int n)
int original,rem,rev=0;
original=n;
while (n > 0) {
rem = n % 10;
n =n/10;
if(original==rev)
int main() {
int n;
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()
lar = n;
sm=n;
scanf ("%d",&n);
if (n>lar)
lar=n;
if (n<sm)
sm=n;
}
printf ("n The largest number is %d", lar);
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