C Practicals Ishrat
C Practicals Ishrat
C-Language
Practical(s)
Ishrat
D/o Muhammad Mubeen ul Haq
Institute:
Govt Monotechnic Institute,
Orangi 7, Karachi
2
Index
3
S# Practical Objective Date Sign
01 To learn Turbo-C Programming environment.
02 To print our name on screen
03 To study three data types of C language:
04 To study the use of format specifiers:
4
Lab 1
Objective:
To learn Turbo-C Programming environment.
Theory:
C Program consists of functions the main() function is the one which controls is passed when the is executed.
Code:
#include<stdio.h> //header file
#include<conio.h> //header file
Practical Output:
Hello World
5
Lab 2
Objective:
To print our name on screen
Theory:
The printf function is a versatile and powerful function.
Syntax:
printf(“ some string ”);
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("Ishrat”);
getche();
}
Practical Output:
Ishrat
6
Lab 3
Objective:
To study three data types of C language:
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int int1; //declaring integer variable
char ch1; //declaring character variable
float fl1; //declaring float variable
//initializing variables
int1=10;
ch1='g';
fl1=0.1;
//printing variable values
printf("Value of int1 is %d\n",int1);
printf("Value of ch1 is %c\n",ch1);
printf("Value of int1 is %f\n",fl1);
getche();
}
Practical Output:
7
Lab 4
Objective:
To study the use of format specifiers:
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
printf("This is number %d",20);
getche();
}
Practical Output:
8
Lab 5
Objective:
To study the use of format specifiers by using a variable:
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int a; // variable declaration
a=2;
getche();
}
Practical Output:
9
Lab 6
Objective:
To make marks sheet of exam:
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int math,eng,urdu,islamiat,science; //declaration of variables
math=70;
eng=60;
urdu=20;
islamiat=80;
science=90;
printf("Math=%d\nEnglish=%d\nUrdu=%d\nIslamiat=%d\nScience=
%d\n",math,eng,urdu,islamiat,science);
getche();
}
Practical Output:
10
Lab 7
Objective:
To declare multiple variables and show them on screen:
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int num1, num2;
char option1, option2;
float point1,point2;
num1=10;
num2=20;
option1='y';
option2='n';
point1=20.2;
point2=30.2;
printf("Option 1=%c\nNumber 1= %d\nPoint 1=%f",option1,num1,point1);
printf("\nOption 2=%c\nNumber 2= %d\nPoint 2=%f",option2,num2,point2);
getche();
}
Practical Output:
11
Lab 8
Objective:
To take age of a user in years as input and find out the number of days :
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
getche();
}
Practical Output:
12
Lab 9
Objective:
Write a program that takes input from user and display on screen.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int age;
float height;
char gender;
printf("Type your age, gender(m/f), and height.\n");
scanf("%d %c %f",&age,&gender,&height);
printf("You are %d years old, your height is %f feet, your gender is
%c",age,height,gender);
getche();
}
Practical Output:
13
Lab 10
Objective:
To find out the percentage of marks in exam by user input.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int cp,as,isl,math,phy;
float sum, perc;
printf("Enter marks of Computer Programming.\n");
scanf("%d",&cp);
printf("Enter marks of Application Software.\n");
scanf("%d",&as);
printf("Enter marks of Islamiat.\n");
scanf("%d",&isl);
printf("Enter marks of Mathematics.\n");
scanf("%d",&math);
printf("Enter marks of Physics.\n");
scanf("%d",&phy);
sum=cp+as+isl+math+phy;
perc=sum*100/500;
printf("Your percentage is %.2f",perc);
getche();
}
Practical Output:
14
15
Lab 11(a)
Objective:
Write a program which uses field specifiers.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
float event=27.25;
printf("event %.2f\n",event);
printf("event %.1f\n",event);
printf("event %f",event);
getche();
}
Practical Output:
16
Lab 11(b)
Objective:
Write a program which uses field specifiers.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
float event=27.25;
printf("%8.1f %8.1f %8.1f\n",3.0,12.5,523.3);
printf("%8.1f %8.1f %8.1f\n",300.0,1200.5,5300.3);
getche();
}
Practical Output:
17
Lab 11(c)
Objective:
Write a program which uses field specifiers.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
float event=27.25;
printf("%-8.1f %-8.1f %-8.1f\n",3.0,12.5,523.3);
getche();
}
Practical Output:
18
Lab 12
Objective:
Write a program which counts the total number of characters typed.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int charcnt=0;
int wordcnt=0;
char ch;
printf("Type in a phrase: \n");
while(ch=getche()!='\r')
{ charcnt++; }
Practical Output:
19
Lab 13
Objective:
Write a program which takes user input as characters and for character ‘Y’ it displays a
message.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
char ch;
printf("Type any character.\n");
ch=getche();
if(ch=='Y')
{
printf("\nYou typed Y.\n");
printf("You did it\n");
}
printf("\nTry again :-( please.");
getche();
}
Practical Output:
20
Lab 14
Objective:
Modify the program of lab#13 by using if-else condition.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
char ch;
printf("Type any character.\n");
ch=getche();
if(ch=='Y')
{
printf("\nYou typed Y.\n");
}
else
{
printf("\nYou did not type Y\n");
}
getche();
}
Practical Output:
21
Lab 15
Objective:
Write a program to avoid unnecessary repetition by using for loop.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int i;
for(i=0;i<10;i++)
printf("\n\t Count = %d",i);
getche();
}
Practical Output:
22
Lab 16
Objective:
Write a program that uses for loop and a function to print a text in a box.
Code:
#include<stdio.h>
#include<conio.h>
void line(void);
Practical Output:
23
Lab 17
Objective:
Write a program which can calculate minimum and maximum of the two numbers by
passing parameters in functions.
Code:
#include<stdio.h>
#include<conio.h>
double minimum(double a, double b);
double maximum(double a,double b);
void main(void)
{
clrscr();
printf("Minimum of 11 and 23 is = %f\n",minimum(11, 23));
printf("Maximum of 10 and 20 is = %f",maximum(01, 20));
getche();
}
double minimum(double a, double b)
{
return a < b ? a : b;
}
double maximum(double a,double b)
{
return a > b ? a : b;
}
Practical Output:
24
Lab 18
Objective:
Write a program which can calculate factorial of a given number.
Code:
#include<stdio.h>
#include<conio.h>
int factorial(unsigned int number)
{
if(number <= 1)
return 1;
return number * factorial(number - 1);
}
void main()
{
clrscr();
int x = 5;
printf("factorial of %d is %d",x,factorial(x));
getche();
}
Practical Output:
25
Lab 19
Objective:
Write a program which shows at different locations of an array.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
short age[4]; // array declaration
age[0]=23; //array elemets index wise assignment
age[1]=34;
age[2]=65;
age[3]=74;
Practical Output:
26
Lab 20
Objective:
Write a program which shows at different locations of an array.
Code:
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
short mat[3][3],i,j;
Practical Output:
27