Experiment 6 To 18
Experiment 6 To 18
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));
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
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++)
{
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
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
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() ;
scanf("%d", &n) ;
if ( n%2 == 0 )
else
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;
max= a;
if(b>max)max=b;
if(c>max)max=c;
Output
Enter a, b, c:
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
scanf("%d", &n) ;
if ( n%5 == 0 )
Experiment – 12
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() ;
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() ;
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;
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”);
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”);
break;
default:
printf(“Unrecognized number”);
Output:
June
Experiment – 13
#include<stdio.h>
int main()
int num;
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
Experiment – 14
#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
Experment – 15
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
1 3 5 9
Experiment – 16
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");
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
0, 2
0, 3
1, 0
1, 1
1, 2
1, 3
Experiment – 17
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;
for(i=0;i<5;i++)
scanf("%d", &array[i]);
for(i=0;i<5;i++)
getch();
Output:
78
45
12
89
56
Experiment – 18
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
15
16
the matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
sum=136
}}Output
Enter the elements of the matrix:1
2
3
4
5
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++)
{
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
}
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
matrix b is:
1 2 3
4 5 6
7 8 9
Multiplication is c:
30 36 42
66 81 96
102 126 150