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

c programming lab manual

The document is a laboratory record for a programming course (CS3271) detailing various C programming experiments conducted in the year 2023-2024. It includes aims, algorithms, coding examples, and results for multiple programs, such as calculating the area of a circle, swapping variables, finding factorials, and more. Each program is structured with a clear aim, algorithm steps, coding, and output results, demonstrating practical applications of C programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

c programming lab manual

The document is a laboratory record for a programming course (CS3271) detailing various C programming experiments conducted in the year 2023-2024. It includes aims, algorithms, coding examples, and results for multiple programs, such as calculating the area of a circle, swapping variables, finding factorials, and more. Each program is structured with a clear aim, algorithm steps, coding, and output results, demonstrating practical applications of C programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Name : ………………………………………………………………

Roll No : …………….………………………………………………...

Year / Semester : ………………………………………………..…………….

Department : ………………………………………………..……………..
CS3271 – PROGRAMMING IN C LABORATORY

LABORATORY RECORD

Name : ………………………………………………………………

Roll No : …………….………………………………………………...

Year / Semester : ………………………………………………..…………….

Department : ………………………………………………..……………...

Certified that this is the bonafide record of work done by the above student during the year

2023 - 2024.

Faculty-in-charge Head of the Department

Submitted for the University Practical Examination held on …………………………..

Internal Examiner External Examiner


INDEX

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.

Step2: Declare the variables – radius, area, cf.

Step3: Read the radius as an input.


Step4: Calculate the area of circle.

Step5: Calculate the circumference of circle.

Step6: Stop the process.


Coding:

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

Step1: Start the Program.

Step2: Declare the variables – var1,var2.

Step3: Read the input of two variables.

Step4: Define the functions as swapw and swapwo.

Step5: Call the functions and display the results.

Step6: Stop the process.


Coding:

void swapw(int num1,int num2)


{
int temp=num1;
num1=num2;
num2=temp;
printf("\n After Swapping using Temp Variable:num1=%d,num2=%d",num1,num2);
}
void swapwo(int num1,int num2)
{
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("\n\n After Swapping without using Temp Variable:num1=%d,num2=%d",num1,
num2);
}
int main()
{
int var1,var2;
clrscr();
printf("\n Enter two integers:");
scanf("%d %d",&var1,&var2);
printf("Before Swapping \n num1=%d\n num2=%d\n",var1,var2);
swapw(var1,var2);
swapwo(var1,var2);
getch();
return 0;
}
Output:

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:

Step1: Start the Program.

Step2: Declare the variables – n, i, fact.

Step3: Read any number as an input.

Step4: Check the given number is less than 0 or not.

Step5: If the number is less than 0 then the message will display as Error! Factorial of
Negative Number Doesn't Exist.

Step6: Otherwise it will calculate the factorial of given number.

Step7: Display the results.

Step8:Stop the process.


Coding:

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

Thus the C program to find factorial of a number was executed successfully.


Program No:4
DISPLAYING THE PATTERN
Date:

Aim:
To write a C program to display the following pattern .
*
**
***
****
*****

Algorithm:

Step1: Start the Program.

Step2: Declare the variables – n, i, fact.

Step3: Read no of rows as an input.

Step4: Display the above mentioned pattern using for loop.


Step5: Stop the process.
Coding:

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

Step1: Start the Program.

Step2: Declare the variables – n, last, first, sum.

Step3: Read four digit number as an input


Step4: Find the last digit using n%10.
Step5: Find the first digit using n/1000.

Step6: And find the sum of last and first digit using addition arithmetic operator.

Step7: Display the result.

Step8: Stop the process.


Coding:

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

Step2: Declare the variables – n, reverse=0, remainder.

Step3: Read the number as an input.


Step4: Reverse the given integer using while loop and operators.
Step5: Display the reversed number.

Step6: Stop the process.


Coding:

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

Step2: Declare the variables.

Step3: Read the no of rows and columns.


Step4: Read the elements for the given rows and columns.
Step5: Display the accessed elements in a matrix form.

Step6: Stop the process.


Coding:

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

Step2: Declare the variables.

Step3: Read the no of rows and columns.


Step4: Read the elements for first and second matrix.
Step5: Multiply the both matrices.
Step6: Display the multiplied matrix.

Step7: Stop the process.


Coding:

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

Thus the C program to multiply two 3 × 3 matrices was executed successfully.


Program No:9
COUNT THE NUMBER OF VOWELS
Date:

Aim:
To write the C program to count the number of vowels present in this string.

Algorithm:
Step1: Start the Program.

Step2: Declare the variables.

Step3: Read the string as an input.


Step4: Count the no of vowels present in a string.
Step5: Display the vowel count.
Step6: Stop the process.
Coding:

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

Step1: Start the Program.

Step2: Declare the variables.

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.

Step5: Display the copied string.

Step 6: Stop the program.


Coding:

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

Step1: Start the Program.

Step2: Declare the variables.

Step3: Define a function named factorial().

Step4: Read the number as an input.

Step5: And call factorial() function to find the factorial of given number.

Step6: The function factorial() will call by itself and return the result.

Step7: Display the value of factorial


Step8: Stop the program.
Coding:

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

Step3: Define the function named as revfun().


Step4: Call the function revfun() with parameter num to reverse the given integer.

Step5: Then the recursive function will call by itself and return the reversed integer.

Step6: Display the reversed integer.

Step7: Stop the program.


Coding:

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

You might also like