Beruflich Dokumente
Kultur Dokumente
Benazir Bhutto
BENAZIR University,
BHUTTO Shaheed
UNIVERSITY, SHAHEED Benazirabad
BENAZIR ABAD
DEPARTMENT OF INFORMATION TECHNOLOGY
Department of Information Technology
SBBUSBA
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
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
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
2. strcat() Program:............................................................................................................................... 48
4. strcmp() Program:............................................................................................................................. 49
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
Isalnum(); ............................................................................................................................................ 1
Isalpha(); ............................................................................................................................................. 1
Iscntrl(); .............................................................................................................................................. 1
Isdigit(); ............................................................................................................................................... 1
Isgraph();............................................................................................................................................. 1
Islower();............................................................................................................................................. 1
Isupper(); ............................................................................................................................................ 1
Ispunct();............................................................................................................................................. 1
isalnum() Prgram:.................................................................................................................................... 61
isdigit() Program:..................................................................................................................................... 64
islower() Program:................................................................................................................................... 65
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
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
#include<stdio.h>
void main()
clrscr();
printf(“Hello Word!”);
getch();
Output:
Hello Word!
#include<stdio.h>
void main()
{ clrscr();
printf(“44”);
getch();
Output:
44
#include<stdio.h>
void main()
clrscr();
int number=44;
printf(“%d”,number);
getch();
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();
Output:
45.6
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();
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();
Output:
Welcome SBBU
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();
scanf(“%d”,number1);
scanf(“%d”,number2);
clrscr();
temp=number1;
number1=number2;
number2=temp;
getch();
Output:
Program No.7: Write a program that add two numbers and prints the sum on the screen.
#include<stdio.h>
void main()
clrscr();
num1=10; num2=15;
result=num1+num2;
getch();
Output:
Program No.8: Write a program that subtracts two numbers and prints the difference on the
screen.
#include<stdio.h>
void main()
clrscr();
num1=50;
num2=15;
result=num1-num2;
getch();
Output:
Program No.9: Write a program that multiplies two numbers and prints the result on the screen.
#include<stdio.h>
void main()
clrscr();
num1=5; num2=10;
result=num1*num2;
getch();
Output:
Program No. 10: Write a program that divides two numbers and prints the result on the screen.
#include<stdio.h>
void main()
clrscr();
num1=50;
num2=10;
result=num1/num2;
getch();
Output:
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();
num1=50; num2=10;
result=num1%num2;
printf(“%dResult = ”,result);
getch();
Output:
Result = 0
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();
scanf(“%d”,&num1);
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();
Output:
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;
scanf(“%d”,&number);
if(number%2=0)
printf(“%d is even”,number);
printf(“%d is odd”,number);
getch();
Output:
Number is even.
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();
scanf(“%d”,¤cy);
currency_result=currency_amount/100;
getch();
Output:
Amount in dollars= 50
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();
scanf(“%d”,&distance_kilometres);
distance_metres=distance_metres*1000;
getch();
Output:
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();
Output:
Enter an alphabet… r
r is consonant
Program No.17: Write a program that takes two integers from user and compares which number
is greater.
#include<stdio.h>
void main()
clrscr();
scanf(“%d”,&num1);
scanf(“%d”,&num2);
else
getch();
Output:
8 is greater
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;
scanf(“%d”,&year);
if(year%400=0 || year%4=0)
else
getch();
Output:
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;
scanf(“%d”,&age);
else
getch();
Output:
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();
}
Output:
10
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();
scanf(“%d”,&number);
factorial=factorial*counter;
getch();
}
Output:
Factorial of 5 =120
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();
scanf(“%d”,&number);
printf(“\n %d * %d = %d”,number,counter,number*counter);
getch();
}
Output:
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
#include<stdio.h>
void main()
clrscr();
scanf(“%d”,&number);
result=number*counter;
printf(“\n %d * %d = %d”,number,counter,result);
getch();
}
Output:
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
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();
long result;
scanf(“%d”,&base);
scanf(“%d”,&exp);
while(exp!=0)
result=result*base;
exp--;
printf(“%dAnswer = ”,result);
getch();
Output:
Answer = 125
Program No.24: Write a program that generates triangle of stars on screen. (Nested loop)
#include<stdio.h>
void main()
clrscr();
for(inner=1;inner<=outer;inner++)
printf(“*”);
Printf(“\n”);
getch();
}
Output:
*
**
***
****
*****
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:
4+3=7
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;
scanf(“%d”,&month_number);
switch(month_number)
case 1:
case 2:
case 3:
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:
case 6:
case 7:
case 8:
case 9:
case 10:
}
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:
case 12:
default:
getch();
Output:
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;
scanf(“%d”,&month_number);
switch(month_number)
case 1:
case 2:
case 3:
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:
case 6:
case 7:
case 8:
case 9:
case 10:
}
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:
case 12:
default:
getch();
Output:
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;
scanf(“%d”,&age);
if(age>=1 || age<=15)
else if (age>30)
getch();
Output:
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();
Output:
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();
Output:
Hello Word!
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];
for(counter=0;counter<5;counter++)
scanf(“%d”,&arr[counter]);
} clrscr();
for(counter=0;counter<5;counter++)
Printf(“%d,”,arr[counter]);
getch();
Output:
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();
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;
/* 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();
Output:
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();
float result;
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();
Output:
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()
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
}
getch();
Output:
45
23
56
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 resultant_matrix[2][2];
scanf(“%d”&matrix1[outer][inner]);
printf(“\n”);
for(outer=0;outer<2;outer++)
for(inner=0;inner<2;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();
Output:
Resultant Matrix:
18
10
44
56
• Strcpy();
• Strcat();
• Strlen();
• Strcmp();
• Strchr();
• Strstr();
1. strcpy() Program:
#include<stdio.h>
#include<string.h>
void main()
Program”); printf(“Copied
Output:
2. strcat() Program:
#include<stdio.h>
#include<string.h>
void main()
clrscr();
printf(“%s”,string1);
getch();
Output:
3. strlen() Program:
#include<stdio.h>
#include<string.h>
void main()
clrscr();
int len=strlen(str);
getch();
Output:
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)
else
getch();
Output:
5. strchr() Program:
#include<stdio.h>
#include<string.h>
void main()
char *s;
s = strchr(str , ’t’);
if (s ! = “”)
getch();
Output:
6. strstr() Program:
#include<stdio.h>
#include<string.h>
Void main()
clrscr();
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);
getch();
Output:
Program No.37: Write a program that passes a value through function, and displays in the
screen.
#include<stdio.h>
void main()
clrscr();
disp(value);
getch();
Void disp(int a)
Printf(“%d”,a);
Output:
100
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 main()
clrscr();
int arr[]={12,34,32,43,25};
displayNumber(arr[2]);
getch();
Printf(“%d”,num);
Output:
32
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>
void main()
clrscr();
int number1,number2,result;
scanf(“%d”,number1);
scanf(“%d”,numbers);
result = displaySum(number1,number2);
printf(“%dSum= ”,result);
getch();
int result=num1+num2;
return result;
Output:
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;
scanf(“%d”,number1);
scanf(“%d”,numbers);
highest_number = displaySum(number1,number2);
getch();
return num1;
else
Return num2;
Output:
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 main()
clrscr();
scanf(“%d”, &num1);
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();
int temp;
temp=num2;
num1=temp;
printf(“After Swapping:, the value of A is: %d and the value of B is: %d”,num1,num2);
Output:
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()
scanf(“%d”,num);
result=oddEven(num);
getch();
If(num%2=0)
else
Output:
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>
void main()
clrscr();
scanf(“%d”,&num1);
scanf(“%d”,&num2);
add(&num1,&num2,&result);
getch();
result=num1+num2;
Output:
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();
printf(“%d”,num);
Output:
12345
Built in functions:
Isalnum();
Isalpha();
Iscntrl();
Isdigit();
Isgraph();
Islower();
Isupper();
Ispunct();
isalnum() Prgram:
#include<stdio.h>
#include<ctype.h>
void main()
if(isalnum(value1))
else
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:
Isalpha() Program:
#include<stdio.h>
#include<ctype.h>
void main()
if(isalpha(value1))
else
getch();
Output:
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]))
break;
i++;
getch();
Output:
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()
if(isdigit(value1))
else
getch();
Output:
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()
if(isgraph(value1))
else
if(isgraph(value2))
else
getch();
Output:
islower() Program:
The C library function islower() checks whether the passed character is lowercasr letter.
#include<stdio.h>
#include<ctype.h>
void main()
if(islower(value1))
else
if(islower(value2))
else
getch();
Output:
isupper() Program:
#include<stdio.h>
#include<ctype.h>
void main()
if(isupper(value1))
else
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
getch();
Output:
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()
if(ispunct(value1))
else
if(ispunct(value2))
getch();
Output:
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;
scanf(“%s”,i.name);
scanf(“%d”,&i.age);
printf(“Name: %s\n”,i.name);
printf(“Age: %d\n”,i.age);
getch();
Output:
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;
scanf(“%s”,si.name);
scanf(“%d”,&si.roll_numbere);
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(“GPA: %f\n”,si.gp);
getcch();
Output:
Name: ABC
Roll Number: 15
GPA: 3.2
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);
getch();
Output:
Value: 10
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;
scanf(“%d”,value);
printf(“Number: %d\n”,value);
getch();
Output:
Number: 15
#include<stdio.h>
#include<graphics.h>
void main();
initgraph(&gdriver, &gmode,”c:\\tc\\bgi”);
line(x1,y1,x2,y2);
getch();
Output:
#include<stdio.h>
#include<graphics.h>
void main();
&gmode,”c:\\tc\\bgi”); circle(x,y,radius);
getch();
Output:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
Void main()
clrscr();
initgraph(&gdriver, &gmode,”c:\\tc\\bgi”);
bar(150,200,400,350);
getch();
Output:
#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();
Output:
#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();
Output:
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”);
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();
Output:
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 angle = 0;
int x, y;
initgraph(&gdriver, &gmode,”c:\\tc\\bgi”);
y = 50*sin(angle*3.141/180);
y = getmaxy()/2 - y;
putpixel(x, y, 15);
delay(100);
angle+=5;
getch();
closegraph();
Output:
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];
percentage; printf(“\nEnter
scanf(“%s”,name_of_student);
scanf(“%d”,&roll_number);
scanf(“%d”,&marks_english);
scanf(“%d”,&marks_maths);
scanf(“%d”,&marks_ict);
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();
ICT: ”marks_ict);
getch();
Output:
Name: Ahmed
Roll Number: 14
Marks in English: 70
Marks in Maths: 80
Marks in ICT: 90
Percentage: 80
Status: Pass
#include<stdio.h>
Void main()
clrscr();
scanf(“%f”,&length);
getch();
Output:
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;
scanf(“%1f”,&side1);
scanf(“%1f”,&side2);
scanf(“%1f”,&side3);
getch();
Output:
Area of triangle:
Program No.59: Write a program that calculates area of circle from given input of length by
user.
#include<stdio.h>
void main()
clrscr();
scanf(“%f”, &radius);
getch();
Output:
Program No.60: Write a program that sets the screen background color “Red”.
#include<stdio.h>
#include<graphics.h>
void main()
clrsccr();
setbkcolor(RED);
getch();
Output: