c program to print a number in reverse order

Explanation of the program

This program takes a number as input, and prints it in the reverse order. The number is read into the variable n. Inside the while loop, the remainder after the division of n by 10 is calculated by using the % (modulo) operator and it is stored in the variable a, this will give the last digit of any number. Next n is divided by 10 to avoid the last digit, and the computation is continued with the remaining digits. Each time after obtaining the last digit, it is printed. The loop terminates when the value of n is less then or equal to 0.

Program


/*reverse a number c program*/

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int n,a;

printf("Enter the number");

scanf("%d",&n);

printf("Reverse is");

while(n>0)

{

a=n%10;

printf("");

printf("%d",a);

n=n/10;

}

getch();

}


Output



Find the reverse of a number in c
C program to find the reverse of a number


c program to check if a string is palindrome or not

Palindrome


A string which reads the same, when it is read forward and backward is called a palindrome.

Eg - ANNA

Explanation of the program


This program reads a string as input, checks whether it is palindrome or not and prints the result. Initially the variable i is set to 0. Input string is read into character array a[]. The length of the string if found by using the string handling function strlen() and is stored in the variable j. Now the values at a[i] and a[j] is compared in the while loop, at each iteration, the value of i is incremented and the value of j is decremented. At each iteration, if the value of a[i] and [j] are equal then the string is palindrome, otherwise the string is not palindrome. 

Program


//palindrome program in c

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

clrscr();

int flag=0,i=0,j;

char a[10];

printf("Enter the string\n");

scanf("%s",a);

j=strlen(a)-1;

while(a[i]!='\0' && j>=0)

{

if(a[i]!=a[j])

{

flag=1;

printf("Not palindrome");

break;

}

i++;

j--;

}

if(flag==0)

{

printf("palindrome");

}

getch();

}


Output



C program to check if a string is palindrome
C program to check whether a string is palindrome

c program to implement merging of arrays

Explanation of the program



This merges two arrays and prints the resultant array after merging. The elements of first array and second array is read into a[ ] and b[ ] respectively. The elements of the array a[ ] is copied into array    c[ ] in the positions c[0] to c[9] and the elements of array b[ ] is copied into array c[ ] in the positions c[10] to c[19]. Now c[ ] contains the resultant array after merging.

Program


#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int a[10],b[10],c[20],j;

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

for(int i=0;i<10;i++)

{

scanf("%d",&a[i]);

}

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

for(i=0;i<10;i++)

{

scanf("%d",&b[i]);

}

for(i=0;i<10;i++)

{

c[i]=a[i];

}

i=0;

for(j=10;j<20;j++)

{

c[j]=b[i];

i++;

}

printf("Array after merging");

for(i=0;i<20;i++)

{

printf("\n%d",c[i]);

}

getch();

}


Output



merging of arrays in c
C program to merge two arrays


c program to implement linear search

Explanation of the program



This program uses linear search to find an element in an array. The elements are read into the array a[]. The element to be searched is read into the variable n. The for loop uses the loop variable i, at each iteration in the for loop the value of a[i] is compared with the value of n, if a match is found, the execution of for loop is terminated and the value of i gives the position of the element. The variable flag is initially set to 0, if a match is found during the search in the array flag will be set to 1. After exiting from the for loop, if the value of flag is 0, then the item is not present in the array.

Program


#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],n,flag=0;

clrscr();

printf("Enter the numbers");

for(int i=0;i<10;i++)

{

scanf("%d",&a[i]);

}

printf("Enter the item to be searched");

scanf("%d",&n);

for(i=0;i<10;i++)

{

if(a[i]==n)

{

printf("Item %d found at location %d",n,i);

flag=1;

break;

}

}

if(flag==0)

{

printf("Item not found in the array");

}

getch();

}


Output



C program to search using linear search
C program for linear search

c program to find the number of occurences of a letter in a string

Explanation of the program



This program finds the number of occurrence of a letter in a string. The string is read into character array a[] and the letter to be searched is read into variable b. The while loop uses the loop variable i, which is initially set to 0. The loop continues until a[i]= '\0', (it marks the end of string). At each iteration in the while loop, the value of a[i] is compared with the value of b, if it equals the value of count is incremented. After exiting from the while loop the value in the variable count gives the total number of occurrence of the letter in the string.

Program



// c strings - number of occurrence of a letter

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int i=0,count=0;

char a[10];

char b[10];

printf("Enter the string");

scanf("%s",&a);

printf("Enter the letter");

scanf("%s",&b);

while(a[i]!='\0')

{

if(a[i]==b[0])

{

count++;

}

i++;

}

printf("%d",count);

getch();

}


Output



Number of occurrence of a letter in a string
C program to find the number of occurrence of a letter in a string





c program for student grade calculation

If else if ladder


If else if ladder can be used for a series of condition checks. The if and else if checks for a condition, if it matches, the statements associated with it will be executed. If none of these conditions matches then the statement associated with else will be executed.

Explanation of the program


This program uses if else if ladder for student grade calculation. The marks for three subjects is read as input and stores in the variables m1, m2 and m3 respectively. The average mark is calculated by the statement  g=(m1+m2+m3)/3. The value of g is checked in the if else if construct to print the corresponding grade. 

Program



#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

float m1,m2,m3,g;

printf("enter mark1");

scanf("%f",&m1);

printf("enter mark2");

scanf("%f",&m2);

printf("enter mark3");

scanf("%f",&m3);

g=(m1+m2+m3)/3;

if(g>=80)

printf("A grade");

else if(g>=60)

printf("B grade");

else if(g>=50)

printf("C grade");

else

printf("Failed");

getch();

}


Output



C program for student grade calculation using if else if construct.
C program for student grade calculation using if else if ladder


c program for electricty bill calculation


Switch case in c



Switch case can be used for condition checking. The switch case takes a variable as input and in each case, the value stored in the variable is checked for a certain condition. If the condition matches, the statements associated with that case will be executed.


Explanation of the program



This program uses switch case for electricity bill calculation. The program reads the value for current reading and previous reading and stores it in the variables cr and pr respectively. Then the unit is calculated by using the statement unit=cr-pr. Each case checks the value of unit and calculate the corresponding amount.


Program



#include<stdio.h>

#include<conio.h>

void main()

{

int cr,pr,unit,ud,amt;

clrscr();

printf("Enter the current reading\n& previous reading: ");

scanf("%d%d",&cr,&pr);

unit=cr-pr;

ud=unit/100;

switch(ud)

{

case 1: amt=unit+(unit*0.5);

break;

case 2: amt=unit+(unit*1);

break;

case 3: amt=unit+(unit*1.5);

break;

default: amt=unit+(unit*2);

break;

}

printf("Total amount is %d",amt);

getch();

}



Output



c program for electricity bill calculation using switch
C program to calculate electricity bill using switch

c program to print the numbers which are divisible by 7 between 100 & 200

Explanation of the program


This program finds the numbers which are divisible by 7 between 100 and 200. Initially the variable i is set to 100. The while loop uses each value of i to check whether it is divisible by 7 by using the "%" operator, if so the count is incremented and the value of i is printed. At each iteration in the while loop, the value of i is incremented, and this process continues till the value of i is less than 200.

Program


#include<stdio.h>

#include<conio.h>

void main()

{

int d,i=100,count=0,val;

clrscr();

printf("The numbers which are divisible by 7\nbetween 100 and 200: \n");

while(i<200)

{

if(i%7==0)

{

printf("\n");

count=count+1;

val=i;

printf("%d",val);

}

i=i+1;

}

printf("\n");

printf("count=%d",count);

getch();

}




Output




numbers divisible by 7 between 100 and 200
Numbers divisible by 7 between 100 and 200



c program to implement a cloth showroom activities

Description


This program implements a cloth showroom bill calculation. The users can purchase either hand loom, mill cloth or both.  The discount depends on both the type of cloth and the total amount of purchase. The program uses a switch statement to find the discount for different purchases by different users. Once the discount is calculated the statement tot=amt-d; finds the final amount the user has to pay.

Program

 
#include<stdio.h>

#include<conio.h>

#include<process.h>

void main()

{

 clrscr();

 int type,i;

 float d,amt,tot,hl,ml;

 printf("Enter the cloth type 1 :handloom 2 :mill cloth 3 :both");

 scanf("%d",&type);

 if(type >3 | type<1)

 {

  exit(0);

 }

     if(type==3)

  {

  printf("enter the value of handloom & mill cloth:");

  scanf("%f%f",&hl,&ml);

  amt=hl+ml;

  goto l;

  }

 printf("\nenter the amount:");

 scanf("%f",&amt);

 l:

 i=amt/100;

 switch(i)

  {

   case 1 :

   if(type==1)

    {

    d=0;

    break;

    }

   if(type==2)

    {

    d=(amt*5)/100;

    break;

    }

   else

    {

    d=(ml*5)/200;

    break;

    }

   case 2 : if(type==1)

    {

    d=(amt*5)/100;

    break;

    }

    if(type==2)

     {

     d=(amt*7.5)/100;

     break;

     }

    else

     {

     d=((ml*5)/200)+((amt*7.5)/200);

     break;

     }

   case 3 : if(type==1)

    {

    d=(amt*7.5)/100;

    break;

    }

    if(type==2)

     {

     d=(amt*10)/100;

     break;

     }

    else

     {

     d=((ml*7.5)/200)+((amt*10)/200);

     break;

     }

   default  : if(type==1)

    {

    d=(amt*10)/100;

    break;

    }

    if(type==2)

     {

     d=(amt*15)/100;

     break;

     }

    else

     {

     d=((ml*10)/100)+((amt*15)/100);

     break;

     }

   }

 printf("\nDiscount is: %f" ,d);

 tot=amt-d;

 printf("\n Total amount is: %f",tot);

 getch();

}





c program to implement multiple choice questions evaluation

Description


This program implements multiple choice test. The are total 10 questions, the users have to give their option for each question, one by one and it will be read to the array a[]. The correct option is stored in the array b[]. Each choice by the user is compared with the stored answers, if it matches, the count of correct answers will be incremented,  otherwise the count for wrong answers will be incremented.

Program


#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

char a[10],b[10]={'a','b','c','d','a','b','c','d','a','b'};

int ct=0,cf=0,i=0;

printf("Enter the answers");

while(i<10)

{

printf("%d: ",i+1);

scanf("%s",&a[i]);

}

if(a[i]==b[i])

{

ct++;

}

else

{

cf++;

}

i++;

}

printf("Number of correct answers=%d",ct);

printf("\nNumber of wrong answers=%d",cf);

getch();

}


c program to find the average and range of n numbers

Average and Range



Average of n numbers is the sum of numbers divided by n. The range of n numbers is the difference between the largest and smallest element.

Explanation of the program



The program reads the limit and stores it in the variable n. The value of each element is stored in the variable a, each time the value stored in the variable l and s is checked with the value of a to find the new smallest and largest element. The variable sum stores the sum of these numbers.

Program



#include<stdio.h>

#include<conio.h>

void main()

{

int n,a,l=0,flag=1,sum=0,range,i=1,s,avg;

clrscr();

printf("Enter the limit:  ");

scanf("%d",&n);

printf("Enter the numbers: ");

while(i<=n)

{

scanf("%d",&a);

if(flag==1)

{

s=a;

flag=0;

}

if(a>l)

{

l=a;

}

if(a<s)

{

s=a;

}

sum=sum+a;

i++;

}

avg=sum/n;

range=l-s;

printf("Average= %d",avg);

printf("\nRange= %d",range);

getch();

}


Output



c program for average and range
C program to find the average and range

c program to demonstrate function without argument

//Function without argument c program

Function without arguments


Functions can be of two types.

2. Function without arguments

A function with arguments will have arguments or parameters passed along with it, eg- Sum(a,b), here a and b are the arguments passed along with the function Sum(). The values stored in a and b can be used for computations inside the Sum() function.
                                                            A function without arguments will not contain any arguments or parameters passed along with it, eg- Sum().

Explanation of the program 


This program creates a user defined function called add(). The function takes as input the values for three variables a, b and c. It then adds these three variables and assigns the result to a variable sum and the value stored in sum is returned from the function. The statement c=add(); calls the function and assigns the value returned from the function to the variable c. 

Program

#include<stdio.h>

#include<conio.h>

int add(void);

void main()

{

clrscr();

int c;

c=add();                      //function call

printf("Sum is %d",c);

getch();

}

int add(void)     //function definition

{

int a,b,c,sum;

printf("Enter three numbers");

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

sum=a+b+c;

return(sum);

}


Output

C program for function without arguments.
C program for function without arguments


C Program to find the factorial using recursion

Recursion


Recursion is a process of calling a function repeatedly until a certain condition is met.

Explanation of the program


This program takes as input, the number for which the factorial needs to be find, and stores it in the variable n. The variable f calls the fact() function, which takes as input the number n. The value returned from the fact() is stored if f, which is the factorial of the number.

The fact() function takes as input the number n, this function recursively calls itself until the value of its parameter equals to 1, each time decreasing the value of n.


Example


n=3

Below phases shows function call, condition check and return value respectively.

Phase 1

fact(3)              3 !=1               3*=fact(2)
Execution is paused since value for fact(2) is not available.

Phase 2

fact(2)              2!=1                2*=fact(1)
Execution is paused since value for fact(1) is not available.

Phase 3

fact(1)              1=1                  return 1

going back to phase 2 with the value for fact(1)

2*=1   => a =2

going back to phase 1 with the value for fact(2)

3*=2  => a = 6

Factorial is 6

Program


 //factorial program in c

#include<stdio.h> #include<conio.h> int fact(int n); void main() { clrscr(); int n,f; printf("Enter the number"); scanf("%d",&n); f=fact(n); printf("Factorial is %d",f); getch(); } int fact(int a) { if(a==1) return 1; else a*=fact(a-1); return(a); }


Output


C program for factorial of a number using recursion
C program to find the factorial of a number using recursion

C program to find factorial without recursion