0% found this document useful (0 votes)
12 views27 pages

Experiment 6 To 18

This document is a lab manual for a Programming in C course, detailing various experiments and exercises involving C programming concepts such as operators, input/output, control statements, and arrays. Each experiment includes sample C programs with their respective outputs, demonstrating practical applications of the concepts taught. The manual is prepared by B. Naresh and spans multiple programming topics suitable for a second-semester curriculum.

Uploaded by

geriwe2004
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)
12 views27 pages

Experiment 6 To 18

This document is a lab manual for a Programming in C course, detailing various experiments and exercises involving C programming concepts such as operators, input/output, control statements, and arrays. Each experiment includes sample C programs with their respective outputs, demonstrating practical applications of the concepts taught. The manual is prepared by B. Naresh and spans multiple programming topics suitable for a second-semester curriculum.

Uploaded by

geriwe2004
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/ 27

II Semester - Programming in C Lab Manual

Output: Enter an integer: 8


8*11=88

Experiment – 6
Exercise on special operators
Program 15: Write a C program for Comma Operator
#include <stdio.h>
int main()
{
int a;
a=1,2,3;
//a = (1, 2, 3); // Evaluated as (a = 1), 2, 3
printf("%d", a);
return 0;
}
Output : a=1
Program 16: Write a C program for and *(pointer) operator
#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
Output: 50
Program 17: Write a C program for sizeof operator
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));

Prepared by B. Naresh Page 14


II Semester - Programming in C Lab Manual

return 0;
}
Output:
storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8
Program 17.2: Write a C program using comma operator inside for loop
#include <stdio.h>
int main()
{
int i, j;
for(i=0, j=0; i<5; i++)
{
printf("\n value of J: %d", j);
j++;
}
return(0);
}
Output:
value of J: 0
value of J: 1
value of J: 2
value of J: 3
value of J: 4

Prepared by B. Naresh Page 15


II Semester - Programming in C Lab Manual

Experiment – 7
Exercise on input and output of characters
Program 18: Write a C program to find sum of digits in a number
#include <stdio.h>
#include<conio.h>
int sumOfDigit(int num);
int s,a;
void main()
{
int num, sum;
//clrscr();
printf("Enter a number\t");
scanf("%d", &num);
sum=sumOfDigit(num);
printf("The sum of digit %d is %d", num, sum);
getch();
}
int sumOfDigit(int num)
{
s=s+(num%10);
a=num/10;
if(a>0)
{
sumOfDigit(a);

}
return s;
}
Output:
Enter a number 12345
The sum of digit 12345 is 15
Program 19: Write a C program to find Factorial of a number using for loop
#include <stdio.h>
#include<conio.h>
void main()
{
int fact, i, n;
fact=1;
printf("Enter the number:");
scanf("%d", &n);
for(i=1; i<=n; i++)
{

Prepared by B. Naresh Page 16


II Semester - Programming in C Lab Manual

fact=fact*i;
}
printf("Factorial of %d is %d", n, fact);
getch();
}
Output: Enter the number:7
Factorial of 7 is 5040
Program 20: Write a C program to find Factorial of a Number using Recursion.
#include <stdio.h>
#include<conio.h>
int factorial(int n);
void main()
{
int fact, i, n;
printf("Enter the number: ");
scanf("%d", &n);
fact=factorial(n);
printf("Factorial of %d is %d", n, fact);
getch();
}
int factorial(int n)
{
int fact=1;
if(n==1)
{
return fact;
}
else
{
fact=n*factorial(n-1);
return fact;
} }
Output: Enter the number: 5
Factorial of 5 is 120

Prepared by B. Naresh Page 17


II Semester - Programming in C Lab Manual

Experiment – 8
Exercise on formatted input and output
Program 21: Write a C Program which prints integer number with integer input.
#include <stdio.h>
int main()
{
int testInteger;
printf("Enter an Integer:");
scanf("%d", &testInteger);
printf("Enteed Number = %d", testInteger);
return 0;
}
Output: Enter an Integer:4
Enteed Number = 4
Program 22: Write a C Program which prints float number with floating input
#include <stdio.h>
int main()
{
float f;
printf("Enter a floating input: ");
scanf("%f", &f);
printf("Floating Value = %f", f);
return 0;
}
Output: Enter a floating input: 14.56
Floating Value = 14.560000
Program 23: Write a C program to print character and ASCII value of given
Character.
#include <stdio.h>
int main()
{
char ch;
printf("Enter a Character: ");
scanf("%c", &ch);
printf("Entered Character %c \n", ch);
printf("ASCII value of %c is %d.", ch, ch);
return 0;
}
Output: Enter a Character: f
Entered Character f
ASCII value of f is 102.
Experiment – 9

Prepared by B. Naresh Page 18


II Semester - Programming in C Lab Manual

Exercise on simple if statement


Program 24: Write a C program to Test whether given number is divisible by 5?
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
//clrscr() ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n%5 == 0 )
printf("Given number is divisible by 5\n");
printf("statement does not belong to if!!!");
}
Output: Enter any integer number: 25
Given number is divisible by 5
statement does not belong to if!!!
Program 25: Write a C program to find the largest of three given numbers by using if
statement.
#include<stdio.h>
#include<conio.h>
void main()
{
float a, b, c, max;
printf("Enter a, b, c:\n");
scanf("%f%f%f", &a, &b, &c);
max=a;
if(b>max)max=b;
if(c>max)max=c;
printf("Largest of three given numbers = %f\n", max);
}Output:
Enter a, b, c: 12.00 5.00 30.00
Largest of three given numbers = 30.00

Prepared by B. Naresh Page 19


II Semester - Programming in C Lab Manual

Experiment – 10
Exercise on if..else statement
Program 26: Write a C program to test whether given number is even or odd by using if
else statement.

#include<stdio.h>

#include<conio.h>

void main(){

int n ;

clrscr() ;

printf("Enter any integer number: ") ;

scanf("%d", &n) ;

if ( n%2 == 0 )

printf("Given number is EVEN\n") ;

else

printf("Given number is ODD\n") ;

Output: Enter an integer number: 24

Given number is EVEN

Prepared by B. Naresh Page 20


II Semester - Programming in C Lab Manual

Experiment – 11
Exercise on else..if ladder statement
Program 27: Write a C program to find the largest of three given numbers by using if
else ladder.

#include<stdio.h>

#include<conio.h>

main()

float a, b, c, max;

printf(“ Enter a, b, c:\n);

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

max= a;

if(b>max)max=b;

if(c>max)max=c;

printf(“largest of three given numbers = %f\n”, max);

Output

Enter a, b, c:

12.00 5.00 30.00

Largest of three given numbers = 30.00

Program 28: Write a C program to Test whether given number is divisible by 5.

#include<stdio.h>

#include<conio.h>

void main(){

int n ;

clrscr() ;

printf("Enter any integer number: ") ;

Prepared by B. Naresh Page 21


II Semester - Programming in C Lab Manual

scanf("%d", &n) ;

if ( n%5 == 0 )

printf("Given number is divisible by 5\n") ;

printf("statement does not belong to if!!!") ;

Output: Enter any integer number: 32

statement does not belong to if!!!

Prepared by B. Naresh Page 22


II Semester - Programming in C Lab Manual

Experiment – 12

Exercise on switch statement

Program 29: Write a C program to Display pressed digit in words by using switch
statement.

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

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

printf("Enter any digit: ") ;


scanf("%d", &n) ;

switch( n )
{
case 0: printf("ZERO") ;
break ;
case 1: printf("ONE") ;
break ;
case 2: printf("TWO") ;
break ;
case 3: printf("THREE") ;
break ;
case 4: printf("FOUR") ;
break ;
case 5: printf("FIVE") ;
break ;
case 6: printf("SIX") ;
break ;
case 7: printf("SEVEN") ;
break ;
case 8: printf("EIGHT") ;
break ;
case 9: printf("NINE") ;
break ;
default: printf("Not a Digit") ;
}
getch() ;

Prepared by B. Naresh Page 23


II Semester - Programming in C Lab Manual
}
Output

Enter any digit: 7

Seven

Program 30: Write a C program to read a value in range 1 to 12 and print the name of
that month by using switch statement.

#include<stdio.h>

#include<conio.h>

main()

int month;

printf(“Enter a number between 1 to 12\n:”);

scanf(“%d”, &month);

switch(month)

case 1:

printf(“January\n”);

break;

case 2:

printf(“February\n”);

break;

case 3:

printf(“March\n”);

break;

case 4:

printf(“April\n”);

Prepared by B. Naresh Page 24


II Semester - Programming in C Lab Manual

break;

case 5:

printf(“May\n”);

break;

case 6:

printf(“June\n”);

break;

case 7:

printf(“July\n”);

break;

case 8:

printf(“August\n”);

break;

case 9:

printf(“September\n”);

break;

case 10:

printf(“October\n”);

break;

case 11:

printf(“November\n”);

break;

case 12:

printf(“December\n”);

Prepared by B. Naresh Page 25


II Semester - Programming in C Lab Manual

break;

default:

printf(“Unrecognized number”);

Output:

Enter a number between 1 to 12: 6

June

Prepared by B. Naresh Page 26


II Semester - Programming in C Lab Manual

Experiment – 13

Exercise on conditional operator


Program 31: Write a C program to check whether given number is odd or even

#include<stdio.h>

int main()

int num;

printf(“Enter the number : “);

scanf(“%d”, &num);

(num%2==0)?printf(“Even”):printf(“Odd”);

Output:
Enter the number: 7
Odd

Program 32: Write a C program to find the eligible for voting by age.
#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age); // taking user input for age variable
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for
voting")); // conditional operator
return 0;
}
Output: Enter your age23

eligible for voting

Prepared by B. Naresh Page 27


II Semester - Programming in C Lab Manual

Experiment – 14

Exercise on while statement


Program 33: Write a C program to display even numbers upto 10 by using while
statement.

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

void main(){
int n = 0;
clrscr() ;
printf("Even numbers upto 10\n");

while( n <= 10 )
{
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}
getch() ;
}
Output:

Even numbers up to 10

0 2 4 6 8 10

Prepared by B. Naresh Page 28


II Semester - Programming in C Lab Manual

Experment – 15

Exercise on do...while statement

Program 34: Write a C program to display odd numbers upto 10 by using do while
loop.
#include<stdio.h>
#include<conio.h>

void main(){
int n = 0;
clrscr() ;
printf("Odd numbers upto 10\n");

do
{
if( n%2 == 1)
printf("%d\t", n) ;
n++ ;
}while( n <= 10 ) ;

getch() ;
}
Output

Enter odd numbers up to 10

1 3 5 9

Prepared by B. Naresh Page 29


II Semester - Programming in C Lab Manual

Experiment – 16

Exercise on for statement

Program 35: Write a C program display even numbers upto 10 by using for statement.
#include<stdio.h>
#include<conio.h>

void main(){
int n ;
clrscr() ;
printf("Even numbers upto 10\n");

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


{
if( n%2 == 0)
printf("%d\t", n) ;
}

getch() ;
}
Output
Even numbers upto 10
0 2 4 6 8 10
Program 36: Write a C program to display i and j values by using nested for loop.
#include<stdio.h>
void main()
{

int i, j;
for(i=0; i<2; i++)
{
for(j=0; j<4; j++)
{
printf("%d, %d\n", i,j);
}
printf("\n");
}
}
Output: 0, 0
0, 1

Prepared by B. Naresh Page 30


II Semester - Programming in C Lab Manual

0, 2
0, 3

1, 0
1, 1
1, 2
1, 3

Prepared by B. Naresh Page 31


II Semester - Programming in C Lab Manual

Experiment – 17

Exercise on one dimensional arrays

Program 37: Write a C program to store the elements in the array and to print them
from the array

#include<stdio.h>

void main()

int array[5], i;

printf("Ente 5 numbers to store then in array \n");

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

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

}printf("Elements in the array are - \n \n");

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

printf("Element stored at a[%d] = %d \n", i, array[i]);

getch();

Output:

Ente 5 numbers to store then in array

78

45

12

89

56

Elements in the array are -

Prepared by B. Naresh Page 32


II Semester - Programming in C Lab Manual

Element stored at a[0] = 78

Element stored at a[1] = 45

Element stored at a[2] = 12

Element stored at a[3] = 89

Element stored at a[4] = 56

Prepared by B. Naresh Page 33


II Semester - Programming in C Lab Manual

Experiment – 18

Exercise on two dimensional arrays

Program 38: Write a C program to print matrix and calculate sum by using 2d array
#include <stdio.h>
int main()
{
int x[4][4], i,j,sum=0;
printf("Enter the elements of the array:");
for(i=0;i<4;i++) // entering array elements
{
for(j=0; j<4;j++)
{
scanf("%d", &x[i][j]);
}
}
printf(" the matrix is: \n");
for(i=0;i<4;i++) // printing array elements
{
for(j=0; j<4;j++)
{
printf("%d\t", x[i][j]);
sum=sum+x[i][j];
}
printf("\n");
}
printf("\n sum=%d", sum);
}
Output
Enter the elements of the array:1
2
3
4
5
6
7
8
9
10
11
12
13
14

Prepared by B. Naresh Page 34


II Semester - Programming in C Lab Manual

15
16
the matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

sum=136

Program 39: Write a C program to print Transpose of matrix by using 2D/Multi


dimensional array
#include<stdio.h>
int main()
{
int x[2][3], i,j;
printf("Enter the elements of the matrix:");
for(i=0;i<2;i++) // entering array elements
{
for(j=0; j<3;j++)
{
scanf("%d", &x[i][j]);
}
}
printf(" the matrix is: \n");
for(i=0;i<2;i++) // printing array elements
{
for(j=0; j<3;j++)
{
printf("%d\t", x[i][j]);
}
printf("\n");
}
printf("Transpose of matrix is: \n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t", x[j][i]);
}
printf("\n");

}}Output
Enter the elements of the matrix:1
2
3
4
5

Prepared by B. Naresh Page 35


II Semester - Programming in C Lab Manual

6
the matrix is: 1 2 3
4 5 6
Transpose of matrix is:
1 4
2 5
3 6
Program 40: Write a C program to add two matrixs by using 2D/Multi dimensional
array
#include<stdio.h>
int main()
{
int A[2][3],B[2][3],C[2][3],i,j;
printf("Enter the elements of the matrix A:");
for(i=0;i<2;i++) // entering array elements of A matrix
{
for(j=0; j<3;j++)
{
scanf("%d", &A[i][j]);
}
}
printf("Enter the elements of the matrix B:");
for(i=0;i<2;i++) // entering array elements of B matrix
{
for(j=0; j<3;j++)
{
scanf("%d", &B[i][j]);
}
}
printf(" matrix A is: \n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t", A[i][j]);
}
printf("\n");

}
printf(" matrix B is: \n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{

Prepared by B. Naresh Page 36


II Semester - Programming in C Lab Manual

printf("%d\t", B[i][j]);
}
printf("\n");

}
printf(" The matrix C is: \n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=A[i][j]+B[i][j];
printf("%d \t", C[i][j]);
}
printf("\n");
}
}
Output
Enter the elements of the matrix A:1
2
3
4
5
6
Enter the elements of the matrix B:1
2
3
4
5
6
matrix A is:
1 2 3
4 5 6
matrix B is:
1 2 3
4 5 6
The matrix C is:
2 4 6
8 10 12

Prepared by B. Naresh Page 37


II Semester - Programming in C Lab Manual

Program 41: Write a C program to find matrix Multiplication by using 2D/Multi


dimensional array
#include <stdio.h>
#define N 50
int main()
{
int a[N][N], b[N][N], c[N][N], i, j, k, sum, m, n, p, q;
printf("enter Enter rows and columns of a matrix: \n");
scanf("%d%d", &m, &n);
printf("Enter a matrix: \n");
for(i=0; i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &a[i][j]);
}
}
printf("enter Enter rows and columns of b matrix: \n");
scanf("%d%d", &p, &q);
printf("Enter a matrix: \n");
for(i=0; i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d", &b[i][j]);
}
}
printf(" matrix a is: \n");
for(i=0; i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t", a[i][j]);
}
printf("\n");
}
printf("\n matrix b is: \n");
for(i=0; i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d \t", b[i][j]);
}
printf("\n");

Prepared by B. Naresh Page 38


II Semester - Programming in C Lab Manual

}
if(n!=p)
{
printf(" can not multiply");
}
else
{
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
{
sum=0;
for(k=0; k<m; k++)
{
sum=sum+(a[i][k]*b[k][j]);
}
c[i][j]=sum;
}
}
printf("Multiplication is c: \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d \t", c[i][j]);
}
printf("\n");
}
}
}
Output
Enter rows and columns of a matrix:
3
3
Enter a matrix:
1
2
3
4
5
6
7
8
9

Prepared by B. Naresh Page 39


II Semester - Programming in C Lab Manual

Enter rows and columns of b matrix:


3
3
Enter a matrix:
1
2
3
4
5
6
7
8
9
matrix a is:
1 2 3
4 5 6
7 8 9

matrix b is:
1 2 3
4 5 6
7 8 9
Multiplication is c:
30 36 42
66 81 96
102 126 150

Prepared by B. Naresh Page 40

You might also like