Lab 2
Lab 2
Applied Sciences
Computing Fundamentals
Laboratory Exercise-02
Self-Test
Without executing this program write the output of this program on a paper, once you have written the predicted
output on the paper , execute the program and compared the output shown by this program on the screen with
the output you wrote down on the paper.
#include <conio.h>
#include <stdio.h>
void main(void)
{
clrscr();
printf("This is year %d\n",2011);
printf("The sun will rise at %d:%d in the morning today \n",6,30); printf("If
we multiply %d with %d the answer will be %d \n",12,3,36); printf("My friend
%s came to visit me yesterday at %d-%d p.m.\n", "Ahmed",5,30);
printf("\n\t%d\n\t%d+\n\t----\n\t%d",10,20,30);
getch(); }
Type Conversion
Copy the code given in the following examples, execute the first example and see if the out is according to normal
arithmetic rules or something is different? Try to answer the question given after example‐1.
Example-1
#include<stdio.h>
#include<conio.h>
void main()
{
int a=4,b=37;
float result;
result=b/a;
printf(“Result of 37 / 5 = %f”,result);
getch();
}
Execute the second version of example a and check the output again and see the question given after the second
example.
#include<stdio.h>
#include<conio.h>
int main()
{
float a=4.0,b=37.0;
float result;
result=b/a;
printf(“Result of 37 / 5 = %f”,result);
Explanation:
You may read the following explanation or skip if you know answer to the question.
At line 3 , the division b / a is an integer division, as in c/c++ if an operation involve integers only, the answer will
must be an integer, in our case fraction produced during division will be converted to integer and later will be
stored as float in the variable result;
We know that b/a = 37/4 = 9.25 but as the result is to be stored as it is an integer division the answer will be an
integer i.e. 9 only, as this value has to be stored in a float variable this value will be promoted (converted) to float
first i.e. 9 will be converted to 9.0 and then stored in result.
In case of result=10/3 , the value stored in result will be 3.0 not 3 or 3.3333… for same reasons
In case of result=10/3.0 or result=10.0/3 second value will be first promoted to float type i.e. in 10/3.0 10 will be
converted to 10.0 and the division will be 10.0/3.0 which a division of floating point numbers, the answer will also
be a float point number 3.333…. this value will be stored in variable result.
Activity 1
Try to predict the value x in following statements, write programs to verify your answers. If your predicted
answer do not match with output of your program see explanation at the end of this document.
Activity 2
A car is travelling with a velocity of 30 km/hour , how much time will it take to reach its destination which is 600 km
from starting point, represent the time in hours, minutes and seconds.
Modify the above program such that it can find the time for any given velocity V and distance S, the values of S
and V are provided by the user as input.
Activity 3
Write a program to find the area of a circle whose radius is r, the program should take the value of r from the
user using scanf function and print the area of the circle.
Activity 4
Write a program to solve the following equation for any values of a and b (a+b)2=
(a2+b2+2ab)
When programming, the aim of the program will often require checking of one value stored by a variable against
another value to determine whether one is larger, smaller, or equal to the other. There are a number of operators
that allow these checks. When a comparison is performed by relation operator the result is either true or false.
Here are some of the relational operators, along with examples:
Logical Operators
Logical operators are used to combine expressions containing relation operators. In C, there are 3 logical
operators:
&& Logical AND If c=5 and d=2 then, ((c==5) && (d>5)) returns false.
Explanation
For expression, ((c==5) && (d>5)) to be true, both c==5 and d>5 should be true but, (d>5) is false in the given
example. So, the expression is false. For expression ((c==5) || (d>5)) to be true, either the expression should be
true. Since, (c==5) is true. So, the expression is true. Since, expression (c==5) is true, !(c==5) is false.
if
The structure of an if statement is as follows:
if (Comparison)
Line of code
The line of code will execute only if the comparison is true, if there are multiple line of code to be executed
when a comparison is true, the structure will be as follows
if (Comparison){
Line of code-1
Line of code-2
Line of code-3
Line of code-.
Line of code-.
Line of code-N
}
else
Sometimes when the condition in an if statement evaluates to false, it would be nice to execute some code instead
of the code executed when the statement evaluates to true. The "else" statement effectively says that whatever
if ( TRUE ) {
/* Execute these statements if TRUE */
}
else {
/* Execute these statements if FALSE */
}
Example‐02
#include <stdio.h>
#include <conio.h>
void main(){
int age;
printf( "Please enter your age" ); //Asks for age
scanf( "%d", &age ); //The input is put in age
getch();
}
Activity 5
Just for exercise convert the following statements into conditional statements for A, B, C and D, you do not
need to write program for this, either do it verbally or write down on your notebook. If you feel you
understand the concepts and do not need this exercise you may proceed to next part. To better understand
the task see the examples.
Given that A, B, C, D are all integer variables having some initial value (which is not known)
Example (Single condition)
Statement Conditional Statement
A is a positive number A>0
B is not zero B!=0
Single Condition
Statement Conditional Statement
A and B are equal
A is an even number
5 C is a factor of C
B divides C and there is no remainder
C is greater than D
B is an odd number
Sum of A and B is less than C
C is greater than 100 and less than 200
Multiple Condition
Statement Conditional Statement
C and D are negative numbers
A is greater than B and C is greater than D
A is even or B is odd number
A or B or C is a positive number
A and B are greater than 20 or C and D are less than
100
A and B are even numbers
C is positive and D is negative
A or B is even and C is odd
Write a C program to read temperature in centigrade and display a suitable message according to
temperature state below:
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot
We can check if a number is even or odd by using % (mod) operator if a number is even the remainder with 2 will
3 %2 = 1 (3 is odd)
7 %2 = 1 (7 is odd)
6% 2 = 0 (6 is even)
The following program will get an integer from the user and will check whether the number is even or odd
Example-03
#include<stdio.h>
#include<conio.h>
int main(){
int num;
printf(“Enter a Number : “);
scanf(“%d”,&num)
if(num%2==0)
printf(“\nEven Number”);
else
printf(“\nOdd Number”);
getch();
}
Activity 7
Write program and check that the number entered by the user is a multiple of 7 or not.
Activity 8
Get two numbers X and Y from the user, check that X completely divides Y or not. If X does not divides Y print
the remainder. The output of the program should be as follows
Enter value for X: 3
Enter value for Y: 10
X does not Divides Y
Remainder is = 1
Press any key to terminate the program. . .
#include <stdio.h>
#include <conio.h>
int main(){
int a,b,c;
printf(“Enter value for A : “)
scanf(“%d”,&a);
getch();
}
Activity 9
Re-write the program given in Example-04 and check which of the three numbers is greatest, print the value of
the number as well. See sample output
Sample Output-01
Enter value of A : 5
Enter value of B : 8
Enter value of C : 9
Sample Output-02
Enter value of A : 5
Enter value of B : 8
Enter value of C : 8
Activity 10
Write a program that lets user enter an integer value between 1 and 10, the program validates the input, if the
value entered is between 1 and 10 the program prints the message “Valid Number” and value entered
otherwise the program should print message “Invalid Number” and value. For better understanding of the
problem following are two sample outputs of the program
Sample Output-1
Sample Output-2
Write a program that ask users to input marks of a subject and print the letter grade (See grading criteria
below). For details see the following sample execution of the program
Grading Criteria
Marks 51 – 60 D
Marks 61 – 70 C
Marks 71 – 80 B
Marks 81 – 100 A
Sample Output-1
Enter the marks: 80
Grade: A
Activity 12
Write a program that gets a three digit integer input and prints the sum of its digits. For example if user enters
123 the program should calculate 1+2+3 = 6 as answer. To get an idea about the solution see the program
below
#include<stdio.h>
#include<conio.h>
void main(){
int num=12;
int digit1,digit2;
digit1=num%10;
digit2=num/10;
Activity 13
Armstrong Numbers: An Armstrong number is an n-digit number that is equal to the sum of the nth powers of
its digits. For example 153 is an Armstrong number as it is a three digit number and if each digit is raised to
the 3rd power and summed the result will be 153.
13+53+33=153
Write a program that gets a three digit number from the user and checks whether the number is an
Armstrong number or not. Output of your program should be as under.
Hint: To check that a number is an Armstrong or not you first need to separate its digits as done in last activity
Sample Output
Enter a number 342
Not an Armstrong number
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case,
and the variable being switched on is checked for each case.
Syntax:
switch(expression){
case constant-expression :
statement(s);
break;//optional
case constant-expression :
statement(s);
break;//optional
The expression used in a switch statement must have an integral or character type.
You can have any number of case statements within a switch. Each case is followed by the value to be
compared to and a colon.
The constant‐expression for a case must be the same data type as the variable in the switch, and it
must be a constant or a literal.
When the variable being switched on is equal to a case, the statements following that case will
execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the
next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to
subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The
default case can be used for performing a task when none of the cases is true. No break is needed in the
default case.
switch(grade)
{
case'A':
printf("Excellent!\n”);
break;
case'B':
printf("Great!\n”);
break;
case'C':
printf("Well done\n”);
break;
case'D':
printf("You passed\n”);
break;
case'F':
printf("Better try again\n”);
break;
default:
printf("Invalid grade\n”);
}
Activity 14
Modify the above program, let the user enter total marks and marks obtained, calculate the letter grade (as
you did in Activity 11 using if statement), assign letter grade to a character variable (let’s say grade), pass the
variable to switch statement to print appropriate statement about the letter grade
Activity 15
Write a program that ask user to enter a two digit number, the program should print this number in words,
for example if user enters 12 the program should print twelve, this would be a long task so it is better that
you restrict the program to work for numbers between 25 and 65 only.
Activity 16
Write a program that allow user to enter the day of the week as integer value e.g. for Monday the user can
enter 1, for Sunday user can enter 7.
The program should print the name of the day by using this integer value