0% found this document useful (0 votes)
2 views

c Lab experiments 5-8

The document contains a series of C programming exercises focusing on various operators, expressions, and input/output operations. It includes programs that demonstrate arithmetic, relational, logical, and special operators, as well as functions for calculating the sum of digits, factorial, and printing ASCII values. Each program is accompanied by sample outputs to illustrate their functionality.

Uploaded by

geriwe2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c Lab experiments 5-8

The document contains a series of C programming exercises focusing on various operators, expressions, and input/output operations. It includes programs that demonstrate arithmetic, relational, logical, and special operators, as well as functions for calculating the sum of digits, factorial, and printing ASCII values. Each program is accompanied by sample outputs to illustrate their functionality.

Uploaded by

geriwe2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment – 5

Exercise on operators and expressions


Program 9: Write a C program to perform all arithmetic operators by given
numbers.
#include <stdio.h>
int main()
{
int a,b, add,sub,mul,div,mod;
printf("Enter a and b values: ");
scanf("%d%d", &a, &b);
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}
Output:
Enter a and b values: 40 20
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
Program 10: write a C program by perform relational operator.
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
Output: m and n are not equal
Program 11: write a C program by using Logical Operators
#include <stdio.h>
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf("&& Operator : Both conditions are true\n");
}
if (o>p || p!=20)
{
printf("|| Operator : Only one condition is true\n");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are true\n");
}
else
{
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}
}
Output:
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false
Program 12: Write a C Program to find the biggest of three given numbers
#include <stdio.h>
void main()
{
int a, b, c;
printf("Enter the values of a, b and c: ");
scanf("%d%d%d", &a, &b, &c);
printf("a=%d\t b=%d\t c=%d\n", a, b, c);
if(a>b)
{
if(a>c)
{
printf("A is the greatest among three numbers \n");
}
else
{
printf("C is the greatest among three numbers\n");
}
}
else if(b>c)
printf("B is the greatest among three numbers\n");
else
printf("C is the greatest among three numbers\n");
}
Output1: Enter the values of a, b and c: 8 10 12
a=8 b=10 c=12
C is the greatest among three numbers
Output2: Enter the values of a, b and c: 100 80 95
a=100 b=80 c=95
A is the greatest among three numbers
Program 13: Write a C program to find the sum of N natural numbers.
#include <stdio.h>
void main()
{
int n, i, sum=0;
printf("Enter an integer number: \n");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
sum=sum+i;
}
printf("sum of first %d natural numbers=%d\n", n, sum);
}
Output: Enter an integer number:
100
Sum of first 100 natural numbers=5050
Program 14: Write a C program to print mathematical table.
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for(i=1; i<=10; ++i);
{
printf("%d*%d=%d\n", n, i, n*i);
}
return 0;
}
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));
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 18: 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.

You might also like