C Program
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.
Recursion
Example of recursion in c programming
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.
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
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
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.
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
Output:
Sum of prime numbers is: 1060
Sample output:
Enter min range: 50
Enter max range: 100
Sum of prime numbers is: 732
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.
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
Sample output:
Enter the minimum range: 1
Enter the maximum range: 100
Sum of odd numbers in given range is: 2500
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.
#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
Sample output:
Enter a number: 1221
1221 is a palindrome
#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
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
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
Sample output:
Enter the number range: 4
Sum of Fibonacci series is: 4
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
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
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
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
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
#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
#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
Sample output:
Enter a number: 45
Sum of digits of number: 9
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;
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;
}
#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;
}
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;
}
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;
}
Sample output:
Enter any two integers: 5 10
Before swapping: a = 5, b=10
After swapping: a = 10, b=10
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
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
#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;
}
#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;
}
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;
}
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;
}
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
Sample output:
Enter any decimal number: 50
Equivalent octal value of decimal number 50: 62
#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
#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
#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
#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
Sample output:
Enter any number any binary number: 1111111111111111111
1111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111
11111111
Equivalent octal value: 3777777777777777777777777777777
7777777777777777777777777777777777777777777777777777
#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
Sample output:
Enter any number any binary number: 1101
Equivalent hexadecimal value: D
Sample output:
Enter any number any binary number: 1010011011100011110
001001111011110001000100011101110111011110
Equivalent hexadecimal value: 14DC789EF111DDDE
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
Sample output:
Enter any fractional binary number: 11.11
Equivalent decimal value: 3.750000
Sample output:
Enter any fractional decimal number: 5.7
Equivalent binary value: 101.101100
Sample output:
Enter any natural number: 87
Roman number will be: LXXXVII
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
#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
#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;
}
Sample output:
Enter a string: Robert De Niro
RDN
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
Sample output:
Enter any string: mona
Reversed string is: anom
Sample output:
Enter first string: cquestionbank
Enter second string: @[Link]
String after concatenation: cquestionbank@[Link]
Sample output:
Enter first string: [Link]
Enter second string: [Link]
Both strings are equal.
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;
}
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;
}
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;
}
The matrix is
3 5 2
1 5 8
3 9 2
Inverse of matrix is:
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
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
Sample output:
Enter the 4 elements of first matrix: 1
2
3
4
Enter the 4 elements of second matrix: 5
6
7
8
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]));
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
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
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
}
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
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
}
Explanation:
Function int stat(char *, struct stat *) store the
information of open file in form of structure struct stat
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
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
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
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.
Code 1:
1. How to use complex numbers in c
2. C language complex numbers
#include<stdio.h>
int main(){
int a,b;
return 0;
Code 2:
1. C program to printf complex numbers
#include<stdio.h>
int main(){
int a,b;
return 0;
Series:
Write a c program to find out the sum of series 1 + 2 + …. + n.
#include<stdio.h>
int main(){
int n,i;
int sum=0;
sum = (n * (n + 1)) / 2;
printf("Sum of the series: ");
return 0;
}
Sample output:
Mathematical Formula:
Write a c program to find out the sum of series 1^2 + 2^2 + …. + n^2.
#include<stdio.h>
int main(){
int n,i;
int sum=0;
sum = (n * (n + 1) * (2 * n + 1 )) / 6;
for(i =1;i<=n;i++){
if (i != n)
printf("%d^2 + ",i);
else
printf("%d^2 = %d ",i,sum);
}
return 0;
}
Sample output:
Mathematical Formula:
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;
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
int main(){
int a,d,n,i,tn;
int sum=0;
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:
Tn = a + (n-1) d
#include<stdio.h>
#include<math.h>
int main(){
float a,r,i,tn;
int n;
float sum=0;
return 0;
}
Sample output:
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.
Tn = arn-1
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;
}
#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];
}
Sample output:
Enter the size of the array: 5
Enter 5 elements in to the array: 5 3 2 1 0
Second biggest: 3
#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];
}
#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;
}
#include<stdio.h>
int main(){
int a[50],i,pos,size;
printf("\nEnter size of the array: ");
scanf("%d",&size);
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;
}
#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;
}
#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;
}
#include<stdio.h>
int main(){
int s,temp,i,j,a[20];
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];
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;
}
return 0;
}
Output:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting: 0 2 3 7 9
#include<stdio.h>
int main(){
int s,i,j,temp,a[20];
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;
}
}
}
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
#include<stdio.h>
int main(){
int x[20],size,i;
quicksort(x,0,size-1);
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
#include<stdio.h>
#define MAX 50
int main(){
int merge[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);
partition(merge,0,n-1);
return 0;
}
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,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:
Recursion:
#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;
}
#include<stdio.h>
int main(){
int n,sum;
sum = getSum(n);
return 0;
}
int getSum(n){
if(n>0){
sum = sum + n;
getSum(n-1);
}
return sum;
}
Sample output:
int main(){
int n,sum;
sum = getSum(n);
return 0;
}
int getSum(n){
int sum=0;
while(n>0){
sum = sum + n;
n--;
}
return sum;
}
int main(){
int a[MAX][MAX],b[MAX][MAX],i,j,k;
if(n!=o){
multiplyMatrix(a,b);
Sample output:
1 2
3 4
The Second matrix is:
2 3
4 5
The multiplication of two matrix is:
10 13
22 29
#include<stdio.h>
#define MAX 10
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{
multiplyMatrix(a,b);
}
int sum,i=0,j=0,k=0;
#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);
return 0;
}
return product;
}
Sample output:
#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);
return 0;
}
return product;
}
#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);
return 0;
}
return temp;
}
Sample output:
#include<stdio.h>
#define MAX 100
int lcm(int,int);
int main(){
int a,b,l;
if(a>b)
l = lcm(a,b);
else
l = lcm(b,a);
return 0;
}
int temp = a;
while(1){
if(temp % b == 0 && temp % a == 0)
break;
temp++;
}
return temp;
}
#include<stdio.h>
#define MAX 100
int main(){
int arr[MAX],max,i;
max=getMaxElement(arr);
return 0;
}
return max;
}
Sample output:
#include<stdio.h>
#define MAX 100
int main(){
int arr[MAX],max,i;
max=getMaxElement(arr);
return 0;
}
int i=1,max;
max=arr[0];
return max;
}
#include<stdio.h>
int isPrime(int,int);
int main(){
int num,prime;
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;
}
if(i==1){
return 1;
}else{
if(num%i==0)
return 0;
else
isPrime(num,i-1);
}
}
Sample output:
int isPrime(int);
int main(){
int num,prime;
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 i=2;
while(i<=num/2){
if(num%i==0)
return 0;
else
i++;
}
return 1;
}
#include<stdio.h>
long toBinary(int);
int main(){
long binaryNo;
int decimalNo;
return 0;
}
if(decimalNo != 0){
remainder = decimalNo % 2;
binaryNo = binaryNo + remainder * factor;
factor = factor * 10;
toBinary(decimalNo / 2);
}
return binaryNo;
}
Sample output:
#include<stdio.h>
long toBinary(int);
int main(){
long binaryNo;
int decimalNo;
binaryNo = toBinary(decimalNo);
printf("Binary value is: %ld",binaryNo);
return 0;
}
long binaryNo,remainder,factor = 1;
while(decimalNo != 0){
remainder = decimalNo % 2;
binaryNo = binaryNo + remainder * factor;
factor = factor * 10;
decimalNo = decimalNo / 2;
}
return binaryNo;
}
#include<stdio.h>
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
return 0;
}
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
#include<stdio.h>
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
return 0;
}
while(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
n--;
}
}
#include<stdio.h>
#define MAX 100
char* getReverse(char[]);
int main(){
char str[MAX],*rev;
if(*str){
getReverse(str+1);
rev[i++] = *str;
}
return rev;
}
Sample output:
#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;
}
#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;
}
#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;
}
#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;
}
#include<stdio.h>
int main(){
int a[10],i,n,m,c,l,u;
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 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;
}
#include<stdio.h>
int main(){
int a[10],i,n,m,c=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
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0,l,u,mid;
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.
#include<stdio.h>
int main(){
int a[10],i,n,m,c,l,u;
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 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.