0% found this document useful (0 votes)
157 views

Programs C

The document contains 6 code examples that demonstrate how to write C programs to check if a number is prime, perfect, or Armstrong. The codes use loops to divide a number by its factors and check if the sum of factors equals the number. The summaries provided define each type of special number and explain the logic and algorithms used in the C programs to identify these numbers.

Uploaded by

Amar A V W
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views

Programs C

The document contains 6 code examples that demonstrate how to write C programs to check if a number is prime, perfect, or Armstrong. The codes use loops to divide a number by its factors and check if the sum of factors equals the number. The summaries provided define each type of special number and explain the logic and algorithms used in the C programs to identify these numbers.

Uploaded by

Amar A V W
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 139

Find out the perfect number using c program

Code 1:
1. C program to check perfect number

#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

Code 2:
1. C program to find perfect numbers
2. C perfect number code
3. 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

Code 3:
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

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
Check the given number is armstrong number or not using c
program

Code 1:
1. Warp to check a number is Armstrong

2. C program to check whether a number is Armstrong or


not

3. Simple c program for Armstrong number

4. Armstrong number in c with output

#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

The time complexity of a program that determines


Armstrong number is: O (Number of digits)

Code 2:
1. Write a c program for Armstrong number

2. C program for Armstrong number generation

3. How to find Armstrong number in c

4. 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

Code 3:
1. 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

Code 4:
1. C program to print Armstrong numbers from 1 to 500

2. 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

Definition of Armstrong number or what is an 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

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.

Code 1:
1. C program to determine prime number
2. Determining if a number is prime in c
3. C program to find given number is prime or not

#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

Code 2:
1. C program for prime numbers between 1 to 100
2. How to find prime numbers from 1 to 100 in c
3. 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

Code 3:
1. C program for prime numbers between 1 to n
2. C program to find prime numbers up to n
3. C program to list prime numbers
4. Write a c program to generate n prime numbers
5. 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

Code 4:
1. C program to find prime numbers using while loop
2. Wap to find prime numbers in c
3. Write a c program to generate prime number
4. 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

Code 5:
1. How to find out prime numbers in c programming
2. Display prime numbers in c
3. C program to find prime numbers between two numbers
4. 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

Code 6:
1. 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

Code 7:
1. 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

1. Write a c program to check given number is perfect number


or not.
Sum of prime numbers is: 732
Write a c program to check given number is strong number or
not.

Code 1:
1. Write a c program to check whether a number is
strong or not

#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

Code 2:
1. C program for strong number
2. 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
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
C program for odd or even number

Code 1:
1. C program to check even or odd
2. C determine odd or even
3. How to check odd number in c
4. How to determine odd or even in c
5. 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.

Code 2:
1. Display odd numbers in c
2. 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

Code 3:
1. Even and odd numbers program in c
2. 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

Code 4:
1. 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

Code 5:
1. 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

Algorithm:
Number is called even number if it is divisible by two
otherwise odd.

Example of even numbers: 0,2,4,8,9,10 etc.


Example of odd numbers: 1, 3,5,7,9 etc.
Check the given number is palindrome number or not using c
program
Code 1:
1. Wap to check a number is palindrome
2. 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

Code 2:
1. Write a c program for palindrome
2. C program to find palindrome of a number
3. 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

Code 3:
1. 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

Code 4:
1. 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

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.
Write a c program to check given string is palindrome number
or not

#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;
}

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.

C program for solving quadratic equation

1. C program to calculate roots of a quadratic equation


2. 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
1. 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
TO FIND FIBONACCI SERIES USING C PROGRAM
Code 1:
1. Write a program to generate the Fibonacci series
in c
2. Write a program to print Fibonacci series in c
3. Basic c programs Fibonacci series
4. How to print Fibonacci series in c
5. How to find Fibonacci series in c programming
6. 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

Code 2:
1. Fibonacci series using array in c
2. Fibonacci series program in c language
3. Source code of Fibonacci series in c
4. 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

Code 3:
1. Fibonacci series in c using while loop
2. C program to calculate Fibonacci series
3. C program to display Fibonacci series
4. Fibonacci series in c with explanation
5. 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

Code 4:
1. 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

Algorithm:
What is Fibonacci series?

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.
TO FIND FACTORIAL OF A NUMBER USING C PROGRAM

Code 1:
1. C code for factorial of a number
2. C program to find the factorial of a given number
3. Factorial program in c using while loop
4. 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

Code 2:
1. Factorial program in c using for loop
2. Simple factorial program in c
3. 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;
}

Code 3:
1. Factorial program in c using pointers
2. How to calculate factorial in c
3. 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;
}

Code 4:
1. Factorial program in c using function
2. 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

Code 5:
1. 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

Algorithm:
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
Write a c program for Floyd’s triangle.
1. Write a c program to print Floyd’s triangle

2. C program to display Floyd’s triangle

3. How to print Floyd’s triangle in c

#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
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

What is Floyd’s triangle?

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
2 3
4 5 6
7 8 9 10

Example 2:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Write a c program to print Pascal triangle.
1. Pascal triangle in c without using array

2. C code to print Pascal triangle

3. Simple c program for Pascal triangle

4. C program to generate Pascal triangle

5. Pascal triangle program in c language

6. 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
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
TO FIND MULTIPLICATION TABLE USING C PROGRAM

1. Multiplication tables in c program


2. Write a c program to print multiplication table
3. Code for multiplication table in c
4. Multiplication table in c language
5. 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
PRINTING ASCII VALUE USING C PROGRAM

Printing ascii value using c program

C code for ASCII table

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
C program to print hello world without using semicolon

C program to print hello world without using semicolon


#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")){
}
}
write a c program which produces its own source code as its
output

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;
}

Reverse any number using c program

Code 1:
1. Write a c program to reverse a given number
2. C program to find reverse of a number
3. C program to reverse the digits of a number
4. 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

Code 2:
1. Reverse very large or big numbers beyond the range
of long int
2. 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

Code 3:
1. C program to reverse a number using for loop
2. How to find reverse of a number in c
3. 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

Code 4:
1. 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

Write a c program to find out sum of digit of given number

Code 1:
1. C program to add digits of a number
2. C program for sum of digits of a number
3. 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

Code 2:
1. 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

Code 3:
1. 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
FIND POWER OF A NUMBER USING C PROGRAM

How to calculate power of a number in c

How to write power in c

#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;
}

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

Algorithm:
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
Write a c program to subtract two numbers without using
subtraction operator

Write a c program or code to subtract two numbers


without using subtraction operator

#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
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;
}

Alogrithm:
**

Write a c program to find largest among three numbers using


conditional operator

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;
}

FIND OUT GENERIC ROOT OF A NUMBER By C


PROGRAM
C program for generic root

#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;
}

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

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
FIND PRIME FACTORS OF A NUMBER USING C
PROGRAM

Prime factor of a number in c

#include<stdio.h>
int main(){
int num,i=1,j,k;
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num){
k=0;
if(num%i==0){
j=1;
while(j<=i){
if(i%j==0)
k++;
j++;
}
if(k==2)
printf("\n%d is a prime factor",i);
}
i++;
}
return 0;
}
Write a c program to find out NCR factor of given number

Write a c program to find out NCR factor of given


number Or
C program to find the ncr value by using recursive
function

#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;
}
Algorithm:
In the mathematics nCr has defined as
n
Cr = n! /((n-r)!r!)
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
C program for swapping of two numbers
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;
}

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

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
Program to find largest of n numbers in c

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
Split number into digits in c programming

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
C program to count number of digits in a number

Code 1:
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

Code 2:
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

Code 3:
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
C program for Fibonacci series using recursion
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--;
}
}

Reverse a string using recursion in c


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
FIND FACTORIAL OF A NUMBER USING RECURSION IN C
PROGRAM

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
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;
}
FIND POWER OF A NUMBER USING RECURSION USING
C PROGRAM

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 c program to find out L.C.M. of two numbers.

LCM program in c with two numbers :


#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;
}
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;
}

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;
}

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.

Find g.c.d of two number using c program


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:

Write a c program for finding gcd (greatest common


divisor) of two given numbers

#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;
}

Other logic : 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;
}

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;
}
Find g.c.d of two number using c program
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:

Write a c program for finding gcd (greatest common


divisor) of two given numbers

#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;
}

Other logic : 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;
}

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;
}
SWAP TWO VARIABLES WITHOUT USING THIRD USING
C PROGRAM VARIABLE

Swapping in c without temporary variable

Swap 2 numbers without using third variable in c

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;
}
SWAP TWO VARIABLES WITHOUT USING THIRD USING
C PROGRAM VARIABLE
Swapping in c without temporary variable

Swap 2 numbers without using third variable in c

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;
}
Write a c program for swapping of two arrays

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;
}
SWAPPING OF STRINGS USING C PROGRAM

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;
}
CONVERSION FROM UPPERCASE TO LOWER CASE
USING C PROGRAM

Conversion from uppercase to lower case using c program

#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;
}

Algorithm:
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
Write a c program to convert the string from lower case to
upper case

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;
}

Algorithm:
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
CONCATENATION OF TWO STRINGS USING POINTER IN
C PROGRAM

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;
}
Write a c program to reverse a string

Reverse a string in c without using temp


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;
}

String reverse in c without using strrev


String reverse in c without using string function
How to reverse a string in c without using reverse
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 : cquestionbank.blogspot.com
Reverse of string is : moc.topsgolb.knabnoitseuqc

Reverse a string in c using pointers


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
String copy without using strcpy in c

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: cquestionbank.blogspot.com
After copying: cquestionbank.blogspot.com
How to compare two strings in c without using strcmp

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: cquestionbank.blogspot.com
Enter second string: cquestionbank.blogspot.com
Both strings are equal.
String concatenation in c without using strcat
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: @blogspot.com
String after concatenation: [email protected]
ADDITION OF TWO MATRICES USING C PROGRAM

C program for addition of two matrices using arrays


source code. Matrix addition in c language:

C code:
#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;
}

Algorithm:

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:
SUBTRACTION OF TWO MATRICES USING C PROGRAM
#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;
}

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:
MULTIPLICATION OF TWO MATRICES USING C
PROGRAM

1. C code for matrix multiplication

2. C program for matrix multiplication


3. Write a program for matrix multiplication in c
4. How to multiply two matrixes in c
5. Matrix multiplication program in c language
6. Matrix multiplication in c using array

#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;
}

Alogrithm:
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:

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
C program to find out the sum of infinite G.P. series:
geometric progression
Sum of infinite GP series in c programming language

#include<stdio.h>

int main(){

float a,r;
float sum=0;

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


");
scanf("%f",&a);

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


scanf("%f",&r);

if(1 > r)
sum = a/(1-r);
else
sum = a/(r-1);

printf("\nSum of the infinite G.P. series:


%f",sum);

return 0;
}

Sample output:

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


Enter the common ratio of G.P. series: .5

Sum of the infinite G.P. series: 2.000000

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


Enter the common ratio of G.P. series: 2

Sum of the infinite G.P. series: 5.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
FIND OUT LARGEST NUMBER IN AN ARRAY USING C
PROGRAM

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 largest and smallest number in an array

C code to find largest and smallest number in an array

#include<stdio.h>
int main(){
int a[50],size,i,big,small;

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("Largest element: %d",big);

small=a[0];
for(i=1;i<size;i++){
if(small>a[i])
small=a[i];
}
printf("Smallest element: %d",small);

return 0;
}

Sample Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1
BUBBLE SORT USING C PROGRAM

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
FIND FACTORIAL OF A NUMBER USING RECURSION IN C
PROGRAM

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

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;
}
FIND POWER OF A NUMBER USING RECURSION USING
C PROGRAM

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 c program to find the area of a right angled triangle
C program for area of right angled triangle

Formula of area of right angled triangle:

Area = (1/2) * height * width

C code:
#include<stdio.h>

int main(){

float h,w;
float area;

printf("Enter height and width of the right angled


triangle : ");
scanf("%f%f",&h,&w);

area = 0.5 * h * w;

printf("Area of right angled triangle is:


%.3f",area);

return 0;
}

Sample output:

Enter height and width of the right angled triangle: 10


5
Area of right angled triangle is: 25.000
Write a c program to find the area of an equilateral triangle
C program for area of equilateral triangle

Formula of area of equilateral triangle:


Area = (√3)/4 * a2
C code:

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

int main(){

float a;
float area;

printf("Enter size of side of the equilateral


triangle : ");
scanf("%f",&a);

area = sqrt(3)/4*(a*a);

printf("Area of equilateral triangle is:


%.3f",area);

return 0;
}

Sample output:

Enter size of side of the equilateral triangle: 5


Area of equilateral triangle is: 10.825
C PROGRAM TO CALCULATE AREA OF A CIRCLE

C program for area of circle

#include <stdio.h>
#define PI 3.141
int main(){
float r, a;
printf("Radius: ");
scanf("%f", &r);
a = PI * r * r;
printf("%f\n", a);
return 0;
}

Mathematical formula for area of circle:

Here Pie is constant which is equal to

Pie = 22/7 or
3.14159265358979323846264338327950288419716939937510...

Radius is radius of the circle.


Write a c program to find the area of a rectangle
C program for area of a rectangle

Formula of area of rectangle:

Area = length * width

C code:

#include<stdio.h>

int main(){

float l,w;
float area;

printf("Enter size of each sides of the rectangle :


");
scanf("%f%f",&l,&w);

area = l * w;
printf("Area of rectangle is: %.3f",area);

return 0;
}

Sample output:
Enter size of each sides of the rectangle: 5.2 20
Area of rectangle is: 104.000
Write a c program to find the area of a trapezium
C program for area of a trapezium

Formula of area of trapezium:

Area = (1/2) * (a + b) * h

C code:

#include<stdio.h>

int main(){

float b1,b2,h;
float area;

printf("Enter the size of two bases and height of


the trapezium : ");
scanf("%f%f%f",&b1,&b2,&h);

area = 0.5 * ( b1 + b2 ) * h ;

printf("Area of trapezium is: %.3f",area);

return 0;
}

Sample output:
Enter the size of two bases and height of the
trapezium: 5 8 3
Area of trapezium is: 19.500
Write a c program to find the volume and surface area of a
cube
C program for area of a cube

Formula of surface area of cube:


Surface_area = 6 * a2

Formula of volume of cube:


Volume = a3

C code:

#include<stdio.h>

int main(){

float a;
float surface_area,volume;

printf("Enter size of any side of a cube : ");


scanf("%f",&a);

surface_area = 6 * (a * a);
volume = a * a * a;

printf("Surface area of cube is:


%.3f",surface_area);
printf("\nVolume of cube is : %.3f",volume);

return 0;
}

Sample output:

Enter size of any side of a cube: 3


Surface area of cube is: 54.000
Volume of cube is: 27.000
Write a c program to find the volume and surface area of
cylinder
C program for area of a cylinder

Formula of surface area of cylinder:


Surface_area = 2 * Pie * r * (r + h)

Formula of volume of cylinder:


Volume = Pie * r * r * h
Pie = 22/7 or
3.14159265358979323846264338327950288419716939937510...
C code:

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

int main(){

float r,h;
float surface_area,volume;

printf("Enter size of radius and height of a


cylinder : ");
scanf("%f%f",&r,&h);

surface_area = 2 * M_PI * r * (r + h);


volume = M_PI * r * r * h;

printf("Surface area of cylinder is:


%.3f",surface_area);
printf("\nVolume of cylinder is : %.3f",volume);

return 0;
}

Sample output:

Enter size of radius and height of a cylinder: 4 10


Surface area of cylinder is: 351.858
Volume of cylinder is: 502.655
Write a c program to find the volume and surface area of
sphere
C program for area of a sphere
Formula of surface area of sphere:
Surface_area = 4 * Pie * r2

Formula of volume of sphere:


Volume = 4/3 * Pie * r3
Pie = 22/7 or
3.14159265358979323846264338327950288419716939937510...

C code:

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

int main(){

float r;
float surface_area,volume;

printf("Enter radius of the sphere : ");


scanf("%f",&r);

surface_area = 4* M_PI * r * r;
volume = (4.0/3) * M_PI * r * r * r;

printf("Surface area of sphere is:


%.3f",surface_area);
printf("\nVolume of sphere is : %.3f",volume);

return 0;
}

Sample output:

Enter radius of the sphere: 5


Surface area of sphere is: 314.159
Volume of sphere is: 523.599
Write a c program to find the volume and surface area of cone

Formula of surface area of cone:


Surface_area = Pie * r * (r + √ (r2 + h2))

Formula of volume of cone:


Volume = 1/3 * Pie * r2 * h
Pie = 22/7 or
3.14159265358979323846264338327950288419716939937510...

Write a c program or code to find or


calculate the volume and surface area of
cone
#include<stdio.h>
#include<math.h>

int main(){

float r,h;
float surface_area,volume;

printf("Enter size of radius and height of a cone :


");
scanf("%f%f",&r,&h);

surface_area = M_PI * r * (r + sqrt(r*r + h*h));


volume = (1.0/3) * M_PI * r * r * h;

printf("Surface area of cone is:


%.3f",surface_area);
printf("\nVolume of cone is : %.3f",volume);

return 0;
}

Sample output:

Enter size of radius and height of a cone: 3 10


Surface area of cone is: 126.672
Volume of cone is: 94.248
C program to find power of a large number

C code for power of very big numbers:

How to get power of two very large numbers larger or


beyond than long int in c programming language:

#include<stdio.h>
#include<string.h>
#define MAX 10000

char * multiply(char [],char[]);


int main(){
char a[MAX];
char *c;
int i,n;
printf("Enter the base number: ");
scanf("%s",a);
printf("Enter the power: ");
scanf("%d",&n);

printf("Power of the %s^%d: ",a,n);

c = multiply(a,"1");
for(i=0;i<n-1;i++)
c = multiply(a,c);

while(*c)
if(*c =='0')
c++;
else
break;

printf("%s",c);
return 0;
}

char * multiply(char num1[],char num2[]){


static char mul[MAX];
char a[MAX];
char b[MAX];
char c[MAX];
char temp[MAX];
int la,lb;
int i=0,j,k=0,x=0,y;
long int r=0;
long sum = 0;

while(num1[i]){
a[i] = num1[i];
i++;
}
a[i]= '\0';
i=0;
while(num2[i]){
b[i] = num2[i];
i++;
}
b[i]= '\0';

la=strlen(a)-1;
lb=strlen(b)-1;

for(i=0;i<=la;i++){
a[i] = a[i] - 48;
}

for(i=0;i<=lb;i++){
b[i] = b[i] - 48;
}

for(i=lb;i>=0;i--){
r=0;
for(j=la;j>=0;j--){
temp[k++] = (b[i]*a[j] + r)%10;
r = (b[i]*a[j]+r)/10;
}
temp[k++] = r;
x++;
for(y = 0;y<x;y++){
temp[k++] = 0;
}
}

k=0;
r=0;
for(i=0;i<la+lb+2;i++){
sum =0;
y=0;
for(j=1;j<=lb+1;j++){
if(i <= la+j){
sum = sum + temp[y+i];
}
y += j + la + 1;
}
c[k++] = (sum+r) %10;
r = (sum+r)/10;
}
c[k] = r;
j=0;
for(i=k-1;i>=0;i--){
mul[j++]=c[i] + 48;
}
mul[j]='\0';
return mul;
}

Sample output:

Enter the base number: 5


Enter the power: 100
Power of the 5^100: 78886090522101180541172856528278622
96732064351090230047702789306640625
C program for modular division of large number

1. C code for modular division of big numbers:

2. How to get modular division of two very large


numbers larger or beyond than long int in c programming
language

#include<stdio.h>
#include<string.h>
#define MAX 10000

int validate(char []);


int modulerDivision(char[],unsigned long);

int main(){

char dividend[MAX];
unsigned long int divisor,remainder;

printf("Enter dividend: ");


scanf("%s",dividend);
if(validate(dividend))
return 0;

printf("Enter divisor: ");


scanf("%lu",&divisor);

remainder = modulerDivision(dividend,divisor);

printf("Modular division: %s %% %lu =


%lu",dividend,divisor,remainder);

return 0;
}

int validate(char num[]){


int i=0;

while(num[i]){
if(num[i] < 48 || num[i]> 57){
printf("Invalid positive integer:
%s",num);
return 1;
}
i++;
}

return 0;
}
int modulerDivision(char dividend[],unsigned long divis
or){

unsigned long temp=0;


int i=0;

while(dividend[i]){

temp = temp*10 + (dividend[i] -48);


if(temp>=divisor){
temp = temp % divisor;
}

i++;
}

return temp;
}

Sample output:
Enter dividend: 123456789
Enter divisor: 56
Modular division: 123456789 % 56 = 29

Algorithm to find the modular division of large


numbers:

Use array to store big numbers.


Write a c program which takes password from user

Write a c program which takes password from users

C source code for password:


#include<stdio.h>
#define MAX 500

int main(){

char password[MAX];
char p;
int i=0;

printf("Enter the password:");

while((p=getch())!= 13){
password[i++] = p;
printf("*");
}

password[i] = '\0';

if(i<6)
printf("\nWeak password");

printf("\nYou have entered: %s",password);

return 0;
}

Sample output:
Enter the password:*******
You have entered: fgt67m,
Write a scanf function in c which accept sentence from user

Write a scanf function in c programming language which


accept sentence from user

#include<stdio.h>
#define MAX 500

int main(){

char arr[MAX];

printf("Enter any sentence which can include


spaces.\n");
printf("To exit press enter key.\n");
scanf("%[^\n]s",arr);

printf("You had entered: \n");


printf("%s",arr);

return 0;
}

Sample output:

Enter any sentence which can include spaces.


To exit press enter key.
May I help you?
You had entered:
May I help you?
Write a scanf function in c which accept paragraph from user

Write a scanf function in c programming language which


accept paragraph from users

#include<stdio.h>
#define MAX 500

int main(){

char arr[MAX];
printf("Enter any paragraph which can include
spaces or new line.\n");
printf("To exit press the tab key.\n");
scanf("%[^\t]s",arr);

printf("You had entered: \n");


printf("%s",arr);

return 0;
}

Sample output:

Enter any paragraph which can include spaces or new


line.
To exit, press the tab key.
C is powerful language.
I am learning c from
cquestionbank.blogspot.com

You had entered:


C is powerful language.
I am learning c from
cquestionbank.blogspot.com
Print prime numbers between 1-300 using break and continue
in c
Print prime numbers between 1-300 using break and
continue in c

#include <math.h>
#include <stdio.h>
main(){
int i, j;
i = 2;
while ( i < 300 ){
j = 2;
while ( j < sqrt(i) ){
if ( i % j == 0 )
break;
else{
++j;
continue;
}
}
if ( j > sqrt(i) )
printf("%d\t", i);
++i;
}
return 0;
}

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.

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.
Palindrome in c without using string function

#include<stdio.h>
int main(){
char str[100];
int i=0,j=-1,flag=0;

printf("Enter a string: ");


scanf("%s",str);

while(str[++j]!='\0');
j--;

while(i<j)
if(str[i++] != str[j--]){
flag=1;
break;
}

if(flag == 0)
printf("The string is a palindrome");
else
printf("The string is not a palindrome");

return 0;
}

You might also like