Sie sind auf Seite 1von 23

Lab Sheet 2

Submitted To: Abdul Karim Shahid

Submitted By: Hamza Rauf

Registration No: FA18-BCS-020

Section-D

Course: Programming Fundamentals


Program#1:
Write a program to calculate the electricity bill. The rates of electricity per unit are
as follow:
If the units consumed are equal or less than 300, then the cost is Rs. 3/- per unit
If units consumed are more than 300, then the cost is Rs. 3.5/- per unit and
surcharge of 5% of bill is added

Code:
Output:
Program#2
Write a program that reads 5 marks of different subjects out of 100 from the keyboard and
determines and displays the sum and percentile of the marks. Then print grades and credit points
on the basic of percentile as per following table:
Grades Letter Credit Points Percentage Marks
A (Excellent) 4.0 90and above
A- 3.7 85-89
B+ 3.3 80-84
B (Good) 3.0 75-79
B- 2.7 70-74
C+ 2.3 65-69
C (Average) 2.0 60-64
C- 1.7 55-59
D (Minimum passing) 1.3 50-54
F (Failing) 0.0 Less than 50

Code:
#include<stdio.h>
#include<stdlib.h>
/* This program was prepared by HAMZA RAUF (FA18-BCS-020) on 7-03-19 */
int main()
{
int ICT,CALCULUS,PHYSICS,ISLAMIAT,ENGLISH,marks;
float percentage;
printf("Enter marks of five subjects\n",ICT);
scanf("%d%d%d%d%d",&ICT,&CALCULUS,&PHYSICS,&ISLAMIAT,&ENGLISH);
marks=ICT+CALCULUS+PHYSICS+ISLAMIAT+ENGLISH;
percentage=(float)marks/500*100;
printf("Obtained Marks is %d\n",marks);
printf("Percentage marks is %f\n",percentage);
if(percentage>=90 && percentage<=100){
printf("Grade A\n");
printf("Credit points 4.0\n");
}
if(percentage>=85 && percentage<=89){
printf("Grade A-\n");
printf("credit points 3.7\n");
}
if(percentage>=80 && percentage<=84){
printf("Grade B+\n");
printf("credit points 3.3\n");
}
if(percentage>=75 && percentage<=79){
printf("Grade B\n");
printf("credit points 3.0\n");
}
if(percentage>=70 && percentage<=74){
printf("Grade B-\n ");
printf("credit points 2.7\n");
}
if(percentage>=65 && percentage<=69){
printf("Grade C+\n");
printf("credit points 2.3\n");
}
if(percentage>=60 && percentage<=64){
printf("Grade C\n");
printf("credit points 2.0\n");
}
if(percentage>=55 && percentage<=59){
printf("Grade C-\n");
printf("credit points 1.7\n");
}
if(percentage>=50 && percentage<=54){
printf("Grade D\n");
printf("credit points 1.3\n");
}
if(percentage<50){
printf("Grade F\n");
printf("credit points 0.0\n");
}
system("pause");
return 0;
}

Output:
Enter marks of five subjects
92
91
85
83
87
Obtained Marks is 438
Percentage marks is 87.599998
Grade A-
credit points 3.7
Press any key to continue . . .
Program#3
While purchasing certain items, a discount of 10% is offered if the quantity
purchased is more than 1000. If quantity and price per item are input through the keyboard, write
a program to calculate the total expenses.

Code:
#include <stdio.h>
#include<stdlib.h>
/* This program was prepared by HAMZA RAUF (FA18-BCS-020) on 7-03-19 */

int main( )

{
int qty, dis = 0 ;
float rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
system("pause");
return 0;

Output:
Enter quantity and rate 1300 60
Total expenses = Rs. 70200.000000
Press any key to continue . . .

Program#4:
A company insures its drivers in the following cases:
− If the driver is married.
− If the driver is unmarried, male &amp; above 30 years of age.
− If the driver is unmarried, female &amp; above 25 years of age.
In all other cases the driver is not insured. If the marital status, gender and age of the
driver are the inputs, write a programme to determine whether the driver is to be insured or not.

Code:
#include <stdio.h>
#include<conio.h>
/* This program was prepared by HAMZA RAUF (FA18-BCS-020) on 7-03-19 */

int main( )
{
char gender, ms ;
int age ;
printf ( "Enter age, sex, marital status " ) ;
scanf ( "%d %c %c", &age, & gender, &ms ) ;

if ( ms == 'M' || ms == 'm' )
printf ( "Driver is insured" ) ;
else if(ms == 'U' || ms == 'u')
{
if (gender == 'M' || gender == 'm' )
{if ( age > 30 )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
else if(gender == 'F' || gender == 'f' )
{
if ( age > 25 )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
else
printf("\nInvalid gender charachter!!");
}
else
printf("\nInvalid marital status charachter!!");
return 0;
}

Output:
Enter age, sex, marital status 27 F M
Driver is insured
Process returned 0 (0x0) execution time: 13.806 s
Press any key to continue.

Program#5:
Print the following series using for loop
a. Print numbers from 1 to 100 with increment of 1
b. Print numbers from 100 to 1 with decrement of 1
c. Print numbers from 20 to 2 in steps of -2
d. Print sequence of numbers: 2, 5, 8, 11, 14, 17, 20
e. Print sequence of numbers: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0

Code:
#include<stdio.h>
#include<stdlib.h>
/* This program was prepared by HAMZA RAUF (FA18-BCS-020) on 7-03-19 */

int main()
{
int i;
for(i=1;i<=100;i++)
printf("%22d \n",i);
for(i=100;i>=1;i--)
printf("%2d\n",i);
for(i=20;i>=2;i=i-2)
printf("%d\n",i);
for(i=2;i<=20;i=i+3)
printf("%d\n",i);
for(i=99;i>=0;i=i-11)
printf("%d\n",i);
system("pause");
return 0;
}

Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
20
18
16
14
12
10
8
6
4
2
2
5
8
11
14
17
20
99
88
77
66
55
44
33
22
11
0
Press any key to continue . . .
Program#6:
Write a program that reads in five integers and then determines and prints the largest
and smallest integers in the group. Use only the techniques you have learnt so far, and make sure
that the program only uses three variables.

Code:

Output:
Enter five numbers : 5 7 8 9 11
Max value is 11
Min value is 5
Process returned 0 (0x0) execution time : 5.488 s
Press any key to continue.
Program#7:
Write a program that reads a number and determines and prints whether it is odd or
even.

Code:
#include<stdio.h>
#include<conio.h>
/* This program was prepared by HAMZA RAUF (FA18-BCS-020) on 7-03-19 */

int main()
{
int n;
printf(“Enter an integer : ”);
scanf(“%d”,&n);
switch(n%2)
{
case 0:
printf(“Number is Even”);
break;
case 1:
printf(“Number is Odd”);
}
return 0;
}

Output:
Enter an integer: 41
Number is Odd
Process returned 0 (0x0) execution time : 5.488 s
Press any key to continue.

Program#8:
Write a program which asks the user to enter 10 numbers and prints out the message
“even” if the number is even and “divisible by three” if the number is divisible by three.

Code:
#include<stdio.h>
#include<stdlib.h>
/* This program was prepared by HAMZA RAUF (FA18-BCS-020) on 7-03-19 */

int main()

{
int n = 0;
printf("Enter a Number : ");
scanf("%d",&n);
if(n%3==0)
{
printf("Number %d", n);
printf(" is divisible by 3\n");
}
printf("OK\n");
system("pause");
return 0;
}

Output:
Enter a Number: 12
Number 12 is divisible by 3
OK
Press any key to continue . . .

Program#9:
A person invests $1000.0 in a savings account yielding 5% interest. Assuming that
all interest is left on deposit in the account, calculate and print the amount of money in he
account at the end of each year for 10 years. Use the following formula for determining these
amounts:
A person invests $1000.0 in a savings account yielding 5% interest. Assuming that
all interest is left on deposit in the account, calculate and print the amount of money in he
account at the end of each year for 10 years. Use the following formula for determining these
amounts:

Code:
#include<stdio.h>
#include<math.h>
/* This program was prepared by HAMZA RAUF (FA18-BCS-020) on 7-03-19 */

int main(void)
{
double amount;
double principal = 1000.0;
double rate = .05;
int year;
printf("%4s%21s\n","Year","Amount on deposit");
for(year = 1; year<=10; year++){
amount = principal * pow(1.0 + rate,year);
printf("%4d%21.2f\n",year,amount);
}
return 0;

Output:
Year Amount on deposit
1 1050.00
2 1102.50
3 1157.63
4 1215.51
5 1276.28
6 1340.10
7 1407.10
8 1477.46
9 1551.33
10 1628.89

Process returned 0 (0x0) execution time : 0.013 s


Press any key to continue.

Das könnte Ihnen auch gefallen