Home / Notes / Btech First Year Notes / C Programs Part-I

C Programs Part-I

Introducing you the important programs of C language with coding . These programs  are  provided by Mr. Anuj Khanna (Asst. Professor),Krishna Institute of Technology,Kanpur

Topics

Arithmetic Operation,       if – else Statements

Switch- Case Statements,    Loops & Iterations

 

 

______________________________________________________________

Important C Programs with Coding

Basic Programs

1. Program to find the simple interest, given principle, rate of interest and time.

#include <stdio.h>

#include <conio.h>

void main()

{

float p, r, si;

int t;

clrscr();

printf(“Enter the values of p,r and t\n”);

scanf (“%f %f %d”, &p, &r, &t);

si = (p * r * t)/ 100.0;

printf (“Amount = Rs. %5.2f\n”, p);

printf (“Rate = Rs. %5.2f%\n”, r);

printf (“Time = %d years\n”, t);

printf (“Simple interest = %5.2f\n”, si);

getch() ;

}

2.Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, andhouse rent allowance is 20% of basic salary. Write a program to calculate his Gross Salary.

#include<stdio.h>

#include<conio.h>

void main()

{

float bs , da= 0.0, hra=0.0 ,gross=0.0 ;

clrscr() ;

printf(“Enter the basic salary\n”) ;

scanf(“%f” ,&bs) ;

printf(“BASIC SALARY OF RAMESH=%f\n ”, bs) ;

da = (bs*40) / 100 ;

hra = (bs *20) /100 ;

gross = bs + da + hra ;

printf(“*************************************************** ”) ;

printf(“GROSS SALRY OF RAMESH = % f ”, gross) ;

getch() ;

}

3. Program to find the area of a circle, given the radius.

#include <stdio.h>

#include <conio.h>

#include <math.h>

 

#define PI 3.142

void main()

{

float radius, area;

clrscr();

printf(“Enter the radius of a circle\n”);

scanf (“%f”, &radius);

area = PI * pow (radius,2);

printf (“Area of a circle = %5.2f\n”, area);

getch() ;

}

 

**Note : Similarly we can compute area of rectangle also.

 

4. Program to find the area of a triangle, given three sides a, b and c.
#include <stdio.h>

#include <conio.h>

#include <math.h>

void main()

{ float a, b, c ;

float s =0.0 , area =0.0 ;

clrscr();

printf(“Enter the values of a,b and c\n”);

scanf (“%f %f%f “, &a, &b, &c);

s = (a + b + c) / 2; /* computes perimeter */

area = sqrt ( s * (s-a) * (s-b) * (s-c)); /* compute the area */

printf (“Area of a triangle = %f\n”, area);

}

5. Program to swap the values of two variables without using third variable.

#include<stdio.h>

#include<conio.h>

void main()

{

int a , b ;

clrscr() ;

printf(“Enter the values of a and b \n”) ;

scanf(“%d %d” ,&a , &b) ;

printf(“Values variables before swapping are a=%d \t b=%d: \n”, a , b) ;

a = a + b ;

b = a – b ;

a = a – b ;

printf(“Values of a and b after swapping are:\n”) ;

printf(“a=%d\t\t b=%d”,a ,b) ;

getch();

}

Operators

6. Program for finding the sum of a five digit no.

#include<stdio.h>

#include<conio.h>

void main()

{

long int num , sum =0 , rem ; // Max. five digit no. can be 99999, so long int declared

clrscr() ;

printf(“Enter the five digit number\n”) ;

scanf(“%ld” , &num) ;

printf(“The five digit number=%ld\n”, num) ;

rem1 = num%10 ;

num = num /10 ;

rem2 = num%10 ;

num = num /10 ;

rem3 = num%10 ;

num=num/10 ;

rem4 = num%10;

num=num/10 ;

sum = rem1 + rem2 + rem3 + rem4 + num ;

printf(“Sum of the digits of %ld =%ld”, num ,sum) ;

getch();

}

 

7.Program to multiply a given number with 2n ,without using multiplication operator.

#include<stdio.h>

#include<conio.h>

void main()

{

int num , n , result ;

clrscr() ;

printf(“Enter the number to be multiplied \n”) ;

scanf(“%d” ,&num) ;

printf(“Enter the value of n \t”) ;

scanf(“%d”, &n) ;

result = num << n ;

printf(“Result of multiplication = %d”, result);

getch() ;

}

if – else Statements

 

8. Program to find and output all the roots of a quadratic equation, for non-zero coefficients. In case of errors your program should report suitable error message

 

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <math.h>

 

void main()

{

float A, B, C, root1, root2;

float realp, imagp, disc;

clrscr();

printf(“Enter the values of A, B and C\n”);

scanf(“%f %f %f”, &A,&B,&C);

 

/* If A = 0, it is not a quadratic equation */

 

if( A==0 || B==0 || C==0)

{

printf(“Error: Roots cannot be determined\n”);

exit(1);

}

else

{

disc = B*B – 4.0*A*C;

if(disc < 0)

{

printf(“Imaginary Roots\n”);

realp = -B/(2.0*A) ;

imagp = sqrt(abs(disc))/(2.0*A);

printf(“Root1 = %f +i %f\n”,realp, imagp);

printf(“Root2 = %f -i %f\n”,realp, imagp);

}

else if(disc == 0)

{

printf(“Roots are real and equal\n”);

root1 = -B/(2.0*A);

root2 = root1;

printf(“Root1 = %f \n”,root1);

printf(“Root2 = %f \n”,root2);

}

else if(disc > 0 )

{

printf(“Roots are real and distinct\n”);

root1 =(-B+sqrt(disc))/(2.0*A);

root2 =(-B-sqrt(disc))/(2.0*A);

printf(“Root1 = %f \n”,root1);

printf(“Root2 = %f \n”,root2);

}

}

 

} /* End of main() */

 

9. Write a C program to find the largest of three numbers.

 

#include <stdio.h>

#include <conio.h>

#include <math.h>

void main()

{

int a, b, c; clrscr();

printf(“Enter the values of a,b and c\n”);

scanf (“%d %d %d”, &a, &b, &c);

printf (“a = %d\tb = %d\tc = %d\n”, a,b,c);

 

if ( a > b)

{

if ( a > c)

{

printf (“A is the greatest among three\n”);

}

else

{

printf (“C is the greatest among three\n”);

}

}

else if (b > c)

{

printf (“B is the greatest among three\n”);

}

else

printf (“C is the greatest among three\n”); }

 

10. Write a C program to check whether a given integer is odd or even.

 

#include <stdio.h>

#include <conio.h>

void main()

{

int ival, remainder;

clrscr();

printf(“Enter an integer :”);

scanf (“%d”, &ival);

 

remainder = ival % 2;

if (remainder == 0)

{

printf (“%d, is an even integer\n”, ival);

else

printf (“%d, is an odd integer\n”, ival);

}

} /* End of main() */

 

11. Program to determine whether a year is a leap year or not using logical operator.

#include<stdio.h>

#include<conio.h>

void main()

{ int year ;

clrscr() ;

printf (“Enter the year : \n” ) ;

scanf(“%d” & year ) ;

if ( ( (year % 4 = = 0 ) && (year % 100 !=0) ) || (year % 4 00= = 0 ) )

printf(“%d is a leap year” , year) ;

else

printf(“%d is not a leap year” , year) ;

getch () ;

}

 

12. Program to determine whether a triangle is right angle , scalene, equilateral or isosceles where the Sides a , b and c are entered by the user. Also check whether the triangle is possible or not?

#include<stdio.h>

#include<conio.h>

void main()

{ float a , b , c ;

clrscr() ;

printf ( “Enter the sides of the triangle : \n” ) ;

scanf(“%f %f %f ” , & a , & b , &c) ;

if ((a + b < c) || (b + c < a) || ( a + c < b) )

printf (“Triangle is NOT POSSIBLE” ) ;

if ( (a * a = = b * b + c * c) || (b * b = = a * a + c * c) ) || (c * c = = a * a + c * c) ) ;

printf( “ Triangle is RIGHT ANGLED” ) ;

if ((a = = b ) && (b != c)) ;

printf( “Triangle is ISOSCELES”) ;

if ((b = = c ) && (c!= a)) ;

printf( “Triangle is ISOSCELES”) ;

if ((c = = a ) && (a != b)) ;

printf( “Triangle is ISOSCELES”) ;

if ( ( a = = b) && ( b = = c) ) ;

printf( “ Triangle is EQUILATERAL” ) ;

if ( (a != b) && (b != c) && (c!=a) ) ;

printf( “ Triangle is SACLENE” ) ;

getch () ;

}

 

Switch- Case Statements

 
13. Program to simulate a simple calculator to perform arithmetic operations.Error message should be reported if any attempt is made to divide by zero.

 

#include <stdio.h>

#include <conio.h>

void main()

{

char oper; /* oper is an operator to be selected */

float n1, n2, result;

clrscr();

printf (“Simulation of a Simple Calculator\n\n”);

printf(“Enter two numbers\n”);

scanf (“%f %f”, &n1, &n2);

printf(“Enter the operator [+,-,*,/]\n”);

scanf (“%c”, &oper);

 

switch (oper)

{

case ‘+’: result = n1 + n2;

break;

case ‘-‘: result = n1 – n2;

break;

case ‘*’: result = n1 * n2;

break;

case ‘/’: result = n1 / n2;

break;

default : printf (“Error in operation\n”);

break;

}

 

printf (“\n%5.2f %c %5.2f= %5.2f\n”, n1,oper, n2, result);

 

}

Loops & Iterations

 

14. Write a C program to find the factorial of a given number

#include<conio.h>

#include <stdio.h>

void main()

{

long int i,fact=1,num;

clrscr() ;

printf(“Enter the number\n”);

scanf(“%ld”,&num);

if( num <= 0)

fact = 1;

else

{

for(i = 1; i <= num; i++)

{ fact = fact * i ;}

} /* End of else */

printf(“Factorial of %ld =%ld\n”, num, fact ); } /* End of main() */

 

15. Program to generate the table of any number using loop

#include<stdio.h>

#include<conio.h>

void main()

{

int t , num , t ;

clrscr() ;

printf (“Enter the number whose table is to be generated : \n” ) ;

scanf(“%d”, & num ) ;

printf ( “ Table of %d is :” , num) ;

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

{

t = num * i ;

printf( “\n %d x %d = %d ” , num , i , t ) ;

}

getch () ;

}

 

16. Program to check whether a given number is prime or not and output the given number with suitable message.

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

void main()

{

int num, j, flag;

clrscr();

printf(“Enter a number\n”);

scanf(“%d”, &num);

if ( num <= 1)

{

printf(“%d is not a prime numbers\n”, num);

exit(1);

}

flag = 0;

for ( j=2; j<= num/2; j++)

{ if( ( num % j ) == 0)

{ flag = 1;

break; }

}

if(flag == 0)

printf(“%d is a prime number\n”,num);

else

printf(“%d is not a prime number\n”, num); }

 

17.Program to generate Fibonacci sequence Fibonacci sequence is 0 1 1 2 3 5 8 13 21.

 

#include <stdio.h>

#include <stdio.h>

void main()

{

int fib1=0, fib2=1, fib3, limit, count=0;

printf(“Enter the limit to generate the fibonacci sequence\n”);

scanf(“%d”, &limit);

printf(“Fibonacci sequence is …\n”);

printf(“%d”,fib1);

printf(“%d”,fib2);

count = 2; /* fib1 and fib2 are already used */

 

while( count < limit)

{

fib3 = fib1 + fib2; count ++;

printf(“%d\n”,fib3);

fib1 = fib2;

fib2 = fib3;

}

 

} /* End of main() */

 

18. Program to accept an integer and reverse it.

#include<conio.h>

#include <stdio.h>

void main()

{

long num, rev = 0, temp, digit;

printf(“Enter the number\n”); /*For better programming,choose ‘long int’ */

scanf(“%ld”, &num); /*reverse of a five digit no. can be out of range of integer*/

temp = num;

while(num > 0)

{

digit = num % 10;

rev = rev * 10 + digit;

num /= 10;

}

printf(“Given number = %ld\n”, temp);

printf(“Its reverse is = %ld\n”, rev);

}

Check Also

Switchgear & protection

Notes: Switchgear & Protection (NEE-602), Unit-I

Introducing you the topics of Switchgear & Protection NEE-602 (Unit-I) in very summarized way. These notes are …

Leave a Reply

Your email address will not be published. Required fields are marked *