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

Lab File

The document contains 10 programming practices written in C language to perform basic operations like checking even-odd, prime, swapping, finding vowel-consonant, calculating sum-product of digits, finding greatest among two numbers, building a basic calculator, calculating factorial recursively, adding two numbers using pointers, and taking input in an array. The practices cover conditional statements, loops, functions, pointers and arrays to solve common problems encountered while learning C programming.

Uploaded by

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

Lab File

The document contains 10 programming practices written in C language to perform basic operations like checking even-odd, prime, swapping, finding vowel-consonant, calculating sum-product of digits, finding greatest among two numbers, building a basic calculator, calculating factorial recursively, adding two numbers using pointers, and taking input in an array. The practices cover conditional statements, loops, functions, pointers and arrays to solve common problems encountered while learning C programming.

Uploaded by

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

Programming Using C

Practical File
Session: 2021-2022

Submitted To: Submitted By:


Ms. Shivani Chauhan Name-
Lecturer Computer Branch-
Semester-
College-
Table of Contents

S. No. Practical Signature

1 Write a program to find whether a number is even or odd.

2 Write a program to find whether a number is Prime or not.

Write a program to swap two numbers using third


3
variable.

Write a program to check whether a character is vowel


4
(capital and small) or consonant.

Write a program to find sum and product of digits of any


5
number.

Write a program to find the greatest number among two


6
numbers using conditional operator.

Write a program for calculator including arithmetic


7
operators using switch case statement.

Write a program to find factorial of a number using


8
recursive function.

9 Write a program to add two numbers using pointer.

Write a program to take 6 values from the user and store


10
them in an array.
*Practical 1-
Write a program to find whether a number is even or odd.

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

printf("\nEnter an integer number :");


scanf("%d",&n);
sum=0;
pro=1;

while(n>0)
{
dig=n%10;
sum+=dig;
pro*=dig;
n=n/10;
}

printf("\nSUM of all Digits is : %d",sum);


printf("\nPRODUCT of all digits: %d",pro);

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

printf("Displaying integers: ");


for(int i = 0; i < 6; ++i) {
printf("%d\n", values[i]);
}
return 0;
}

OUTPUT:

You might also like