Lab File
Lab File
Practical File
Session: 2021-2022
#include<stdio.h>
int main()
{
int n;
printf("Enter a number");
scanf("%d",&n);
if(n%2==0)
printf("%d is even",n);
else
printf("%d is odd",n);
return 0;
}
OUTPUT:
*Practical 2-
Write a program to find whether a number is Prime or not.
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
OUTPUT:
*Practical 3-
Write a program to swap two numbers using third variable.
#include<stdio.h>
int main()
{
int v1, v2, v3;
printf("Enter the value of v1");
scanf("%d", &v1);
printf("Enter the value of v2");
scanf("%d", &v2);
v3 = v1;
v1 = v2;
v2 = v3;
printf("After exchanging/swapping variables: v1 = %d and v2 = %d",
v1,v2);
return(0);
}
OUTPUT:
*Practical 4-
Write a program to check whether a character is vowel (capital and small) or
consonant.
Note: This program assumes that the user will enter an alphabet. If the user enters a
non-alphabetic character, it displays that character is consonant. Evaluate for both
lowercase and uppercase.
#include<stdio.h>
int main()
{
char c;
int lowercase, uppercase;
printf("Enter a character: ");
scanf("%c",&c);
lowercase = (c=='a' || c=='e' ||c=='i' || c=='o' || c=='u');
uppercase = (c=='A' || c=='E' ||c=='I' || c=='O' || c=='U');
if (lowercase||uppercase)
printf("character %c is a vowel", c);
else
printf("character %c is a consonant", c);
return 0;
}
OUTPUT:
* Practical 5-
Write a program to find sum and product of digits of any number.
#include <stdio.h>
int main()
{
int n;
int dig, sum,pro;
while(n>0)
{
dig=n%10;
sum+=dig;
pro*=dig;
n=n/10;
}
return 0;
}
OUTPUT:
*Practical 6-
Write a program to find the greatest number among two numbers using
conditional operator.
#include <stdio.h>
int main() {
int a, b, largest;
printf("Please Enter Two Different Values\n");
scanf("%d %d", &a, &b);
if(a == b)
{
printf("Both are Equal\n");
}
else {
largest = (a > b) ? a : b;
printf("%d is Largest\n", largest);
}
return 0;
}
OUTPUT:
*Practical 7-
Write a program for calculator including arithmetic operators using switch case
statement.
#include<stdio.h>
int main()
{
int op;
float a,b;
printf("Enter two operands: ");
scanf("%f %f", &a, &b);
printf("Enter an option: ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("addition of a and b is %f", a+b);
break;
case 2:
printf("subtraction of a and b is %f", a-b);
break;
case 3:
printf("Multiplication of a and b is %f", a*b);
break;
case 4:
printf("division of a and b is %f", a/b);
break;
default:
printf("You have typed incorrect operator");
}
return 0;
}
OUTPUT:
*Practical 8-
Write a program to find factorial of a number using recursive function.
#include<stdio.h>
int rec(int);
int main()
{
int num, fact;
printf("\nEnter any integer number:");
scanf("%d",&num);
fact =rec(num);
printf("\nfactorial of %d is: %d",num, fact);
return 0;
}
int rec(int n)
{
if(n==0)
return(1);
return(n*rec(n-1));
}
OUTPUT:
*Practical 9-
Write a program to add two numbers using pointer.
#include<stdio.h>
int main()
{
int a,b,*x,*y,sum;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
x = &a;
y = &b;
sum = *x + *y;
printf("\n Sum of two numbers is %d",sum);
return 0;
}
OUTPUT:
*Practical 10-
Write a program to take 6 values from the user and store them in an array.
#include <stdio.h>
int main() {
int values[6];
printf("Enter 6 integers: ");
for(int i = 0; i < 6; ++i) {
scanf("%d", &values[i]);
}
OUTPUT: