0% found this document useful (0 votes)
144 views18 pages

Programming in C Lab Manual FOR Diploma in Ece/Eee

This document contains 15 C programming examples covering basic concepts like: - Performing arithmetic operations on two numbers - Interchanging values of two variables with and without a third variable - Calculating the area of a rectangle - Identifying the greater of two numbers - Checking if a number is even or odd - Classifying triangles based on side lengths - Finding roots of a quadratic equation - Calculating factorials - Computing sums of natural and odd numbers - Printing patterns using loops and characters
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views18 pages

Programming in C Lab Manual FOR Diploma in Ece/Eee

This document contains 15 C programming examples covering basic concepts like: - Performing arithmetic operations on two numbers - Interchanging values of two variables with and without a third variable - Calculating the area of a rectangle - Identifying the greater of two numbers - Checking if a number is even or odd - Classifying triangles based on side lengths - Finding roots of a quadratic equation - Calculating factorials - Computing sums of natural and odd numbers - Printing patterns using loops and characters
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

PROGRAMMING IN C

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

2. To interchange the numeric values of two variables using third variable.

# 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 ( );

4. To calculate area of a rectangle .


# include <stdio.h>
# include <conio.h>
void main ( )
{
int a , b , area ; OUTPUT :
clrscr ( ) ;
printf(“ Enter length of the rectangle =”) ; Enter length of the rectangle = 15
Enter width of the rectangle =10
scanf ( “ %d “, &a); Area is = 150
printf(“ Enter width of the rectangle =”) ;
scanf ( “ %d “, &b);
area = a * b ;
printf( “Area is = %d”, area);
getch ( );

5. Identify greater number between two numbers using C program.

# 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=");

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


if( a + b > c || a + c > b || b + c > a)
{
printf("triangle is possible \n");
if(a==b && b==c) OUTPUT :
{
printf("Equilateral triangle"); Enter the value of three sides = 5 8 5
triangle is possible
} Isoceles triangle

else
{
if(a= =b || b= =c || a= =c)
printf(" Isoceles triangle ");
else
printf(" Scalene triangle ");
}
}
else
printf(" Triangle is not possible ");
getch ( ) ;
}

8. To find the roots of a quadratic equation.

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

9. Find the factorial of given number.

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

10. To find the sum of n natural numbers.


#include <stdio.h>
#include<conio .h >
void main ( )
{
int n, i, sum=0 ;
clrscr ( ) ;
printf("Enter an integer = "); OUTPUT :

scanf("%d",&n); Enter an integer = 7


for(i =1; i <=n ; i++) Sum is = 28
{
sum = sum + i ;
}
printf("Sum is = %d",sum);
getch ( ) ;
}

11. Print the sum of 1 + 3 + 5 + 7 +…………..+ n

#include <stdio.h>
#include<conio .h >
void main ( )
{
int n, i, sum=0 ;
clrscr ( ) ;
printf("Enter the range = "); OUTPUT :

scanf("%d",&n); Enter the range =9


for(i =1; i <=n ; i = i + 2) Sum is = 25
{
sum = sum + i ;
}
printf("Sum is = %d",sum);
getch ( ) ;
}
12. Print the pattern .
*
* *
* * *
* * * *
* * * * *

#include <stdio.h>
#include<conio .h >
void main ( )
{
int i, j, rows;
clrscr ( ) ;
printf("Enter the number of rows: ");
scanf("%d",&rows);

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


{
for(j=1; j<=i; j++)
{
printf (" * ");
}
printf("\n");
}
getch ( ) ;
}

13. Print the pattern


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include <stdio.h>
#include<conio .h >
void main ( )
{
int i, j, rows;
clrscr ( ) ;
printf("Enter the number of rows: ");
scanf("%d",&rows);

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


{
for(j=1; j<= i ; j++)
{
printf (" %d" , j ) ;
}
printf("\n") ;
}
getch ( ) ;
}

14. Print the pattern


A
A B
A B C
A B C D
A B C D E
#include <stdio.h>
#include<conio .h >
void main( )
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1 ; j<=i ; j++)
{
printf("%c" ,'A' + j-1);
}
printf("\n");
}

getch ( );
}

15. Print the pattern


* * * * *
* * * *
* * *
* *
*

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

16. Check whether an input number is palindrome or not.


#include <stdio.h>
#include < conio. h >
void main( )
{
int n, reverse = 0, temp ,r;
clrscr ( ) ;
printf ("Enter a number to check if it is a palindrome or not=");
scanf ("%d", &n);
temp = n;

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

17. To find Sum of digits of an integer.

# include < stdio. h>


# include < conio . h>
void main( )
{
int n, sum = 0, r ;
printf(" Enter an integer=" ) ;
OUTPUT :
scanf("%d", &n);
Enter a number to check if it is a
while (n != 0) palindrome or not= 151
{
r = n % 10; 151 is a palindrome number
sum = sum + r ;
n = n / 10 ;
}
printf("Sum of digits =%d “ , sum);
getch ( ) ;
}

18. Find the G.C.D and L.C.M of two numbers.

# include <stdio.h>
# include <conio. h>
void main ( )
{
int a, b, x, y, t, gcd, lcm ;
clrscr ( );
printf("Enter two integers=") ;

scanf("%d%d", &x, &y) ; OUTPUT :

Enter two integers = 15 18


a = x;
b = y; Greatest common divisor of 15 and 18 = 3
Least common divisor of 15 and 18 = 90
while (b != 0)
{
t = b;
b = a % b;
a = t;
}

gcd = a;
lcm = (x*y) / gcd;

printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);


printf("Least common multiple of %d and %d = %d\n", x, y, lcm);

getch ( ) ;
}

19. Print the sum of 2 + 4 + 6 + 8 +………………..+n

#include <stdio.h>
#include<conio .h >
void main ( )
{
int n, i = 2, sum=0 ;
clrscr ( ) ;
printf("Enter the range = "); OUTPUT :

scanf("%d",&n); Enter the range = 10


do Sum is = 30
{
sum = sum + i ;
i=i+2;
}while ( i <= n) ;
printf("Sum is = %d", sum) ;
getch ( ) ;
}

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 :

scanf("%d",&n); Enter the range = 5


do Sum is = 55
{
sum = sum + i * i ;
i++;
}while ( i <= n) ;
printf("Sum is = %d", sum) ;
getch ( ) ;
}

21. To check a given number is prime or not.

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

scanf("%d",&n); Enter a number to check if it is


prime Sum is = 17
for ( c = 2 ; c <= n - 1 ; c++ ) 17 is prime
{
if ( n%c = = 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);

getch ( ) ;
}

22. Use of “continue “ statement .

#include<stdio.h>
#include<conio .h >
void main ( )
{
int j ;
clrscr ( ) ;
for ( j=0; j<=8; j++) OUTPUT :

{ 01235678
if (j==4)
{
continue ;
}

printf("%d ", j);


}
getch ( ) ;
}

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 :

scanf("%c", &ch); Enter a character= E


switch ( ch ) E is vowel
{
case 'a' :
case 'A' :
case 'e' :
case 'E' :
case 'i' :
case 'I' :
case 'o' :
case 'O' :
case 'U' :
case 'u' :
printf(" %c is vowel ", ch);
break;
default:
printf(" %c is not vowel ",ch);
}
getch ( );
}

24 . Write a C program to print day of week name using switch case.


#include<stdio.h>
#include<conio.h>
void main()
{
int week;
clrscr();
printf("Enter week number(1-7): ");
scanf("%d", &week);

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

25. C program to print number of days in a month using switch case?


#include<stdio.h>
#include<conio.h>

void main()
{
int month;
clrscr();

printf("Enter month number(1-12): "); OUTPUT:


scanf("%d", &month);
Enter month number(1-12): 2
switch(month)
{ 28/29 days
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: printf("31 days");
break;
case 4:
case 6:
case 9:
case 11: printf("30 days");
break;
case 2: printf("28/29 days");
break;
default: printf("Invalid input! Please enter month number between 1-12");

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

28. Find out length of a string .


#include <stdio.h>
#include<conio.h>
void main() OUTPUT :
{ Enter a string: programming
char s[90] , i; Length of string : 11
printf("Enter a string: ");
scanf("%s",s);
for(i=0; s[i]!='\0'; i++);
printf("Length of string: %d",i);
getch ( );
}
29. Find out addition of two matrices .

#include <stdio.h>
#include<conio.h>
void main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]) ;

printf("Sum of entered matrices:-\n");

for (c = 0 ; c < m; c++) {


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}

getch ( ) ;
}

OUTPUT :
Enter the number of rows and columns of matrix
3 2

Enter the elements of first matrix


5 8
4 1
5 2
Enter the elements of second matrix
2 6
4 7
1 2
Sum of entered
matrices 7 14
8 8
6 4
30. To find the summation of three numbers using function.

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

31. To find the maximum between two numbers using function .


#include <stdio.h>
#include<conio .h >
int max ( int x , int y, );
void main ( )
{
int a ,b , r ;
clrscr ( ) ;
printf("Enter two numbers = ");

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


r = max (a, b); OUTPUT :
printf (“ maximum is = %d “, r);
getch ( ); Enter two numbers = 10 20
maximum is = 20
}
int max ( int x, int y )
{
int s;
if (x > y)
s=x;
else
s=y;
return s ;
}
32. Find out square of a number using function .

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

You might also like