Вы находитесь на странице: 1из 32

PE-205 Computer Programming & Applications

NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 01

Title: Basics of Input and Output


Objective: Write a program which prints:
“NED University of Engineering and Technology-Karachi
Petroleum Engineering Department”

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

Title: Input /Output and basic arithmetic


Objective: Write a program which takes two numbers as input and add them and
prints the result in formatted way.

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:

Input First number = 6


Input Second number = 5
the sum of 6 and 5 is equal to 11
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 03

Title: Input /Output and basic arithmetic


Objective: Write a program which calculates the gross and net salary of employees.
Your program will take basic salary as input and apply following
conditions.
Conveyance allowance = 10% of basic
Rent allowance = 25% of basic
Utility allowance = 12% of basic
Tax = 2.5% of gross
Gross salary = basic + allowances
Net salary = gross – tax

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:

Enter your Basic Salary = 25000

Your Net Salary is 35831.25 and Gross Salary is 36750.00


PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 04

Title: Input /Output and basic arithmetic


Objective: Write a program which takes temperature from key board and convert it
into Degree Celsius. Output should be formatted.

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",&degree);
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

Enter the temperature which you want to convert in celsius40


if the temperature is in Degree Fahrenheit press 1 if it is in KELVIN press 2
and then enter 1
The required temperature in degree centigrade is = 4.44
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 05

Title: Looping Structure


Objective: Write a program which prints table of any desired number upto desired
range. Output should be like this
e.g. 3 X 1 = 3

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

Enter the number of which you want to print table = 3


Enter the range uptill which you want table = 10
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 06

Title: Looping Structure


Objective: Write a program which prints the following format, using looping
structure.
*
**
***
****
*****
******
Flow Chart: Output:
start

OBJECT : Write a program which prints the


F For following format, using looping struc
i=1,i< ture
=6
T
*
For F **
j=1,j< a+=1 ***
=a ****
T *****
******
Output
*

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

Title: Looping Structure & If Statement


Objective: 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.
Flow Chart: Output:

start 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
Input
Number
Enter the number(less than 25) of which you want Square
Root and Cube Root= 25
The Square Root of 25.00 is 5.00 and Cube Root is 2.92
If Sqrt(num)
...
Num<= Cbroot of
25 num

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

Title: Looping Structure & If Statement


Objective: Write a program which prints the factorial of any positive number. Your
program must respond for negative numbers.

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

Input the number of which you want factorial= 6


The Factorial of Given number is 720
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 09

Title: Looping Structure & If Statement


Objective: Write a program which prints the Prime numbers up to the desired range.

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:

Enter the range to which you want prime numbers 25


2 3 5 7 11 13 17 19 23
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 10

Title: Looping Structure & If Statement


Objective: Write a program which takes a number as input and check whether it is
negative, positive or zero.

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.

Please enter the number 5


number is positive
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 11

Title: Looping Structure


Objective: Write a program which takes two numbers as input and find the value of
first number raised to power of second number.

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

Enter the first number5


Enter the second number6
Answer of 5 raise to the power 6 is 15625
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 12

Title: Logical & Relational Operators, If Statement


Objective: Write a program which a number as input and check whether it is even,
Odd, zero or negative.
Coding:

#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.

Please Enter the number 56

Number is even
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 13

Title: Logical & Relational Operators, Library functions


Objective: 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
Coding:

#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

Enter the Value of B 1


Enter the value of A 1
Enter the value of C 1
The roots of given quadratic equation are
-0.500000 + 0.866025i and
-0.500000 - 0.866025i
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

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

Enter the first number = 5


Enter the seecond number = 6
Enter the operation you want to do,
enter 1 for addtion 2 for subraction 3 for multiplication and 4 for division4
The Division of 5.00 from 6.00 is 0.83
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 15

Title: If & Relational Operator


Objective: Write a program which takes Cost price ad Selling price as input and
calculate the profit or loss. Also prints the percentage of profit or loss.
Coding:

#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:

Enter the cost price of your product= 5000


Enter the selling price of your product= 5500
You had PROFIT of 500.00 rs and the percentage of profit is 10.00
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

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:

0= 1=☺ 2=☻ 3=♥ 4=♦ 5=♣ 6=♠ 7= 8= 9=


10 =
14 = ♫ 15 = ☼ 16 = ► 17 = ◄ 18 = ↕ 19 = ‼ 20 = ¶ 21 = § 22 = ▬
23 = ↨ 24 = ↑ 25 = ↓ 26 = → 27 = ← 28 = ∟ 29 = ↔ 30 = ▲ 31 = ▼ 32 =
33 = ! 34 = " 35 = # 36 = $ 37 = % 38 = & 39 = ' 40 = ( 41 = ) 42 = *
43 = + 44 = , 45 = - 46 = . 47 = / 48 = 0 49 = 1 50 = 2 51 = 3 52 = 4
53 = 5 54 = 6 55 = 7 56 = 8 57 = 9 58 = : 59 = ; 60 = < 61 = = 62 = >
63 = ? 64 = @ 65 = A 66 = B 67 = C 68 = D 69 = E 70 = F 71 = G 72 = H
73 = I 74 = J 75 = K 76 = L 77 = M 78 = N 79 = O 80 = P 81 = Q 82 = R
83 = S 84 = T 85 = U 86 = V 87 = W 88 = X 89 = Y 90 = Z 91 = [ 92 = \
93 = ] 94 = ^ 95 = _ 96 = ` 97 = a 98 = b 99 = c 100 = d 101 = e 102 = f
103 = g 104 = h 105 = i 106 = j 107 = k 108 = l 109 = m 110 = n 111 = o 112 = p
113 = q 114 = r 115 = s 116 = t 117 = u 118 = v 119 = w 120 = x 121 = y 122 = z
123 = { 124 = | 125 = } 126 = ~ 127 = ⌂ 128 = Ç 129 = ü 130 = é 131 = â 132 = ä
133 = à 134 = å 135 = ç 136 = ê 137 = ë 138 = è 139 = ï 140 = î 141 = ì 142 = Ä
143 = Å 144 = É 145 = æ 146 = Æ 147 = ô 148 = ö 149 = ò 150 = û 151 = ù 152 = ÿ
153 = Ö 154 = Ü 155 = ¢ 156 = £ 157 = ¥ 158 = ₧ 159 = ƒ 160 = á 161 = í 162 = ó
163 = ú 164 = ñ 165 = Ñ 166 = ª 167 = º 168 = ¿ 169 = ⌐ 170 = ¬ 171 = ½ 172 = ¼
173 = ¡ 174 = « 175 = » 176 = ░ 177 = ▒ 178 = ▓ 179 = │ 180 = ┤ 181 = ╡ 182 = ╢
183 = ╖ 184 = ╕ 185 = ╣ 186 = ║ 187 = ╗ 188 = ╝ 189 = ╜ 190 = ╛ 191 = ┐ 192 = └
193 = ┴ 194 = ┬ 195 = ├ 196 = ─ 197 = ┼ 198 = ╞ 199 = ╟ 200 = ╚ 201 = ╔ 202 = ╩
203 = ╦ 204 = ╠ 205 = ═ 206 = ╬ 207 = ╧ 208 = ╨ 209 = ╤ 210 = ╥ 211 = ╙ 212 = ╘
213 = ╒ 214 = ╓ 215 = ╫ 216 = ╪ 217 = ┘ 218 = ┌ 219 = █ 220 = ▄ 221 = ▌ 222 = ▐
223 = ▀ 224 = α 225 = ß 226 = Γ 227 = π 228 = Σ 229 = σ 230 = µ 231 = τ 232 = Φ
233 = Θ 234 = Ω 235 = δ 236 = ∞ 237 = φ 238 = ε 239 = ∩ 240 = ≡ 241 = ± 242 = ≥
243 = ≤ 244 = ⌠ 245 = ⌡ 246 = ÷ 247 = ≈ 248 = ° 249 = ∙ 250 = ∙ 251 = √ 252 = ⁿ
253 = ² 254 = ■ 255 =
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 17

Title: ASCII, If and Logical Operator


Objective: Write a program which takes an alphabet as input from key board and if it
is in Capital case convert into Small case and vice versa

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:

Enter the cost price of your product= 5000


Enter the selling price of your product= 5500
You had PROFIT of 500.00 rs and the percentage of profit is 10.00 Press any key
to continue . . .
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

Laboratory Session 18

Title: ASCII, If and Logical Operator


Objective: Write a program which takes any input from key board and checks
whether it is Capital or Small case alphabet or a digit or a special
character.
Coding:

#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:

Enter any alphabet ,special character or digit= G

It is a Capital Case alphabet


PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

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:

OBJECT: Write a program which calculates the area of circle

Enter the radius of the circle 10


The area of the Circle is 314.20
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

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

Enter the number of which you want factorial 7


7 ! = 5040
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

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:

Enter the number1 ==>12


Enter the number2 ==>23
Enter the number3 ==>34
Enter the number4 ==>45
Enter the number5 ==>56
Enter the number6 ==>65
Enter the number7 ==>67
Enter the number8 ==>78
Enter the number9 ==>89
Enter the number10 ==>91
the numbers in descending order are ===>
91
89
78
67
65
56
45
34
23
12
PE-205 Computer Programming & Applications
NED University of Engineering & Technology - Department of Petroleum Engineering

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:

enter the elements of matrix 1


enter the element 11 = 2
enter the element 12 = 3
enter the element 21 = 4
enter the element 22 = 5
enter the elements of matrix 2
enter the element 11 = 6
enter the element 12 = 7
enter the element 21 = 8
enter the element 22 = 9

The sum of the two matrices is


8 10
12 14

Вам также может понравиться