0% found this document useful (0 votes)
8 views132 pages

C Program

The document is a comprehensive collection of C programming examples and FAQs aimed at helping individuals prepare for technical interviews. It covers a wide range of topics including basic programs, recursion, number systems, string manipulation, file handling, and more, with specific examples provided for each topic. Additionally, it includes definitions and sample outputs for key concepts like perfect numbers and Armstrong numbers.

Uploaded by

bmshanjo
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)
8 views132 pages

C Program

The document is a comprehensive collection of C programming examples and FAQs aimed at helping individuals prepare for technical interviews. It covers a wide range of topics including basic programs, recursion, number systems, string manipulation, file handling, and more, with specific examples provided for each topic. Additionally, it includes definitions and sample outputs for key concepts like perfect numbers and Armstrong numbers.

Uploaded by

bmshanjo
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

C program

It is C programming FAQ code to Crack Interview. It has C language basic and simple source code.
This program contains a big collection of c program. If you have any doubt in the following c program
examples you are free to ask.

Frequently asked c programs in interview


1. Write a c program to check given number is perfect number or not.
2. Write a c program to check given number is Armstrong number or not.
3. Write a c program to check given number is prime number or not.
4. Write a c program to check given number is strong number or not.
5. C program to check a number is odd or even.
6. Write a c program to check given number is palindrome number or not.
8. Write a c program to check given string is palindrome number or not.
7. Write a c program to solve quadratic equation.
8. Write a c program to print Fibonacci series of given range.
9. Write a c program to get factorial of given number.
10. Write a c program for Floyd’s triangle.
11. Write a c program to print Pascal triangle.
12. Write a c program to generate multiplication table.
13. Write a c program to print ASCII value of all characters.
14. C program to print hello world without using semicolon
15. Write a c program which produces its own source code as its output

C program with numbers


1. Write a c program to reverse any number.
2. Write a c program to find out sum of digit of given number.
3. Write a c program to find out power of number.
4. Write a c program to add two numbers without using addition operator.
5. Write a c program to subtract two numbers without using subtraction operator.
6. Write a c program to find largest among three numbers using binary minus operator.
7. Write a c program to find largest among three numbers using conditional operator
8. Write a c program to find out generic root of any number.
9. Write a c program to find out prime factor of given number.
10. Write a c program to find out NCR factor of given number.
11. How to convert string to int without using library functions in c
12. Program in c to print 1 to 100 without using loop
13. C program for swapping of two numbers
14. Program to find largest of n numbers in c
15. Split number into digits in c programming
16. C program to count number of digits in a number

Recursion
Example of recursion in c programming

L.C.M and H.C.F.


1. Write a c program to find out L.C.M. of two numbers.
2. Write a c program to find out H.C.F. of two numbers.
3. Write a c program to find out G.C.D. of two numbers.

Swapping
1. Write a c program to swap two numbers.
2. Write a c program to swap two numbers without using third variable.
3. Write a c program for swapping of two arrays.
4. Write a c program for swapping of two string.
Conversion ( Number System )
1. Write a c program to convert decimal number to binary number.
2. Write a c program to convert decimal number to octal number.
3. Write a c program to convert decimal number to hexadecimal number.
4. Write a c program to convert octal number to binary number.
5. Write a c program to convert octal number to decimal number.
6. Write a c program to convert octal number to hexadecimal number.
7. Write a c program to convert hexadecimal number to binary number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.
11. Write a c program to convert binary number to decimal number.
12. Write a c program to convert binary number to hexadecimal number.
13. C program for addition of binary numbers .
14. C program for multiplication of two binary numbers.
15. C program fractional binary conversion from decimal.
16. C program for fractional decimal to binary fraction conversion.
17. C program to convert decimal number to roman.
18. C program to convert roman number to decimal number.
19. C program to convert each digits of a number in words
20. C program to convert currency or number in word.

Conversion ( Unit )
1. C program for unit conversion.

String
1. Write a c program to convert the string from upper case to lower case.
2. Write a c program to convert the string from lower case to upper case.
3. Write a c program to delete the all consonants from given string.
4. Write a c program to count the different types of characters in given string.
5. Write a c program to sort the characters of a string.
6. Write a c program for concatenation two strings without using string.h header file.
7. Write a c program to find the length of a string using pointer.
8. Write a c program which prints initial of any name.
9. Write a c program to print the string from given character.
10. Write a c program to reverse a string
11. Reverse a string using recursion in c
12. String concatenation in c without using strcat
13. How to compare two strings in c without using strcmp
14. String copy without using strcpy in c
15. Convert a string to ASCII in c

Matrix
1. Write a c program for addition of two matrices.
2. Write a c program for subtraction of two matrices
3. Write a c program for multiplication of two matrices.
4. Write a c program to find out sum of diagonal element of a matrix.
5. Write a c program to find out transport of a matrix.
6. Write a c program for scalar multiplication of matrix.
7. C program to find inverse of a matrix
8. Lower triangular matrix in c
9. Upper triangular matrix in c
10. Strassen's matrix multiplication program in c
11. C program to find determinant of a matrix
File
1. Write a c program to open a file and write some text and close its.
2. Write a c program to delete a file.
3. Write a c program to copy a file from one location to other location.
4. Write a c program to copy a data of file to other file.
5. Write a c program which display source code as a output.
6. Write a c program which writes string in the file.
7. Write a c program which reads string from file.
8. Write a c program which writes array in the file.
9. Write a c program which concatenate two file and write it third file.
10. Write a c program to find out size of any file.
11. Write a c program to know type of file.
12. Write a c program to know permission of any file.
13. Write a c program to know last date of modification of any file.
14. Write a c program to find size and drive of any file.

Complex number
1. Complex numbers program in c
2. Write a c program for addition and subtraction of two complex numbers.
3. Write a c program for multiplication of two complex numbers.
4. Write a c program for division two complex numbers.

Series
1. Write a c program to find out the sum of series 1 + 2 + …. + n.
2. Write a c program to find out the sum of series 1^2 + 2^2 + …. + n^2.
3. Write a c program to find out the sum of series 1^3 + 2^3 + …. + n^3.
4. Write a c program to find out the sum of given A.P.
5. Write a c program to find out the sum of given G.P.
6. Write a c program to find out the sum of given H.P.
7. Write a c program to find out the sum of series 1 + 2 + 4 + 8 … to infinity.

Array
1. Write a c program to find out largest element of an array.
2. Write a c program to find out second largest element of an unsorted array.
3. Write a c program to find out second smallest element of an unsorted array.
4. Write a c program which deletes the duplicate element of an array.
5. Write a c program for delete an element at desired position in an array.
6. Write a c program for insert an element at desired position in an array.
7. C program to find largest and smallest number in an array

Sorting
1. Write a c program for bubble sort.
2. Write a c program for insertion sort.
3. Write a c program for selection sort.
4. Write a c program for quick sort.
5. Write a c program for heap sort.
6. Write a c program for merge sort.
7. Write a c program for shell sort.

Recursion
1. Write a c program to find factorial of a number using recursion.
2. Write a c program to find GCD of a two numbers using recursion.
3. Write a c program to find out sum digits of a number using recursion.
4. Write a c program to find power of a number using function recursion.
5. Write a c program to reverse any number using recursion.
Size of data type
1. Write a c program to find the size of int without using sizeof operator.
2. Write a c program to find the size of double without using sizeof operator.
3. Write a c program to find the size of structure without using sizeof operator.
4. Write a c program to find the size of union without using sizeof operator.

Using pointer
1. Write a c program for concatenation two string using pointer.

Searching
1. Write a c program for linear search.
2. Write a c program for binary search.
3. Write a c program for binary search using recursion.

Area and volume


1. Write a c program to find the area of circle.
2. Write a c program to find the area of any triangle.
3. Write a c program to find the area of equilateral triangle.
4. Write a c program to find the area of right angled triangle.
5. Write a c program to find the area of rectangle.
6. Write a c program to find the area of trapezium.
7. Write a c program to find the area of rhombus.
8. Write a c program to find the area of parallelogram.
9. Write a c program to find the volume and surface area of cube.
10. Write a c program to find the volume and surface area of cuboids.
11. Write a c program to find the volume and surface area of cylinder.
12. Write a c program to find the surface area and volume of a cone.
13. Write a c program to find the volume and surface area of sphere.
14. Write a c program to find the perimeter of a circle, rectangle and triangle.

C program with very large numbers


1. Write a c program to find factorial of 100 or very large numbers
2. Write a c program to multiply the two very large number (larger the long int)
3. Write a c program for division of large number (larger than long int)
4. C code for modular division of large number.
5. C code for division of large number.
6. C code for power of large numbers.

Other
1. C program for ATM transaction.
2. Write a c program which passes one dimension array to function.
3. Write a c program which passes two dimension array to function.
4. Write a c program which takes password from user.
5. Write a scanf function in c which accept sentence from user.
6. Write a scanf function in c which accept paragraph from user.
7. Write a c program to print the all prime numbers between 1 to 300.
8. Write a c program which passes structure to function.
9. Palindrome in c without using string function
10. How to get the ASCII value of a character in c
11. C program to get last two digits of year
12. C program without main function
1. C program to check perfect number
Definition of perfect number or What is perfect number?
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
Note: 6 is the smallest perfect number.
Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128

#include<stdio.h>
int main(){
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}

Sample output:
Enter a number: 6
6 is a perfect number

2. C program to find perfect numbers (or) C perfect number code (or) Perfect number program in c
language
#include<stdio.h>
int main(){
int n,i,sum;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Perfect numbers in given range is: ");
for(n=min;n<=max;n++){
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}

if(sum==n)
printf("%d ",n);
}
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Perfect numbers in given range is: 6

3. C program to print perfect numbers from 1 to 100


#include<stdio.h>
int main(){
int n,i,sum;
printf("Perfect numbers are: ");
for(n=1;n<=100;n++){
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;
}

Output:
Perfect numbers are: 6 28

4. Check the given number is armstrong number or not using c program (or) Warp to check a
number is Armstrong (or) C program to check whether a number is Armstrong or not (or) Simple c
program for Armstrong number
Definition according to c programming point of view:
THOSE NUMBERS WHICH SUM OF THE CUBE OF ITS DIGITS IS EQUAL TO THAT
NUMBER ARE KNOWN AS ARMSTRONG NUMBERS. FOR EXAMPLE 153 SINCE 1^3 + 5^3 + 3^3
= 1+ 125 + 9 =153 OTHER ARMSTRONG NUMBERS: 370,371,407 ETC.
IN GENERAL DEFINITION:
THOSE NUMBERS WHICH SUM OF ITS DIGITS TO POWER OF NUMBER OF ITS DIGITS IS
EQUAL TO THAT NUMBER ARE KNOWN AS ARMSTRONG NUMBERS.
EXAMPLE 1: 153
TOTAL DIGITS IN 153 IS 3
AND 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Example 2: 1634
Total digits in 1634 is 4
And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634
Examples of Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748,
92727, 93084, 548834, 1741725

#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}

Sample output:
Enter a number: 153
153 is an Armstrong number

5. Write a c program for Armstrong number (or) C program for Armstrong number generation (or)
How to find Armstrong number in c (or) Code for Armstrong number in c
#include<stdio.h>
int main(){
int num,r,sum,temp;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Armstrong numbers in given range are: ");
for(num=min;num<=max;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);
}
return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 200
Armstrong numbers in given range are: 1 153

6. Armstrong number in c using for loop


#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter 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",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}

Sample output:
Enter a number: 370
370 is an Armstrong number
Logic of Armstrong number in c

7. C program to print Armstrong numbers from 1 to 500 (or) C program for finding Armstrong
numbers.
#include<stdio.h>
int main(){
int num,r,sum,temp;
for(num=1;num<=500;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);
}
return 0;
}

Output:
1 153 370 371 407

8. C program to determine prime number (or) Determining if a number is prime in c (or) C program
to find given number is prime or not.

Check given number is prime number or not using c program


Definition of prime number:
A natural number greater than one has not any other divisors except 1 and itself. In other word we can say
which has only two divisors 1 and number itself. For example: 5
Their divisors are 1 and 5.
Note: 2 is only even prime number.
Logic for prime number in c
We will take a loop and divide number from 2 to number/2. If the number is not divisible by any of the
numbers then we will print it as prime number.

Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193,
197, 199 etc.

#include<stdio.h>
int main(){
int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}

Sample output:
Enter a number: 5
5 is a prime number

9. C program for prime numbers between 1 to 100. (or) How to find prime numbers from 1 to 100 in c
(or) How to print prime numbers from 1 to 100 in c
#include<stdio.h>
int main(){
int num,i,count;
for(num = 1;num<=100;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
return 0;
}
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

10. C program for prime numbers between 1 to n. (or) C program to find prime numbers up to n (or)
C program to list prime numbers. (or) Write a c program to generate n prime numbers. (or) C
program to find n prime numbers
#include<stdio.h>
int main(){
int num,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);
for(num = 1;num<=n;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
return 0;
}

Sample output:
Enter max range: 50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

11. C program to find prime numbers using while loop. (or) Wap to find prime numbers in c. (or)
Write a c program to generate prime number (or) How to get prime numbers in c
#include<stdio.h>
int main(){
int num,i,count,min,max;
printf("Enter min range: ");
scanf("%d",&min);
printf("Enter max range: ");
scanf("%d",&max);
num = min;
while(num<=max){
count = 0;
i=2;
while(i<=num/2){
if(num%i==0){
count++;
break;
}
i++;
}
if(count==0 && num!= 1)
printf("%d ",num);
num++;
}
return 0;
}
Sample output:
Enter min range: 50
Enter max range: 100
53 59 61 67 71 73 79 83 89 97

12. How to find out prime numbers in c programming (or) Display prime numbers in c (or) C
program to find prime numbers between two numbers (or) C code to display prime numbers within a
range
#include<stdio.h>
int main(){
int num,i,count,min,max;
printf("Enter min range: ");
scanf("%d",&min);
printf("Enter max range: ");
scanf("%d",&max);
for(num = min;num<=max;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
return 0;
}

Sample output:
Enter min range: 10
Enter max range: 50
11 13 17 19 23 29 31 37 41 43 47

13. Sum of prime numbers from 1 to 100 in c


#include<stdio.h>
int main(){
int num,i,count,sum=0;
for(num = 1;num<=100;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
sum = sum + num;
}
printf("Sum of prime numbers is: %d ",sum);
return 0;
}

Output:
Sum of prime numbers is: 1060

14. C program to find sum of prime numbers


#include<stdio.h>
int main(){
int num,i,count,min,max,sum=0;
printf("Enter min range: ");
scanf("%d",&min);
printf("Enter max range: ");
scanf("%d",&max);
for(num = min;num<=max;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
sum = sum + num;
}
printf("Sum of prime numbers is: %d ",sum);
return 0;
}

Sample output:
Enter min range: 50
Enter max range: 100
Sum of prime numbers is: 732

15. Write a c program to check whether a number is strong or not


Definition of strong number:
A number is called strong number if sum of the factorial of its digit is equal to number itself. For example:
145 since 1! + 4! + 5! = 1 + 24 + 120 = 145
#include<stdio.h>
int main(){
int num,i,f,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num){
i=1,f=1;
r=num%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
return 0;
}
Sample output:
Enter a number: 145
145 is a strong number

16. C program for strong number (or) Strong number program in c


#include<stdio.h>
int main(){
int num,i,f,r,sum,temp;
int min,max;
printf("Enter minimum range: ");
scanf("%d",&min);
printf("Enter maximum range: ");
scanf("%d",&max);
printf("Strong numbers in given range are: ");
for(num=min; num <= max; num++){
temp = num;
sum=0;
while(temp){
i=1;
f=1;
r=temp%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
temp=temp/10;
}
if(sum==num)
printf("%d ",num);
}
return 0;
}

Sample output:
Enter minimum range: 100
Enter maximum range: 100000
Strong numbers in given range are: 145 40585

17. C program to check even or odd. (or) C determine odd or even (or) How to check odd number in c.
(or) How to determine odd or even in c. (or) C even odd test.
#include<stdio.h>
int main(){
int number;
printf("Enter any integer: ");
scanf("%d",&number);
if(number % 2 ==0)
printf("%d is even number.",number);
else
printf("%d is odd number.",number);
return 0; }
Sample output:
Enter any integer: 5
5 is odd number.

18. Display odd numbers in c (or) How to print odd numbers in c


#include<stdio.h>
int main(){
int number;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Odd numbers in given range are: ");
for(number = min;number <= max; number++)
if(number % 2 !=0)
printf("%d ",number);
return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17 19

19. Even and odd numbers program in c. (or) C program to find even or odd
#include<stdio.h>
int main(){
int number;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Odd numbers in given range are: ");
for(number = min;number <= max; number++)
if(number % 2 !=0)
printf("%d ",number);
printf("\nEven numbers in given range are: ");
for(number = min;number <= max; number++)
if(number % 2 ==0)
printf("%d ",number);
return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Odd numbers in given ranges are: 1 3 5 7 9 11 13 15 17 19
Even numbers in given ranges are: 2 4 6 8 10 12 14 16 18 20

20. Sum of odd numbers in c


#include<stdio.h>
int main(){
int number;
int min,max;
long sum =0;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
for(number = min;number <= max; number++)
if(number % 2 !=0)
sum = sum + number;
printf("Sum of odd numbers in given range is: %ld",sum);
return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 100
Sum of odd numbers in given range is: 2500

21. Sum of odd and even numbers c program


#include<stdio.h>
int main(){
int number;
int min,max;
long odd_sum =0,even_sum = 0;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
for(number = min;number <= max; number++)
if(number % 2 != 0)
odd_sum = odd_sum + number;
else
even_sum = even_sum + number;
printf("Sum of even numbers in given range is: %ld\n",even_sum);
printf("Sum of odd numbers in given range is: %ld",odd_sum);
return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 10
Sum of even numbers in given range is: 30
Sum of odd numbers in given range is: 25

22. Check the given number is palindrome number or not using c program. (or) Wap to check a
number is palindrome. (or) C program to find whether a number is palindrome or not.

Definition of Palindrome number or What is palindrome number?


A number is called palindrome number if it is remain same when its digits are reversed. For example
121 is palindrome number. When we will reverse its digit it will remain same number i.e. 121
Palindrome numbers examples: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121,
131, 141, 151, 161, 171, 181, 191 etc.

#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num){
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}

Sample output:
Enter a number: 131
131 is a palindrome

23. Write a c program for palindrome. (or) C program to find palindrome of a number. (or)
Palindrome number in c language
#include<stdio.h>
int main(){
int num,r,sum,temp;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Palindrome numbers in given range are: ");
for(num=min;num<=max;num++){
temp=num;
sum=0;
while(temp){
r=temp%10;
temp=temp/10;
sum=sum*10+r;
}
if(num==sum)
printf("%d ",num);
}
return 0;}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 50
Palindrome numbers in given range are: 1 2 3 4 5 6 7 8 9 11 22 33 44

24. How to check if a number is a palindrome using for loop


#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
for(temp=num;num!=0;num=num/10){
r=num%10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}

Sample output:
Enter a number: 1221
1221 is a palindrome

25. C program to check if a number is palindrome using recursion


#include<stdio.h>
int checkPalindrome(int);
int main(){
int num,sum;
printf("Enter a number: ");
scanf("%d",&num);
sum = checkPalindrome(num);
if(num==sum)
printf("%d is a palindrome",num);
else
printf("%d is not a palindrome",num);
return 0;
}
int checkPalindrome(int num){
static int sum=0,r;
if(num!=0){
r=num%10;
sum=sum*10+r;
checkPalindrome(num/10);
} return sum;
}
Sample output:
Enter a number: 25
25 is not a palindrome

26. Write a c program to check given string is palindrome number or not


Definition of Palindrome string:
A string is called palindrome if it symmetric. In other word a string is called palindrome if string remains
same if its characters are reversed. For example: asdsa
If we will reverse it will remain same i.e. asdsa
Example of string palindrome: a,b, aa,aba,qwertrewq etc

#include<string.h>
#include<stdio.h>
int main(){
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrome");
else
printf("\nThe string is a palindrome");
return 0;
}

27. C program for solving quadratic equation. (or) C program to calculate roots of a quadratic
equation. (or) Quadratic equation in c language.
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float d,root1,root2;
printf("Enter a, b and c of quadratic equation: ");
scanf("%f%f%f",&a,&b,&c);
d = b * b - 4 * a * c;
if(d < 0){
printf("Roots are complex number.\n");
printf("Roots of quadratic equation are: ");
printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));
return 0;
}
else if(d==0){
printf("Both roots are equal.\n");
root1 = -b /(2* a);
printf("Root of quadratic equation is: %.3f ",root1);
return 0;
}
else{
printf("Roots are real numbers.\n");
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2);
}
return 0;
}

Sample output:
Enter a, b and c of quadratic equation: 2 4 1
Roots are real numbers.
Roots of quadratic equation are: -0.293, -1.707

28. How to find a b and c in a quadratic equation


#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float d,root1,root2;
printf("Enter quadratic equation in the format ax^2+bx+c: ");
scanf("%fx^2%fx%f",&a,&b,&c);
d = b * b - 4 * a * c;
if(d < 0){
printf("Roots are complex number.\n");
return 0;
}
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2);
return 0;
}

Sample output:
Enter quadratic equation in the format ax^2+bx+c: 2x^2+4x+-1
Roots of quadratic equation are: 0.000, -2.000

29. TO FIND FIBONACCI SERIES USING C PROGRAM.


Logic of Fibonacci series
Definition of Fibonacci numbers:
We assume first two Fibonacci are 0 and 1. A series of numbers in which each sequent number is
sum of its two previous numbers is known as Fibonacci series and each numbers are called Fibonacci
numbers. So Fibonacci numbers is
Algorithm for Fibonacci series
Fn = Fn-2 + Fn-1
Example of Fibonacci series:
0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 ...
5 is Fibonacci number since sum of its two previous number i.e. 2 and 3 is 5
8 is Fibonacci number since sum of its two previous number i.e. 3 and 5 is 8 and so on.

Write a program to generate the Fibonacci series in c (or) Write a program to print Fibonacci
series in c. (or) Basic c programs Fibonacci series. (or) How to print Fibonacci series in c. (or) How to
find Fibonacci series in c programming. (or) Fibonacci series in c using for loop

#include<stdio.h>
int main(){
int k,r;
long int i=0l,j=1,f;
//Taking maximum numbers form user
printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j); //printing firts two values.
for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
printf(" %ld",j);
}
return 0;
}

Sample output:
Enter the number range: 15
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

30. Fibonacci series using array in c. (or) Fibonacci series program in c language. (or) Source code of
Fibonacci series in c. (or) Wap to print Fibonacci series in c
#include<stdio.h>
int main(){
int i,range;
long int arr[40];
printf("Enter the number range: ");
scanf("%d",&range);
arr[0]=0;
arr[1]=1;
for(i=2;i<range;i++){
arr[i] = arr[i-1] + arr[i-2];
}
printf("Fibonacci series is: ");
for(i=0;i<range;i++)
printf("%ld ",arr[i]);
return 0;
}
Sample output:
Enter the number range: 20
Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

31. Fibonacci series in c using while loop. (or) C program to calculate Fibonacci series. (or) C program
to display Fibonacci series. (or) Fibonacci series in c with explanation. (or) C code to generate
Fibonacci series.
#include<stdio.h>
int main(){
int k=2,r;
long int i=0l,j=1,f;
printf("Enter the number range:");
scanf("%d",&r);
printf("Fibonacci series is: %ld %ld",i,j);
while(k<r){
f=i+j;
i=j;
j=f;
printf(" %ld",j);
k++;
}
return 0;
}

Sample output:
Enter the number range: 10
Fibonacci series is: 0 1 1 2 3 5 8 13 21 34

32. Sum of Fibonacci series in c


#include<stdio.h>
int main(){
int k,r;
long int i=0,j=1,f;
long int sum = 1;
printf("Enter the number range: ");
scanf("%d",&r);
for(k=2;k<r;k++){
f=i+j;
i=j;
j=f;
sum = sum + j;
}
printf("Sum of Fibonacci series is: %ld",sum);
return 0;
}

Sample output:
Enter the number range: 4
Sum of Fibonacci series is: 4

33. TO FIND FACTORIAL OF A NUMBER USING C PROGRAM.


Factorial value
Factorial of number is defined as:
Factorial (n) = 1*2*3 … * n
For example: Factorial of 5 = 1*2*3*4*5 = 120
Note: Factorial of zero = 1
C code for factorial of a number. (or) C program to find the factorial of a given number. (or)
Factorial program in c using while loop (or) Factorial program in c without using recursion
#include<stdio.h>
int main(){
int i=1,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
while(i<=num){
f=f*i;
i++;
}
printf("Factorial of %d is: %d",num,f);
return 0;
}
Sample output:
Enter a number: 5
Factorial of 5 is: 120

34. Factorial program in c using for loop. (or) Simple factorial program in c. (or) C program to
calculate factorial
#include<stdio.h>
int main(){
int i,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
f=f*i;
printf("Factorial of %d is: %d",num,f);
return 0;
}

35. Factorial program in c using pointers. (or) How to calculate factorial in c. (or) Factorial program
in c language
#include<stdio.h>
void findFactorial(int,int *);
int main(){
int i,factorial,num;
printf("Enter a number: ");
scanf("%d",&num);
findFactorial(num,&factorial);
printf("Factorial of %d is: %d",num,*factorial);
return 0;
}
void findFactorial(int num,int *factorial){
int i;
*factorial =1;
for(i=1;i<=num;i++)
*factorial=*factorial*i;
}

36. Factorial program in c using function. (or) C program to find factorial of a number
#include<stdio.h>
int findFactorial(int);
int main(){
int i,factorial,num;
printf("Enter a number: ");
scanf("%d",&num);
factorial = findFactorial(num);
printf("Factorial of %d is: %d",num,factorial);
return 0;
}
int findFactorial(int num){
int i,f=1;
for(i=1;i<=num;i++)
f=f*i;
return f;
}
Sample output:
Enter a number: 8
Factorial of 8 is: 40320

37. Factorial series in c


#include<stdio.h>
int main(){
long f=1;
int i,num,min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Factorial series in given range: ");
for(num=min;num<=max;num++){
f=1;
for(i=1;i<=num;i++)
f=f*i;
printf("%ld ",f);
}
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 10
Factorial series in given range: 1 2 6 24 120 720 5040 40320 362880 3628800

38. Write a c program to print Floyd’s triangle. (or) C program to display Floyd’s triangle. (or) How
to print Floyd’s triangle in c.
Definition of floyd's triangle:
Floyd's triangle is a right angled-triangle using the natural numbers. Examples of floyd's triangle:
Example 1:
1
23
456
7 8 9 10
#include<stdio.h>
int main(){
int i,j,r,k=1;
printf("Enter the range: ");
scanf("%d",&r);
printf("FLOYD'S TRIANGLE\n\n");
for(i=1;i<=r;i++){
for(j=1;j<=i;j++,k++)
printf(" %d",k);
printf("\n");
}
return 0;
}

Sample output:
Enter the range: 10
FLOYD'S TRIANGLE

1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55

39. Pascal triangle in c without using array. (or) C code to print Pascal triangle. (or) Simple c
program for Pascal triangle. (or) C program to generate Pascal triangle. (or) Pascal triangle program
in c language. C program to print Pascal triangle using for loop
#include<stdio.h>
long fact(int);
int main(){
int line,i,j;
printf("Enter the no. of lines: ");
scanf("%d",&line);
for(i=0;i<line;i++){
for(j=0;j<line-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
return 0;
}
long fact(int num){
long f=1;
int i=1;
while(i<=num){
f=f*i;
i++ }
return f;
}
Sample output:
Enter the no. of lines: 8
1
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1

40. TO FIND MULTIPLICATION TABLE USING C PROGRAM


Multiplication tables in c program. (or) Write a c program to print multiplication table. (or)
Code for multiplication table in c. (or) Multiplication table in c language. (or) Write a c program to
print multiplication table.
#include<stdio.h>
int main(){
int r,i,j,k;
printf("Enter the number range: ");
scanf("%d",&r);
for(i=1;i<=r;i++){
for(j=1;j<=10;j++)
printf("%d*%d=%d ",i,j,i*j);
printf("\n");
}
return 0;
}

Sample Output:
Enter the number range: 5
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 4*10=40
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50

41. Printing ascii value using c program. (or) C code for ASCII table. (or) C program to display
ASCII values
#include<stdio.h>
int main(){
int i;
for(i=0;i<=255;i++)
printf("ASCII value of character %c: %d\n",i,i);
return 0;
}

Output:
ASCII value of character : 0
ASCII value of character ☺: 1
ASCII value of character ☻: 2
ASCII value of character ♥: 3
ASCII value of character ♦: 4
ASCII value of character ♣: 5
ASCII value of character ♠: 6
ASCII value of character : 7
ASCII value of character: 8
ASCII value of character :
ASCII value of character : 10
ASCII value of character ♂: 11
ASCII value of character ♀: 12
: 13I value of character
ASCII value of character ♫: 14
ASCII value of character ☼: 15
ASCII value of character ►: 16
ASCII value of character ◄: 17
ASCII value of character ↕: 18
ASCII value of character ‼: 19
ASCII value of character ¶: 20
ASCII value of character §: 21
ASCII value of character ▬: 22
ASCII value of character ↨: 23
ASCII value of character ↑: 24
ASCII value of character ↓: 25
ASCII value of character →: 26
ASCII value of character ←: 27
ASCII value of character ∟: 28
ASCII value of character ↔: 29
ASCII value of character ▲: 30
ASCII value of character ▼: 31
ASCII value of character : 32
ASCII value of character !: 33
ASCII value of character ": 34
ASCII value of character #: 35
ASCII value of character $: 36
ASCII value of character %: 37
ASCII value of character &: 38
ASCII value of character ': 39
ASCII value of character (: 40
ASCII value of character ): 41
ASCII value of character *: 42
ASCII value of character +: 43
ASCII value of character ,: 44
ASCII value of character -: 45
ASCII value of character .: 46
ASCII value of character /: 47
ASCII value of character 0: 48
ASCII value of character 1: 49
ASCII value of character 2: 50
ASCII value of character 3: 51
ASCII value of character 4: 52
ASCII value of character 5: 53
ASCII value of character 6: 54
ASCII value of character 7: 55
ASCII value of character 8: 56
ASCII value of character 9: 57
ASCII value of character :: 58
ASCII value of character ;: 59
ASCII value of character <: 60
ASCII value of character =: 61
ASCII value of character >: 62
ASCII value of character ?: 63
ASCII value of character @: 64
ASCII value of character A: 65
ASCII value of character B: 66
ASCII value of character C: 67
ASCII value of character D: 68
ASCII value of character E: 69
ASCII value of character F: 70
ASCII value of character G: 71
ASCII value of character H: 72
ASCII value of character I: 73
ASCII value of character J: 74
ASCII value of character K: 75
ASCII value of character L: 76
ASCII value of character M: 77
ASCII value of character N: 78
ASCII value of character O: 79
ASCII value of character P: 80
ASCII value of character Q: 81
ASCII value of character R: 82
ASCII value of character S: 83
ASCII value of character T: 84
ASCII value of character U: 85
ASCII value of character V: 86
ASCII value of character W: 87
ASCII value of character X: 88
ASCII value of character Y: 89
ASCII value of character Z: 90
ASCII value of character [: 91
ASCII value of character \: 92
ASCII value of character ]: 93
ASCII value of character ^: 94
ASCII value of character _: 95
ASCII value of character `: 96
ASCII value of character a: 97
ASCII value of character b: 98
ASCII value of character c: 99
ASCII value of character d: 100
ASCII value of character e: 101
ASCII value of character f: 102
ASCII value of character g: 103
ASCII value of character h: 104
ASCII value of character i: 105
ASCII value of character j: 106
ASCII value of character k: 107
ASCII value of character l: 108
ASCII value of character m: 109
ASCII value of character n: 110
ASCII value of character o: 111
ASCII value of character p: 112
ASCII value of character q: 113
ASCII value of character r: 114
ASCII value of character s: 115
ASCII value of character t: 116
ASCII value of character u: 117
ASCII value of character v: 118
ASCII value of character w: 119
ASCII value of character x: 120
ASCII value of character y: 121
ASCII value of character z: 122
ASCII value of character {: 123
ASCII value of character |: 124
ASCII value of character }: 125
ASCII value of character ~: 126
ASCII value of character ⌂: 127
ASCII value of character Ç: 128
ASCII value of character ü: 129
ASCII value of character é: 130
ASCII value of character â: 131
ASCII value of character ä: 132
ASCII value of character à: 133
ASCII value of character å: 134
ASCII value of character ç: 135
ASCII value of character ê: 136
ASCII value of character ë: 137
ASCII value of character è: 138
ASCII value of character ï: 139
ASCII value of character î: 140
ASCII value of character ì: 141
ASCII value of character Ä: 142
ASCII value of character Å: 143
ASCII value of character É: 144
ASCII value of character æ: 145
ASCII value of character Æ: 146
ASCII value of character ô: 147
ASCII value of character ö: 148
ASCII value of character ò: 149
ASCII value of character û: 150
ASCII value of character ù: 151
ASCII value of character ÿ: 152
ASCII value of character Ö: 153
ASCII value of character Ü: 154
ASCII value of character ¢: 155
ASCII value of character £: 156
ASCII value of character ¥: 157
ASCII value of character ₧: 158
ASCII value of character ƒ: 159
ASCII value of character á: 160
ASCII value of character í: 161
ASCII value of character ó: 162
ASCII value of character ú: 163
ASCII value of character ñ: 164
ASCII value of character Ñ: 165
ASCII value of character ª: 166
ASCII value of character º: 167
ASCII value of character ¿: 168
ASCII value of character ⌐: 169
ASCII value of character ¬: 170
ASCII value of character ½: 171
ASCII value of character ¼: 172
ASCII value of character ¡: 173
ASCII value of character «: 174
ASCII value of character »: 175
ASCII value of character ░: 176
ASCII value of character ▒: 177
ASCII value of character ▓: 178
ASCII value of character │: 179
ASCII value of character ┤: 180
ASCII value of character ╡: 181
ASCII value of character ╢: 182
ASCII value of character ╖: 183
ASCII value of character ╕: 184
ASCII value of character ╣: 185
ASCII value of character ║: 186
ASCII value of character ╗: 187
ASCII value of character ╝: 188
ASCII value of character ╜: 189
ASCII value of character ╛: 190
ASCII value of character ┐: 191
ASCII value of character └: 192
ASCII value of character ┴: 193
ASCII value of character ┬: 194
ASCII value of character ├: 195
ASCII value of character ─: 196
ASCII value of character ┼: 197
ASCII value of character ╞: 198
ASCII value of character ╟: 199
ASCII value of character ╚: 200
ASCII value of character ╔: 201
ASCII value of character ╩: 202
ASCII value of character ╦: 203
ASCII value of character ╠: 204
ASCII value of character ═: 205
ASCII value of character ╬: 206
ASCII value of character ╧: 207
ASCII value of character ╨: 208
ASCII value of character ╤: 209
ASCII value of character ╥: 210
ASCII value of character ╙: 211
ASCII value of character ╘: 212
ASCII value of character ╒: 213
ASCII value of character ╓: 214
ASCII value of character ╫: 215
ASCII value of character ╪: 216
ASCII value of character ┘: 217
ASCII value of character ┌: 218
ASCII value of character █: 219
ASCII value of character ▄: 220
ASCII value of character ▌: 221
ASCII value of character ▐: 222
ASCII value of character ▀: 223
ASCII value of character α: 224
ASCII value of character ß: 225
ASCII value of character Γ: 226
ASCII value of character π: 227
ASCII value of character Σ: 228
ASCII value of character σ: 229
ASCII value of character µ: 230
ASCII value of character τ: 231
ASCII value of character Φ: 232
ASCII value of character Θ: 233
ASCII value of character Ω: 234
ASCII value of character δ: 235
ASCII value of character ∞: 236
ASCII value of character φ: 237
ASCII value of character ε: 238
ASCII value of character ∩: 239
ASCII value of character ≡: 240
ASCII value of character ±: 241
ASCII value of character ≥: 242
ASCII value of character ≤: 243
ASCII value of character ⌠: 244
ASCII value of character ⌡: 245
ASCII value of character ÷: 246
ASCII value of character ≈: 247
ASCII value of character °: 248
ASCII value of character ∙: 249
ASCII value of character ·: 250
ASCII value of character √: 251
ASCII value of character ⁿ: 252
ASCII value of character ²: 253
ASCII value of character ■: 254
ASCII value of character : 255

42. C program to print hello world without using semicolon


Solution: 1
#include<stdio.h>
void main(){
if(printf("Hello world")){
}
}

Solution: 2
#include<stdio.h>
void main(){
while(!printf("Hello world")){
}
}

Solution: 3
#include<stdio.h>
void main(){
switch(printf("Hello world")){
}
}

43. How do you write a program which produces its own source code as its output in c language?
#include<stdio.h>
int main(){
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do{
c= getc(fp);
putchar(c);
}
while(c!=EOF);

fclose(fp);
return 0;
}

Output:
#include<stdio.h>
int main(){
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do{
c= getc(fp);
putchar(c);
}
while(c!=EOF);
fclose(fp);
return 0;
}

44. Write a c program to reverse a given number. (or) C program to find reverse of a number (or) C
program to reverse the digits of a number. (or) Reverse of a number in c using while loop

#include<stdio.h>
int main(){
int num,r,reverse=0;
printf("Enter any number: ");
scanf("%d",&num);
while(num){
r=num%10;
reverse=reverse*10+r;
num=num/10;
}
printf("Reversed of number: %d",reverse);
return 0;
}
Sample output:
Enter any number: 12
Reversed of number: 21

45. Reverse very large or big numbers beyond the range of long int. (or) Reverse five digit number c
program

Logic is we accept the number as string


#include<stdio.h>
#define MAX 1000
int main(){
char num[MAX];
int i=0,j,flag=0;

printf("Enter any positive integer: ");


scanf("%s",num);

while(num[i]){
if(num[i] < 48 || num[i] > 57){
printf("Invalid integer number");
return 0;
}
i++;
}
printf("Reverse: ");
for(j=i-1;j>=0;j--)
if(flag==0 && num[j] ==48){
}
else{
printf("%c",num[j]);
flag =1;
}
return 0;
}

Sample output:
Enter any positive integer: 234561000045645679001237800000000000
Reverse: 8732100976546540000165432

46. C program to reverse a number using for loop. (or) How to find reverse of a number in c. (or)
Wap to reverse a number in c

#include<stdio.h>
int main(){
int num,r,reverse=0;
printf("Enter any number: ");
scanf("%d",&num);
for(;num!=0;num=num/10){
r=num%10;
reverse=reverse*10+r;
}
printf("Reversed of number: %d",reverse);
return 0;
}

Sample output:
Enter any number: 123
Reversed of number: 321

47. C program to reverse a number using recursion

#include<stdio.h>
int main(){
int num,reverse;
printf("Enter any number: ");
scanf("%d",&num);
reverse=rev(num);
printf("Reverse of number: %d",reverse);
return 0;
}
int rev(int num){
static sum,r;
if(num){
r=num%10;
sum=sum*10+r;
rev(num/10);
}
else
return 0;
return sum;
}

Sample output:
Enter any number: 456
Reverse of number: 654

48. C program to add digits of a number. (or) C program for sum of digits of a number. (or) C
program to calculate sum of digits

#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
while(num){
r=num%10;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}

Sample output:
Enter a number: 123
Sum of digits of number: 6

49. Sum of digits of a number in c using for loop

#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
for(;num!=0;num=num/10){
r=num%10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}

Sample output:
Enter a number: 567
Sum of digits of number: 18

50. Sum of digits in c using recursion


#include<stdio.h>
int getSum(int);
int main(){
int num,sum;
printf("Enter a number: ");
scanf("%d",&num);
sum = getSum(num);
printf("Sum of digits of number: %d",sum);
return 0;
}
int getSum(int num){
static int sum =0,r;
if(num!=0){
r=num%10;
sum=sum+r;
getSum(num/10);
}
return sum;
}

Sample output:
Enter a number: 45
Sum of digits of number: 9

51. FIND POWER OF A NUMBER USING C PROGRAM


#include<stdio.h>
int main(){
int pow,num,i=1;
long int sum=1;
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
while(i<=pow){
sum=sum*num;
i++;
}
printf("\n%d to the power %d is: %ld",num,pow,sum);
return 0;
}

52. Write a c program to add two numbers without using addition operator & Add two numbers in c
without using operator & How to add two numbers without using the plus operator in c.

#include<stdio.h>
int main(){
int a,b;
int sum;

printf("Enter any two integers: ");


scanf("%d%d",&a,&b);
//sum = a - (-b);
sum = a - ~b -1;
printf("Sum of two integers: %d",sum);
return 0;
}

Sample output:
Enter any two integers: 5 10
Sum of two integers: 15

53. Write a c program or code to subtract two numbers without using subtraction operator
In c ~ is 1's complement operator. This is equivalent to:
~a = -b + 1
So, a - ~b -1
= a-(-b + 1) + 1
=a+b–1+1
=a+b

#include<stdio.h>
int main(){
int a,b;
int sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
sum = a + ~b + 1;
printf("Difference of two integers: %d",sum);
return 0;
}

Sample Output:
Enter any two integers: 5 4
Difference of two integers: 1

54. Write a c program to find largest among three numbers using binary minus operator
#include<stdio.h>
int main(){
int a,b,c;
printf("\nEnter 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a-b>0 && a-c>0)
printf("\nGreatest is a :%d",a);
else
if(b-c>0)
printf("\nGreatest is b :%d",b);
else
printf("\nGreatest is c :%d",c);
return 0;
}

55. Write a c program to find largest among three numbers using conditional operator
#include<stdio.h>
int main(){
int a,b,c,big;
printf("\nEnter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf("\nThe biggest number is: %d",big);
return 0;
}

56. C program for generic root


Meaning of generic root:
It sum of digits of a number unit we don't get a single digit. For example:
Generic root of 456: 4 + 5 + 6 = 15 since 15 is two digit numbers so 1 + 5 = 6
So, generic root of 456 = 6

#include<stdio.h>
int main(){
long int num,sum,r;
printf("\nEnter a number:-");
scanf("%ld",&num);

while(num>10){
sum=0;
while(num){
r=num%10;
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
printf("\nSum of the digits in single digit is: %ld",sum);
return 0;
}

57. C code for calculation of generic root in one line


#include <stdio.h>
int main(){
int num,x;
printf("Enter any number: ");
scanf("%d",&num);
printf("Generic root: %d",(x=num%9)?x:9);
return 0;
}

Sample output:
Enter any number: 731
Generic root: 2

58. Write a c program to find out NCR factor of given number (Or) C program to find the ncr value
by using recursive function
In the mathematics nCr has defined as
n
Cr = n! /((n-r)!r!)

#include<stdio.h>
int main(){
int n,r,ncr;
printf("Enter any two numbers->");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("The NCR factor of %d and %d is %d",n,r,ncr);
return 0;
}
int fact(int n){
int i=1;
while(n!=0){
i=i*n;
n--;
}
return i;
}

59. How to convert string to int without using library functions in c programming language
#include<stdio.h>
int stringToInt(char[] );
int main(){
char str[10];
int intValue;
printf("Enter any integer as a string: ");
scanf("%s",str);
intValue = stringToInt(str);
printf("Equivalent integer value: %d",intValue);
return 0;
}

int stringToInt(char str[]){


int i=0,sum=0;
while(str[i]!='\0'){
if(str[i]< 48 || str[i] > 57){
printf("Unable to convert it into integer.\n");
return 0;
}
else{
sum = sum*10 + (str[i] - 48);
i++;
}
}
return sum;
}
Sample output:
Enter any integer as a string: 123
Equivalent integer value: 123

60. Program in c to print 1 to 100 without using loop


#include<stdio.h>
int main(){
int num = 1;
print(num);
return 0;
}
int print(num){
if(num<=100){
printf("%d ",num);
print(num+1);
}
}

Output:
Sample output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

61. C program for swapping of two numbers (or) Code for swapping in c
#include<stdio.h>
int main(){
int a,b,temp;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping: a = %d, b=%d",a,b);
temp = a;
a = b;
b = temp;
printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}

62. C program for swapping of two numbers using pointers


#include<stdio.h>
int main(){
int a,b;
int *ptra,*ptrb;
int *temp;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping: a = %d, b=%d",a,b);
ptra = &a;
ptrb = &b;
temp = ptra;
*ptra = *ptrb;
*ptrb = *temp;
printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}

Sample output:
Enter any two integers: 5 10
Before swapping: a = 5, b=10
After swapping: a = 10, b=10

63. Swapping program in c using function


#include<stdio.h>
void swap(int *,int *);
int main(){
int a,b;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping: a = %d, b=%d",a,b);
swap(&a,&b);
printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}
void swap(int *a,int *b){
int *temp;
temp = a;
*a=*b;
*b=*temp;
}

Sample output:
Enter any two integers: 3 6
Before swapping: a = 3, b=6
After swapping: a = 6, b=6

64. Program to find largest of n numbers in c (or) Simple program of c find the largest number.
#include<stdio.h>
int main(){
int n,num,i;
int big;
printf("Enter the values of n: ");
scanf("%d",&n);
printf("Number %d",1);
scanf("%d",&big);
for(i=2;i<=n;i++){
printf("Number %d: ",i);
scanf("%d",&num);
if(big<num)
big=num;
}
printf("Largest number is: %d",big);
return 0;
}
Sample Output:
Enter the values of n:
Number 1: 12
Number 2: 32
Number 3: 35
Largest number is: 35

65. Split number into digits in c programming (or) Extract digits from integer in c language.
#include<stdio.h>
int main(){
int num,temp,factor=1;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(temp){
temp=temp/10;
factor = factor*10;
}
printf("Each digits of given number are: ");
while(factor>1){
factor = factor/10;
printf("%d ",num/factor);
num = num % factor;
}
return 0;
}

Sample output:
Enter a number: 123
Each digits of given number are: 1 2 3

66. Count the number of digits in c programming language


#include<stdio.h>
int main(){
int num,count=0;
printf("Enter a number: ");
scanf("%d",&num);
while(num){
num=num/10;
count++;
}
printf("Total digits is: %d",count);
return 0;
}
Sample output:
Enter a number: 23
Total digits is: 2

67. C code to count the total number of digit using for loop
#include<stdio.h>
int main(){
int num,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(;num!=0;num=num/10)
count++;
printf("Total digits is: %d",count);
return 0;
}
Sample output:
Enter a number: 456
Total digits is: 3
68. Count the digits of a given number in c language using recursion
#include<stdio.h>
int countDigits(num);
int main(){
int num,count;
printf("Enter a number: ");
scanf("%d",&num);
count = countDigits(num);
printf("Total digits is: %d",count);
return 0;
}
int countDigits(int num){
static int count=0;
if(num!=0){
count++;
countDigits(num/10);
}
return count;
}

Sample output:
Enter a number: 1234567
Total digits is: 7

69. Write a c program to find out L.C.M. of two numbers.


Definition of LCM (Least common multiple):
LCM of two integers is a smallest positive integer which is multiple of both integers that it is
divisible by the both of the numbers.
For example: LCM of two integers 2 and 5 is 10 since 10 is the smallest positive numbers which is divisible
by both 2 and 5.

#include<stdio.h>
int main(){
int n1,n2,x,y;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
x=n1,y=n2;
while(n1!=n2){
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("L.C.M=%d",x*y/n1);
return 0;
}

70. LCM program in c with two numbers (Other logic) :


#include<stdio.h>
int lcm(int,int);
int main(){
int a,b,l;
printf("Enter any two positive integers ");
scanf("%d%d",&a,&b);
if(a>b)
l = lcm(a,b);
else
l = lcm(b,a);
printf("LCM of two integers is %d",l);
return 0;
}
int lcm(int a,int b){
int temp = a;
while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}
return temp;
}

71. LCM program in c with multiple numbers :


#include<stdio.h>
int lcm(int,int);
int main(){
int a,b=1;
printf("Enter positive integers. To quit press zero.");
while(1){
scanf("%d",&a);
if(a<1)
break;
else if(a>b)
b = lcm(a,b);
else
b = lcm(b,a);
}
printf("LCM is %d",b);
return 0;
}
int lcm(int a,int b){
int temp = a;
while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}
return temp;
}

72. Write a c program for finding HCF of two given numbers


Definition of HCF (Highest common factor):
HFC is also called greatest common divisor (gcd). HCF of two numbers is a largest positive numbers
which can divide both numbers without any remainder. For example HCF of two numbers 4 and 8 is 2 since
2 is the largest positive number which can dived 4 as well as 8 without a remainder.

Logic of HCF or GCD of any two numbers:


In HCF we try to find any largest number which can divide both the number. For example: HCF or
GCD of 20 and 30. Both number 20 and 30 are divisible by 1, 2,5,10.
HCF=max (1, 2, 3, 4, 10) =10

Logic for writing program:


It is clear that any number is not divisible by greater than number itself. In case of more than one
numbers, a possible maximum number which can divide all of the numbers must be minimum of all of that
numbers.

For example: 10, 20, and 30


Min (10, 20, 30) =10 can divide all there numbers. So we will take one for loop which will start form min of
the numbers and will stop the loop when it became one, since all numbers are divisible by one. Inside for
loop we will write one if conditions which will check divisibility of both the numbers.
Program:

#include<stdio.h>
int main(){
int x,y,m,i;
printf("Insert any two number: ");
scanf("%d%d",&x,&y);
if(x>y)
m=y;
else
m=x;

for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nHCF of two number is : %d",i) ;
break;
}
}
return 0;
}

73. HCF (Highest common factor) program with two numbers in c


#include<stdio.h>
int main(){
int n1,n2;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
while(n1!=n2){
if(n1>=n2-1)
n1=n1-n2;
else
n2=n2-n1;
}
printf("\nGCD=%d",n1);
return 0;
}

74. HCF program with multiple numbers in c


#include<stdio.h>
int main(){
int x,y=-1;
printf("Insert numbers. To exit insert zero: ");

while(1){
scanf("%d",&x);
if(x<1)
break;
else if(y==-1)
y=x;
else if (x<y)
y=gcd(x,y);
else
y=gcd(y,x);
}
printf("GCD is %d",y);
return 0;
}

int gcd(int x,int y){


int i;
for(i=x;i>=1;i--){
if(x%i==0&&y%i==0){
break;
}
}
return i;
}

75. Swapping in c without temporary variable. (or) Swap 2 numbers without using third variable in c.
(or) How to swap two numbers in c without using third variable

#include<stdio.h>
int main(){
int a=5,b=10;

//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);

//process two
a=5;b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);

//process three
a=5;b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d b= %d",a,b);

//process four
a=5;b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d b= %d",a,b);

//process five
a=5,b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d b= %d",a,b);
return 0;
}

76. Write a c program for swapping of two arrays


#include<stdio.h>
int main(){
int a[10],b[10],c[10],i;
printf("Enter First array->");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nEnter Second array->");
for(i=0;i<10;i++)
scanf("%d",&b[i]);
printf("Arrays before swapping");
printf("\nFirst array->");
for(i=0;i<10;i++){
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++){
printf("%d",b[i]);
}
for(i=0;i<10;i++){
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
printf("\nArrays after swapping");
printf("\nFirst array->");
for(i=0;i<10;i++){
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++){
printf("%d",b[i]);
}
return 0;
}

77. Swapping of strings using c programming language


#include<stdio.h>
int main(){
int i=0,j=0,k=0;
char str1[20],str2[20],temp[20];
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before swaping the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
temp[j++]=str1[i++];
}
temp[j]='\0';
i=0,j=0;
while(str2[i]!='\0'){
str1[j++]=str2[i++];
}
str1[j]='\0';
i=0,j=0;
while(temp[i]!='\0'){
str2[j++]=temp[i++];
}
str2[j]='\0';
printf("After swaping the strings are\n");
puts(str1);
puts(str2);
return 0;
}

78. Decimal to binary conversion in c programming language. (or) C source code for decimal to
binary conversion:
Binary number system: It is base 2 number system which uses the digits from 0 and 1.
Decimal number system:
It is base 10 number system which uses the digits from 0 to 9
Convert from decimal to binary algorithm:
Following steps describe how to convert decimal to binary
Step 1: Divide the original decimal number by 2
Step 2: Divide the quotient by 2
Step 3: Repeat the step 2 until we get quotient equal to zero.
Equivalent binary number would be remainders of each step in the reverse order.
Decimal to binary conversion with example:
For example we want to convert decimal number 25 in the binary.
Step 1: 25 / 2 Remainder : 1 , Quotient : 12
Step 2: 12 / 2 Remainder : 0 , Quotient : 6
Step 3: 6 / 2 Remainder : 0 , Quotient : 3
Step 4: 3 / 2 Remainder : 1 , Quotient : 1
Step 5: 1 / 2 Remainder : 1 , Quotient : 0
So equivalent binary number is: 11001
That is (25)10 = (11001)2

#include<stdio.h>
int main(){
long int decimalNumber,remainder,quotient;
int binaryNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
printf("Equivalent binary value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
return 0;
}

Sample output:
Enter any decimal number: 50
Equivalent binary value of decimal number 50: 110010

79. C code for decimal to octal converter


Octal number system: It is base 8 number system which uses the digits from 0 to 7.
Decimal number system:
It is base 10 number system which uses the digits from 0 to 9
Decimal to octal conversion method:
Step 1: Divide the original decimal number by 8
Step 2: Divide the quotient by 8
Step3: Repeat the step 2 until we get quotient equal to zero.
Result octal number would be remainders of each step in the reverse order.
Decimal to octal conversion with example:
For example we want to convert decimal number 525 in the octal.
Step 1: 525 / 8 Remainder : 5 , Quotient : 65
Step 2: 65 / 8 Remainder : 1 , Quotient : 8
Step 3: 8 / 8 Remainder : 0 , Quotient : 1
Step 4: 1 / 8 Remainder : 1 , Quotient : 0
So equivalent octal number is: 1015
That is (525)10 = (1015)8
#include<stdio.h>
int main(){
long int decimalNumber,remainder,quotient;
int octalNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
octalNumber[i++]= quotient % 8;
quotient = quotient / 8;
}
printf("Equivalent octal value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",octalNumber[j]);
return 0;
}

Sample output:
Enter any decimal number: 50
Equivalent octal value of decimal number 50: 62

80. Easy way to convert decimal number to octal number in c


#include<stdio.h>
int main(){
long int decimalNumber;
printf("Enter any decimal number : ");
scanf("%d",&decimalNumber);
printf("Equivalent octal number is: %o",decimalNumber);
return 0;
}
Sample output:
Enter any decimal number: 25
Equivalent octal number is: 31

81. C code to convert decimal to hexadecimal


Hexadecimal number system: It is base 16 number system which uses the digits from 0 to 9 and A,
B, C, D, E, F.
Decimal number system:
It is base 10 number system which uses the digits from 0 to 9
Decimal to hexadecimal conversion method:
Following steps describe how to convert decimal to hexadecimal
Step 1: Divide the original decimal number by 16
Step 2: Divide the quotient by 16
Step 3: Repeat the step 2 until we get quotient equal to zero.
Equivalent binary number would be remainders of each step in the reverse order.
Decimal to hexadecimal conversion example:
For example we want to convert decimal number 900 in the hexadecimal.
Step 1: 900 / 16 Remainder : 4 , Quotient : 56
Step 2: 56 / 16 Remainder : 8 , Quotient : 3
Step 3: 3 / 16 Remainder : 3 , Quotient : 0
So equivalent hexadecimal number is: 384
That is (900)10 = (384)16
#include<stdio.h>
int main(){
long int decimalNumber,remainder,quotient;
int i=1,j,temp;
char hexadecimalNumber[100];
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
temp = quotient % 16;
//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}
printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%c",hexadecimalNumber[j]);
return 0;
}
Sample output:
Enter any decimal number: 45
Equivalent hexadecimal value of decimal number 45: 2D
82. Easy way to convert decimal number to hexadecimal number:
#include<stdio.h>
int main(){
long int decimalNumber;
printf("Enter any decimal number: ");
scanf("%d",&decimalNumber);
printf("Equivalent hexadecimal number is: %X",decimalNumber);
return 0;
}
Sample output:
Enter any decimal number: 45
Equivalent hexadecimal number is: 2D

83. C program to convert octal to binary.


Octal to binary conversion method:
To convert or change the octal number to binary number replace the each octal digits by a binary
number using octal to binary chart.
Octa Binary
l
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
Octal to binary table
Octal to binary conversion examples:
For example we want to convert or change octal number 65201 to decimal. For this we will replace
each octal digit to binary values using the above table:
Octal number: 6 5 2 0 1
Binary values: 110 101 010 000 001
So (65201)8 = (110101010000001)2

#include<stdio.h>
#define MAX 1000
int main(){
char octalNumber[MAX];
long int i=0;
printf("Enter any octal number: ");
scanf("%s",octalNumber);
printf("Equivalent binary value: ");
while(octalNumber[i]){
switch(octalNumber[i]){
case '0': printf("000"); break;
case '1': printf("001"); break;
case '2': printf("010"); break;
case '3': printf("011"); break;
case '4': printf("100"); break;
case '5': printf("101"); break;
case '6': printf("110"); break;
case '7': printf("111"); break;
default: printf("\nInvalid octal digit %c ",octalNumber[i]); return 0;
}
i++;
}
return 0;
}

Sample output:
Enter any octal number: 123
Equivalent binary value: 001010011

84. C code to convert octal number to decimal number

#include<stdio.h>
#include<math.h>
int main(){
long int octal,decimal =0;
int i=0;
printf("Enter any octal number: ");
scanf("%ld",&octal);
while(octal!=0){
decimal = decimal + (octal % 10) * pow(8,i++);
octal = octal/10;
}
printf("Equivalent decimal value: %ld",decimal);
return 0;
}

Sample output:
Enter any octal number: 346
Equivalent decimal value: 230

85. C program to change octal to decimal


Octal to decimal conversion method:
To convert an octal number to decimal number multiply each digits separately of octal number from
right side by 8^0,8^1,8^2,8^3 … respectively and then find out the sum of them.
Octal to decimal conversion examples:
For example we want to convert octal number 3401 to decimal number.
Step 1: 1 * 8 ^0 = 1*1 =1
Step 2: 0 * 8 ^1 = 0*8 =0
Step 3: 4 * 8 ^2 = 4*64 =256
Step 4: 3 * 8 ^3 = 3*512 =1536
Sum= 1 + 0 + 256 + 1536 = 1793
So, (3401)8 = (1793)10
#include<stdio.h>
int main(){
long int octalNumber;
printf("Enter any octal number: ");
scanf("%o",&octalNumber);
printf("Equivalent decimal number is: %d",octalNumber);
return 0;
}
Sample output:
Enter any octal number: 17
Equivalent decimal number is: 15
86. C program for hexadecimal to binary conversion
Hexadecimal to binary conversion method:
To convert or change the hexadecimal number to binary number replace the each octal digits by a
binary number using hexadecimal to binary chart.
Hexadecimal Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
Hexadecimal to binary table
Hexadecimal to binary conversion examples:
For example we want to convert or change hexadecimal number 65B2 to binary. For this we will
replace each hexadecimal digit to binary values using the above table:
Hexadecimal number: 6 5 B 2
Binary values: 0110 0101 1011 0010
So (65B2)16 = (0110010110110010)2

#include<stdio.h>
#define MAX 1000
int main(){
char binaryNumber[MAX],hexaDecimal[MAX];
long int i=0;
printf("Enter any hexadecimal number: ");
scanf("%s",hexaDecimal);
printf("\nEquivalent binary value: ");
while(hexaDecimal[i]){
switch(hexaDecimal[i]){
case '0': printf("0000"); break;
case '1': printf("0001"); break;
case '2': printf("0010"); break;
case '3': printf("0011"); break;
case '4': printf("0100"); break;
case '5': printf("0101"); break;
case '6': printf("0110"); break;
case '7': printf("0111"); break;
case '8': printf("1000"); break;
case '9': printf("1001"); break;
case 'A': printf("1010"); break;
case 'B': printf("1011"); break;
case 'C': printf("1100"); break;
case 'D': printf("1101"); break;
case 'E': printf("1110"); break;
case 'F': printf("1111"); break;
case 'a': printf("1010"); break;
case 'b': printf("1011"); break;
case 'c': printf("1100"); break;
case 'd': printf("1101"); break;
case 'e': printf("1110"); break;
case 'f': printf("1111"); break;
default: printf("\nInvalid hexadecimal digit %c ",hexaDecimal[i]); return 0;
}
i++;
}
return 0;
}

Sample output:
Enter any hexadecimal number: 2AD5
Equivalent binary value: 0010101011010101

87. C program c program to convert binary to octal


Binary to octal conversion method:
Step1: Arrange the binary number in the group 3 from right side.
Step 2: Replace the each group with following values:
Binary Octal
number values
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7
Binary to octal chart
Binary to octal conversion examples:
For example we want to convert binary number 1011010101001101 to octal.
Step 1: 001 011 010 101 001 101
Step 2: 1 3 2 5 1 5
So (1011010101001101)2 = (132515)8

#include<stdio.h>
int main(){
long int binaryNumber,octalNumber=0,j=1,remainder;
printf("Enter any number any binary number: ");
scanf("%ld",&binaryNumber);
while(binaryNumber!=0){
remainder=binaryNumber%10;
octalNumber=octalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
printf("Equivalent octal value: %lo",octalNumber);
return 0;
}

Sample output:
Enter any number any binary number: 1101
Equivalent hexadecimal value: 15

88. C code for how to convert large binary to octal


#include<stdio.h>
#define MAX 1000
int main(){
char binaryNumber[MAX],octalNumber[MAX];
long int i=0,j=0;
printf("Enter any number any binary number: ");
scanf("%s",binaryNumber);
while(binaryNumber[i]){
binaryNumber[i] = binaryNumber[i] -48;
++i;
}
--i;
while(i-2>=0){
octalNumber[j++] = binaryNumber[i-2] *4 + binaryNumber[i-1] *2 + binaryNumber[i] ;
i=i-3;
}
if(i ==1)
octalNumber[j] = binaryNumber[i-1] *2 + binaryNumber[i] ;
else if(i==0)
octalNumber[j] = binaryNumber[i] ;
else
--j;
printf("Equivalent octal value: ");
while(j>=0){
printf("%d",octalNumber[j--]);
}
return 0;
}

Sample output:
Enter any number any binary number: 1111111111111111111
1111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111
11111111
Equivalent octal value: 3777777777777777777777777777777
7777777777777777777777777777777777777777777777777777

89. C code for binary to decimal conversion:


Binary number system: It is base 2 number system which uses the digits from 0 and 1.
Decimal number system:
It is base 10 number system which uses the digits from 0 to 9
Convert from binary to decimal algorithm:
For this we multiply each digit separately from right side by 1, 2, 4, 8, 16 … respectively then add
them.
Binary number to decimal conversion with example:
For example we want to convert binary number 101111 to decimal:
Step1: 1 * 1 = 1
Step2: 1 * 2 = 2
Step3: 1 * 4 = 4
Step4: 1 * 8 = 8
Step5: 0 * 16 = 0
Step6: 1 * 32 = 32
Its decimal value: 1 + 2 + 4+ 8+ 0+ 32 = 47
That is (101111)2 = (47)10

#include<stdio.h>
int main(){
long int binaryNumber,decimalNumber=0,j=1,remainder;
printf("Enter any number any binary number: ");
scanf("%ld",&binaryNumber);
while(binaryNumber!=0){
remainder=binaryNumber%10;
decimalNumber=decimalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
printf("Equivalent decimal value: %ld",decimalNumber);
return 0;
}

Sample output:
Enter any number any binary number: 1101
Equivalent decimal value: 13

90. C program for binary to Hexadecimal conversion


Binary to hexadecimal conversion method
Step1: Arrange the binary number in the group 4 from right side.
Step 2: Replace the each group with following values:
Binary Hexadecimal
number values
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F
Binary to hexadecimal chart
Binary to hexadecimal conversion examples:
For example we want to convert binary number 11011010101001101 to hexadecimal.
Step 1: 0001 1011 0101 0100 1101
Step 2: 1 B 5 4 D
So (1011010101001101)2 = (1B54D)16
#include<stdio.h>
int main(){
long int binaryNumber,hexadecimalNumber=0,j=1,remainder;
printf("Enter any number any binary number: ");
scanf("%ld",&binaryNumber);
while(binaryNumber!=0){
remainder=binaryNumber%10;
hexadecimalNumber=hexadecimalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
printf("Equivalent hexadecimal value: %lX",hexadecimalNumber);
return 0;
}

Sample output:
Enter any number any binary number: 1101
Equivalent hexadecimal value: D

91. How to convert large binary number to hexadecimal


#include<stdio.h>
#define MAX 1000
int main(){
char binaryNumber[MAX],hexaDecimal[MAX];
int temp;
long int i=0,j=0;
printf("Enter any number any binary number: ");
scanf("%s",binaryNumber);
while(binaryNumber[i]){
binaryNumber[i] = binaryNumber[i] -48;
++i;
}
--i;
while(i-2>=0){
temp = binaryNumber[i-3] *8 + binaryNumber[i-2] *4 + binaryNumber[i-1] *2 + binaryNumber[i] ;
if(temp > 9)
hexaDecimal[j++] = temp + 55;
else
hexaDecimal[j++] = temp + 48;
i=i-4;
}
if(i ==1)
hexaDecimal[j] = binaryNumber[i-1] *2 + binaryNumber[i] + 48 ;
else if(i==0)
hexaDecimal[j] = binaryNumber[i] + 48 ;
else
--j;
printf("Equivalent hexadecimal value: ");
while(j>=0){
printf("%c",hexaDecimal[j--]);
}
return 0;
}

Sample output:
Enter any number any binary number: 1010011011100011110
001001111011110001000100011101110111011110
Equivalent hexadecimal value: 14DC789EF111DDDE

92. C code for sum of two binary numbers:


Rule of binary addition:
0+0=0
1+0=1
0+1=1
1 + 1 = 1 and carry = 1
What is the sum of the binary numbers 1101 and 1110?
Answer: 1101 + 1110 = 11011
#include<stdio.h>
int main(){
long int binary1,binary2;
int i=0,remainder = 0,sum[20];
printf("Enter any first binary number: ");
scanf("%ld",&binary1);
printf("Enter any second binary number: ");
scanf("%ld",&binary2);
while(binary1!=0||binary2!=0){
sum[i++] = (binary1 %10 + binary2 %10 + remainder ) % 2;
remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
binary1 = binary1/10;
binary2 = binary2/10;
}
if(remainder!=0)
sum[i++] = remainder;
--i;
printf("Sum of two binary numbers: ");
while(i>=0)
printf("%d",sum[i--]);
return 0;
}

Sample output:
Enter any first binary number: 1100011
Enter any second binary number: 1101
Sum of two binary numbers: 1110000

93. C code for product of two binary numbers (or) C program for multiplication of two binary
numbers.
Rule of binary multiplication:
0*0=0
1*0=0
0*1=0
1*1=1
What is the product of the binary numbers 0110 and 0011?
Answer: 0110 * 0011 = 10010
#include<stdio.h>
int binaryAddition(int,int);
int main(){
long int binary1,binary2,multiply=0;
int digit,factor=1;
printf("Enter any first binary number: ");
scanf("%ld",&binary1);
printf("Enter any second binary number: ");
scanf("%ld",&binary2);
while(binary2!=0){
digit = binary2 %10;
if(digit ==1){
binary1=binary1*factor;
multiply = binaryAddition(binary1,multiply);
}
else
binary1=binary1*factor;
binary2 = binary2/10;
factor = 10;
}
printf("Product of two binary numbers: %ld",multiply);
return 0;
}
int binaryAddition(int binary1,int binary2){
int i=0,remainder = 0,sum[20];
int binarySum=0;
while(binary1!=0||binary2!=0){
sum[i++] = (binary1 %10 + binary2 %10 + remainder ) % 2;
remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
binary1 = binary1/10;
binary2 = binary2/10;
}
if(remainder!=0)
sum[i++] = remainder;
--i;
while(i>=0)
binarySum = binarySum*10 + sum[i--];
return binarySum;
}

Sample output:
Enter any first binary number: 1101
Enter any second binary number: 11
Product of two binary numbers: 100111

94. C code for fractional binary to decimal converter:


Convert the fractional binary to decimal or floating point binary to decimal:
Step1. First we convert the integral part of binary number to decimal. For this we multiply each digit
separately from right side by 1, 2, 4, 8, 16 … respectively then add them this is integral part of decimal
number.
Step2. Now we convert the fractional part of binary number to decimal. For this we multiply each
digit separately from left side by 1/2, 1/4, 1/8, 1/16 … respectively then add them this is fractional part of
decimal number.
Step3: Add the integral and fractional part of decimal number.
Example for floating point binary to decimal:
For example we want to convert the binary number 101111.1101 to decimal number.
Step1: Conversions of 101111 to decimal:
S1: 1 * 1 = 1
S2: 1 * 2 = 2
S3: 1 * 4 = 4
S4: 1 * 8 = 8
S5: 0 * 16 = 0
S6: 1 * 32 = 32
Integral part of decimal number: 1 + 2 + 4+ 8+ 0+ 32 = 47
Step2: Conversions of .1101 to decimal:
S1: 1 * (1/2) = 0.5
S2: 1 * (1/4) = 0.25
S3: 0 * (1/8) = 0
S4: 1 * (1/16) = 0.0625
Fractional part of decimal number = 0.5 + 0.25 + 0 + 0.0625 = 0.8125
So equivalent binary number is: 0.101100
Step 3: So binary value of binary number 101111.1101 will be 47 + 0.8125 = 47.8125
#include<stdio.h>
#define MAX 1000
int main(){
long double fraDecimal=0.0,dFractional=0.0 ,fraFactor=0.5;
long int dIntegral = 0,bIntegral=0,bFractional[MAX];
long int intFactor=1,remainder,i=0,k=0,flag=0;
char fraBinary[MAX];
printf("Enter any fractional binary number: ");
scanf("%s",&fraBinary);
while(fraBinary[i]){
if(fraBinary[i] == '.')
flag = 1;
else if(flag==0)
bIntegral = bIntegral * 10 + (fraBinary[i] -48);
else
bFractional[k++] = fraBinary[i] -48;
i++;
}
while(bIntegral!=0){
remainder=bIntegral%10;
dIntegral= dIntegral+remainder*intFactor;
intFactor=intFactor*2;
bIntegral=bIntegral/10;
}
for(i=0;i<k;i++){
dFractional = dFractional + bFractional[i] * fraFactor;
fraFactor = fraFactor / 2;
}
fraDecimal = dIntegral + dFractional ;
printf("Equivalent decimal value: %Lf",fraDecimal);
return 0;
}

Sample output:
Enter any fractional binary number: 11.11
Equivalent decimal value: 3.750000

95. C code for fractional decimal to binary converter:


How to convert fractional decimal to binary:
Following steps describe how to convert decimal to binary
Step1. First we convert the integral part of binary number to decimal.
Following steps describe how to convert decimal number to binary number:
S1: Divide the decimal number by 2
S2: Divide the quotient by 2
S3: Repeat the step 2 until we get quotient equal to zero.
Equivalent binary number would be remainders of each step in the reverse order.
Step2. Now we convert the fractional part of decimal number to binary.
Following steps describe how to convert floating decimal to binary
S1: Multiply the decimal number by 2
S2: Integral part of resultant decimal number will be first digit of fraction binary number.
S3: Repeat the S1 using only fractional part of decimal number and then S2.
Step3: Add the integral and fractional part of binary number.
Example for floating point decimal to binary:
For example we want to convert the decimal number 25.7 to binary number.
Step1: Conversions of 25 to binary.

S1: 25 / 2 Remainder : 1 , Quotient : 12


S2: 12 / 2 Remainder : 0 , Quotient : 6
S3: 6 / 2 Remainder : 0 , Quotient : 3
S4: 3 / 2 Remainder : 1 , Quotient : 1
S5: 1 / 2 Remainder : 1 , Quotient : 0
So equivalent binary number is: 11001
Step2: Conversions of .7 to binary.
S1: 0.7 * 2 = 1.4, Integral part = 1
S2: 0.4 * 2 = 0.8, Integral part = 0
S3: 0.8 * 2 = 1.6, Integral part = 1
S4: 0.6 * 2 = 1.2, Integral part = 1
S5: 0.2 * 2 = 0.4, Integral part = 0
S6: 0.4 * 2 = 0.8, Integral part = 0
So equivalent binary number is: 0.101100
Step 3: So binary value of decimal number 25.7 will be
11001 + 0.101100 = 1101.101100
#include<stdio.h>
int main(){
long double fraDecimal,fraBinary,bFractional = 0.0,dFractional,fraFactor=0.1;
long int dIntegral,bIntegral=0;
long int intFactor=1,remainder,temp,i;
printf("Enter any fractional decimal number: ");
scanf("%Lf",&fraDecimal);
dIntegral = fraDecimal;
dFractional = fraDecimal - dIntegral;
while(dIntegral!=0){
remainder=dIntegral%2;
bIntegral=bIntegral+remainder*intFactor;
dIntegral=dIntegral/2;
intFactor=intFactor*10;
}
for(i=1;i<=6;i++){
dFractional = dFractional * 2;
temp = dFractional;
bFractional = bFractional + fraFactor* temp;
if(temp ==1)
dFractional = dFractional - temp;
fraFactor=fraFactor/10;
}
fraBinary = bIntegral + bFractional;
printf("Equivalent binary value: %lf",fraBinary);
return 0;
}

Sample output:
Enter any fractional decimal number: 5.7
Equivalent binary value: 101.101100

96. Convert numbers to roman numerals in c


#include<stdio.h>
void predigits(char c1,char c2);
void postdigits(char c,int n);
char roman_Number[1000];
int i=0;
int main(){
int j;
long int number;
printf("Enter any natural number: ");
scanf("%d",&number);
if(number <= 0){
printf("Invalid number");
return 0;
}
while(number != 0){
if(number >= 1000){
postdigits('M',number/1000);
number = number - (number/1000) * 1000;
}
else if(number >=500){
if(number < (500 + 4 * 100)){
postdigits('D',number/500);
number = number - (number/500) * 500;
}
else{
predigits('C','M');
number = number - (1000-100);
}
}
else if(number >=100){
if(number < (100 + 3 * 100)){
postdigits('C',number/100);
number = number - (number/100) * 100;
}
else{
predigits('L','D');
number = number - (500-100);
}
}
else if(number >=50){
if(number < (50 + 4 * 10)){
postdigits('L',number/50);
number = number - (number/50) * 50;
}
else{
predigits('X','C');
number = number - (100-10);
}
}
else if(number >=10){
if(number < (10 + 3 * 10)){
postdigits('X',number/10);
number = number - (number/10) * 10;
}
else{
predigits('X','L');
number = number - (50-10);
}
}
else if(number >=5){
if(number < (5 + 4 * 1)){
postdigits('V',number/5);
number = number - (number/5) * 5;
}
else{
predigits('I','X');
number = number - (10-1);
}
}
else if(number >=1){
if(number < 4){
postdigits('I',number/1);
number = number - (number/1) * 1;
}
else{
predigits('I','V');
number = number - (5-1);
}
}
}

printf("Roman number will be: ");


for(j=0;j<i;j++)
printf("%c",roman_Number[j]);
return 0;
}
void predigits(char c1,char c2){
roman_Number[i++] = c1;
roman_Number[i++] = c2;
}
void postdigits(char c,int n){
int j;
for(j=0;j<n;j++)
roman_Number[i++] = c;
}

Sample output:
Enter any natural number: 87
Roman number will be: LXXXVII

97. C code for roman numbers to English numbers


#include<stdio.h>
#include<string.h>
int digitValue(char);
int main(){
char roman_Number[1000];
int i=0;
long int number =0;
printf("Enter any roman number (Valid digits are I, V, X, L, C, D, M): \n");
scanf("%s",roman_Number);
while(roman_Number[i]){
if(digitValue(roman_Number[i]) < 0){
printf("Invalid roman digit : %c",roman_Number[i]);
return 0;
}
if((strlen(roman_Number) -i) > 2){
if(digitValue(roman_Number[i]) < digitValue(roman_Number[i+2])){
printf("Invalid roman number");
return 0;
}
}

if(digitValue(roman_Number[i]) >= digitValue(roman_Number[i+1]))


number = number + digitValue(roman_Number[i]);
else{
number = number + (digitValue(roman_Number[i+1]) - digitValue(roman_Number[i]));
i++;
}
i++;
}
printf("Its decimal value is : %ld",number);
return 0;
}
int digitValue(char c){
int value=0;
switch(c){
case 'I': value = 1; break;
case 'V': value = 5; break;
case 'X': value = 10; break;
case 'L': value = 50; break;
case 'C': value = 100; break;
case 'D': value = 500; break;
case 'M': value = 1000; break;
case '\0': value = 0; break;
default: value = -1;
}
return value;
}

Sample output:
Enter any roman number (Valid digits are I, V, X, L, C, D, M):
XVII
Its decimal value is: 17

98. C code to covert each digits of a number in English word (or) Convert digits to words in c
#include<stdio.h>
int main(){
int number,i=0,j,digit;
char * word[1000];
printf("Enter any integer: ");
scanf("%d",&number);
while(number){
digit = number %10;
number = number /10;
switch(digit){
case 0: word[i++] = "zero"; break;
case 1: word[i++] = "one"; break;
case 2: word[i++] = "two"; break;
case 3: word[i++] = "three"; break;
case 4: word[i++] = "four"; break;
case 5: word[i++] = "five"; break;
case 6: word[i++] = "six"; break;
case 7: word[i++] = "seven"; break;
case 8: word[i++] = "eight"; break;
case 9: word[i++] = "nine"; break;
}
}
for(j=i-1;j>=0;j--){
printf("%s ",word[j]);
}
return 0;
}
Sample output:
Enter any integer: 23451208
two three four five one two zero eight

99. C code to convert any number to English word:


#include<stdio.h>
#include<string.h>
void toWord(int,int);
char * getPositionValue(int);
char * digitToWord(int);
char word[100][30];
int i =0;
int main(){
int j,k,subnumer;
unsigned long int number;
printf("Enter any postive number: ");
scanf("%lu",&number);
if(number ==0){
printf("Zero");
return 0;
}
while(number){
if(i==1){
toWord(number %10,i);
number = number/10;
}else{
toWord(number %100,i);
number = number/100;
}
i++;
}
printf("Number in word: ");
*word[i-1] = *word[i-1] - 32;
for(j=i-1;j>=0;j--){
printf("%s",word[j]);
}
return 0;
}
void toWord(int number,int position){
char numberToword[100]={" "};
if(number ==0){
}else if (number < 20 ||number %10==0){
strcpy(numberToword,digitToWord(number));
}else{
strcpy(numberToword,digitToWord((number/10)*10));
strcat(numberToword,digitToWord(number%10));
}
strcat(numberToword,getPositionValue(position));
strcpy(word[i],numberToword);
}
char * getPositionValue(int postion){
static char positionValue[10]=" ";
switch(postion){
case 1: strcpy(positionValue,"hundreds "); break;
case 2: strcpy(positionValue,"thousand "); break;
case 3: strcpy(positionValue,"lakh "); break;
case 4: strcpy(positionValue,"crore "); break;
case 5: strcpy(positionValue,"arab "); break;
case 6: strcpy(positionValue,"kharab "); break;
case 7: strcpy(positionValue,"neel "); break;
case 8: strcpy(positionValue,"padam "); break;
}
return positionValue;
}
char * digitToWord(int digit){
static char digitInWord[10]=" ";
switch(digit){
case 1: strcpy(digitInWord , "one "); break;
case 2: strcpy(digitInWord , "two "); break;
case 3: strcpy(digitInWord , "three "); break;
case 4: strcpy(digitInWord , "four "); break;
case 5: strcpy(digitInWord , "five "); break;
case 6: strcpy(digitInWord , "six "); break;
case 7: strcpy(digitInWord , "seven "); break;
case 8: strcpy(digitInWord , "eight "); break;
case 9: strcpy(digitInWord , "nine ");break;
case 10: strcpy(digitInWord , "ten "); break;
case 11: strcpy(digitInWord , "eleven "); break;
case 12: strcpy(digitInWord , "twelve "); break;
case 13: strcpy(digitInWord , "thirteen "); break;
case 14: strcpy(digitInWord , "fourteen "); break;
case 15: strcpy(digitInWord , "fifteen "); break;
case 16: strcpy(digitInWord , "sixteen "); break;
case 17: strcpy(digitInWord , "seventeen "); break;
case 18: strcpy(digitInWord , "eighteen "); break;
case 19: strcpy(digitInWord , "nineteen "); break;
case 20: strcpy(digitInWord , "twenty "); break;
case 30: strcpy(digitInWord , "thirty "); break;
case 40: strcpy(digitInWord , "fourty "); break;
case 50: strcpy(digitInWord , "fifty "); break;
case 60: strcpy(digitInWord , "sixty "); break;
case 70: strcpy(digitInWord , "seventy "); break;
case 80: strcpy(digitInWord , "eighty "); break;
case 90: strcpy(digitInWord,"ninety "); break;
}
return digitInWord;
}

100. C code to convert one unit to other unit:


#include<stdio.h>
#include<math.h>
int main(){
char fromUnit,toUnit;
char *fUnit,*tUnit;
long double fromValue,meterValue,toValue;
int power =0;
printf("To convert the 12 Inch to Foot\n");
printf("enter the the unit in the format : dc12\n");
printf("Ell: a\n");
printf("Femi: b\n");
printf("Foot: c\n");
printf("Inch: d\n");
printf("Light year: e\n");
printf("Metre: f\n");
printf("Mile: g\n");
printf("Nano meter: h\n");
printf("Pace: i\n");
printf("Point: j\n");
printf("Yard: k\n");
printf("Mili meter: l\n");
printf("Centi meter: m\n");
printf("Deci meter: n\n");
printf("Deca meter: o\n");
printf("Hecto meter: p\n");
printf("Kilo meter: q\n");
scanf("%c%c%Lf",&fromUnit,&toUnit,&fromValue);
switch(fromUnit){
case 'a': meterValue = fromValue * 1.143; fUnit="ell"; break;
case 'b': meterValue = fromValue ; power = -15; fUnit="fm"; break;
case 'c': meterValue = fromValue * 0.3048; fUnit="ft"; break;
case 'd': meterValue = fromValue * 0.0254; fUnit="in"; break;
case 'e': meterValue = fromValue * 9.4607304725808; power =15; fUnit="ly"; break;
case 'f': meterValue = fromValue; fUnit="m"; break;
case 'g': meterValue = fromValue * 1609.344; fUnit="mi"; break;
case 'h': meterValue = fromValue; fUnit="nm"; power = -9; break;
case 'i': meterValue = fromValue * 0.762 ; fUnit="pace"; break;
case 'j': meterValue = fromValue * 0.000351450; fUnit="pt"; break;
case 'k': meterValue = fromValue * 0.9144; fUnit="yd"; break;
case 'l': meterValue = fromValue * 0.001; fUnit="mm"; break;
case 'm': meterValue = fromValue * 0.01; fUnit="cm";break;
case 'n': meterValue = fromValue * 0.1; fUnit="deci meter"; break;
case 'o': meterValue = fromValue * 10; fUnit="deca meter"; break;
case 'p': meterValue = fromValue * 100; fUnit="hm";break;
case 'q': meterValue = fromValue * 1000; fUnit="km";break;
default:
printf("Invalid input"); exit(0);
}
switch(toUnit){
case 'a': toValue = meterValue/1.143; tUnit="ell";break;
case 'b': toValue = meterValue; tUnit="fm"; break;
case 'c': toValue = meterValue/0.3048; tUnit="ft";break;
case 'd': toValue = meterValue/0.0254; tUnit="in";break;
case 'e': toValue = meterValue/9.4607304725808; tUnit="ly"; break;
case 'f': toValue = meterValue; tUnit="m";break;
case 'g': toValue = meterValue/1609.344; tUnit="mi";break;
case 'h': toValue = meterValue; tUnit="nm"; break;
case 'i': toValue = meterValue/0.762; tUnit="pace";break;
case 'j': toValue = meterValue/0.000351450; tUnit="pt"; break;
case 'k': toValue = meterValue/0.9144; tUnit="yd";break;
case 'l': toValue = meterValue/0.001; tUnit="mm";break;
case 'm': toValue = meterValue/0.01; tUnit="cm";break;
case 'n': toValue = meterValue/0.1; tUnit="deci meter"; break;
case 'o': toValue = meterValue/10; tUnit="deca meter"; break;
case 'p': toValue = meterValue/100; tUnit="hm";break;
case 'q': toValue = meterValue/1000; tUnit="km";break;
default:
printf("Invalid input"); exit(0);
}
if(power==0)
printf("%.4Lf %s = %.4Lf %s",fromValue,fUnit,toValue,tUnit);
else{
while(tovalue > 10
printf("%.4Lf %s = %.4Lf*10^%d %s",fromValue,fUnit,toValue,power,tUnit);
}
return 0;
}
101. Conversion from uppercase to lower case using c program
ASCII value of 'A' is 65 while 'a' is 97. Difference between them is 97 – 65 = 32
So if we will add 32 in the ASCII value of 'A' then it will be 'a' and if will we subtract 32 in ASCII
value of 'a' it will be 'A'. It is true for all alphabets.
In general rule:
Upper case character = Lower case character – 32
Lower case character = Upper case character + 32

#include<stdio.h>
#include<string.h>
int main(){
char str[20];
int i;
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in lower case is->%s",str);
return 0;
}

102. Write a c program to convert the string from lower case to upper case

ASCII value of 'A' is 65 while 'a' is 97. Difference between them is 97 – 65 = 32


So if we will add 32 in the ASCII value of 'A' then it will be 'a' and if will we subtract 32 in ASCII
value of 'a' it will be 'A'. It is true for all alphabets.
In general rule:
Upper case character = Lower case character – 32
Lower case character = Upper case character + 32

#include<stdio.h>
int main(){
char str[20];
int i;
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nThe string in lowercase is->%s",str);
return 0;
}

103. COUNTING DIFFERENT CHARACTERS IN A STRING USING C PROGRAM


#include <stdio.h>
int isvowel(char chk);
int main(){
char text[1000], chk;
int count;
count = 0;
while((text[count] = getchar()) != '\n')
count++;
text[count] = '\0';
count = 0;
while ((chk = text[count]) != '\0'){
if (isvowel(chk)){
if((chk = text[++count]) && isvowel(chk)){
putchar(text[count -1]);
putchar(text[count]);
putchar('\n');
}
}
else
++count;
}
return 0;
}
int isvowel(char chk){
if(chk == 'a' || chk == 'e' || chk == 'i' || chk == 'o' || chk == 'u')
return 1;
return 0;
}

104. Program for sorting of string in c language


#include<stdio.h>
int main(){
int i,j,n;
char str[20][20],temp[20];
puts("Enter the no. of string to be sorted");
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("The sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
return 0;
}

105. Concatenation of two strings in c programming language


#include<stdio.h>
int main(){
int i=0,j=0;
char str1[20],str2[20];
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before concatenation the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0'){
i++;
}
while(str2[j]!='\0'){
str1[i++]=str2[j++];
}
str1[i]='\0';
printf("After concatenation the strings are\n");
puts(str1);
return 0;
}

106. Concatenation of two strings using pointer in c programming language


#include<stdio.h>
int main(){
int i=0,j=0;
char *str1,*str2,*str3;
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before concatenation the strings are\n");
puts(str1);
puts(str2);
while(*str1){
str3[i++]=*str1++;
}
while(*str2){
str3[i++]=*str2++;
}
str3[i]='\0';
printf("After concatenation the strings are\n");
puts(str3);
return 0;
}

107. C code which prints initial of any name


#include<stdio.h>
int main(){
char str[20];
int i=0;
printf("Enter a string: ");
gets(str);
printf("%c",*str);
while(str[i]!='\0'){
if(str[i]==' '){
i++;
printf("%c",*(str+i));
}
i++;
}
return 0;
}

Sample output:
Enter a string: Robert De Niro
RDN

108. Write a c program to print the string from given character


#include<string.h>
#include<stdio.h>
int main(){
char *p;
char s[20],s1[1];
printf("\nEnter a string: ");
scanf("%[^\n]",s);
fflush(stdin);
printf("\nEnter character: ");
gets(s1);
p=strpbrk(s,s1);
printf("\nThe string from the given character is: %s",p);
return 0;
}

109. Reverse a string in c without using temp (or) String reverse using strrev in c programming
language
#include<stdio.h>
#include<string.h>
int main(){
char str[50];
char *rev;
printf("Enter any string : ");
scanf("%s",str);
rev = strrev(str);
printf("Reverse string is : %s",rev);
return 0;
}

110. String reverse in c without using strrev (or) String reverse in c without using string function
#include<stdio.h>
int main(){
char str[50];
char rev[50];
int i=-1,j=0;
printf("Enter any string : ");
scanf("%s",str);
while(str[++i]!='\0');
while(i>=0)
rev[j++] = str[--i];
rev[j]='\0';
printf("Reverse of string is : %s",rev);
return 0;
}
Sample output:
Enter any string : [Link]
Reverse of string is : [Link]

111. Reverse a string in c using pointers (or) C program to reverse a string using pointers

#include<stdio.h>
int main(){
char str[50];
char rev[50];
char *sptr = str;
char *rptr = rev;
int i=-1;
printf("Enter any string : ");
scanf("%s",str);
while(*sptr){
sptr++;
i++;
}
while(i>=0){
sptr--;
*rptr = *sptr;
rptr++;
--i;
}
*rptr='\0';
printf("Reverse of string is : %s",rev);
return 0;
}

Sample output:
Enter any string : Pointer
Reverse of string is : retnioP

112. C code to reverse a string by recursion:


#include<stdio.h>
#define MAX 100
char* getReverse(char[]);
int main(){
char str[MAX],*rev;
printf("Enter any string: ");
scanf("%s",str);
rev = getReverse(str);
printf("Reversed string is: %s",rev);
return 0;
}
char* getReverse(char str[]){
static int i=0;
static char rev[MAX];
if(*str){
getReverse(str+1);
rev[i++] = *str;
}
return rev;
}

Sample output:
Enter any string: mona
Reversed string is: anom

113. String concatenation in c without using string functions


#include<stdio.h>
void stringConcat(char[],char[]);
int main(){
char str1[100],str2[100];
int compare;
printf("Enter first string: ");
scanf("%s",str1);
printf("Enter second string: ");
scanf("%s",str2);
stringConcat(str1,str2);
printf("String after concatenation: %s",str1);
return 0;
}
void stringConcat(char str1[],char str2[]){
int i=0,j=0;
while(str1[i]!='\0'){
i++;
}
while(str2[j]!='\0'){
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
}

Sample output:
Enter first string: cquestionbank
Enter second string: @[Link]
String after concatenation: cquestionbank@[Link]

114. C program to compare two strings without using string functions


#include<stdio.h>
int stringCompare(char[],char[]);
int main(){
char str1[100],str2[100];
int compare;
printf("Enter first string: ");
scanf("%s",str1);
printf("Enter second string: ");
scanf("%s",str2);
compare = stringCompare(str1,str2);
if(compare == 1)
printf("Both strings are equal.");
else
printf("Both strings are not equal");
return 0;
}
int stringCompare(char str1[],char str2[]){
int i=0,flag=0;
while(str1[i]!='\0' && str2[i]!='\0'){
if(str1[i]!=str2[i]){
flag=1;
break;
}
i++;
}
if (flag==0 && str1[i]=='\0' && str2[i]=='\0')
return 1;
else
return 0;
}

Sample output:
Enter first string: [Link]
Enter second string: [Link]
Both strings are equal.

115. String copy without using strcpy in c programming language


#include<stdio.h>
void stringCopy(char[],char[]);
int main(){
char str1[100],str2[100];
printf("Enter any string: ");
scanf("%s",str1);
stringCopy(str1,str2);
printf("After copying: %s",str2);
return 0;
}

void stringCopy(char str1[],char str2[]){


int i=0;
while(str1[i]!='\0'){
str2[i] = str1[i];
i++;
}
str2[i]='\0';
}

Sample output:
Enter any string: [Link]
After copying: [Link]
116. Program to convert string into ASCII values in c programming language:
#include<stdio.h>
int main(){
char str[100];
int i=0;
printf("Enter any string: ");
scanf("%s",str);
printf("ASCII values of each characters of given string: ");
while(str[i])
printf("%d ",str[i++]);
return 0;
}

Sample Output:
Enter any string: [Link]
ASCII values of each characters of given string: 99 113 117 101 115 116 105 111 110 98 97 110 107 46 98
108 111 103 115 112 111 116 46 99 111 109

117. C program for addition of two matrices using arrays source code. (or) Matrix addition in c
language:
Addition of two matrices:
Rule: Addition of two matrices is only possible if both matrices are of same size.
Suppose two matrices A and B is of same size m X n
Sum of two matrices is defined as
(A + B)ij = Aij + Bij
Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
For example:
Suppose two matrices A and B of size of 2 X 3 is as follow:

#include<stdio.h>
int main(){
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the First matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe Second matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nThe Addition of two matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
return 0;
}

118. SUBTRACTION OF TWO MATRICES USING C PROGRAM


Subtraction of two matrixes:
Rule: Subtraction of two matrixes is only possible if both matrixes are of same size.
Suppose two matrixes A and B is of same size m X n
Subtraction of two marixes is defined as
(A - B)ij = Aij - Bij
Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
For example:
Suppose two matrixes A and B of size of 3 X 2 is as follow:
#include<stdio.h>
int main(){
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the First matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe Second matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]-b[i][j];
printf("\nThe Subtraction of two matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
return 0;
}

119. C code for matrix multiplication


Multiplication of two matrixes:
Rule: Multiplication of two matrixes is only possible if first matrix has size m X n and other matrix
has size n x r. Where m, n and r are any positive integer.
Multiplication of two matrixes is defined as

Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
For example:
Suppose two matrixes A and B of size of 2 x 2 and 2 x 3 respectively:
#include<stdio.h>
int main(){
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
printf("\nEnter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o){
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}
else{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++){ //row of first matrix
for(j=0;j<p;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",c[i][j]);
}
}
return 0;
}

120. Sum of diagonal elements of a matrix in c


#include<stdio.h>
int main(){
int a[10][10],i,j,sum=0,m,n;
printf("\nEnter the row and column of matrix: ");
scanf("%d %d",&m,&n);
printf("\nEnter the elements of matrix: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i==j)
sum=sum+a[i][j];
}
}
printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);
return 0;
}
Sample output:
Enter the row and column of matrix: 3 3
Enter the elements of matrix: 2
3
5
6
7
9
2
6
7
The matrix is
2 3 5
6 7 9
2 6 7
Sum of the diagonal elements of a matrix is: 16

121. C program for transpose of a matrix (or) C program to find transpose of given matrix
#include<stdio.h>
int main(){
int a[10][10],b[10][10],i,j,k=0,m,n;
printf("\nEnter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
b[i][j]=a[j][i];
printf("\n%d",b[i][j]);
}
}
printf("\n\nTraspose of a matrix is -> ");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",b[i][j]);
}
}
return 0;
}
122. How to find inverse of a matrix in c (or) C code to find inverse of a matrix (or) Inverse of a 3x3
matrix in C.
#include<stdio.h>
int main(){
int a[3][3],i,j;
float determinant=0;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
printf("\nInverse of matrix is: \n\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3]
[(j+1)%3]))/ determinant);
printf("\n");
}

return 0;
}

Enter the 9 elements of matrix: 3


5
2
1
5
8
3
9
2

The matrix is

3 5 2
1 5 8
3 9 2
Inverse of matrix is:

0.70 -0.25 0.07


-0.09 -0.00 0.14
-0.34 0.25 -0.11

123. C code to print or display lower triangular matrix


#include<stdio.h>
int main(){
int a[3][3],i,j;
float determinant=0;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nThe matrix is\n");


for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nSetting zero in upper triangular matrix\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
if(i<=j)
printf("%d\t",a[i][j]);
else
printf("%d\t",0);
}
return 0;
}

Sample Output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9

The matrix is
1 2 3
4 5 6
7 8 9

Setting zero in upper triangular matrix


1 2 3
0 5 6
0 0 9

124. C code to print or display Upper triangular matrix


#include<stdio.h>
int main(){
int a[3][3],i,j;
float determinant=0;

printf("Enter the 9 elements of matrix: ");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}

printf("\nSetting zero in upper triangular matrix\n");


for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
if(i>=j)
printf("%d\t",a[i][j]);
else
printf("%d\t",0);
}

return 0;
}

Sample output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9

The matrix is
1 2 3
4 5 6
7 8 9

Setting zero in upper triangular matrix


1 0 0
4 5 0
7 8 9

125. C code of two 2 by 2 matrix multiplication using Strassen algorithm:


#include<stdio.h>
int main(){
int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;
printf("Enter the 4 elements of first matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("Enter the 4 elements of second matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("\nThe first matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe second matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
}

m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);


m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
printf("\nAfter multiplication using \n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
}
return 0;
}

Sample output:
Enter the 4 elements of first matrix: 1
2
3
4
Enter the 4 elements of second matrix: 5
6
7
8

The first matrix is


1 2
3 4
The second matrix is
5 6
7 8
After multiplication using
19 22
43 50
126. Code to find determinant of a matrix (or) C program to calculate determinant of a matrix
#include<stdio.h>
int main(){
int a[3][3],i,j;
int determinant=0;

printf("Enter the 9 elements of matrix: ");


for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nThe First matrix is\n");


for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}

for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

printf("\nDeterminant of matrix is: %d",determinant);

return 0;
}

127. C code for Determinant of 2X2 matrix:


#include<stdio.h>
int main(){
int a[2][2],i,j;
long determinant;
printf("Enter the 4 elements of matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}
determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1];
printf("\nDeterminant of 2X2 matrix: %ld",determinant);
return 0;
}

Enter the 4 elements of matrix: 4


8
3
9
The matrix is
4 8
3 9
Determinant of 2X2 matrix: 12
128. C code for Determinant of 3X3 matrix:
#include<stdio.h>
int main(){
int a[3][3],i,j;
long determinant;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
determinant = a[0][0]*((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1]*(a[1][0]*a[2][2] - a[2][0]*a[1][2]) +
a[0][2]*(a[1][0]*a[2][1] - a[2][0]*a[1][1]);
printf("\nDeterminant of 3X3 matrix: %ld",determinant);
return 0;
}

Sample output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9
The matrix is
1 2 3
4 5 6
7 8 9
Determinant of 3X3 matrix: 0

C program to create a file


C program to write to a file

C program to open a file

#include<stdio.h>
int main(){
FILE *fp;
char ch;
fp=fopen("[Link]","w");
printf("\nEnter data to be stored in to the file:");
while((ch=getchar())!=EOF)
putc(ch,fp);
fclose(fp);
return 0;
}

COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM

#include<stdio.h>
int main(){
FILE *p,*q;
char file1[20],file2[20];
char ch;
printf("\nEnter the source file name to be copied:");
gets(file1);
p=fopen(file1,"r");
if(p==NULL){
printf("cannot open %s",file1);
exit(0);
}
printf("\nEnter the destination file name:");
gets(file2);
q=fopen(file2,"w");
if(q==NULL){
printf("cannot open %s",file2);
exit(0);
}
while((ch=getc(p))!=EOF)
putc(ch,q);
printf("\nCOMPLETED");
fclose(p);
fclose(q);
return 0;
}

DISPLAY SOURCE CODE AS OUTPUT IN C PROGRAM

#include<stdio.h>
int main(){
FILE *p;
char ch;
p=fopen("raja.c","r");
while((ch=getc(p))!=-1)
putchar(ch);
fclose(p);
return 0;
}
How to read a text file by c program

#include<stdio.h>
int main(){
char str[70];
FILE *p;
if((p=fopen("[Link]","r"))==NULL){
printf("\nUnable t open file [Link]");
exit(1);
}
while(fgets(str,70,p)!=NULL)
puts(str);
fclose(p);
return 0;
}

Writing of entire array to a file using c program

#include<stdio.h>
int main(){
FILE *p;
int i,a[10];
if((p=fopen("[Link]","wb"))==NULL){
printf("\nUnable to open file [Link]");
exit(1);
}
printf("\nEnter ten values, one value on each line\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
fwrite(a,sizeof(a),1,p);
fclose(p);
return 0;
}

Concatenate many files and store them in a file in c


programming language

#include<stdio.h>
void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc);
int main(int argc,char *argv[]){
FILE *fp1,*fp2;
concatenate(fp1,fp2,argv,argc);
return 0;
}

void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc){


int i,ch;
fp2=fopen("files","a");
for(i=1;i<argc-1;i++){
fp1=fopen(argv[i],"r");
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
}
}

WRITE A C PROGRAM TO FIND SIZE OF A FILE


C program to get total file size
#include <time.h>
#include <sys\stat.h>
#include <stdio.h>
void main()
{
struct stat status;
FILE *fp;
fp=fopen("[Link]","r");
fstat(fileno(fp),&status);
clrscr();
printf("Size of file : %d",status.st_size);
printf("Drive name : %c",65+status.st_dev);
getch();
}

Explanation:
Function int fstat (char *, struct stat *) store the
information of open file in form of
structure struct stat Structure struct stat has been defined
in sys\stat.h as

struct stat {
short st_dev, st_ino;
short st_mode, st_nlink;
int st_uid, st_gid;
short st_rdev;
long st_size, st_atime;
long st_mtime, st_ctime;
};

Here

(a)st_dev: It describe file has stored in which drive of your


computer ,it returns a number.
(b)st_mode: It describes various modes of file like file is
read only, write only, folder, character file etc.

(c)st_size: It tells the size of file in byte.

(d)st_ctime:It tells last data of modification of the file in


date format.

Note: 65 is ASCII value of A .So after adding status.st_dev


with 65 it will return appropriate drvie name as in your
computer.

Write a c program to know given file is regular file, character special or it is


directory?
#include "time.h"
#include "sys\stat.h"
#include "stdio.h"
void main(){
struct stat status;
FILE *fp;
stat("c:\\tc\\bin",&status);
clrscr();
if (status.st_mode & S_IFDIR)
printf("It is directory.\n");
if (status.st_mode & S_IFCHR)
printf("It is chracter file.");
if (status.st_mode & S_IFREG)
printf("It is reggular file.");
getch();

}
Output: It is directory.
Explanation:
Function int stat (char *, struct stat *) store the
information of open file in form of
structure struct stat Structure structstat has been defined in
sys\stat.h as

struct stat {
short st_dev, st_ino;
short st_mode, st_nlink;
int st_uid, st_gid;
short st_rdev;
long st_size, st_atime;
long st_mtime, st_ctime;
};
Here

(m)st_dev: It describe file has stored in which drive of your


computer.

(n)st_mode: It describes various modes of file like file is


read only, write only, folder, character file etc.

(o)st_size: It tells the size of file in byte.

(p)St_ctime:It tells last data of modification of


the file.

There are some macro has been defined in sys\stat.h

Name Meaning
S_IFMT File type mask
S_IFDIR Directory
S_IFIFO FIFO special
S_IFCHR Character special
S_IFBLK Block special
S_IFREG Regular file
S_IREAD Owner can read
S_IWRITE Owner can write
S_IEXEC Owner can execute

So masking or bitwise and operation between status.st_mode and


S_IFDIR return true if it is directory and so on

Write a c program to know read/write permission of given file.


#include "time.h"
#include "sys\stat.h"
#include "stdio.h"
void main(){
struct stat status;
FILE *fp;
stat("[Link]",&status);
clrscr();
if (status.st_mode & S_IREAD)
printf("You have read permission.\n");
if (status.st_mode & S_IWRITE)
printf("You have write permission.");
getch();

}
Explanation:
Function int stat(char *, struct stat *) store the
information of open file in form of structure struct stat

Structure struct stat has been defined in sys\stat.h as


struct stat {
short st_dev, st_ino;
short st_mode, st_nlink;
int st_uid, st_gid;
short st_rdev;
long st_size, st_atime;
long st_mtime, st_ctime;
};
Here
(i)st_dev: It describe file has stored in which drive of your
computer.

(j)st_mode: It describe various mode of file like file is


read only, write only, folder, character file etc.

(k)st_size: It tells the size of file in byte.

(l)St_ctime:It tells last data of modification of the file.

There are some macro has been defined in sys\stat.h

Name Meaning
S_IFMT File type mask
S_IFDIR Directory
S_IFIFO FIFO special
S_IFCHR Character special
S_IFBLK Block special
S_IFREG Regular file
S_IREAD Owner can read
S_IWRITE Owner can write
S_IEXEC Owner can execute

So masking or bit wise and operation between status.st_mode


and S_IREAD return true you have read permission and so on

Write a c program to know the last date of modification of any file


#include "time.h"
#include "sys\stat.h"
#include "stdio.h"
int main(){
struct stat status;
FILE *fp;
fp=fopen("[Link]","r");
fstat(fileno(fp),&status);

printf("Last date of modification :


%s",ctime(&status.st_ctime));
return 0;
}
Explanation:
Function int fstat(char *, struct stat *) store the
information of open file in form of structure struct stat

Structure struct stat has been defined in sys\stat.h as

struct stat {
short st_dev, st_ino;
short st_mode, st_nlink;
int st_uid, st_gid;
short st_rdev;
long st_size, st_atime;
long st_mtime, st_ctime;
};
Here

(e)st_dev: It describe file has stored in which drive of your


computer.

(f)st_mode: It describes various modes of file like file is


read only, write only, folder, character file etc.

(g)st_size: It tells the size of file in byte.

(h)st_ctime:It tells last data of modification of the file in


date format.

Function ctime convert date type in string format

Write a c program to find out the size and drive where file has stored of any given
file?
#include "time.h"
#include "sys\stat.h"
#include "stdio.h"
int main(){
struct stat status;
FILE *fp;
fp=fopen("[Link]","r");
fstat(fileno(fp),&status);
printf("Size of file : %d",status.st_size);
printf("Drive name : %c",65+status.st_dev);
return 0;
}

Explanation:
Function int fstat (char *, struct stat *) store the
information of open file in form of structure struct stat

Structure struct stat has been defined in sys\stat.h as

struct stat {
short st_dev, st_ino;
short st_mode, st_nlink;
int st_uid, st_gid;
short st_rdev;
long st_size, st_atime;
long st_mtime, st_ctime;
};
Here
(a)st_dev: It describe file has stored in which drive of your
computer ,it returns a number.

(b)st_mode: It describes various modes of file like file is


read only, write only, folder, character file etc.

(c)st_size: It tells the size of file in byte.

(d)st_ctime:It tells last data of modification of the file in


date format.

Note: 65 is ASCII value of A .So after adding status.st_dev


with 65 it will return appropriate drvie name as in your
computer.

Complex numbers definition

Complex numbers are two dimensional numbers i.e. it consists


real part and imaginary part.
Complex numbers are written as a + ib
Where a and b are real numbers and i2 = -1

Code 1:
1. How to use complex numbers in c
2. C language complex numbers

#include<stdio.h>
int main(){
int a,b;

printf("Enter any complex number in the format a+ib: ");


scanf("%d+i%d",&a,&b);
printf("Real part is: %d",a );
printf("\nImaginary part is: %d",b);

return 0;

Code 2:
1. C program to printf complex numbers

#include<stdio.h>
int main(){
int a,b;

printf("Enter the real part of complex number: ");


scanf("%d",&a);

printf("Enter the imaginary part of complex number: ");


scanf("%d",&b);

printf("\nComplex number is: %d%+di",a,b );

return 0;

Operation on complex numbers:

Addition of complex number


(a + ib) + (c + id) = (a+c) + i(b+d)

Subtraction of complex numbers


(a + ib) -(c + id) = (a+c) + i(b-d)

Multiplication of complex numbers


(a + ib) *(c + id) = (ac-bd) + i(bc+ad)

Divison of complex numbers


(a + ib) / c + id) = ((ac + bd) + i(bc-ad))/(c2 + d2)

ADDITION & SUBTRACTION OF TWO COMPLEX NUMBERS USING C


PROGRAM
C code:
1. Adding two complex numbers in c
2. C program for addition of two complex numbers
3. C program to add two complex numbers
#include<stdio.h>
int main(){
int a,b,c,d,x,y;
printf("\nEnter the first complex number:");
scanf("%d%d",&a,&b);
printf("\nEnter the second complex number:");
scanf("%d%d",&c,&d);
if(b<0)
printf("%d-i\n",a-b);
else
printf("d+i\n",a+b);
if(d<0)
printf("d-i\n",c-d);
else
printf("%d+i\n",c+d);
printf("\nADDITION ");
x=a+c;
y=b+d;
if(y>0)
printf("%d-i%d",x,-y);
else
printf("%d+i%d",x,+y);
printf("\n\nSUBTRACTION ");
x=a-c;
y=b-d;
if(y<0)
printf("%d-i%d",x,-y);
else
printf("%d+i%d",x,+y);
return 0;
}

Series:
Write a c program to find out the sum of series 1 + 2 + …. + n.

Sum of 1 + 2 + …. + n series in c programming language

#include<stdio.h>
int main(){

int n,i;
int sum=0;

printf("Enter the n i.e. max values of series: ");


scanf("%d",&n);

sum = (n * (n + 1)) / 2;
printf("Sum of the series: ");

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


if (i!=n)
printf("%d + ",i);
else
printf("%d = %d ",i,sum);
}

return 0;
}

Sample output:

Enter the n i.e. max values of series: 5


Sum of the series: 1 + 2 + 3 + 4 + 5 = 15

Mathematical Formula:

Sum of the series 1 + 2 + 3 + … + n = n (n+1)/2

Write a c program to find out the sum of series 1^2 + 2^2 + …. + n^2.

Sum of 1^2 + 2^2 + …. + n^2 series in c programming language

#include<stdio.h>

int main(){

int n,i;
int sum=0;

printf("Enter the n i.e. max values of series: ");


scanf("%d",&n);

sum = (n * (n + 1) * (2 * n + 1 )) / 6;

printf("Sum of the series : ");

for(i =1;i<=n;i++){
if (i != n)
printf("%d^2 + ",i);
else
printf("%d^2 = %d ",i,sum);
}

return 0;
}
Sample output:

Enter the n i.e. max values of series: 5


Sum of the series: 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55

Mathematical Formula:

Sum of the series 12 + 22 + 32 + … + n2 =


n (n+1) (2n+1)/6

Write a c program to find out the sum of series 1^3 + 2^3 + …. + n^3
Write a c program or code to find out the sum of series 1^3 +
2^3 + …. + n^3 that is sum of cube of n natural numbers.

#include<stdio.h>
#include<math.h>

int main(){

int n,i;
int sum=0;

printf("Enter the n i.e. max values of series: ");


scanf("%d",&n);

sum = pow(((n * (n + 1) ) / 2),2);

printf("Sum of the series : ");

for(i =1;i<=n;i++){
if (i != n)
printf("%d^3 + ",i);
else
printf("%d^3 = %d ",i,sum);
}

return 0;
}

Sample output:
Enter the n i.e. max values of series: 3
Sum of the series: 1^3 + 2^3 + 3^3 = 36

Mathematical Formula:
Sum of the series 13 + 23 + 33 + … + n3 = (n (n+1)/2)2

Write a c program to find out the sum of in A.P. series


C Code:
#include<stdio.h>
#include<math.h>

int main(){

int a,d,n,i,tn;
int sum=0;

printf("Enter the first number of the A.P. series: ");


scanf("%d",&a);

printf("Enter the total numbers in the A.P. series: ");


scanf("%d",&n);

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


scanf("%d",&d);

sum = ( n * ( 2 * a + ( n -1 ) * d ) )/ 2;
tn = a + (n-1) * d;
printf("Sum of the series A.P.: ");

for(i=a;i<=tn; i= i + d ){
if (i != tn)
printf("%d + ",i);
else
printf("%d = %d ",i,sum);
}

return 0;
}

Sample output:

Enter the first number of the A.P. series: 1


Enter the total numbers in the A.P. series: 5
Enter the common difference of A.P. series: 3
Sum of the series: 1 + 4 + 7 + 10 + 13 = 35

Definition of arithmetic progression (A.P.):

A series of numbers in which difference of any two consecutive


numbers is always a same number that is constant. This
constant is called as common difference.

Example of A.P. series:


5 10 15 20 25 …
Here common difference is 5 since difference of any two
consecutive numbers for example 20 – 15 or 25 -20 is 5.

Sum of A.P. series:


Sn = n/2(2a + (n-1) d)

Tn term of A.P. series:

Tn = a + (n-1) d

Write a c program to find out the sum of G.P series


Sum of GP series in c programming language

#include<stdio.h>
#include<math.h>

int main(){

float a,r,i,tn;
int n;
float sum=0;

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


scanf("%f",&a);

printf("Enter the total numbers in the G.P. series: ");


scanf("%d",&n);

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


scanf("%f",&r);

sum = (a*(1 - pow(r,n+1)))/(1-r);


tn = a * (1 -pow(r,n-1));

printf("tn term of G.P.: %f",tn);


printf("\nSum of the G.P.: %f",sum);

return 0;
}

Sample output:

Enter the first number of the G.P. series: 1


Enter the total numbers in the G.P. series: 5
Enter the common ratio of G.P. series: 2
tn term of G.P. : 16.000000
Sum of the G.P. : 63.000000

Definition of geometric progression (G.P.):


A series of numbers in which ratio of any two consecutive
numbers is always a same number that is constant. This
constant is called as common ratio.

Example of G.P. series:

2 4 8 16 32 64
Here common difference is 2 since ratio any two consecutive
numbers for example 32 / 16 or 64/32 is 2.

Sum of G.P. series:


Sn =a(1–rn+1)/(1-r)

Tn term of G.P. series:

Tn = arn-1

Sum of infinite G.P. series:

Sn = a/(1-r) if 1 > r
= a/(r-1) if r > 1

Array:
C program to find the largest element in an array

#include<stdio.h>
int main(){
int a[50],size,i,big;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ”, size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("\nBiggest: %d",big);
return 0;
}

C program to find the second largest element in an array

#include<stdio.h>
int main(){
int a[50],size,i,j=0,big,secondbig;
printf("Enter the size of the array: ");
scanf("%d",&size);
printf("Enter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);

big=a[0];
for(i=1;i<size;i++){
if(big<a[i]){
big=a[i];
j = i;
}
}

secondbig=a[size-j-1];
for(i=1;i<size;i++){
if(secondbig <a[i] && j != i)
secondbig =a[i];
}

printf("Second biggest: %d", secondbig);


return 0;
}

Sample output:
Enter the size of the array: 5
Enter 5 elements in to the array: 5 3 2 1 0
Second biggest: 3

C program to find the second smallest element in an array

#include<stdio.h>
int main(){
int a[50],size,i,j=0,small,secondsmall;
printf("Enter the size of the array: ");
scanf("%d",&size);
printf("Enter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);

small=a[0];
for(i=1;i<size;i++){
if(small>a[i]){
small=a[i];
j = i;
}
}
secondsmall=a[size-j-1];
for(i=1;i<size;i++){
if(secondsmall > a[i] && j != i)
secondsmall =a[i];
}

printf("Second smallest: %d", secondsmall);


return 0;
}

Enter the size of the array: 5


Enter 5 elements in to the array: 5 7 3 2 6
Second smallest: 3

REMOVE DUPLICATE ELEMENTS IN AN ARRAY USING C PROGRAM

#include<stdio.h>
int main(){
int arr[50];
int *p;
int i,j,k,size,n;
printf("\nEnter size of the array: ");
scanf("%d",&n);
printf("\nEnter %d elements into the array: ",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
size=n;
p=arr;
for(i=0;i<size;i++){
for(j=0;j<size;j++){
if(i==j){
continue;
}
else if(*(p+i)==*(p+j)){
k=j;
size--;
while(k < size){
*(p+k)=*(p+k+1);
k++;
}
j=0;
}
}
}
printf("\nThe array after removing duplicates is: ");
for(i=0;i < size;i++){
printf(" %d",arr[i]);
}
return 0;
}

DELETE ELEMENT FROM AN ARRAY AT DESIRED POSITION USING C


Write a program (wap) to delete an element at desired
positionfrom an array in c language

#include<stdio.h>
int main(){
int a[50],i,pos,size;
printf("\nEnter size of the array: ");
scanf("%d",&size);

printf("\nEnter %d elements in to the array: ",size);


for(i=0;i<size;i++)
scanf("%d",&a[i]);

printf("\nEnter position where to delete: ");


scanf("%d",&pos);

i=0;
while(i!=pos-1)
i++;
while(i<10){
a[i]=a[i+1];
i++;
}

size--;
for(i=0;i<size;i++)
printf(" %d",a[i]);

return 0;
}

INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION USING C


PROGRAM
How to insert or add an element in the array at specific or
desired posting by using c programming language? Source code
is as follow:

#include<stdio.h>
int main(){
int a[50],size,num,i,pos,temp;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ",size);
for(i=0;iscanf("%d",&a[i]);
printf("\nEnter position and number to insert: ");
scanf("%d %d",&pos,&num);
i=0;
while(i!=pos-1)
i++;
temp=size++;
while(i{
a[temp]=a[temp-1];
temp--;
}
a[i]=num;
for(i=0;iprintf(" %d",a[i]);
return 0;
}

C program to find the largest element in an array

#include<stdio.h>
int main(){
int a[50],size,i,big;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ”, size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("\nBiggest: %d",big);
return 0;
}

SORTING:
C program to find the largest element in an array

#include<stdio.h>
int main(){
int a[50],size,i,big;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ”, size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("\nBiggest: %d",big);
return 0;
}

Source code of simple bubble sort implementation using array


ascending order in c programming language

#include<stdio.h>
int main(){

int s,temp,i,j,a[20];

printf("Enter total numbers of elements: ");


scanf("%d",&s);

printf("Enter %d elements: ",s);


for(i=0;i<s;i++)
scanf("%d",&a[i]);

//Bubble sorting algorithm


for(i=s-2;i>=0;i--){
for(j=0;j<=i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf("After sorting: ");


for(i=0;i<s;i++)
printf(" %d",a[i]);

return 0;
}

Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9
After sorting: 0 2 6 9 11
Source code of simple insertion sort implementation using
array in ascending order in c programming language

#include<stdio.h>
int main(){

int i,j,s,temp,a[20];

printf("Enter total elements: ");


scanf("%d",&s);

printf("Enter %d elements: ",s);


for(i=0;i<s;i++)
scanf("%d",&a[i]);

for(i=1;i<s;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}

printf("After sorting: ");


for(i=0;i<s;i++)
printf(" %d",a[i]);

return 0;
}

Output:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting: 0 2 3 7 9

Source code of simple Selection sort implementation using


array ascending order in c programming language

#include<stdio.h>
int main(){

int s,i,j,temp,a[20];

printf("Enter total elements: ");


scanf("%d",&s);

printf("Enter %d elements: ",s);


for(i=0;i<s;i++)
scanf("%d",&a[i]);

for(i=0;i<s;i++){
for(j=i+1;j<s;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

printf("After sorting is: ");


for(i=0;i<s;i++)
printf(" %d",a[i]);

return 0;
}

Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is: 0 4 5 7 21

Source code of simple quick sort implementation using array


ascending order in c programming language

#include<stdio.h>

void quicksort(int [10],int,int);

int main(){
int x[20],size,i;

printf("Enter size of the array: ");


scanf("%d",&size);

printf("Enter %d elements: ",size);


for(i=0;i<size;i++)
scanf("%d",&x[i]);

quicksort(x,0,size-1);

printf("Sorted elements: ");


for(i=0;i<size;i++)
printf(" %d",x[i]);

return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}

Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8

Source code of simple merge sort implementation using array in


ascending order in c programming language

#include<stdio.h>
#define MAX 50

void mergeSort(int arr[],int low,int mid,int high);


void partition(int arr[],int low,int high);

int main(){

int merge[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);

printf("Enter the elements which to be sort: ");


for(i=0;i<n;i++){
scanf("%d",&merge[i]);
}

partition(merge,0,n-1);

printf("After merge sorting elements are: ");


for(i=0;i<n;i++){
printf("%d ",merge[i]);
}

return 0;
}

void partition(int arr[],int low,int high){

int mid;

if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}

void mergeSort(int arr[],int low,int mid,int high){

int i,m,k,l,temp[MAX];

l=low;
i=low;
m=mid+1;

while((l<=mid)&&(m<=high)){

if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}

for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}

Sample output:

Enter the total number of elements: 5


Enter the elements which to be sort: 2 5 0 9 1
After merge sorting elements are: 0 1 2 5 9

Recursion:

1. Factorial program by recursion in c

2. Factorial program in c using recursion

3. C program to calculate factorial using recursion

4. Recursive function for factorial in c

#include<stdio.h>
int fact(int);
int main(){
int num,f;
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
return 0;
}

int fact(int n){


if(n==1)
return 1;
else
return(n*fact(n-1));
}

C code to find the addition of n numbers by recursion:

#include<stdio.h>

int main(){

int n,sum;

printf("Enter the value of n: ");


scanf("%d",&n);

sum = getSum(n);

printf("Sum of n numbers: %d",sum);

return 0;
}

int getSum(n){

static int sum=0;

if(n>0){
sum = sum + n;
getSum(n-1);
}

return sum;
}

Sample output:

Enter the value of n: 10


Sum of n numbers: 55

C code to find the addition of n numbers without using


recursion:
#include<stdio.h>

int main(){

int n,sum;

printf("Enter the value of n: ");


scanf("%d",&n);

sum = getSum(n);

printf("Sum of n numbers: %d",sum);

return 0;
}

int getSum(n){

int sum=0;

while(n>0){
sum = sum + n;
n--;
}

return sum;
}

C code to multiply two matrix by recursion:


#include<stdio.h>
#define MAX 10

void multiplyMatrix(int [MAX][MAX],int [MAX][MAX]);


int m,n,o,p;
int c[MAX][MAX];

int main(){

int a[MAX][MAX],b[MAX][MAX],i,j,k;

printf("Enter the row and column of first matrix: ");


scanf("%d %d",&m,&n);
printf("Enter the row and column of second matrix: ");
scanf("%d %d",&o,&p);

if(n!=o){

printf("Matrix multiplication is not possible");


printf("\nColumn of first matrix must be same as row
of second matrix");
}
else{

printf("Enter the First matrix: ");


for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

printf("Enter the Second matrix: ");


for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);

printf("\nThe First matrix is: \n");


for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}

printf("\nThe Second matrix is: \n");


for(i=0;i<o;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",b[i][j]);
}
}

multiplyMatrix(a,b);

printf("\nThe multiplication of two matrix is: \n");


for(i=0;i<m;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",c[i][j]);
}
}
return 0;
}

void multiplyMatrix(int a[MAX][MAX],int b[MAX][MAX]){

static int sum,i=0,j=0,k=0;

if(i<m){ //row of first matrix


if(j<p){ //column of second matrix
if(k<n){
sum=sum+a[i][k]*b[k][j];
k++;
multiplyMatrix(a,b);
}
c[i][j]=sum;
sum=0;
k=0;
j++;
multiplyMatrix(a,b);
}
j=0;
i++;
multiplyMatrix(a,b);
}
}

Sample output:

Enter the row and column of first matrix: 2 2


Enter the row and column of second matrix: 2 2
Enter the First matrix: 1 2 3 4
Enter the Second matrix: 2 3 4 5

The First matrix is:

1 2
3 4
The Second matrix is:

2 3
4 5
The multiplication of two matrix is:

10 13
22 29

C code to multiply two matrix without using recursion:

#include<stdio.h>
#define MAX 10

void multiplyMatrix(int [MAX][MAX],int [MAX][MAX]);


int m,n,o,p;
int c[MAX][MAX];

int main(){

int a[MAX][MAX],b[MAX][MAX],i,j,k;
printf("Enter the row and column of first matrix: ");
scanf("%d %d",&m,&n);
printf("Enter the row and column of second matrix: ");
scanf("%d %d",&o,&p);

if(n!=o){
printf("Matrix multiplication is not possible");
printf("\column of first matrix must be same as row
of second matrix");
}
else{

printf("Enter the First matrix: ");


for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

printf("Enter the Second matrix: ");


for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);

printf("\nThe First matrix is: \n");


for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}

printf("\nThe Second matrix is: \n");


for(i=0;i<o;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",b[i][j]);
}
}

multiplyMatrix(a,b);
}

printf("\nThe multiplication of two matrix is: \n");


for(i=0;i<m;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",c[i][j]);
}
}
return 0;
}

void multiplyMatrix(int a[MAX][MAX],int b[MAX][MAX]){

int sum,i=0,j=0,k=0;

while(i<m){ //row of first matrix


j=0;
while(j<p){ //column of second matrix
k=0;
sum=0;
while(k<n){
sum=sum+a[i][k]*b[k][j];
k++;
}
c[i][j]=sum;
j++;
}
i++;
}
}

C code to multiply two numbers by recursion:

#include<stdio.h>

int multiply(int,int);

int main(){

int a,b,product;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);

product = multiply(a,b);

printf("Multiplication of two integers is %d",product);

return 0;
}

int multiply(int a,int b){

static int product=0,i=0;

if(i < a){


product = product + b;
i++;
multiply(a,b);
}

return product;
}

Sample output:

Enter any two integers: 5 8


Multiplication of two integers is 40

C code to multiply two numbers without using recursion:

#include<stdio.h>

int multiply(int,int);

int main(){

int a,b,product;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);

product = multiply(a,b);

printf("Multiplication of two integers is %d",product);

return 0;
}

int multiply(int a,int b){

int product =0,i=0;

while(i < a){


product = product + b;
i++;
}

return product;
}

C code to get LCM of two numbers by recursion:

#include<stdio.h>

int lcm(int,int);

int main(){
int a,b,l;
printf("Enter any two positive integers ");
scanf("%d%d",&a,&b);

if(a>b)
l = lcm(a,b);
else
l = lcm(b,a);

printf("LCM of two integers is %d",l);

return 0;
}

int lcm(int a,int b){

static int temp = 1;

if(temp % b == 0 && temp % a == 0)


return temp;
temp++;
lcm(a,b);

return temp;
}

Sample output:

Enter any two positive integers 5 2


LCM of two integers is 10

C code to get LCM of two numbers without using recursion:

#include<stdio.h>
#define MAX 100

int getMaxElement(int []);


#include<stdio.h>

int lcm(int,int);

int main(){

int a,b,l;

printf("Enter any two positive integers ");


scanf("%d%d",&a,&b);

if(a>b)
l = lcm(a,b);
else
l = lcm(b,a);

printf("LCM of two integers is %d",l);

return 0;
}

int lcm(int a,int b){

int temp = a;

while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}

return temp;
}

C code to get largest element of an array by recursion:

#include<stdio.h>
#define MAX 100

int getMaxElement(int []);


int size;

int main(){

int arr[MAX],max,i;

printf("Enter the size of the array: ");


scanf("%d",&size);

printf("Enter %d elements of an array: ", size);


for(i=0;i<size;i++)
scanf("%d",&arr[i]);

max=getMaxElement(arr);

printf("Largest element of an array is: %d",max);

return 0;
}

int getMaxElement(int arr[]){

static int i=0,max =-9999;


if(i < size){
if(max<arr[i])
max=arr[i];
i++;
getMaxElement(arr);
}

return max;
}

Sample output:

Enter the size of the array: 5


Enter 5 elements of an array: 1 4 5 6 2
Largest element of an array is: 6

C code to get largest element of an array without recursion:

#include<stdio.h>
#define MAX 100

int getMaxElement(int []);


int size;

int main(){

int arr[MAX],max,i;

printf("Enter the size of the array: ");


scanf("%d",&size);

printf("Enter %d elements of an array: ", size);


for(i=0;i<size;i++)
scanf("%d",&arr[i]);

max=getMaxElement(arr);

printf("Largest element of an array is: %d",max);

return 0;
}

int getMaxElement(int arr[]){

int i=1,max;

max=arr[0];

while(i < size){


if(max<arr[i])
max=arr[i];
i++;
}

return max;
}

C code to check a number is prime number or not by recursion:

#include<stdio.h>

int isPrime(int,int);

int main(){

int num,prime;

printf("Enter a positive number: ");


scanf("%d",&num);

prime = isPrime(num,num/2);

if(prime==1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);

return 0;
}

int isPrime(int num,int i){

if(i==1){
return 1;
}else{
if(num%i==0)
return 0;
else
isPrime(num,i-1);
}
}

Sample output:

Enter a positive number: 13


13 is a prime number

C code to check a number is prime number or not without


recursion:
#include<stdio.h>

int isPrime(int);

int main(){

int num,prime;

printf("Enter a positive number: ");


scanf("%d",&num);

prime = isPrime(num);

if(prime==1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);

return 0;
}

int isPrime(int num){

int i=2;

while(i<=num/2){
if(num%i==0)
return 0;
else
i++;
}

return 1;
}

C code to convert decimal number to binary number by


recursion:

#include<stdio.h>

long toBinary(int);

int main(){

long binaryNo;
int decimalNo;

printf("Enter any decimal number: ");


scanf("%d",&decimalNo);
binaryNo = toBinary(decimalNo);
printf("Binary value is: %ld",binaryNo);

return 0;
}

long toBinary(int decimalNo){

static long binaryNo,remainder,factor = 1;

if(decimalNo != 0){

remainder = decimalNo % 2;
binaryNo = binaryNo + remainder * factor;
factor = factor * 10;
toBinary(decimalNo / 2);
}

return binaryNo;
}

Sample output:

Enter any decimal number: 10


Binary value is: 1010

C code to convert decimal number to binary number without


recursion:

#include<stdio.h>

long toBinary(int);

int main(){

long binaryNo;
int decimalNo;

printf("Enter any decimal number: ");


scanf("%d",&decimalNo);

binaryNo = toBinary(decimalNo);
printf("Binary value is: %ld",binaryNo);

return 0;
}

long toBinary(int decimalNo){

long binaryNo,remainder,factor = 1;
while(decimalNo != 0){
remainder = decimalNo % 2;
binaryNo = binaryNo + remainder * factor;
factor = factor * 10;
decimalNo = decimalNo / 2;
}

return binaryNo;
}

1. C code to print Fibonacci series by recursion


2. Fibonacci series in c by using recursion

3. C code for Fibonacci series using recursion


4. Program to generate Fibonacci series using recursion in c

#include<stdio.h>

void printFibonacci(int);

int main(){

int k,n;
long int i=0,j=1,f;

printf("Enter the range of the Fibonacci series: ");


scanf("%d",&n);

printf("Fibonacci Series: ");


printf("%d %d ",0,1);
printFibonacci(n);

return 0;
}

void printFibonacci(int n){

static long int first=0,second=1,sum;

if(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}

Sample output:
Enter the range of the Fibonacci series: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89

C code to print Fibonacci series without recursion:

#include<stdio.h>

void printFibonacci(int);

int main(){

int k,n;
long int i=0,j=1,f;

printf("Enter the range of the Fibonacci series: ");


scanf("%d %d ",&n);

printf("Fibonacci Series: ");


printf("%d ",0);
printFibonacci(n);

return 0;
}

void printFibonacci(int n){

long int first=0,second=1,sum;

while(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
n--;
}
}

C code to reverse a string by recursion:

#include<stdio.h>
#define MAX 100
char* getReverse(char[]);

int main(){

char str[MAX],*rev;

printf("Enter any string: ");


scanf("%s",str);
rev = getReverse(str);

printf("Reversed string is: %s",rev);


return 0;
}

char* getReverse(char str[]){

static int i=0;


static char rev[MAX];

if(*str){
getReverse(str+1);
rev[i++] = *str;
}

return rev;
}

Sample output:

Enter any string: mona


Reversed string is: anom

1. Factorial program by recursion in c

2. Factorial program in c using recursion

3. C program to calculate factorial using recursion

4. Recursive function for factorial in c

#include<stdio.h>
int fact(int);
int main(){
int num,f;
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
return 0;
}

int fact(int n){


if(n==1)
return 1;
else
return(n*fact(n-1));
}

Find gcd of a number using recursion in c program

#include<stdio.h>
int main(){
int n1,n2,gcd;
printf("\nEnter two numbers: ");
scanf("%d %d",&n1,&n2);
gcd=findgcd(n1,n2);
printf("\nGCD of %d and %d is: %d",n1,n2,gcd);
return 0;
}

int findgcd(int x,int y){


while(x!=y){
if(x>y)
return findgcd(x-y,y);
else
return findgcd(x,y-x);
}
return x;
}

Sum of digits in c using recursion

#include<stdio.h>
int main(){
int num,x;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
x=findsum(num);
printf("Sum of the digits of %d is: %d",num,x);
return 0;
}

int r,s;
int findsum(int n){
if(n){
r=n%10;
s=s+r;
findsum(n/10);
}
else
return s;
}

Find power of a number using recursion using c program

#include<stdio.h>
int main(){
int pow,num;
long int res;
long int power(int,int);
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
res=power(num,pow);
printf("\n%d to the power %d is: %ld",num,pow,res);
return 0;
}
int i=1;
long int sum=1;
long int power(int num,int pow){
if(i<=pow){
sum=sum*num;
power(num,pow-1);
}
else
return sum;
}

Write a simple code for binary search using function recursion


in c programming language

#include<stdio.h>
int main(){

int a[10],i,n,m,c,l,u;

printf("Enter the size of an array: ");


scanf("%d",&n);

printf("Enter the elements of the array: " );


for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);

l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf("Number is not found.");
else
printf("Number is found.");

return 0;
}

int binary(int a[],int n,int m,int l,int u){

int mid,c=0;

if(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
}
else if(m<a[mid]){
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}

Sample output:
Enter the size of an array: 5
Enter the elements of the array: 8 9 10 11 12
Enter the number to be search: 8
Number is found.

POINTER:
Concatenation of two strings using pointer in c programming
language

#include<stdio.h>
int main(){
int i=0,j=0;
char *str1,*str2,*str3;
puts("Enter first string");
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before concatenation the strings are\n");
puts(str1);
puts(str2);
while(*str1){
str3[i++]=*str1++;
}
while(*str2){
str3[i++]=*str2++;
}
str3[i]='\0';
printf("After concatenation the strings are\n");
puts(str3);
return 0;
}

1. Write a simple code for linear search in c programming


language
2. Wap a c program to search an element in an array using
linear search

#include<stdio.h>
int main(){

int a[10],i,n,m,c=0;

printf("Enter the size of an array: ");


scanf("%d",&n);

printf("Enter the elements of the array: ");


for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}

printf("Enter the number to be search: ");


scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");
return 0;
}

Sample output:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found

1. Write a simple code for binary search in c programming


language
2. Wap a c program to search an element in an array using
binary search

#include<stdio.h>
int main(){

int a[10],i,n,m,c=0,l,u,mid;

printf("Enter the size of an array: ");


scanf("%d",&n);

printf("Enter the elements in ascending order: ");


for(i=0;i<n;i++){
scanf("%d",&a[i]);
}

printf("Enter the number to be search: ");


scanf("%d",&m);

l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
break;
}
else if(m<a[mid]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
return 0;
}

Sample output:
Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11
The number is found.

Write a simple code for binary search using function recursion


in c programming language

#include<stdio.h>
int main(){

int a[10],i,n,m,c,l,u;

printf("Enter the size of an array: ");


scanf("%d",&n);

printf("Enter the elements of the array: " );


for(i=0;i<n;i++){
scanf("%d",&a[i]);
}

printf("Enter the number to be search: ");


scanf("%d",&m);

l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf("Number is not found.");
else
printf("Number is found.");

return 0;
}

int binary(int a[],int n,int m,int l,int u){

int mid,c=0;

if(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
}
else if(m<a[mid]){
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}

Sample output:
Enter the size of an array: 5
Enter the elements of the array: 8 9 10 11 12
Enter the number to be search: 8
Number is found.

You might also like