Programming in C Lab Manual FOR Diploma in Ece/Eee
Programming in C Lab Manual FOR Diploma in Ece/Eee
LAB MANUAL
FOR
DIPLOMA IN ECE/EEE
1. Write a C program to perform addition , subtraction , multiplication and division of two
numbers .
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a , b ,sum , sub , mul , div ;
clrscr ( ) ; OUTPUT :
printf(“ Enter two numbers =”) ;
scanf ( “ %d %d “ , &a, &b); Enter two numbers = 12 2
sum = a + b; addition is = 14
sub= a – b ; subtraction is = 10
mul = a * b ; multiplication is = 24
div = a / b ; division is = 6
printf( “addition is = %d \n“ , sum);
printf( “subtraction is = %d\n “ , sub);
printf( “multiplication is = %d \n“ , mul);
printf( “division is = %d “ , div);
getch ( );
}
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a , b , t ; OUTPUT :
clrscr ( ) ;
printf(“ Enter two numbers =”) ; Enter two numbers = 12 34
scanf ( “ %d %d “ , &a, &b);
After interchange value is a = 34
t = a ; b = 12
a= b ;
b= t ;
printf( “After interchange value is a = %d b=%d “, a , b);
getch ( );
3. To interchange the numeric values of two variables without using third variable.
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a , b , t ;
clrscr ( ) ;
printf(“ Enter two numbers =”) ; OUTPUT :
scanf ( “ %d %d “ , &a, &b);
Enter two numbers = 12
a = a + b; 34
b=a–b;
a=a–b; After interchange value is
printf( “After interchange value is a = %d b=%d “, a , b); a = 34 b = 12
getch ( );
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a , b ;
clrscr ( ) ; OUTPUT :
printf(“ Enter two numbers =”) ;
Enter two numbers= 40 60
scanf ( “ %d %d “ , &a, &b);
if ( a > b) greater is = 60
printf ( “greater is = %d “ , a);
else
printf( “greater is = %d “ , b);
getch ( ) ;
}
6. To check a given number is Even or Odd .
# include <stdio.h>
# include <conio.h>
void main ( )
{
int n ;
clrscr ( ) ; OUTPUT :
printf(“ Enter the number =”) ;
Enter the number = 6
scanf ( “ %d” ,&n);
Even number
if ( n % 2 = = 0)
printf(“even number “);
else
printf(“odd number “);
getch ( );
7. Take three sides of a triangle as input and check whether the triangle can be
drawn or not. If possible, classify the triangle as equilateral, isosceles, or scalene.
#include<stdio.h>
#include<conio.h>
void main ( )
{
int a,b,c;
clrscr ( );
printf("Enter the value of three sides=");
else
{
if(a= =b || b= =c || a= =c)
printf(" Isoceles triangle ");
else
printf(" Scalene triangle ");
}
}
else
printf(" Triangle is not possible ");
getch ( ) ;
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ( )
{
float a,b,c,r1,r2,d;
clrscr ( );
printf("Enter the value of a,b,c");
scanf("%f %f %f", &a, &b, &c);
OUTPUT :
d=sqrt(b*b-4*a*c);
Enter the value of a , b, c =
if(d==0)
1 -3 -4
{
printf("Both roots are Equal\n");
r1=r2=-b/(2*a); Roots are real and unequal
printf("r1=%f r2=%f",r1,r2); r1=4.000000 r2 = -1.000000
}
else
{ Enter the value of a , b, c =
if(d>0) 1 0 -4
{ Roots are real and unequal
printf(" Roots are real and Unequal \n"); r1= -2.000000 r2 = 2.000000
r1 = (-b+d) / (2*a) ;
r2 = (-b-d) / (2*a) ;
printf("r1=%f r2=%f",r1,r2);
}
else
printf("roots are imaginary");
}
getch( );
}
# include <stdio.h>
# include <conio .h>
void main ( )
{
int i, n, fact = 1 ;
clrscr ( ) ;
printf("Enter a number to calculate it's factorial = “) ;
scanf("%d", &n);
OUTPUT :
for (i = 1; i <= n; i++)
fact = fact * i ; Enter a number to calculate it's factorial
= 5
printf("Factorial of %d = %d\n", n, fact); Factorial of 5 is = 120
getch ( );
}
#include <stdio.h>
#include<conio .h >
void main ( )
{
int n, i, sum=0 ;
clrscr ( ) ;
printf("Enter the range = "); OUTPUT :
#include <stdio.h>
#include<conio .h >
void main ( )
{
int i, j, rows;
clrscr ( ) ;
printf("Enter the number of rows: ");
scanf("%d",&rows);
#include <stdio.h>
#include<conio .h >
void main ( )
{
int i, j, rows;
clrscr ( ) ;
printf("Enter the number of rows: ");
scanf("%d",&rows);
getch ( );
}
#include <stdio.h>
#include<conio .h >
void main ( )
{
int i, j, rows;
clrscr ( ) ;
printf("Enter the number of rows: ");
scanf("%d",&rows);
for( i= rows ; i > = 1 ; i - -)
{
for ( j=1; j<= i ; j++)
{
printf (" * “ ) ;
}
printf("\n") ;
}
getch ( ) ;
}
while( temp ! = 0 )
{ OUTPUT :
r = temp % 10
reverse = reverse * 10 + r ; Enter a number to check if it is a
temp = temp/10; palindrome or not= 151
}
151 is a palindrome number
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
getch ( );
}
# include <stdio.h>
# include <conio. h>
void main ( )
{
int a, b, x, y, t, gcd, lcm ;
clrscr ( );
printf("Enter two integers=") ;
gcd = a;
lcm = (x*y) / gcd;
getch ( ) ;
}
#include <stdio.h>
#include<conio .h >
void main ( )
{
int n, i = 2, sum=0 ;
clrscr ( ) ;
printf("Enter the range = "); OUTPUT :
20. Print the sum of ( 1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
#include <stdio.h>
#include<conio .h >
void main ( )
{
int n , i =1, sum = 0 ;
clrscr ( ) ;
printf("Enter the range = "); OUTPUT :
#include<stdio.h>
#include<conio .h >
void main ( )
{
int n , c = 2;
clrscr ( );
printf("Enter a number to check if it is prime\n");
OUTPUT :
getch ( ) ;
}
#include<stdio.h>
#include<conio .h >
void main ( )
{
int j ;
clrscr ( ) ;
for ( j=0; j<=8; j++) OUTPUT :
{ 01235678
if (j==4)
{
continue ;
}
23. To test whether the given character is Vowel or not. ( using switch case )
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr ( );
printf("Enter a character=") ;
OUTPUT :
switch(week) OUTPUT:
{
case 1: printf("MONDAY"); Enter Week number(1-7): 7
break;
SUNDAY
case 2: printf("TUESDAY");
break;
case 3: printf("WEDNESDAY");
break;
case 4: printf("THURSDAY");
break;
case 5: printf("FRIDAY");
break;
case 6: printf("SATURDAY");
break;
case 7: printf("SUNDAY");
break;
default: printf("Invalid input! Please enter week number between 1-7");
}
getch();
}
void main()
{
int month;
clrscr();
}
getch();
}
26. To accept 10 numbers and make the average of the numbers using one
dimensional array.
#include <stdio.h>
#include<conio .h >
void main ( )
{ OUTPUT:
float a[15], sum =0 , avg , i ; Enter values of 10 numbers = 8 2
clrscr ( ) ; 6 3 9 7 11 21 30 22
printf("Enter values of 10 numbers = ");
for( i=0 ; i< 10 ; i ++) Average is = 11 . 900000
{
scanf("%f ",& a [ i ] );
sum = sum + a[ i ] ;
}
avg = sum / 10 ;
printf("Average is = %f ",avg);
getch ( ) ;
}
27. To accept 10 elements and sort them in descending order using one
dimensional array.
#include<stdio.h>
#include<conio.h>
void main ( )
{
int i, j,temp,a[10];
clrscr ( ); OUTPUT:
printf(“Enter 10 integer numbers: \n”);
for(i=0;i<10;i++); Enter 10 integer numbers: 5 2 10 7 6
scanf(“%d”,&a[i]); 1 4 3 8 9
for (i=0;i<10;i++)
{ The 10 numbers sorted in descending order
for(j=i+1;j<10;j++) are:
{
if ( a[i] < a[j] ) 10 9 8 7 6 5 4 3 2 1
{
temp=a[j];
a[ j]=a[i];
a [ i]=temp;
}
}
}
printf(“\n\nThe 10 numbers sorted in descending order are: \n”);
for(i=0;i<10;i++)
printf(“%d\t”,a[i]);
getch ( );
}
#include <stdio.h>
#include<conio.h>
void main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
getch ( ) ;
}
OUTPUT :
Enter the number of rows and columns of matrix
3 2
#include <stdio.h>
#include<conio .h >
int add ( int x , int y, int z);
void main ( ) OUTPUT :
{
int a ,b ,c, r ; Enter three numbers = 10 20 30
clrscr ( ) ; Summation is = 60
printf("Enter three numbers = ");
scanf("%d %d %d “, &a, &b, &c );
r = add (a, b, c);
printf (“ summation is = %d “, r);
getch ( );
}
int add ( int x, int y , int z)
{
int s;
s=x+y+z;
return s;
#include <stdio.h>
#include<conio .h >
int square( int x );
void main ( )
{
int n , r ;
clrscr ( ) ;
printf("Enter the number = ");
scanf("%d “, &n);
r = square ( n ) ; OUTPUT :
printf (“ Square value is = %d “, r);
getch ( ); Enter the number = 10
Square value is = 100
}
int square ( int x )
{
int s ;
s=x*x;
return s ;
}