c programming lab manual
c programming lab manual
Roll No : …………….………………………………………………...
Department : ………………………………………………..……………..
CS3271 – PROGRAMMING IN C LABORATORY
LABORATORY RECORD
Name : ………………………………………………………………
Roll No : …………….………………………………………………...
Department : ………………………………………………..……………...
Certified that this is the bonafide record of work done by the above student during the year
2023 - 2024.
Page Marks
Ex.No. Date Name of the Experiment
No. (10)
Sign
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
Program No:1
AREA AND CIRCUMFERENCE OF A CIRCLE
Date:
Aim:
To create a C program to calculate area and circumference of a circle.
Algorithm:
Step1: Start the Program.
#include<stdio.h>
#include<conio.h>
void main()
{
float radius, area, cf;
clrscr();
printf("\n Enter Radius of Circle:");
scanf("%f",&radius);
area = 3.14 * radius * radius;
printf("\nThe area of circle: is %f",area);
cf = 2 * 3.14 * radius;
printf("\n\nThe circumference of circle is: %f",cf);
getch();
}
Output:
Result:
Thus the C program to calculate area and circumference of a circle was
executed successfully.
Program No:2
SWAPPING TWO VARIABLES USING WITH
Date: AND WITHOUT THIRD VARIABLE
Aim:
To create a C program to swap values of two variables with and without using
third variable.
Algorithm:
Result:
Thus the C program to swap values of two variables was executed successfully.
Program No:3
FACTORIAL OF A NUMBER
Date:
Aim:
To create a C program to find factorial of a number.
Algorithm:
Step5: If the number is less than 0 then the message will display as Error! Factorial of
Negative Number Doesn't Exist.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
unsigned long long fact=1;
clrscr();
printf("Enter any Number:");
scanf("%d",&n);
if(n<0)
{
printf(" Error! Factorial of Negative Number Doesn't Exist");
}
else
{
for(i=1;i<=n;i++)
{
fact*=i;
}
printf("Factorial of %d is: %llu",n,fact);
}
getch();
return 0;
}
Output:
Result:
Aim:
To write a C program to display the following pattern .
*
**
***
****
*****
Algorithm:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,j;
clrscr();
printf("\nEnter no. of Rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
getch();
return 0;
}
Output:
Result:
Thus the C program to display the given pattern was executed successfully.
Program No:5
FINDING THE SUM OF LAST AND FIRST
Date: DIGIT OF GIVEN FOUR DIGIT
Aim:
To write a C program to finding the sum of last and first digit of given four digit.
Algorithm:
Step6: And find the sum of last and first digit using addition arithmetic operator.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,last,first,sum;
clrscr();
printf("\n Enter the four digit number:");
scanf("%d",&n);
last=n%10;
first=n/1000;
sum=last+first;
printf("\nthe first digit is:%d",first);
printf("\nthe last digit is:%d",last);
printf("\nthe sum of first and last digit is:%d",sum);
getch();
return 0;
}
Output:
Result:
Thus the C program to finding the sum of last and first digit of given four digit was
executed successfully.
Program No:6
REVERSE A GIVEN INTEGER
Date:
Aim:
To write a C program to reverse a given integer.
Algorithm:
Step1: Start the Program.
#include<stdio.h>
#include<conio.h>
int main()
{
int n, reverse=0, remainder;
printf("Enter an Integer:");
scanf("%d",&n);
while (n != 0)
{
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed Number is : %d", reverse);
getch();
return 0;
}
Output:
Result:
Thus the C program to reverse a given integer was executed successfully.
Program No:7
ACCESS AN ELEMENT IN 2D ARRAY
Date:
Aim:
To write the C program to access an element in 2D Array.
Algorithm:
Step1: Start the Program.
#include<stdio.h>
#include<conio.h>
int main(){
int i,j,arr[5][5],n,m;
printf("enter no of rows & cols:");
scanf("%d %d",&n,&m);
arr[5][5]=arr[n][m];
printf("enter the element for each row and col:\n");
for (i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("arr[%d][%d]:",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n Element present in an Array:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();
return 0;
}
Output:
Result:
Thus the C program to access an element in 2D Array was executed successfully.
Program No:8
MULTIPLY TWO 3 × 3 MATRICES
Date:
Aim:
To write the C program to multiply two 3 × 3 matrices.
Algorithm:
Step1: Start the Program.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
int a[3][3],b[3][3],multi[3][3];
printf("Enter elements of first matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("b[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
multi[i][j]=0;
for(k=0;k<3;k++)
{
multi[i][j]=multi[i][j]+a[i][j]*b[k][j];
}
}
}
printf("Multiplied matrix is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",multi[i][j]);
}
printf("\n");
}
getch();
return 0;
}
Output:
Result:
Aim:
To write the C program to count the number of vowels present in this string.
Algorithm:
Step1: Start the Program.
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100];
int i,count=0;
printf("Enter a String:");
scanf("%s",&str);
for(i=0;str[i]!= '\0';i++)
{
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
{
count++;
}
}
printf("vowel count = %d\n",count);
getch();
return 0;
}
Output:
Result:
Thus the C program to count the number of vowels present in this string was executed
successfully.
Program No:10
COPY STRING
Date:
Aim:
To write a C program to copy one string to another string.
Algorithm:
Step3: Read the string as an input using gets() function. When we use the gets() function
we have to include <string.h>.
Step4: Take the string1 elements one by one and assign to string2.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int i;
char s1[100],s2[100];
clrscr();
printf("\n Enter any String: ");
gets(s1);
for(i=0;s1[i]!=0;i++)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("\n The copied String is : %s",s2);
getch();
return 0;
}
Output:
Result:
Thus the C program to copy one string to another string was executed successfully.
Program No:11
FACTORIAL USING RECURSION
Date:
Aim:
To write a C program to calculate factorial of a number using recursion.
Algorithm:
Step5: And call factorial() function to find the factorial of given number.
Step6: The function factorial() will call by itself and return the result.
#include<stdio.h>
#include<conio.h>
long factorial(int n);
void main()
{
int num;
long fact;
clrscr();
printf("Enter a number : ");
scanf("%d",&num);
fact = factorial(num);
printf("Factorial of %d is %ld\n",num, fact);
getch();
}
long factorial(int n)
{
if(n==0)
{
return 1;
}
else
{
return (n*factorial(n-1));
}
}
Output:
Result:
Thus the C program to calculate factorial of a number using recursion was executed
successfully.
Program No:12
REVERSE THE NUMBER USING RECURSION
Date:
Aim:
To write a C program to read an integer number and print the reverse of that number
using recursion.
Algorithm:
Step1: Start the Program
Step2: Declare the variables sum, rem as global variables and rev, num as local
variables.
Step5: Then the recursive function will call by itself and return the reversed integer.
#include<stdio.h>
#include<conio.h>
int sum=0,rem;
int revfun(int num){
if(num)
{
rem=num%10;
sum=sum*10*rem;
refun(num/10);
}
else
{
return sum;
}
return sum;
}
int main()
{
int num, rev;
clrscr();
printf(" Enter any number:");
scanf("%d", &num);
rev=revfun(num);
printf("The reverse of entered number is :%d",rev);
getch();
return 0;
}
Output:
Result:
Thus the C program to read an integer number and print the reverse of that number
using recursion was executed successfully.