Beruflich Dokumente
Kultur Dokumente
Laboratory Session 01
Flow Chart:
Start
NED
End
Coding:
#include<stdio.h>
main()
{
printf("NED University of Engineering and Technology-Karachi Petroleum Engineering Department");
}
Output:
NED University of Engineering and Technology-Karachi Petroleum Engineering Department
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Laboratory Session 02
Flow Chart:
Start
Input
num1,
num2
Sum=num1
+num2
output
sum
End
Coding:
#include<stdio.h>
void main()
{
int num1,num2,sum;
printf("Input First number = ");
scanf("%d",&num1);
printf("Input Second number = ");
scanf("%d",&num2);
sum=num1+num2;
printf("the
Output:sum of %d and %d is equal to %d",num1,num2,sum);
}
Output:
Laboratory Session 03
Flow Chart:
start
Input
Basic
salary
Grosssal
Netsal
Gross
salary
Net
salary
End
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Coding:
#include<stdio.h>
main()
{
float basic,conveyal,rental,utilal,grosssal,tax,netsal;
printf("Enter your Basic Salary = ");
scanf("%f",&basic);
conveyal= 0.10*basic;
rental= 0.25*basic;
utilal= 0.12*basic;
grosssal= basic+conveyal+rental+utilal;
tax =0.025*grosssal;
netsal= grosssal-tax;
printf("Your Net Salary is %0.2f and Gross Salary is %0.2f",netsal,grosssal);
}
Output:
Laboratory Session 04
Flow Chart:
Start
Input
temp
Input
°F = 1
K=2
if F
dgree K
=1 °C
°F
°C
End
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Coding:
#include<stdio.h>
main()
{
float num,degree,temp,abc=0;
printf("OBJECT : Write a program which takes temperature from key board and convert it into Degree
Celsius");
printf("\n\n\n");
printf("Enter the temperature which you want to convert in celsius");
scanf("%f",&temp);
printf("if the temperature is in Degree fahrentheit press 1 if it is in KELVIN press 2 and then enter \t");
scanf("%f",°ree);
if(degree<=1)
{
abc= (temp-32)*5/9;
printf("The required temperature in degree centigrade is = %0.2f",abc);
}
else
{
printf("The required temperature in degree centigrade is = %0.2f",temp-273);
}
}
Output:
OBJECT : Write a program which takes temperature from key board and convert it
into Degree Celsius
Laboratory Session 05
Flow Chart:
Start
Input
Num ,
Range
For T Output
i=1,i<=rang Num,i,
e Num*i
F
F
End
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Coding:
#include<stdio.h>
main()
{
int num,range,i;
printf(" OBJECT : Write a program which prints table of any desired number upto desired range. Output
should be like this");
printf("\n\n\n");
printf("Enter the number of which you want to print table = ");
scanf("%d",&num);
printf("Enter the range uptill which you want table = ");
scanf("%d",&range);
for(i=1;i<=range;i++)
{
printf(" \t\t %d\tx\t%d\t =\t %d \n",num,i,num*i);
}
}
Output:
OBJECT : Write a program which prints table of any desired number upto desired
range. Output should be like this
Laboratory Session 06
End
Coding:
#include<stdio.h>
main()
{
int j,i,a=1;
printf(" OBJECT : Write a program which prints the following format, using looping structure");
printf("\n\n\n");
for(i=1;i<=6;i++)
{
for(j=1;j<=a;j++)
{
printf("*");
}
a+=1;
printf("\n");
}
}
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Laboratory Session 07
Wrong
Sqrt &
enter
cbroot
End
Coding:
#include<stdio.h>
#include<math.h>
main()
{
float num,cbroot,sqroot;
printf(" OBJECT : Write a program which prints the square root and cube root of positive number up to desired
range. The range must lie between 1 to 25");
printf("\n\n\n");
printf("Enter the number(less than 25) of which you want Square Root and Cube Root= ");
scanf("%f",&num);
if(num<=25)
{
sqroot= sqrt(num);
Coding:
cbroot= pow(num,1/3.0);
printf(" The Square Root of %0.2f is %0.2f and Cube Root is %0.2f",num,sqroot,cbroot);
}
else
{
printf(" wrong entery");
}
}
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Laboratory Session 08
Flow Chart:
Start
Input
num
If T Output
num< Invalid End
0 entry
F
If F For T
num= i=1,i<tem num=
0 p-1 num*temp-i
T
F
Output
Factorial Output
is 1 Factorial
is num
End
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Coding:
#include<stdio.h>
#include<io.h>
#include<string.h>
main()
{
long int num,temp,i;
printf(" OBJECT : Write a program which prints the factorial of any positive number. Your program must
respond for negative numbers");
printf("\n\n\n");
printf("Input the number of which you want factorial= \t");
scanf("%ld",&num);
temp=num;
if(num<0)
{
printf("invalid entery");
}
else
{
if(num==0)
{
printf("factorial is 1");
}
else
{
for(i=1;i<=temp-1;i++)
{
num=num*(temp-i);
}
printf(" The Factorial of Given number is %ld",num);
}
}
}
Output:
OBJECT : Write a program which prints the factorial of any positive number. Your program must respond
for negative numbers
Laboratory Session 09
Flow Chart:
Start
Input
range
For
i=2, F End
i<=ran
ge
T
For
j=2,
T T
If
j<=i i%j=0
F F
F
If
i=j
Output
i
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Coding:
#include<stdio.h>
main()
{
int i,j,range;
printf("Enter the range to which you want prime numbers ");
scanf("%d",&range);
for(i=2;i<=range;i++)
{
for(j=2;j<=i;j++)
{
if(i%j==0)
{
break;
}
}
if(i==j)
{
printf("%d ",i);
}
}
}
Output:
Laboratory Session 10
Flow Chart:
Start
Input
num
If
num>
Positive
0
If
Num=
0 zero
Negative
End
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Coding:
#include<stdio.h>
void main (void)
{
int num;
printf(" OBJECT : Write a program which takes a number as input and check whether it is negative, positive
or zero.");
printf("\n\n\n");
printf("Please enter the number ");
scanf("%d",&num);
if (num>0)
{
printf("number is positive");
}
else
{
if(num=0)
{
printf("number is equal to zero");
}
else
{
printf("number is negative");
}
}
}
Output:
OBJECT : Write a program which takes a number as input and check whether it is
negative, positive or zero.
Laboratory Session 11
Coding:
#include<stdio.h>
main()
{
int num1,num2,i,j;
printf("Write a program which takes two numbers as input and find the value of first number raised to power
of second number");
printf("\n\n\n");
printf("Enter the first number");
scanf("%d",&num1);
printf("Enter the second number");
scanf("%d",&num2);
j=num1;
for(i=1;i<num2;i++)
{
j=j*num1;
}
printf("Answer of %d raise to the power %d is %d",num1,num2,j);
}
Output:
Write a program which takes two numbers as input and find the value of first nu
mber raised to power of second number
Laboratory Session 12
#include<stdio.h>
main()
{
int num;
printf(" OBJECT: Write a program which a number as input and check whether it is even, Odd, zero or
negative.");
printf("\n\n\n");
printf("Please Enter the number\t");
scanf("%d",&num);
if(num==0)
{
printf("\n number is Zero");
}
else
{
if(num<0)
{
printf("\n Number is negative");
}
else
{
if(num%2==0)
{
printf("\n Number is even");
}
else
{
printf(" \n Number is odd");
}
}
}
}
Output:
OBJECT: Write a program which a number as input and check whether it is even, O
dd, zero or negative.
Number is even
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Laboratory Session 13
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,D,rootx,rooty;
printf(" OBJECT : Write a program which calculates the Roots of quadratic equation. Your program also
calculates imaginary roots and gives output in the following form:X + iY and X - iY ");
printf("\n\n\n");
printf(" Enter the Value of B\t");
scanf("%f",&b);
printf("Enter the value of A\t");
scanf("%f",&a);
printf(" Enter the value of C\t");
scanf("%f",&c);
D = b*b - 4*a*c;
if(D<0)
{
rootx= (-1*b)/(2*a);
rooty= sqrt(-1*D)/(2*a);
printf(" The roots of given quadratic equation are \n%f + %fi and \n%f - %fi",rootx,rooty,rootx,rooty);
}
else
{
rootx= (-1*b)/(2*a);
rooty= sqrt(D)/(2*a);
printf("The roots of given quadratic equation are\n %f and\n %f",rootx+rooty,rootx-rooty);
}
}
Output:
OBJECT : Write a program which calculates the Roots of quadratic equation. Your
program also calculates imaginary roots and gives output in the following form:
X + iY and X - iY
Laboratory Session 14
Title: Switch – Case and Library functions
Objective: Write a program which simulates calculator operation. Your calculator at
least performs the following operations:
Addition
Subtraction
Multiplication
Division
Trigonometric Functions
Coding:
#include<stdio.h>
main()
{
int opr;
float num1,num2;
float div;
printf(" OBJECT : Write a program which simulates calculator operation");
printf("\n\n\n");
printf("Enter the first number = ");
scanf("%f",&num1);
printf("Enter the seecond number = ");
scanf("%f",&num2);
printf("Enter the operation you want to do,\n enter 1 for addtion 2 for subraction 3 for multiplication and 4
for division");
scanf("%d",&opr);
div= num1/num2;
switch (opr)
{
case 1:
printf(" The addtion of %f and %f is %f",num1,num2,num1+num2);
break;
case 2:
printf(" The subraction of %f from %f is %f",num2,num1,num1-num2);
break;
case 3:
printf(" The multiplication of %f and %f is %f",num1,num1,num1*num2);
break;
case 4:
printf(" The Division of %f from %f is %f ",num1,num2,div);
break;
}
}
Output:
OBJECT : Write a program which simulates calculator operation
Laboratory Session 15
#include<stdio.h>
main()
{
float costp,sellp,profit;
printf(" Enter the cost price of your product= ");
scanf("%f",&costp);
printf(" Enter the selling price of your product= ");
scanf("%f",&sellp);
profit=sellp-costp;
if(profit<0)
{
printf(" You had a LOSS of %0.2f rs and the percentage loss is %0.2f ",-1*profit,((-
1*profit)/costp)*100);
}
else
{
printf(" You had PROFIT of %0.2f rs and the percentage of profit is %0.2f ",profit,
(profit/costp)*100);
}
}
Output:
Laboratory Session 16
Title: ASCII codes
Objective: Write a program which prints all ASCII codes.
Coding:
#include<stdio.h>
main()
{
int i;
for(i=0;i<=255;i++)
printf("%d = %c\t",i,i);
}
Output:
Laboratory Session 17
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int num;
printf(" Enter any alphabet ===> ");
num=getche();
if(num>=65&&num<=90||num>=97&&num<=122)
{
if(num>=65&&num<=90)
{
printf(" \nThe small case of %c is \'%c\'\n",num,num+32);
}
else
{
if(num>=97&&num<=122)
{
printf(" \nThe Capital case of %c is \'%c\'\n",num,num-32);
}
else
{
printf(" wrong entry");
}
}
}
}
Output:
Laboratory Session 18
#include<stdio.h>
#include<conio.h>
main()
{
int num;
printf("Enter any alphabet ,special character or digit= ");
num=getche();
if((num>=0&&num<=47)||(num>=58&&num<=64)||(num>=91&&num<=96)||(num>=123&&num<=127))
{
printf("\n\n\nIt is a special Character");
}
else
{
if(num>=48&&num<=57)
{
printf("\n\n\nIt is a Digit");
}
else
{
if(num>=65&&num<=90)
{
printf("\n\n\nIt is a Capital Case alphabet");
}
else
{
printf("\n\n\nIt is a Small Case alphabet");
}
}
}
}
Output:
Laboratory Session 19
Title: Looping
Objective: Write a program which simulate a text editor and continue its typing till
we press “ENTER” key
Coding:
Output:
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Laboratory Session 20
Title: Functions
Objective: Write a program which calculates the area of circle. The radius will be
given by key board. Your program must contain a “function” to calculate
area.
Coding:
#include<stdio.h>
float area(float n);
main()
{
float rad;
printf(" OBJECT: Write a program which calculates the area of circle");
printf("\n\n\n");
printf(" Enter the radius of the circle ");
scanf("%f",&rad);
printf(" The area of the Circle is %0.2f",area(rad));
}
float area(float rad)
{
return(3.142*rad*rad);
}
Output:
Laboratory Session 21
Title: Functions
Objective: Write a program which calculates the factorial of any inputted number.
Your program must contain a recursive function.
Coding:
#include<stdio.h>
long int factorial(int n);
main()
{
int num;
printf(" OBJECT: Write a program which calculates the factorial of any inputted number");
printf("\n\n\n");
printf("Enter the number of which you want factorial \t ");
scanf("%d",&num);
printf("%d ! = %ld",num,factorial(num));
}
long int factorial(int num)
{
if(num<=1)
return(1);
else
return(num*factorial(num-1));
}
Output:
OBJECT: Write a program which calculates the factorial of any inputted number
Laboratory Session 22
Title: Functions
Objective: Write a program which takes five numbers as input and by using function,
calculate s: sum , mean and standard deviation
Coding:
#include<stdio.h>
#include<math.h>
float sum(float a,float b,float c,float d,float e);
float stand(float a,float b,float c,float d,float e);
main()
{
float num1,num2,num3,num4,num5,y;
printf("Enter the first number = ");
scanf("%f",&num1);
printf("Enter the second number = ");
scanf("%f",&num2);
printf("Enter the third number = ");
scanf("%f",&num3);
printf("Enter the fourth number = ");
scanf("%f",&num4);
printf("Enter the fifth number = ");
scanf("%f",&num5);
y=sum(num1,num2,num3,num4,num5);
printf(" sum ==> %0.0f",y);
printf(" Mean==> %0.2f",y/5);
printf(" standard deviation ==> %0.2f",sqrt(stand(num1,num2,num3,num4,num5)));
}
float sum(float num1,float num2,float num3,float num4,float num5)
{
float s;
s=num1+num2+num3+num4+num5;
return(s);
}
float stand(float num1,float num2,float num3,float num4,float num5)
{
float mean,x,a;
mean= (num1+num2+num3+num4+num5)/5;
x=pow(num1-mean,2)+pow(num2-mean,2)+pow(num3-mean,2)+pow(num4-mean,2)+pow(num5-
mean,2);
a=x/5;
return(a);
}
Output:
Enter the first number = 2
Enter the second number = 3
Enter the third number = 2
Enter the fourth number = 3
Enter the fifth number = 2
sum ==> 12 Mean==> 2.40 standard deviation ==> 0.49
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Laboratory Session 23
Title: Array
Objective: Write a program which takes ten numbers as input and arrange them in
descending order.
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int i,num[10],j,temp;
for(i=0;i<=9;i++)
{
printf(" Enter the number%d ==>",i+1);
scanf("%d",&num[i]);
}
for(i=1;i<=9;i++)
{
for(j=0;j<=8;j++)
{
if(num[j]<num[j+1])
{
temp=num[j+1];
num[j+1]=num[j];
num[j]=temp;
}
}
}
printf(" the numbers in descending order are ===> \n");
for(i=0;i<=9;i++)
{
printf(" \t%d\n",num[i]);
}
}
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Output:
Laboratory Session 24
Title: Array
Objective: Write a program which takes elements of two matrices of order 2x2 as
input and prints the result of addition.
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int mat1[2][2],mat2[2][2],i,j;
printf(" \t\tenter the elements of matrix 1\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("enter the element %d%d = ",i+1,j+1);
scanf("%d",&mat1[i][j]);
}
}
printf(" \t\tenter the elements of matrix 2\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("enter the element %d%d = ",i+1,j+1);
scanf("%d",&mat2[i][j]);
}
}
printf("\n The sum of the two matrices is \n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("\t%d",mat1[i][j]+mat2[i][j]);
}
printf("\n");
}
}
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering
Output: