PROGRAMMING IN C LAB MANUALL
LAB EXERCISE :1
1: Write a Program to calculate and display the volume of a CUBE having it height
(h=10cm), width (w=12cm) and depth (8cm).
Algorithm:
1. Start
2. Define variables: h(int), w(int), d(int), vol(int)
3. Assign value to variables: h = 10, w=12, d=8
4. Calculate the volume as: vol = h*w*d
5. Display the volume (vol)
6. StoP
Flowchart:
Program:
#include<stdio.h>
void main()
//start the program
int h,w,d,vol; //variables declaration
h=10;w=12;d=8; //assign value to variables
vol=h*w*d; //calculation using mathematical formula
printf("The Volume of the cube is: %d",vol); //display the
volume
getch();
//end the main program
Output :
The Volume of the cube is: 960
2.Write a C program to create, declare and initialize structure.
#include <stdio.h>
/*structure declaration*/
struct employee{
char name[30];
int empId;
float salary;
};
int main()
/*declare and initialization of structure variable*/
struct employee emp={"Anil",201,80000.00};
printf("\n Name: %s" ,emp.name);
printf("\n Id: %d" ,emp.empId);
printf("\n Salary: %f\n",emp.salary);
return 0;
3.Write a program to store information of 5 students in structure and display
it.
#include<stdio.h>
struct student
char name[30];
int roll;
float marks;
} s[5];
int main( )
{
int i;
printf(“Information of students:”);
for (i=0; i<5; ++i)
s[i].roll =i+1;
printf(“\n Roll number %d, \n”, s[i].roll);
printf(“Enter name:”);
scanf(“%s”, s[i].name);
printf(“Enter marks:”);
scanf(“%f”, &s[i].marks);
printf(“\n Displaying Information:\n”);
for(i=0;i<10;++i)
printf(“\n Roll number:%d \n”, i+1);
printf(“Name:”);
puts(s[i].name);
printf(“\n Marks:%.1f”, s[i].marks);
return 0;
}
4. Write a program to be familiar with different data types, Operators and
Expressions in C.
#include<stdio.h>
#include<conio.h>
void main()
char name[20];
int rollno;
float sub1, sub2, sub3, sub4, , sum, score;
printf("Enter name of student: ");
scanf(“%s”,&name[]);
printf ("\n Enter Roll Number: ");
scanf("%d", &rollno);
printf ("\n Enter Marks in 4 Subjects:\n");
scanf("%f%f%f%f", &sub1, &sub2, &sub3, &sub4);
sum=sub1+sub2+sub3+sub4;
score = (sum/500)*100;
printf("\n Name of student: %s", name[]);
printf("\n Roll Number: %d", rollno);
printf ("\nPercentage score secured: %2.2f%c", score,'%');
getch();
}
Output:
Enter name of student: Ajit Singh
Roll Number: 25
Enter Marks in 4 Subjects:
50
75
85
62
Name of student: Ajit Singh
Roll Number: 25
Percentage score secured: 68.00%
5.Exercise on simple if.
#include<stdio.h>
#include<conio.h>
void main()
int num;
printf("Enter the number: ");
scanf(“%d”,&num);
if(num%2==0)
printf(“\n %d is even”, num);
else
printf(“\n %d is odd”, num);
getch();
Output:
Enter the number: 6
6 is even
6.To understand the programming using Loop & nested loop Statements
(for,while, do-while).
#include<stdio.h>
#include<conio.h>
void main()
int i;
for(i=1; i<=10;i++)
printf(“%d \n”, i);
getch();
//Using WHILE LOOP
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=10)
printf(“%d \n”, i);
i++;
getch();
//Using DO-WHILE LOOP
#include<stdio.h>
#include<conio.h>
void main()
int i=1;
do
printf(“%d \n”, i);
i++;
while(i<=10);
getch()
}
Output:
10
7.Write a program to display the following pattern.
**
***
****
*****
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1; i<=5;i++)
for(j=1;j<=i;j++)
printf(“*”);
printf(“\n”);
getch();
8.Write a program to calculate factorial of a number using recursion.
#include<stdio.h>
long factorial(int); //Function declaration
int main()
int num;
long fact;
printf(“Enter a number to find factorial: \n”);
scanf(“%d”, &num);
if(num<0)
printf(“Factorial of negative no. is not defined. \n”);
else
fact = factorial(num);
printf(“%d!=%d \n”, num, fact);
return 0;
//Function definition
long factorial(int num)
if(num==0)
return 1;
else
return(num*factorial(num-1));
9.Write a program to find biggest among three numbers using pointer.
#include<stdio.h>
#include<conio.h>
int main()
int a,b,c;
int*ptra=&a,*ptrb=&b,*ptrc=&c;
printf("enter three values");
scanf("%d%d%d",ptra,ptrb,ptrc);
printf("a=%d\n b=%d\n c=%d\n",*ptra,*ptrb,*ptrc);
if((*ptra>*ptrb && *ptra>*ptrc))
printf("biggest number=%d",*ptra);
else if((*ptrb>*ptra && *ptrb>*ptrc))
printf("biggest number =%d",*ptrb);
else
printf("biggest number=%d",*ptrc);
getch();
return 0;
10.Write a program to create a file called emp.rec and store information about
a person, in terms of his name, age and salary.
#include <stdio.h>
void main()
FILE *fptr;
char name[20];
int age;
float salary;
/* open for writing */
fptr = fopen("emp.rec", "w");
if (fptr == NULL)
printf("File does not exists \n");
return;
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);
fclose(fptr);
11.Write a program to illustrate how a file stored on the disk is read.
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fptr;
char filename[15];
char ch;
printf("Enter the filename to be opened \n");
scanf("%s", filename);
/* open the file for reading */
fptr = fopen(filename, "r");
if (fptr == NULL)
printf("Cannot open file \n");
exit(0);
ch = fgetc(fptr);
while (ch != EOF)
printf ("%c", ch);
ch = fgetc(fptr);
fclose(fptr);