Sie sind auf Seite 1von 89

ShaheedSHAHEED

Benazir Bhutto
BENAZIR University,
BHUTTO Shaheed
UNIVERSITY, SHAHEED Benazirabad
BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

SBBUSBA

SHAHEED BENAZIR BHUTTO


UNIVERSITY, SHAHEED
BENAZIR ABAD
Department of Information Technology

Lab Manual of C Programming Language

Practical Lab Manual of C Programming Language 1


ShaheedSHAHEED
Benazir Bhutto
BENAZIR University,
BHUTTO Shaheed
UNIVERSITY, SHAHEED Benazirabad
BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Lab Manual of C Programming Language


Practical Outlines:

Contents
Program No.1: Write Hello Word Program. ............................................................................................ 7

Program No.2: Write a program that displays a number 44 on the screen. ............................................ 7

Program No.3: Write a program that displays a number 45.6 on the screen........................................... 6

Program No.4: Write a program that displays a single character, like ‘A’ on the screen. ........................ 7

Program No.5: Write a program that displays a message stored in a string on the screen. .................... 7

Program No.6: Write a program that takes two numbers, then displays the output in a swapping
order. (e.g. if user entered value of a is 10 and value of b is 15, after swapping, the value of a should
15 and value of b should be 10). ............................................................................................................... 8

Program No.7: Write a program that add two numbers and prints the sum on the screen. ................... 9

Program No.8: Write a program that subtracts two numbers and prints the difference on the screen.
................................................................................................................................................................ 10

Program No.9: Write a program that multiplies two numbers and prints the result on the screen. ..... 11

Program No. 10: Write a program that divides two numbers and prints the result on the screen........ 12

Program No.11: Write a program that finds the modulus of two numbers, and shows the result on the
screen. ..................................................................................................................................................... 13

Program No.12: Write a program that takes two numbers from the user, and then adds, subtracts,
multiplies and divides the numbers and finally shows the result on the screen. ................................... 14

Program No.13: Write a program that takes a number from user and displays either the number is
even or odd. ............................................................................................................................................ 15

Program No.14: Write a program that takes currency in Pakistani rupees from the user and then
displays the converted currency into dollars on screen. (Hint: 1 Dollar = 100 PKR)............................... 16

Program No.15: Write a program that takes distance from user in kilometers and then displays the
output in meters. (1 kilometer = 1000 meters) ...................................................................................... 17

Program No.16: Crate a program that takes a character from user and then displays either character is
“vowel” or “consonant”. ......................................................................................................................... 18

Program No.17: Write a program that takes two integers from user and compares which number is
greater..................................................................................................................................................... 19

Practical Lab Manual of C Programming Language 2


ShaheedSHAHEED
Benazir Bhutto
BENAZIR University,
BHUTTO Shaheed
UNIVERSITY, SHAHEED Benazirabad
BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
Program No.18: Write a program that takes a year from user and displays whether the year is leap
year. ........................................................................................................................................................ 20

Program No.19: Write a program that takes age of student. If age of student is more than 18 years, the
message displays “You can vote”. ........................................................................................................... 21

Program No.20: Write a program that displays a number from 1 to 10 using loop. .............................. 22

Program No.21: Write a program that takes a value from user, and then displays the factorial of
number.................................................................................................................................................... 23

Program No.22: Write a program that takes a number from user, and generates the tables of that
number.................................................................................................................................................... 24

Program No.23: Write a program that takes two numbers, first for base and second for power, and
then generates the output (power) into screen. (e.g. 23= 8, where base is 2 and exponent is 3) ......... 26

Program No.24: Write a program that generates triangle of stars on screen. (Nested loop) ................ 27

Program No.25: Write a program that takes two numbers from user and displays the sum on the
screen. After that, the message displays “do you want to continue…?” if user type “y”, once again
takes two input and add them and displays result. But if user types “N”. the program terminates. ..... 28

Program No.26: Write a program that takes number between 1 to 12 from user and then displays the
name of month on screen. (Month names should be both, hijri and AD). ............................................. 30

Program No.27: Write a program that takes month number from user, and then it displays season of
the month it is given by the user. ........................................................................................................... 33

Program No.28: Write a scenario that takes age of a person, output of the program should be as
follows: .................................................................................................................................................... 36

 If user enters age between 1 to 15, the output should be “You are teen age”. ................................ 1

 If user enters age between 16 to 30, the output should be “You are Young”. .................................. 1

 If user enters age above 30, the output should be “You are aged”. .................................................. 1

Program No.29: Write a simple program that displays 5 values stored in an array. .............................. 37

Program No.30: Write a program that displays a message “Hello Word!” stored in an array. .............. 38

Program No.31: Write a program that takes 5 numbers from user and displays on the screen. Numbers
should be store in an array. .................................................................................................................... 39

Program No.32: Write a program that takes 5 random numbers from user and then displays numbers
in an ascending as well as descending order on the screen. .................................................................. 40

Program No.33: Write a program that takes 5 numbers from user and stores into an array,and then
prints the average of that numbers. ....................................................................................................... 42

Practical Lab Manual of C Programming Language 3


ShaheedSHAHEED
Benazir Bhutto
BENAZIR University,
BHUTTO Shaheed
UNIVERSITY, SHAHEED Benazirabad
BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
Program No.34: Write a program that takes 5 numbers form user and stores in an array, and then
prints the largest number among them. ................................................................................................. 43

Program No.35: Write a program that takes numbers from user in a matrix form, and finds the sum of
two matrix of order 2*2. (Two dimensional Array) ................................................................................ 44

Program No.36: Write a program to apply the following string functions: ............................................ 47

• Strcpy(); ...................................................................................................................................... 47

• Strcat(); ...................................................................................................................................... 47

• Strlen(); ...................................................................................................................................... 47

• Strcmp(); .................................................................................................................................... 47

• Strchr(); ...................................................................................................................................... 47

• Strstr(); ....................................................................................................................................... 47

1. strcpy() Program: .............................................................................................................................. 47

2. strcat() Program:............................................................................................................................... 48

3. strlen() Program: .............................................................................................................................. 48

4. strcmp() Program:............................................................................................................................. 49

5. strchr() Program: .............................................................................................................................. 50

6. strstr() Program: ............................................................................................................................... 50

Program No.37: Write a program that passes a value through function, and displays in the screen. ... 52

Program No.38: Write a program that prints a number of location 3 of an array on the screen. Array
should have minimum 5 numbers stored. And pass this array into function. ........................................ 53

Program No.39: Write a program that takes two numbers from user and then passes two values to
function, and prints sum of two numbers. ............................................................................................. 54

Program No.40: Write a program that takes two numbers from the user and then passes two values to
function, and prints the highest number. ............................................................................................... 55

Program No.41: Write a program that takes two numbers, and passes those values into a function,
then displays the output in a swapping order. (e.g. if user entered value of a is 10 and value of b is 15,
after swapping, the value of a should 15 and value of b should be 10). ................................................ 56

Program No.42: Write a program that takes a number from user and pass that value in a function to
display whether the number is even or add. .......................................................................................... 58

Program No.43: Write a program that takes two numbers from user and displays the sum of two
numbers into screen. (using pass by reference) ..................................................................................... 59

Practical Lab Manual of C Programming Language 4


ShaheedSHAHEED
Benazir Bhutto
BENAZIR University,
BHUTTO Shaheed
UNIVERSITY, SHAHEED Benazirabad
BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
Program No.44: Write a program that displays numbers from 1 to 5, stored into an array and pass
these values into a function, then prints values on the screen from that function................................ 60

Built in functions: .................................................................................................................................... 61

 Isalnum(); ............................................................................................................................................ 1

 Isalpha(); ............................................................................................................................................. 1

 Iscntrl(); .............................................................................................................................................. 1

 Isdigit(); ............................................................................................................................................... 1

 Isgraph();............................................................................................................................................. 1

 Islower();............................................................................................................................................. 1

 Isupper(); ............................................................................................................................................ 1

 Ispunct();............................................................................................................................................. 1

isalnum() Prgram:.................................................................................................................................... 61

Isalpha() Program: ................................................................................................................................... 62

iscntrl() Program: .................................................................................................................................... 63

isdigit() Program:..................................................................................................................................... 64

isgraph() Program: .................................................................................................................................. 64

islower() Program:................................................................................................................................... 65

isupper() Program: .................................................................................................................................. 66

ispunct() Program: .................................................................................................................................. 67

Program No.45: Write a program that takes user information and displays into screen using structure.
................................................................................................................................................................ 69

Program No. 46: Write a program that takes one number, one string and one floating point value, and
displays on the screen using structure.................................................................................................... 70

Program No.47: Write a program that displays the memory address of a number stored into a
variable.................................................................................................................................................... 72

Program No.48: Write a program that takes a number from the user and then displays memory
address of currently entered value on the screen. ................................................................................. 73

Program No.49: Write a program that displays a line on the screen. .................................................... 74

Program No.50: Write a program that displays a circle on the screen. .................................................. 75

Program No.51: Write a program that displays a rectangle on the screen. ........................................... 76

Practical Lab Manual of C Programming Language 5


ShaheedSHAHEED
Benazir Bhutto
BENAZIR University,
BHUTTO Shaheed
UNIVERSITY, SHAHEED Benazirabad
BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
Program No.52: Write a program that displays a triangle on the screen. .............................................. 77

Program No.53: Write a program that displays ellipse on the screen. ................................................... 78

Program No.54: Write a program that displays a bar graph on the screen. ........................................... 79

Program No.55: Write a program that displays sine wave graph on the screen. ................................... 80

Program No.56: Write a program that takes name of student, their roll number and marks of three
different subjects, the displays mark sheet as an output. Mark sheet contains subject name, total
marks, obtained marks, percentage and status (pass / fail). .................................................................. 82

Program No.57: Write a program that calculates area of square from given input of length by user. .. 84

Program No.58: Write a program that calculates area of triangle from given input of length by user.. 84

Program No.59: Write a program that calculates area of circle from given input of length by user. .... 86

Program No.60: Write a program that sets the screen background color “Red”. .................................. 87

Practical Lab Manual of C Programming Language 6


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.1: Write Hello Word Program.

#include<stdio.h>

void main()

clrscr();

printf(“Hello Word!”);

getch();

Press ctrl+f9 to run the program.

Output:

Hello Word!

Program No.2: Write a program that displays a number 44 on the screen.

There are two ways to do this program.

1st way (without variable):

#include<stdio.h>

void main()

{ clrscr();

printf(“44”);

getch();

Press ctrl+f9 to run the program.

Output:

44

Practical Lab Manual of C Programming Language 7


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
2nd Way:

#include<stdio.h>

void main()

clrscr();

int number=44;

printf(“%d”,number);

getch();

Press ctrl+f9 to run the program.

Output:

44

Program No.3: Write a program that displays a number 45.6 on the screen.

#include<stdio.h>

void main()

clrscr();

float number=45.6;

printf(“%f”,number);

getch();

Press ctrl+f9 to run the program.

Output:

45.6

Practical Lab Manual of C Programming Language 6


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.4: Write a program that displays a single character, like ‘A’ on the screen.

#include<stdio.h>

void main()

clrscr();

char alpahabet=’A’;

printf(“%c”,alphabet);

getch();

Press ctrl+f9 to run the program.

Output:

Program No.5: Write a program that displays a message stored in a string on the screen.

#include<stdio.h>

void main()

clrscr();

printf(”Welcome SBBU”);

getch();

Press ctrl+f9 to run the program.

Output:

Welcome SBBU

Practical Lab Manual of C Programming Language 7


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.6: Write a program that takes two numbers, then displays the output in a swapping
order. (e.g. if user entered value of a is 10 and value of b is 15, after swapping, the value of a
should 15 and value of b should be 10).

#include<stdio.h>

void main()

clrscr();

int number1, number2, temp;

printf(“\nEnter first Number…”);

scanf(“%d”,number1);

printf(“\nEnter second Number…”);

scanf(“%d”,number2);

clrscr();

printf(“\n Before swapping, number1 = %d\n number2=%d”,number1,number2);

temp=number1;

number1=number2;

number2=temp;

printf(“After swapping, number1=%d\n number2=%d”,number1,number2);

getch();

Press ctrl+f9 to run the program.

Output:

Enter first Number… 25

Enter second Number…15

Before swapping, number1=25, number2=15

After swapping, number1=15, number2=25

Practical Lab Manual of C Programming Language 8


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.7: Write a program that add two numbers and prints the sum on the screen.

#include<stdio.h>

void main()

clrscr();

int num1, num2, result;

num1=10; num2=15;

result=num1+num2;

printf(“%dSum of two numbers = ”,result);

getch();

Press ctrl+f9 to run the program.

Output:

Sum of two numbers = 25

Practical Lab Manual of C Programming Language 9


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.8: Write a program that subtracts two numbers and prints the difference on the
screen.

#include<stdio.h>

void main()

clrscr();

int num1, num2, result;

num1=50;

num2=15;

result=num1-num2;

printf(“%dDifference of two numbers = ”,result);

getch();

Press ctrl+f9 to run the program.

Output:

Difference of two numbers = 35

Practical Lab Manual of C Programming Language 10


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.9: Write a program that multiplies two numbers and prints the result on the screen.

#include<stdio.h>

void main()

clrscr();

int num1, num2, result;

num1=5; num2=10;

result=num1*num2;

printf(“%dMultiplication of two numbers = ”,result);

getch();

Press ctrl+f9 to run the program.

Output:

Multiplication of two numbers = 50

Practical Lab Manual of C Programming Language 11


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No. 10: Write a program that divides two numbers and prints the result on the screen.

#include<stdio.h>

void main()

clrscr();

int num1, num2, result;

num1=50;

num2=10;

result=num1/num2;

printf(“%dDivision of two numbers = ”,result);

getch();

Press ctrl+f9 to run the program.

Output:

Division of two numbers = 5

Practical Lab Manual of C Programming Language 12


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.11: Write a program that finds the modulus of two numbers, and shows the result
on the screen.

#include<stdio.h>

void main()

clrscr();

int num1, num2, result;

num1=50; num2=10;

result=num1%num2;

printf(“%dResult = ”,result);

getch();

Press ctrl+f9 to run the program.

Output:

Result = 0

Practical Lab Manual of C Programming Language 13


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.12: Write a program that takes two numbers from the user, and then adds,
subtracts, multiplies and divides the numbers and finally shows the result on the screen.

#include<stdio.h>

void main()

clrscr();

int num1, num2, result_add,result_sub,result_mul,result_div;

printf(“Enter First Number…”);

scanf(“%d”,&num1);

printf(“Enter Second Number…”);

scanf(“%d”,&num2);

result_add=num1+num2;

result_sub=num1-num2;

result_mul=num1*num2;

result_div=num1/num2;

printf(“%d + %d = ”,result_add);

printf(“%d - %d = ”,result_sub);

printf(“%d * %d = ”,result_mul);

printf(“%d / %d = ”,result_div);

getch();

Press ctrl+f9 to run the program.

Output:

Enter First Number… 10

Enter Second Number… 5

10 + 5 = 15

10 – 5 = 5

10 * 5 = 50

10 / 5 = 2
Practical Lab Manual of C Programming Language 14
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.13: Write a program that takes a number from user and displays either the number
is even or odd.

#include<stdio.h>

void main()

clrscr();

int number;

printf(“Enter any Positive Integer Number…”);

scanf(“%d”,&number);

if(number%2=0)

printf(“%d is even”,number);

printf(“%d is odd”,number);

getch();

Press ctrl+f9 to run the program.

Output:

Enter any Number… 10

Number is even.

Practical Lab Manual of C Programming Language 15


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.14: Write a program that takes currency in Pakistani rupees from the user and then
displays the converted currency into dollars on screen. (Hint: 1 Dollar = 100 PKR)

#include<stdio.h>

void main()

clrscr();

int currency_amount, currency_result;

printf(“Enter Currency in PKR…”);

scanf(“%d”,&currency);

currency_result=currency_amount/100;

printf(“%d Amount in Dollars = ”currency_result);

getch();

Press ctrl+f9 to run the program.

Output:

Enter Currency in PKR… 5000

Amount in dollars= 50

Practical Lab Manual of C Programming Language 16


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.15: Write a program that takes distance from user in kilometers and then displays
the output in meters. (1 kilometer = 1000 meters)

#include<stdio.h>

void main()

clrscr();

int distance_kilometres, distance_metres;

printf(“Enter distance in kilometres…”);

scanf(“%d”,&distance_kilometres);

distance_metres=distance_metres*1000;

printf(“%d Distance in metres = ”distance_metres);

getch();

Press ctrl+f9 to run the program.

Output:

Enter distance in kilometres… 350

Distance in metres= 350000

Practical Lab Manual of C Programming Language 17


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.16: Crate a program that takes a character from user and then displays either
character is “vowel” or “consonant”.

#include<stdio.h>

void main()

clrscr();

char c;

printf(“Enter an alphabet…”);

scanf(“%c”,&c);

if(c==’A’ || c==’a’|| c==’E’|| c==’e’ || c==’I’ || c==’i’ || c==’O’ || c==’o’ || c==’U’ || c==’u’)

printf(“%c is a vowel”,c);

else

printf(“%c is a consonant”,c);

getch();

Press ctrl+f9 to run the program.

Output:

Enter an alphabet… r

r is consonant

Practical Lab Manual of C Programming Language 18


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.17: Write a program that takes two integers from user and compares which number
is greater.

#include<stdio.h>

void main()

clrscr();

int num1, num2;

printf(“Enter First Number…”);

scanf(“%d”,&num1);

printf(“Enter Second Number…”);

scanf(“%d”,&num2);

if(num1 > num2)

printf(“%d is greater number”,num1);

else

printf(“%d is greater number”,num2);

getch();

Press ctrl+f9 to run the program.

Output:

Enter First Nmuber… 8

Enter Second Number… 5

8 is greater

Practical Lab Manual of C Programming Language 19


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.18: Write a program that takes a year from user and displays whether the year is
leap year.

#include<stdio.h>

void main()

clrscr();

int year;

printf(“Enter year to check if it is leap year …”);

scanf(“%d”,&year);

if(year%400=0 || year%4=0)

printf(“%d is a leap year”,year);

else

printf(“%d is not a leap year”,year);

getch();

Press ctrl+f9 to run the program.

Output:

Enter year to check if it is leap year… 2013

2013 is not a leap year

Practical Lab Manual of C Programming Language 20


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.19: Write a program that takes age of student. If age of student is more than 18
years, the message displays “You can vote”.

#include<stdio.h>

void main()

clrscr();

int age;

printf(“Enter your age in years …”);

scanf(“%d”,&age);

if(age > 18)

printf(“Great, You can vote.”);

else

printf(“Sorry, you can not vote at this stage.”);

getch();

Press ctrl+f9 to run the program.

Output:

Enter your age in years… 28

Great, you can vote.

Practical Lab Manual of C Programming Language 21


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.20: Write a program that displays a number from 1 to 10 using loop.

#include<stdio.h>

void main()

clrscr();

int counter;

for(counter=1; counter<=10;counter++)

printf(“%d\n”,counter);

getch();
}

Press ctrl+f9 to run the program.

Output:

10

Practical Lab Manual of C Programming Language 22


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.21: Write a program that takes a value from user, and then displays the factorial of
number.

#include<stdio.h>

void main()

clrscr();

int factorial, counter, number;

printf(“Enter any number to calculate its factorial…”);

scanf(“%d”,&number);

for(counter=1; counter<=number; counter++)

factorial=factorial*counter;

printf(“\n Factorial of %d=%d”,number,factorial);

getch();
}

Press ctrl+f9 to run the program.

Output:

Enter any number to calculate its factorial… 5

Factorial of 5 =120

Practical Lab Manual of C Programming Language 23


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.22: Write a program that takes a number from user, and generates the tables of
that number.

1st Way:

#include<stdio.h>

void main()

clrscr();

int number, counter;

printf(“Enter any number to generate its table…”);

scanf(“%d”,&number);

for(counter=1; counter<=10; counter++)

printf(“\n %d * %d = %d”,number,counter,number*counter);

getch();
}

Press ctrl+f9 to run the program.

Output:

Enter any number to generate its table… 5

5*1=5

5 * 2 = 10

5 * 3 = 15

5 * 4 = 20

5 * 5 = 25

5 * 6 = 30

5 * 7 = 35

5 * 8 = 40

5 * 9 = 45

5 * 10 = 50

Practical Lab Manual of C Programming Language 24


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
2nd Way:

#include<stdio.h>

void main()

clrscr();

int number, counter, result;

printf(“Enter any number to generate its table…”);

scanf(“%d”,&number);

for(counter=1; counter<=10; counter++)

result=number*counter;

printf(“\n %d * %d = %d”,number,counter,result);

getch();
}

Press ctrl+f9 to run the program.

Output:

Enter any number to generate its table… 5

5*1=5

5 * 2 = 10

5 * 3 = 15

5 * 4 = 20

5 * 5 = 25

5 * 6 = 30

5 * 7 = 35

5 * 8 = 40

5 * 9 = 45

5 * 10 = 50

Practical Lab Manual of C Programming Language 25


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.23: Write a program that takes two numbers, first for base and second for power,
and then generates the output (power) into screen. (e.g. 23= 8, where base is 2 and exponent is 3)

#include<stdio.h>

void main()

clrscr();

int base, exp;

long result;

printf(“Enter Base number…”);

scanf(“%d”,&base);

printf(“Enter Exponent Number…”);

scanf(“%d”,&exp);

while(exp!=0)

result=result*base;

exp--;

printf(“%dAnswer = ”,result);

getch();

Press ctrl+f9 to run the program.

Output:

Enter Base Number… 5

Enter Exponent Number…3

Answer = 125

Practical Lab Manual of C Programming Language 26


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.24: Write a program that generates triangle of stars on screen. (Nested loop)

#include<stdio.h>

void main()

clrscr();

int outer, inner;

for(outer=1; outer<=5; outer++)

for(inner=1;inner<=outer;inner++)

printf(“*”);

Printf(“\n”);

getch();
}

Press ctrl+f9 to run the program.

Output:

*
**
***
****
*****

Practical Lab Manual of C Programming Language 27


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.25: Write a program that takes two numbers from user and displays the sum on the
screen. After that, the message displays “do you want to continue…?” if user type “y”, once again
takes two input and add them and displays result. But if user types “N”. the program terminates.

#include<stdio.h>
void main()
{
clrscr();
int number1, number2,result;
char choice;
do
{
printf(“Enter first Number…”);
scanf(“%d”,&number1);
printf(“Enter Second Number…”);
scanf(“%d”,&number2);
result=numer1+number2;
printf(“%d+%d=%d”,number1,number2,result);
printf(“Do you want to continue y or n…”);
scanf(“%c”,alpha);
printf(“Do you want to continue y / n ….”);
scanf(“%c”,&choice);
}
while(choice==’y’ || choice==’Y’);
getch();
}
Press ctrl+f9 to run the program.

Output:

Enter First Number…4

Enter Second Number…3

4+3=7

Practical Lab Manual of C Programming Language 28


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
Do you want to continue… y

Enter First Number…

Enter Second Number…

Practical Lab Manual of C Programming Language 29


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.26: Write a program that takes number between 1 to 12 from user and then displays
the name of month on screen. (Month names should be both, hijri and AD).

#include<stdio.h>

void main()

clrscr();

int month_number;

printf(“Enter Number between 1 to 12 to show name of month…”);

scanf(“%d”,&month_number);

switch(month_number)

case 1:

Printf(“its January and Muharram ”);

case 2:

Printf(“its February and Saffar ”);

case 3:

Printf(“its March and Rabi-ul-Awwal ”);

case 4:

{
Practical Lab Manual of C Programming Language 30
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
Printf(“its April and Rabi-ul-Sani ”);

case 5:

Printf(“its May and Jamad-ul-Awwal ”);

case 6:

Printf(“its June and Jamad-us-Saani ”);

case 7:

Printf(“its July and Rajab ”);

case 8:

Printf(“its August and Shoban ”);

case 9:

Printf(“its September and Ramzan ”);

case 10:

Printf(“its October and Shawal ”);

}
Practical Lab Manual of C Programming Language 31
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
case 11:

Printf(“its November and Zil-qad ”);

case 12:

Printf(“its December and Zil-Hajj ”);

default:

Printf(“You have not entered number range between 1 to 12”);

getch();

Press ctrl+f9 to run the program.

Output:

Enter Number between 1 to 12 to show name of month… 5

Its May and Jamad-ul-Awwal

Practical Lab Manual of C Programming Language 32


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.27: Write a program that takes month number from user, and then it displays
season of the month it is given by the user.

#include<stdio.h>

void main()

clrscr();

int month_number;

printf(“Enter Number between 1 to 12 to show which season is in this month…”);

scanf(“%d”,&month_number);

switch(month_number)

case 1:

printf(“its January and its winter ”);

case 2:

printf(“its February and its winter ”);

case 3:

printf(“its March and its Spring ”);

case 4:

{
Practical Lab Manual of C Programming Language 33
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
printf(“its April and its Spring ”);

case 5:

printf(“its May and its Spring”);

case 6:

printf(“its June and its Summer ”);

case 7:

printf(“its July and its Summer ”);

case 8:

printf(“its August and its Summer”);

case 9:

printf(“its September and its Autumn ”);

case 10:

printf(“its October and its Autumn ”);

}
Practical Lab Manual of C Programming Language 34
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
case 11:

printf(“its November and its Winter ”);

case 12:

printf(“its December and its Winter ”);

default:

printf(“You have not entered number range between 1 to 12”);

getch();

Press ctrl+f9 to run the program.

Output:

Enter Number between 1 to 12 to show name of month… 5

Its May and its Spring

Practical Lab Manual of C Programming Language 35


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.28: Write a scenario that takes age of a person, output of the program should be as
follows:

 If user enters age between 1 to 15, the output should be “You are teen age”.

 If user enters age between 16 to 30, the output should be “You are Young”.

 If user enters age above 30, the output should be “You are aged”.

#include<stdio.h>

void main()

clrscr();

int age;

printf(“Enter your age in years…”);

scanf(“%d”,&age);

if(age>=1 || age<=15)

printf(“You are teen age.”);

else if (age>15 || age<=30)

printf(“You are Young.”);

else if (age>30)

printf(“You are Aged.”);

getch();

Press ctrl+f9 to run the program.

Output:

Enter your age in years… 28

You are young.

Practical Lab Manual of C Programming Language 36


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.29: Write a simple program that displays 5 values stored in an array.

#include<stdio.h>

void main()

clrscr();

int counter;

int arr[5]={1,2,3,4,5}

for (counter=0;counter<=4;counter++)

printf(“%d\n”,arr[counter]);

getch();

Press ctrl+f9 to run the program.

Output:

Practical Lab Manual of C Programming Language 37


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.30: Write a program that displays a message “Hello Word!” stored in an array.

#include<stdio.h>

void main()

clrscr();

int counter;

char arr[10]={‘H’,’e’,’l’,’l’,’o’,’W’,’o’,’r’,’d’,’!’};

for(counter=0;counter<=10;counter++)

printf(“%c”,arr[counter]);

getch();

Press ctrl+f9 to run the program.

Output:

Hello Word!

Practical Lab Manual of C Programming Language 38


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.31: Write a program that takes 5 numbers from user and displays on the screen.
Numbers should be store in an array.

#include<stdio.h>

void main()

clrscr();

int counter;

int arr[5];

printf(“Enter Five Numbers…”);

for(counter=0;counter<5;counter++)

scanf(“%d”,&arr[counter]);

} clrscr();

for(counter=0;counter<5;counter++)

Printf(“%d,”,arr[counter]);

getch();

Press ctrl+f9 to run the program.

Output:

Enter Five Numbers… 5

Practical Lab Manual of C Programming Language 39


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
8

5,6,9,8,7

Program No.32: Write a program that takes 5 random numbers from user and then displays
numbers in an ascending as well as descending order on the screen.

#include<stdio.h>

void main()

clrscr();

int arr[], counter, outer,inner,temp;

printf(“Enter any five numbers…”);

for(counter=0;counter<5;counter++)

scanf(“%d”,&arr[counter]);

for(outer=0;outer<5;outer++)

for(inner=outer;inner<4;inner++)

If(arr[ouer]>arr[inner])

Temp=arr[outer];

arr[outer]=arr[inner];

arr[inner]=temp;

} Practical Lab Manual of C Programming Language 40


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
}

/* Ascending Order */

printf(“Ascending Order:\n”);

for(outer=0;outer<5;outer++)

printf(“%d\n”,arr[outer]);

/* Descending Order */

printf(“Descending Order:\n”);

for(outer=4;outer>=0;outer--)

printf(“%d\n”,arr[outer]);

getch();

Press ctrl+f9 to run the program.

Output:

Enter Five Numbers… 5

65

98

64

20

Ascending Order:

20

64

65

98

Descending Order:
Practical Lab Manual of C Programming Language 41
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
98

65

64

20

Program No.33: Write a program that takes 5 numbers from user and stores into an array,and
then prints the average of that numbers.

#include<stdio.h>

void main()

clrscr();

int arr[5], avg,counter;

float result;

printf(“Enter any five numbers…”);

for(counter=0;counter<5;counter++)

scanf(“%d”,arr[counter]);

for(counter=0;counter<5;counter++)

avg+=arr[counter];

} result=avg/5;

printf(“%dAverage = ”,result);

getch();

Practical Lab Manual of C Programming Language 42


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
Press ctrl+f9 to run the program.

Output:

Enter Five Numbers… 5

95

Average = 24.2

Program No.34: Write a program that takes 5 numbers form user and stores in an array, and
then prints the largest number among them.

#include<stdio.h>

void main()

Int arr[5], counter,largest;

printf(“Enter any five numbers…”);

for(counter=0;counter<5;counter++)

scanf(“%d”,&arr[counter]);

largest=arr[0];

for(counter=0;counter<5;counter++)

If(arr[counter]>largest)

Largest=arr[counter];
Practical Lab Manual of C Programming Language 43
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
}

Printf(“ Largest Number: %d”,largest);

getch();

Press ctrl+f9 to run the program.

Output:

Enter any five Numbers… 6

45

23

56

322

Largest Number: 322

Program No.35: Write a program that takes numbers from user in a matrix form, and finds the
sum of two matrix of order 2*2. (Two dimensional Array)

#include<stdio.h>

void main()

clrscr();

int matrix1[2][2], matrix2[2][2],outer,inner;

int resultant_matrix[2][2];

printf(“Enter values of first matrix…”);

for (outer=0; outer<2;outer++)


Practical Lab Manual of C Programming Language 44
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
for(inner=0;inner<2;inner++)

printf(“Enter Element a[%d][%d]: ”,outer,inner);

scanf(“%d”&matrix1[outer][inner]);

printf(“\n”);

Printf(“Enter Values of Second Matrix”);

for(outer=0;outer<2;outer++)

for(inner=0;inner<2;inner++)

printf(“Enter Element b[%d][%d]: ”,outer,inner)

scanf(“%d”,&matrix[outer][inner]);

for(outer=0;outeR<2;outer++)

for(inner=0;inner<2;inner++)

resultant_matrix[outer][inner]=matrix1[outer][inner]+matrix2[outer][inner];

printf(“Resultant Matrix:”);

for(outer=0;outer<2;outer++)

for(inner=0;inner<2;inner++)

Printf(“%d ”,resultant_matrix[outer][inner]);

if(inner==2)

printf(“\n\n”);

}
Practical Lab Manual of C Programming Language 45
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
getch();

Press ctrl+f9 to run the program.

Output:

Enter Values of First Matrix

Enter Element a00… 6

Enter Element a01… 4

Enter Element a10… 12

Enter Element a11… 14

Enter Values of Second Matrix

Enter Element a00… 12

Enter Element a01… 6

Enter Element a10… 32

Enter Element a11… 42

Resultant Matrix:

18

10

44

56

Practical Lab Manual of C Programming Language 46


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.36: Write a program to apply the following string functions:

• Strcpy();

• Strcat();

• Strlen();

• Strcmp();

• Strchr();

• Strstr();

1. strcpy() Program:

#include<stdio.h>

#include<string.h>

void main()

char arr[50]; strcpy(arr,”Test

Program”); printf(“Copied

Text: %s”,arr); getch();

Press ctrl+f9 to run the program.

Output:

Copied Text: Test Program

Practical Lab Manual of C Programming Language 47


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

2. strcat() Program:

#include<stdio.h>

#include<string.h>

void main()

clrscr();

char string1[50]=”A quick brown fox”;

strcat(string1,”jumps over the lazy dog”);

printf(“%s”,string1);

getch();

Press ctrl+f9 to run the program.

Output:

A quick brown fox jumps over the lazy dog

3. strlen() Program:

#include<stdio.h>

#include<string.h>

void main()

clrscr();

char str[50]=”Welcome to SBBUSBA.”;

int len=strlen(str);

printf(“Length of %s is: %d”,str,len);

getch();

Practical Lab Manual of C Programming Language 48


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
}

Press ctrl+f9 to run the program.

Output:

Length of Welcome to SBBUSBA is: 33

4. strcmp() Program:

#include<stdio.h>

#include<string.h>

void main()

clrscr();

char str1[50]=”ABCD”;

char str2[50]=”abcd”;

int comp=strcmp(str1,str2);

if(comp < 0)

printf(“str1 is less than str2”);

else if(comp > 0)

printf(“str2 is less than str1”);

else

printf(“str1 is equal to str2”);

getch();

Press ctrl+f9 to run the program.

Output:

Str2 is less than str1

Practical Lab Manual of C Programming Language 49


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

5. strchr() Program:

#include<stdio.h>

#include<string.h>

void main()

char *s;

char str[] = ”This is a test program”;

s = strchr(str , ’t’);

if (s ! = “”)

printf(“found a ‘t’ at %s\n”,s);

getch();

Press ctrl+f9 to run the program.

Output:

Found a ‘t’ at test

6. strstr() Program:

#include<stdio.h>

#include<string.h>

Void main()

clrscr();

char str1[20]=”Java Programming”;

char str2[20]=”Visual Programming”;

char subst;
Practical Lab Manual of C Programming Language 50
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
subst=strstr(str1,str2);

printf(“Substring is: ”, subst);

getch();

Press ctrl+f9 to run the program.

Output:

Substring is: Programming

Practical Lab Manual of C Programming Language 51


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.37: Write a program that passes a value through function, and displays in the
screen.

#include<stdio.h>

void disp(int a);

void main()

clrscr();

int value = 100;

disp(value);

getch();

Void disp(int a)

Printf(“%d”,a);

Press ctrl+f9 to run the program.

Output:

100

Practical Lab Manual of C Programming Language 52


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.38: Write a program that prints a number of location 3 of an array on the screen.
Array should have minimum 5 numbers stored. And pass this array into function.

#include<stdio.h>

void displayNumber(int num);

void main()

clrscr();

int arr[]={12,34,32,43,25};

displayNumber(arr[2]);

getch();

void displayNumber(int num)

Printf(“%d”,num);

Press ctrl+f9 to run the program.

Output:

32

Practical Lab Manual of C Programming Language 53


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.39: Write a program that takes two numbers from user and then passes two values
to function, and prints sum of two numbers.

#include<stdio.h>

Int displaySum(int, int)

void main()

clrscr();

int number1,number2,result;

printf(“Enter First Number…”);

scanf(“%d”,number1);

printf(“Enter Second Number…”);

scanf(“%d”,numbers);

result = displaySum(number1,number2);

printf(“%dSum= ”,result);

getch();

Int displaySum(int num1, int num2)

int result=num1+num2;

return result;

Press ctrl+f9 to run the program.

Output:

Enter First Number… 14

Enter Second Number… 15

Sum= 29
Practical Lab Manual of C Programming Language 54
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.40: Write a program that takes two numbers from the user and then passes two
values to function, and prints the highest number.

#include<stdio.h>

int displayHighestNumber(int,int);

Void main()

clrscr();

int number1,number2,highest_number;

printf(“Enter First Number…”);

scanf(“%d”,number1);

printf(“Enter Second Number…”);

scanf(“%d”,numbers);

highest_number = displaySum(number1,number2);

printf(“%dHighest Number= ”,highest_number);

getch();

int displayHighestNumber(int num1,int num2)

If(num1 > num2)

return num1;

else

Return num2;

Press ctrl+f9 to run the program.

Output:

Practical Lab Manual of C Programming Language 55


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
Enter First Number… 30

Enter Second Number… 15

Highest Number= 30

Program No.41: Write a program that takes two numbers, and passes those values into a
function, then displays the output in a swapping order. (e.g. if user entered value of a is 10 and
value of b is 15, after swapping, the value of a should 15 and value of b should be 10).

#include<stdio.h>

void swap(int, int);

void main()

clrscr();

int num1, num2;

printf(“Enter the value of A: ”);

scanf(“%d”, &num1);

printf(“Enter the value of B: ”);

scanf(“%d”, &num2);

clrscr();

printf(“Before Swapping:, the value of A is: %d and the value of B is: %d”,num1,num2);

swap(num1,num2);

getch();

void swap(int num1, int num2)

int temp;

temp=num2;

Practical Lab Manual of C Programming Language 56


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
num2=num1;

num1=temp;

printf(“After Swapping:, the value of A is: %d and the value of B is: %d”,num1,num2);

Press ctrl+f9 to run the program.

Output:

Enter the value of A… 10

Enter the value of B… 15

Before Swapping, the value of A is: 10 and the value of B is: 15

After Swapping, the value of A is: 15 and the value of B is: 10

Practical Lab Manual of C Programming Language 57


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.42: Write a program that takes a number from user and pass that value in a
function to display whether the number is even or add.

#include<stdio.h>

void oddEven(int);

void main()

int num, result;

printf(“Enter any Number…”);

scanf(“%d”,num);

result=oddEven(num);

getch();

void oddEven(int num)

If(num%2=0)

printf(“The Number is Even”);

else

printf(“The Number is Odd”);

Press ctrl+f9 to run the program.

Output:

Enter any Number… 19

The Number is Odd

Practical Lab Manual of C Programming Language 58


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.43: Write a program that takes two numbers from user and displays the sum of two
numbers into screen. (using pass by reference)

#include<stdio.h>

int add(int *num1, int *num2, int *result);

void main()

clrscr();

int num1, num2, result;

printf(“Enter First Number…”);

scanf(“%d”,&num1);

printf(“Enter Second Number…”);

scanf(“%d”,&num2);

add(&num1,&num2,&result);

printf(“Sum of %d and %d is: %d”, num1,num2,result);

getch();

Int add(int *num1, int *num2, int *result)

result=num1+num2;

Press ctrl+f9 to run the program.

Output:

Enter First Number… 43

Enter Second Number… 14

Sum of 43 and 14 is: 57


Practical Lab Manual of C Programming Language 59
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.44: Write a program that displays numbers from 1 to 5, stored into an array and
pass these values into a function, then prints values on the screen from that function.

#include<stdio.h>

int disp(int);

void main()

clrscr();

int arr[5]={1,2,3,4,5};

int counter;

for(counter=0; counter<5;counter++)

disp(arr[counter]);

getch();

Int disp(int num)

printf(“%d”,num);

Press ctrl+f9 to run the program.

Output:

12345

Practical Lab Manual of C Programming Language 60


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Built in functions:

 Isalnum();

 Isalpha();

 Iscntrl();

 Isdigit();

 Isgraph();

 Islower();

 Isupper();

 Ispunct();

isalnum() Prgram:

The C library function isalnum() checks if the passed character is alphanumeric.

#include<stdio.h>

#include<ctype.h>

void main()

int value1= ‘a’;

if(isalnum(value1))

printf(“Value |%c| is alphanumeric ”);

else

printf(“Value |%c| is not alphanumeric ”);

getch();

}
Practical Lab Manual of C Programming Language 61
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
Press ctrl+f9 to run the program.

Output:

Value |a| is alphnumeric.

Isalpha() Program:

The C library function isalpha() checks if the passed character is alphabetic.

#include<stdio.h>

#include<ctype.h>

void main()

int value1= ‘\t’;

if(isalpha(value1))

printf(“Value |%c| is alphabet ”);

else

printf(“Value |%c| is not alphabet ”);

getch();

Press ctrl+f9 to run the program.

Output:

Value | | is not alphabet.

Practical Lab Manual of C Programming Language 62


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

iscntrl() Program:

The C library function iscntrl() checks if the passed character is control character (\a, \b, \f, \n, \r, \t) or
not.

#include<stdio.h>

#include<ctype.h>

Void main()

char ch[10]=”ab\b”;

int i=0;

while(1)

If(iscntrl(ch[i]))

printf(“Control Character is found at ” \ “%d Position\n”,i+1);

break;

i++;

getch();

Press ctrl+f9 to run the program.

Output:

Control Character is found at 3 Position

Practical Lab Manual of C Programming Language 63


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

isdigit() Program:

The C library function isdigit() checks if the passed character is a decimal digit character.

#include<stdio.h>

#include<ctype.h>

void main()

int value1= ‘5’;

if(isdigit(value1))

printf(“Value |%c| is a digit ”);

else

printf(“Value |%c| is not a digit ”);

getch();

Press ctrl+f9 to run the program.

Output:

Value |5| is a digit.

isgraph() Program:

The C library function isgraph() checks if the character has graphical representation.

The characters with graphical representations are all those characters that can be printed except for
whitespace characters (like ' '), which is not considered as isgraph characters.

#include<stdio.h>

#include<ctype.h>

void main()

int value1 = ‘s’;


Practical Lab Manual of C Programming Language 64
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
int vaalue2 = ’ ’;

if(isgraph(value1))

printf(“Value |%c| can be printed ”);

else

printf(“Value |%c| can’t be printed ”);

if(isgraph(value2))

printf(“Value |%c| can be printed ”);

else

printf(“Value |%c| can’t be printed ”);

getch();

Press ctrl+f9 to run the program.

Output:

Value |s| can be printed

Value | | can’t be printed

islower() Program:

The C library function islower() checks whether the passed character is lowercasr letter.

#include<stdio.h>

#include<ctype.h>

void main()

int value1 = ‘S’;

int vaalue2 = ’s’;

if(islower(value1))

Practical Lab Manual of C Programming Language 65


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
printf(“Value |%c| is lower case letter ”);

else

printf(“Value |%c| is not a lower case letter”);

if(islower(value2))

printf(“Value |%c| is lower case letter”);

else

printf(“Value |%c| is not a lower case letter”);

getch();

Press ctrl+f9 to run the program.

Output:

Value |S| is not a lower case letter

Value |s| is lower case letter

isupper() Program:

#include<stdio.h>

#include<ctype.h>

void main()

int value1 = ‘S’;

int vaalue2 = ’s’;

if(isupper(value1))

printf(“Value |%c| is upper case letter ”);

else

printf(“Value |%c| is not a upper case letter”);

if(isupper(value2))
Practical Lab Manual of C Programming Language 66
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
printf(“Value |%c| is upper case letter”);

else

printf(“Value |%c| is not a upper case letter”);

getch();

Press ctrl+f9 to run the program.

Output:

Value |S| is a upper case letter

Value |s| is not a upper case letter

ispunct() Program:

The C library ispunct() checks whether the passed character is a punctuation character. A punctuation
character is any graphic character (as I isgraph) that is not alphanumeric.

#include<stdio.h>

#include<ctype.h>

void main()

int value1 = ‘a’;

int vaalue2 = ’/’;

if(ispunct(value1))

printf(“Value |%c| is a punctuation character ”);

else

printf(“Value |%c| is not a punctuation character”);

if(ispunct(value2))

printf(“Value |%c| is a punctuation character”);

Practical Lab Manual of C Programming Language 67


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
else

printf(“Value |%c| is not a punctuation character”);

getch();

Press ctrl+f9 to run the program.

Output:

Value |a| is not a punctuation character

Value |/| is a punctuation character

Practical Lab Manual of C Programming Language 68


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.45: Write a program that takes user information and displays into screen using
structure.

#include<stdio.h>

struct info

char name[50];

int age;

};

int main()

struct info i;

printf(“Enter Your Name: ”);

scanf(“%s”,i.name);

printf(“Enter Your age in years: ”);

scanf(“%d”,&i.age);

printf(“Name: %s\n”,i.name);

printf(“Age: %d\n”,i.age);

getch();

Press ctrl+f9 to run the program.

Output:

Enter Your Name: ABC

Enter Your age in years: 15

Name: ABC

Age: 15
Practical Lab Manual of C Programming Language 69
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No. 46: Write a program that takes one number, one string and one floating point
value, and displays on the screen using structure.

#include<stdio.h>

Struct studentInfo

Char name[50];

int roll_number;

float gp;

Int main()

{ Clrscr();

studentInfo si;

printf(“Enter Your Name: ”);

scanf(“%s”,si.name);

printf(“Enter Your Roll Number: ”);

scanf(“%d”,&si.roll_numbere);

printf(“Enter GPA: ”);

scanf(“%f”,si.gp);

clrscr();
Practical Lab Manual of C Programming Language 70
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
printf(“Name: %s\n”,si.name);

printf(“Roll Number: %d\n”,si.roll_number);

printf(“GPA: %f\n”,si.gp);

getcch();

Press ctrl+f9 to run the program.

Output:

Enter Your Name: ABC

Enter Your Roll Number: 15

Enter GPA: 3.2

Name: ABC

Roll Number: 15

GPA: 3.2

Practical Lab Manual of C Programming Language 71


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.47: Write a program that displays the memory address of a number stored into a
variable.

#include<stdio.h>

void main()

int value=10;

printf(“Value: %d\n”,value);

printf(“Its Memory Address:%d”,&value);

getch();

Press ctrl+f9 to run the program.

Output:

Value: 10

Its Memory Address: 1234567

Practical Lab Manual of C Programming Language 72


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.48: Write a program that takes a number from the user and then displays memory
address of currently entered value on the screen.

#include<stdio.h>

void main()

int value;

printf(“Enter any Number…”,value);

scanf(“%d”,value);

printf(“Number: %d\n”,value);

printf(“Its Memory Address:%d”,&value);

getch();

Press ctrl+f9 to run the program.

Output:

Enter any Number… 15

Number: 15

Its Memory Address: 3452769

Practical Lab Manual of C Programming Language 73


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.49: Write a program that displays a line on the screen.

#include<stdio.h>

#include<graphics.h>

void main();

int gdriver=DETECT, gmode;

int x1=10, y1=300;

int x2=300, y2=300;

initgraph(&gdriver, &gmode,”c:\\tc\\bgi”);

line(x1,y1,x2,y2);

getch();

Press ctrl+f9 to run the program.

Output:

Practical Lab Manual of C Programming Language 74


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.50: Write a program that displays a circle on the screen.

#include<stdio.h>

#include<graphics.h>

void main();

int gdriver=DETECT, gmode; int x=200,

y=200, radius=80; initgraph(&gdriver,

&gmode,”c:\\tc\\bgi”); circle(x,y,radius);

getch();

Press ctrl+f9 to run the program.

Output:

Practical Lab Manual of C Programming Language 75


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.51: Write a program that displays a rectangle on the screen.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

Void main()

clrscr();

int gdriver=DETECT, gmode;

initgraph(&gdriver, &gmode,”c:\\tc\\bgi”);

bar(150,200,400,350);

getch();

Press ctrl+f9 to run the program.

Output:

Practical Lab Manual of C Programming Language 76


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.52: Write a program that displays a triangle on the screen.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

Void main()

int gdrive=DETECT,gmode;

initgraph(&gdriver, &gmode,”c:\\tc\\bgi”);

line(300,100,200,200);

line(300,100,400,200);

line(200,200,400,200);

getch();

Press ctrl+f9 to run the program.

Output:

Practical Lab Manual of C Programming Language 77


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.53: Write a program that displays ellipse on the screen.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

Void main()

int gdrive=DETECT,gmode;

initgraph(&gdriver, &gmode,”c:\\tc\\bgi”);

ellipse(100,100,0,360,50,25);

getch();

Press ctrl+f9 to run the program.

Output:

Practical Lab Manual of C Programming Language 78


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.54: Write a program that displays a bar graph on the screen.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

Void main()

int gdrive=DETECT,gmode;

initgraph(&gdriver, &gmode,”c:\\tc\\bgi”);

settextstyle(BOLD_FONT, HORIZ_DIR,2);

outtextxy(275,0,”Bar Graph”);

/*Draw x and y axis */

line(90,410,90,50);

line(90,410,590,410);

line(85,60,90,50);

line(95,60,90,50);

line(585,405,590,410);

line(585,415,590,410);

outtextxy(65,60,”Y”);

outtextxy(570,420,”X”);

outtextxy(70,415,”0”);

setfillstyle(XHATCH_FILL,RED);

bar(150,80,200,410);

bar(225,100,275,410);

bar(300,120,350,410);

bar(375,170,425,410);

bar(450,135,500,410);

getch();

Practical Lab Manual of C Programming Language 79


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
}

Press ctrl+f9 to run the program.

Output:

A bar graph will be displayed.

Program No.55: Write a program that displays sine wave graph on the screen.

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

#include<dos.h>

Void main()

int gdriver=DETECT, gmode,

int angle = 0;

int x, y;

initgraph(&gdriver, &gmode,”c:\\tc\\bgi”);

line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);

for(x = 0; x < getmaxx(); x+=3) {

/* calculate y value given x */

y = 50*sin(angle*3.141/180);

y = getmaxy()/2 - y;

Practical Lab Manual of C Programming Language 80


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
/* color a pixel at the given position */

putpixel(x, y, 15);

delay(100);

angle+=5;

getch();

closegraph();

Press ctrl+f9 to run the program.

Output:

A Wave graph will be displayed.

Practical Lab Manual of C Programming Language 81


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.56: Write a program that takes name of student, their roll number and marks of
three different subjects, the displays mark sheet as an output. Mark sheet contains subject name,
total marks, obtained marks, percentage and status (pass / fail).

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

char name_of_student[50];

cahr status[50];

int roll_number, marks_english, marks_maths, marks_ict, obtained_marks;

int total_marks = 300; float

percentage; printf(“\nEnter

Your Name… ”);

scanf(“%s”,name_of_student);

printf(“\nEnter Your Roll Number…”);

scanf(“%d”,&roll_number);

printf(“\nEnter Marks of English…”);

scanf(“%d”,&marks_english);

printf(“\nEnter Marks of Maths…”);

scanf(“%d”,&marks_maths);

printf(“\nEnter Marks of ICT…”);

scanf(“%d”,&marks_ict);

obtained_marks=marks_english + marks_maths + marks_ict;

percentage = obtained_marks / total_marks * 100;

if(percentage < 40)

status = “fail”;

else

status=”Pass”;
Practical Lab Manual of C Programming Language 82
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
clrscr();

printf(“%s\n Name: ”name_of_student); printf(“%d\n

Roll Number: ”roll_number); printf(“%d\n Marks in

English: ”marks_english); printf(“%d\n Marks in

MAthematics: ”marks_maths); printf(“%d\n Marks in

ICT: ”marks_ict);

printf(“%d\n Total Marks: ”total_marks);

printf(“%d\n Obtained Marks: ”obtained_ marks);

printf(“%f\n Percentage: ”percentage);

printf(“%s\n Status: ”status);

getch();

Press ctrl+f9 to run the program.

Output:

Enter Your Name… Ahmed

Enter Your Roll Number… 14

Enter Marks of English… 70

Enter Marks of Maths… 80

Enter Marks of ICT… 90

Name: Ahmed

Roll Number: 14

Marks in English: 70

Marks in Maths: 80

Marks in ICT: 90

Total Marks: 300

Obtained Marks: 240

Percentage: 80

Status: Pass

Practical Lab Manual of C Programming Language 83


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
Program No.57: Write a program that calculates area of square from given input of length by
user.

#include<stdio.h>

Void main()

clrscr();

float length, area;

printf(“\nEnter the length of square…”);

scanf(“%f”,&length);

area= length * length;

printf(“%f Area of Square: ”&area);

getch();

Press ctrl+f9 to run the program.

Output:

Enter the length of square… 6

Area of Square: 36

Program No.58: Write a program that calculates area of triangle from given input of length by user.

#include<stdio.h>

#include<math.h>

void main()

clrscr();
Practical Lab Manual of C Programming Language 84
Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
double side1, side2, side3, s, area;

printf(“Enter first side of triangle…”);

scanf(“%1f”,&side1);

printf(“Enter second side of triangle…”);

scanf(“%1f”,&side2);

printf(“Enter third side of triangle…”);

scanf(“%1f”,&side3);

s = (side1 + side2 + side3) / 2;

area = sqrt(s * (s – side1) * (s – side2) * (s – side3));

printf(“Area of triangle: %1f”,area);

getch();

Press ctrl+f9 to run the program.

Output:

Enter first side of triangle… 6

Enter second side of triangle… 3

Enter third side of triangle… 4

Area of triangle:

Practical Lab Manual of C Programming Language 85


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.59: Write a program that calculates area of circle from given input of length by
user.

#include<stdio.h>

void main()

clrscr();

float radius , area;

printf(“\nEnter Radius of circle…”);

scanf(“%f”, &radius);

area = 3.14 * radius * radius;

printf(“\nArea of Circle: %f”, area);

getch();

Press ctrl+f9 to run the program.

Output:

Enter Radius of circle… 5

Area of circle: 78.5

Practical Lab Manual of C Programming Language 86


Shaheed Benazir
SHAHEEDBhutto
BENAZIR University, Shaheed
BHUTTO UNIVERSITY, Benazirabad
SHAHEED BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology

Program No.60: Write a program that sets the screen background color “Red”.

#include<stdio.h>

#include<graphics.h>

void main()

clrsccr();

int gdriver = DETECT , gmode;

initgraph(&gdriver, &gmode, “c:\\tc\\bgi”);

setbkcolor(RED);

getch();

Press ctrl+f9 to run the program.

Output:

Screen Background color will be green.

Practical Lab Manual of C Programming Language 87

Das könnte Ihnen auch gefallen