Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Pakistan Institute of Engineering and

Applied Sciences

Computing Fundamentals & Programming


Spring 2024
Laboratory Exercise-07

April 4th, 2024

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


Instructions:

1. Using Mobile Without Permission Will Be Marked As Absent And 50% Marks Of This
Lab Will Be Deducted.

2. Copying Assignment / Using the internet During Lab tasks and Marking Proxy will Lead
you to an “F” Grade in the Lab. Be very careful.

3. Use an appropriate naming convention for variable name. e.g to calculate the sum of the
number variable name can be sum, sum_of_number. Random variable names are not allowed.
4. Lab Tasks must be submitted in pdf with a screen shot of output.
5. File name should be as

Degree_Full Name

6. Submission Deadline of Lab Task is on the same day, during Lab, by 10:20 am

Topics Covered

1. If statements
2. If else statements
3. If else if statements
4. Break Statement
5. Continue Statement
6. Exit function

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


Self-Activity

SA - Task -1
Write a c program which declare arithmetic assignment operators with declaration of variables
and display the mathematical expressions on the screen.

// demonstrates arithmetic assignement operators


#include <stdio.h>
int main()
{
int ans = 27;
ans += 10; //same as: ans = ans + 10;
printf(" %d, ",ans);
ans -= 7; //same as: ans = ans - 7;
printf(" %d, ",ans);
ans *= 2; //same as: ans = ans * 2;
printf(" %d, ",ans);
ans /= 3; //same as: ans = ans / 3;
printf(" %d, ",ans);
ans %= 3; //same as: ans = ans % 3;
printf(" %d, \n",ans);
getchar(); return 0;
}

SA- Task -2

Write a c program which describe of increment operators ++ and display these on the screen.

// demonstrates arithmetic increment


operators
#include <stdio.h>
int main()
{
int i= 6;
printf("%d\n",i++); /* Prints 6 sets i to 7 */
printf("%d\n",i); // 7

getchar(); return 0;
}

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


SA- Task -3

Write a c program which describe of decrement operators -- and display these on the screen.

// demonstrates arithmetic decrement


operators
#include <stdio.h>
int main()
{
int i= 6;
printf("%d\n",i--);
printf("%d\n",i); //

getchar(); return 0;
}

SA- Task -4

Write a c program as follows.

#include<stdio.h>
int main()
{
int a = 7, b = 20, c, d;
c = a++;
printf("%d", c);
printf("\n%d",a);
d = ++b;
printf("\n%d", d);
printf("\n%d",b);
getchar(); return 0;
}

Within Lab - Tasks

WL - Task -1

Write a similar c program which initialize variable V =20, declare variable d,c and apply
increment and decrement by one number

Output

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


The if Statement
Use the if statement to specify a block of code to be executed if a condition is true.

Syntax
if (condition) {
// block of code to be executed if the condition is true
}

SA – Task 5

Write a C program to print even number or odd number

#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number:\n");
scanf("%d",&num);
if(num%2==0)
printf("\nThe number %d is an even
number",num);
if(num%2!=0)
printf("\nThe number %d is an odd
number",num);
getchar(); getchar();
return 0;
}

WL - Task -2

Write a C program using if else statement for detecting even or odd number

Output

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


The if else Statement
Use the else statement to specify a block of code to be executed if the condition
is false.

Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

WL - Task -3

Write a C program to implement using if else statement, if age is less than 16 print “ Go
to Child Specialist Room 10 otherwise go to Adult Specialist room number 9

WL - Task -4

Write a C program using if else statement, If age is less than 12 print The Fee is 500
otherwise Fee is 200.

Output

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


WL - Task -5

Write a C program to implement a program only if statements, if age is less than or


equal to 12 , print “Go to Pediatrics in Room 10” otherwise print “Go to Medical Specialists in
Room 15”.

Output

WL - Task -6

Write a C program to implement a program if age is less than 12, print”Please go to


child specialist in Room 10, Fee is Rupees 400”, if age is greater 12 and less than 60 print “
Please go Medical Specialist in Room 15, Fee is Rupees 500”, if age is greater than 60
print”Please go to Medicial Specialist in Room 19, Fee is Rupees 200”

Output

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


The if else if Statement
Use the else if statement to specify a new condition if the first condition is false.

Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is
true
} else {
// block of code to be executed if the condition1 is false and condition2 is
false
}

WL - Task -7

Write a C program to implement a program using if else if statements if age is is less


than 12 print “Please go to Child specialist in Room 10, Fee is rupees 200” else if print “Please
go Medical Specialist in Room 15, Fee is is 400”, else print “ Please go to Medical Specialist in
Room 19”

Ouput

WL - Task -08

Write a C program which takes two input numbers (x and y) from user if x is greater
than y “print x is greater than y” if x =y, print “x is equal to y” if x is less than y print “x is
smaller than y”

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


Break Statement
The break is a keyword in C which is used to bring the program control out of the loop. The
break statement is used inside loops or switch statement. The break statement breaks the
loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then
proceeds to outer loops.

Syntax:

for (counter_initialization; test_condition; counter_increment) {


//statement(s)
if(test_condition)
break;
//statement(s)
}
//outer statement(s)

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


SA Task -06

Output:
value of variable num is: 0
value of variable num is: 1
value of variable num is: 2
Out of while-loop

SA Task -07

Output:
var: 100
var: 99
Out of for-loop

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


WL Task 9

Write a C program that takes a sequence of integers from the user using a for loop. Display
each integer and break the loop if the user enters a number that is a multiple of 7.

Continue Statement
The continue statement in C programming works somewhat like the break statement. Instead
of forcing termination, it forces the next iteration of the loop to take place, skipping any code in
between. For the for loop, continue statement causes the conditional test and increment portions of
the loop to execute.

Syntax:

for (counter_initialization; test_condition; counter_increment) {


//statement(s)
if(test_condition)
continue;
//statement(s)
}

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


SA Task -08

Output:
10986543210

SA Task -09

Output:
0, 1, 2, 3, 4, 5, 6, 8, 9

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


WL Task 10

Write a C program that reads 10 numbers from the user using a for loop. Calculate and print
the sum of all numbers divisible by 3, skipping the others using the continue statement.

exit() function
Description
The C library function void exit(int status) terminates the calling process immediately.

The line of End of program is not printing.

WL Task 11
Write a c program which exit itself when someone enter 0.

WL Task 12

For a school develop a program which assists teachers by displaying the respective grade of
student when marks are entered. Teacher will enter Marks and a Letter grade should be
displayed as per following breakdown:

• If marks are greater than 80, grade will be “A”


• If marks are greater than 70 but less than 80, grade will be “B”
• If marks are greater than 60 but less than 70, grade will be “C”
• If marks are greater than 50 but less than 60, grade will be “D”
• If marks are less than 50, grade will be “F”
• If marks are less than 0, it will be considered as Invalid input.

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]


Home Task Section
n

Q1. Write a c program for following


(a) Increment a code from 1 to 50 and decrement from 50 to 1 using
for loop, while loop and do while loop.
(b) Check the following coditions (Do the program using if statements,
if else statements, if else if statements)
If age is <12 print(The patient should go to room no 10)
If age is >12 print(The patient should go to room no 11)
If age =12print (The patient should go to room no 12)

Q2. Write a C program for following formulas


Assign the value i=10 and increment it upto 1000 and decrement j=1000 to
10. And print the statement (“I am a bonafied student of PIEAS”) for every
increment and decrement.

Non-Submission Important Practice Tasks

Computing Fundamentals & Programming Lab 07 [Type here] [Type here]

You might also like