1). Swap two values without using third variable
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1,n2;
printf("Enter your first number: ");
scanf("%d", &n1);
printf("Enter your second number: ");
scanf("%d", &n2);
//swapping rules without third variable
n1 = n1+n2;
n2= n1-n2;
n1 = n1-n2;
printf("After swapping\n");
printf("First number: %d\n", n1);
printf("Second number: %d", n2);
return 0; }
Output:
Enter your first number: 10
Enter your second number: 20
After swapping
First number: 20
Second number: 10
--------------------------------------------------------------------------------------------------------------------------
2). Swap two values using pointer.
#include <stdio.h>
#include <stdlib.h>
void swapping(int *ptr1, int *ptr2);
int main()
{
int *ptr1, *ptr2, n1, n2;
printf("Enter the first integer number: ");
scanf("%d", &n1);
printf("Enter the first integer number: ");
scanf("%d", &n2);
swapping(&n1, &n2);
printf("After swapping\n");
printf("First number is: %d\n", n1);
printf("Second number is: %d\n", n2);
return 0;
}
void swapping(int *ptr1, int *ptr2){
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2= temp;
}
Output:
Enter the first integer number: 10
Enter the first integer number: 30
After swapping
First number is: 30
Second number is: 10
--------------------------------------------------------------------------------------------------------------------------
3). Convert celcius to farenheit in c.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float celsius, fahrenheit;
printf("Enter Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius*9)/5+32;
printf("Fahrenheit: %f", fahrenheit);
return 0;
}
Output:
Enter Celsius: 100
Fahrenheit: 212.00
--------------------------------------------------------------------------------------------------------------------------
4). Largest value among three variables
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c;
printf("Enter the value of a: ");
scanf("%d", &a);
printf("Enter the value of b: ");
scanf("%d", &b);
printf("Enter the value of c: ");
scanf("%d", &c);
if(a>b){
if(a>c){
printf("a is the largest number");
}
else{
printf("c is the largest number");
}
}
else{
if(b>c){
printf("b is the largest number");
}
else{
printf("c is the largest number");
}
}
return 0;
}
Output:
Enter the value of a: 20
Enter the value of b: 10
Enter the value of c: 15
a is the largest number
--------------------------------------------------------------------------------------------------------------------------
5). Even Odd Number using if else in c.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
printf("Enter the value of num: ");
scanf("%d", &num);
if(num%2==0){
printf("%d is even number", num);
}
else {
printf("%d is odd number", num);
}
return 0;
}
Output:
Enter the value of num: 30
30 is even number
--------------------------------------------------------------------------------------------------------------------------
6). Reverse number in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,rem, rev = 0;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0){
rem = n%10;
rev = rev *10+rem;
n = n/10;
}
printf("The reverse number is %d",rev);
return 0;
}
Enter a number: 123
The reverse number is 321
--------------------------------------------------------------------------------------------------------------------------
7). Palindrome Number in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,num,rem,rev = 0;
printf("Enter a number: ");
scanf("%d", &num);
n = num;
while(num !=0){
rem = num%10;
rev = rev*10+rem;
num = num/10;
}
if(n == rev)
printf("%d is palindrome", n);
else
printf("%d not is palindrome", n);
return 0;
}
Output:
Enter a number: 121
121 is palindrome
--------------------------------------------------------------------------------------------------------------------------
8). Fibonacci Series using loop in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1 =0,n2=1,n3,i,range;
printf("Enter the range of the fibonacci series: ");
scanf("%d", &range);
for(i = 0;i<range;i++){
if(i<=1){
n3 = i;
}
else{
n3 = n1+n2;
n1= n2;
n2 = n3;
}
printf("%d ",n3);
}
return 0;
}
Output:
Enter the range of the fibonacci series: 5
0 1 1 2 3
--------------------------------------------------------------------------------------------------------------------------
9). Fibonacci Sequence Up to a Certain Number in c
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,n,n1 = 0,n2=1, nextTerm;
printf("Enter a positive number: ");
scanf("%d", &n);
printf("Fibonacci series are: %d %d ", n1,n2);
nextTerm = n1+n2;
while(nextTerm<n){
printf("%d ", nextTerm);
n1 = n2;
n2 = nextTerm;
nextTerm = n1+n2;
}
}
Output:
Enter a positive number: 100
Fibonacci series are: 0 1 1 2 3 5 8 13 21 34 55 89
--------------------------------------------------------------------------------------------------------------------------
10). Check Armstrong Number in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,num,sum = 0,rem;
printf("Please Enter the number:");
scanf("%d", &num);
for(i=num;i!= 0;i=i/10)
{
rem = i%10;
sum = sum + rem*rem*rem;
}
if ( num == sum )
printf("%d is an armstrong number.",num);
else
printf("%d is not an armstrong number.",num);
return 0;
}
Output:
Please Enter the number:371
371 is an armstrong number
.…………………………………………
Please Enter the number:123
123 is not an armstrong number.
OR
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int i,num,sum = 0,rem,count=0;
printf("Please Enter the number:");
scanf("%d", &num);
int n = num;
while(n!=0){
n = n/10;
count++;
}
n = num;
printf("%d",count);
for(i=n;i!= 0;i=i/10)
{
rem = i%10;
sum = sum + pow(rem,count);
}
if ( num == sum )
printf("%d is an armstrong number.",num);
else
printf("%d is not an armstrong number.",num);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------
11). Reverse String in C
#include <stdio.h>
int main()
{
char str[100], rev[100];
int beg,end, count = 0;
printf("Input a string: ");
gets(str);
// Calculating string length
while (str[count] != '\0')
count++;
end = count - 1;
for (beg = 0; beg < count; beg++) {
rev[beg] = str[end];
end--;
}
rev[beg] = '\0';
printf("%s\n", rev);
return 0;
}
Output:
Input a string: sajib
After Revese Sting is: bijas
--------------------------------------------------------------------------------------------------------------------------
12). Write a program array is sorted or not?
#include <stdio.h>
#include <stdlib.h
int arraySortedOrNot(int arr[], int n)
{
// Array has one or no element or the
// rest are already checked and approved.
if (n == 1 || n == 0)
return 1;
// Unsorted pair found (Equal values allowed)
//int arr[] = {20, 23, 23, 45, 78, 45};
if (arr[n-1] < arr[n-2])
return 0;
// Last pair was sorted
// Keep on checking
return arraySortedOrNot(arr, n-1);
}
// Driver code
int main()
{
int arr[] = {20, 10, 23, 45, 46, 78};
int n = sizeof(arr)/sizeof(arr[0]);
int sorted = arraySortedOrNot(arr, n);
if (sorted==1)
printf("Array is sorted");
else
printf("Array is not sorted");
}
Output:
Input array is: 20 10 23 45 46 78
Array is not sorted
………………….........
Input array is: 20 22 23 45 46 78
Array is sorted
-------------------------------------------------------------------------------------------------------------------------
13). Program to print full pyramid using *
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row,col,n;
printf("Enter number of rows:");
scanf("%d", &n);
for(row=1;row<=n;row++){
for(col=1;col<=n-row;col++){
printf(" ");
}
for(col=1;col<=2*row-1;col++){
printf("*");
}
printf("\n");
}
return 0;
}
Output:
Enter number of rows:5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
--------------------------------------------------------------------------------------------------------------------------
14). Program to print full pyramid using digits
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row,col,n;
printf("Enter number of rows:");
scanf("%d", &n);
for(row=1;row<=n;row++){
for(col=1;col<=n-row;col++){
printf(" ");
}
for(col=1;col<=2*row-1;col++){
printf("%d ", col); // for digit only change *
}
printf("\n");
}
return 0;
}
Output:
Enter number of rows:5
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
--------------------------------------------------------------------------------------------------------------------------
15). Inverted full pyramid using *
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row,col,n;
printf("Enter number of rows:");
scanf("%d", &n);
for(row=n;row>=1;row--){
for(col=1;col<=n-row;col++){
printf(" ");
}
for(col=1;col<=2*row-1;col++){
printf("* ");
// for digit only change *
}
printf("\n");
}
return 0;
}
Output:
Enter number of rows:5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
--------------------------------------------------------------------------------------------------------------------------
16). C Program to find the Sum of Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n,sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for(i = 1;i<=n;i++){
sum = sum+(i*i);
}
printf("Sum of Series: %d",sum);
return 0;
}
Output:
Enter the value of n: 3
Sum of Series: 14
--------------------------------------------------------------------------------------------------------------------------
17). Sum of Series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)
#include <stdio.h>
#include <stdlib.h>
/*(1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)*/
int main()
{
int i,j,n,sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for(i = 1;i<=n;i++){
for(j=1;j<=i;j++){
sum = sum+j;
}
}
printf("Sum of Series:= %d",sum);
return 0;
}
Output:
Enter the value of n: 4
Sum of Series:= 20
--------------------------------------------------------------------------------------------------------------------------
All Recursion Program
1). Calculate Factorial using recustion in c.
#include <stdio.h>
#include <stdlib.h>
// Function Prototype
int fact(int n);
int main()
{
int num,factorial;
printf("Enter a number: ");
scanf("%d", &num);
factorial = fact(num);
printf("Factorial %d! = %d",num, factorial);
return 0;
}
// Function Definition
int fact(int n){
if(n==1){
return 1;
}
else{
return n*fact(n-1);
}
}
Output:
Enter a number: 5
Factorial 5! = 120
--------------------------------------------------------------------------------------------------------------------------
2). Fibonacci by Recursion in c
#include <stdio.h>
#include <stdlib.h>
int fibo(int n);
int main()
{
int n,i;
printf("Enter the number of item: ");
scanf("%d", &n);
printf("first %d terms of Fibonacci series are:", n);
for(i=0;i<n;i++){
printf("%d ", fibo(i));
}
return 0;
}
int fibo(int n){
if(n==0 || n==1)
return n;
else return fibo(n-1)+fibo(n-2);
}
Output:
Enter the number of item: 5
first 5 terms of fibonacci numbers are:0 1 1 2 3
--------------------------------------------------------------------------------------------------------------------------
3). Reverse by Recursion in c
#include <stdio.h>
#include <stdlib.h>
int reverse(int n);
int main()
{
int n;
printf("Enter the number: ");
scanf("%d", &n);
printf("Reverse of %d = %d", n,reverse(n));
return 0;
}
int rev = 0,rem;
int reverse(int n){
if(n==0){
return rev;
}
else{
rem = n%10;
rev = rev*10+rem;
reverse(n/10);
return rev;
}
}
Output:
Enter the number: 123
Reverse of 123 = 321
-------------------------------------------------------------------------------------------------------------------
1. Write C program and flowchart to
find the largest of three numbers.
#include<stdio.h>
int main(){
int a,b,c;
printf("Enter
the 3 number");
scanf("%d",
&a);
scanf("%d",
&b);
scanf("%d",
&c);
if(a>b){
if(a>c){
printf("%d is the laargest
number", a);
}
else{
printf("%d is the largest
number", c);
}
}
else{
if(b>c){
printf("%d is the largest
number", b);
}
else{
printf("%d is the largest
number", c);
}
}
return 0;
}
- Algorithm :
- Step 1 : Start
- Start 2 : Input a, b, c
- Start 3 : if a > b
goto step 4, otherwise goto step 5
- Start 4 : if a > c
goto step 6, otherwise goto step 8
- Start 5 : if b > c
goto step 7, otherwise goto step 8
- Start 6 : Output "a
is the largest", goto step 9
- Start 7 : Output "b
is the largest", goto step 9
- Start 8 : Output "
c is the largest", goto step 9
- Start 9 : Stop
Flowchart to find the
largest of three numbers.
2. Write Fibonacci Series up to n number of terms in c.
Next number is the sum of
previous two numbers for example 0, 1,
1, 2, 3, 5, 8 etc. The first two numbers of fibonacci series are 0 and 1.
There
are two ways to write the fibonacci series program:
- Fibonacci Series without
recursion
- Fibonacci Series using
recursion
#include<stdio.h>
int main(){
int i, n1 =
0, n2 = 1, n3, number;
printf("Enter
the numbers of element: ");
scanf("%d",
&number);
printf("Fibonacci
series: %d %d ", n1, n2);
for(i=2;i<number;i++){
n3 =
n1+n2;
n1 = n2;
n2 = n3;
printf("%d ", n3);
}
return 0;
}
3). How to
calculate Factorial Using Recursion in c?
// Factorial
rules n! = n*(n-1)
#include<stdio.h>
int main(){
int n;
printf("Please
enter your number: ");
scanf("%d",
&n);
int result =
fact (n);
printf("Factorial
%d! = %d",n, result);
}
int fact(int
n){
if(n==1){
return 1;
}
else{
return n*fact(n-1);
}
return 0;
}
4. Write a
program to find the given number is prime or not?
#include<stdio.h>
int main(){
int i,n,flag
= 0;
printf("Please
enter the number: ");
scanf("%d",
&n);
for(i = 1;
i<=n;i++){
if(n%i == 0){
flag = flag+1;
}
}
if(flag ==2){
printf("%d is Prime number.", n);
}
else{
printf("%d number is not
Prime.",n);
}
return 0;
}
5).
Calculate electricity bill in c program
#include <stdio.h>
//For first 50 units Rs. 0.50/unit
//For next 100 units Rs. 0.75/unit
//For next 100 units Rs. 1.20/unit
//For unit above 250 Rs. 1.50/unit
//An additional surcharge of 20% is added to the bill.
int main()
{
int unit;
float amt,
total_amt, sur_charge;
/* Input unit
consumed from user */
printf("Enter
total units consumed: ");
scanf("%d", &unit);
/* Calculate
electricity bill according to given conditions */
if(unit <= 50)
{
amt = unit *
0.50;
}
else if(unit <=
150)
{
amt = 25 +
((unit-50) * 0.75);
}
else if(unit <=
250)
{
amt = 100 +
((unit-150) * 1.20);
}
else
{
amt = 220 +
((unit-250) * 1.50);
}
/*
* Calculate total
electricity bill
* after adding
surcharge
*/
sur_charge = amt *
0.20;
total_amt = amt + sur_charge;
printf("Electricity Bill = Rs. %.2f", total_amt);
return 0;
}
6). Given a interger number write using while loop to reverse
the digit of the number for example the number 12345. Should be written as
54321
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,rem, rev = 0;
printf("Enter
a number: ");
scanf("%d", &n);
while(n!=0){
rem = n%10;
rev = rev
*10+rem;
n = n/10;
}
printf("The
reverse number is %d",rev);
return 0;
}
7). Program to find the factorial of the number?
#include
<stdio.h>
#include
<stdlib.h>
int main()
{
int number,i,fact = 1;
printf("Enter a number: ");
scanf("%d", &number);
for(i=1;i<=number;i++){
fact = fact*i;
}
printf("Factorial %d! =
%d",number,fact);
return 0;
}
8). Write
a program to calculate factorial of a number using recursion?
#include
<stdio.h>
#include
<stdlib.h>
int
factorial(int n);
int main()
{
int number,fact;
printf("Enter the nummber: ");
scanf("%d",&number);
fact = factorial(number);
printf("Factorial of %d is %d",number,
fact);
return 0;
}
int
factorial(int n){
if(n==1){
return 1;
}
else {
return n*factorial(n-1);
}
}
9). Write
a program to print Fibonacci series.
#include
<stdio.h>
#include
<stdlib.h>
int main()
{
int n1 =0,n2=1,n3,i,range;
printf("Enter the range of the
fibonacci series: ");
scanf("%d", &range);
for(i = 0;i<range;i++){
if(i<=1){
n3 = i;
}
else{
n3 = n1+n2;
n1= n2;
n2 = n3;
}
printf("%d ",n3);
}
return 0;
}
10).