Assignment 1
Assignment 1
Lab Assignment
Subject: Programming for problem solving Discipline: B – Tech (All)
Subject Code: ES-CS291 Semester: 2nd
Assignment – 1
The objective of this assignment is to learn how to write C program using Input/output
Function and conditional statements using:
(a) ‘if else’ condition
(b) ‘if else if’ condition and how to use logical operators.
(c) ‘switch case’ statement
Assignment:
The objective of this assignment is to learn how to write C program using Input/output Function and
conditional statements using:
a. ‘if else’ condition
b. ‘if else if’ condition and how to use logical operators.
(c) ‘switch case’ statement
Assignment:
1. Write a C program to swap two numbers. (with and without third variable)
[Hints: Swapping is used in various programs like sorting the array. It is mainly used in
the area when we want to store old values without using much space.
Algorithm:
Using Third variable:
STEP 1: Declare a variable a,b and c as integer;
STEP 2: Read two numbers a and b;
STEP 3: c=a;
STEP 4: a=b;
STEP 5: b=a;
STEP 6: Print a and b
Without using third variable:
STEP 1: START
STEP 2: ENTER A, B
STEP 3: PRINT A, B
STEP 4: A = A + B
STEP 5: B= A - B
STEP 6: A =A - B
STEP 7: PRINT A, B
STEP 8:
END ]
2. Write a C program to check whether a number is even or odd using if-else statement.
[Hint:
Algorithm:
Step 1: Start
Step 2: Taking input of a number say n
Step 3: Read the number n
Step 4: Check if((n%2)==1), then
Print n is an odd number
Else
Print n is an even number
Step 5: End
3. Write a C program to check whether a year is a leap year or not.
[Hint:
In the Gregorian calendar, a normal year consists of 365 days. Because the actual length of a
sidereal year (the time required for the Earth to revolve once about the Sun) is actually
4. Write a C program to find all roots of a quadratic equation ax^2+bx+c=0 for all possible combinations
of a, b and c. A quadratic equation will have two roots which are obtained using the following expression
X=(-b±√(b^2-4ac))/2a where b^2-4ac is called discriminate.
Note: When, b^2-4ac>0 roots are real and unequal.
b^2-4ac=0 roots are real and equal i.e.x=-b/2a .
b^2-4ac<0 roots are imaginary i.e.x=-b/2a±(√(b^2-4ac) )/2a*i.
[Hint: Quadratic equations are the polynomial equations of degree 2 in one variable of type:
f(x) = ax2+ bx + c where a, b, c, ∈ R and a ≠ 0. It is the general form of a quadratic equation
where 'a' is called the leading coefficient and 'c' is called the absolute term of f (x).
A quadratic equation will always have two roots. The nature of roots may be either real or
imaginary.
The roots of a quadratic equation are given by the quadratic formula:
Step 1. Start
Step 2. Read the coefficients of the equation, a, b and c from the user.
Step 3. Calculate discriminant = (b * b) – (4 * a * c)
Step 4. If discriminant > 0:
4.1: Calculate root1 = ( -b + sqrt(discriminant)) / (2 * a)
4.2: Calculate root2 = ( -b - sqrt(discriminant)) / (2 * a)
4.3: Display “Roots are real and different”
4.4: Display root1 and root2 Step
Step 5: Else if discriminant = 0:
5.1: Calculate root1 = -b / (2 *a)
5.2: root2 = root1
5.3: Display “Root are real and equal”
5.4: Display root1 and root2
Step 6. Else:
6.1: Calculate real = -b / (2 * a)
6.2:Calculate imaginary = sqrt(-discriminant) / (2 * a)
6.3: Display “Roots are imaginary”
6.4: Display real, “±” , imaginary, “i”
Step 7. Stop ]
5. Mr. Sayan Ghosh is an employee of a Private Firm. His Basic is Rs. 5500/-. Now the dearness
allowance is 74% of his basic salary and house rent allowance is 15% of basic salary. Write a program
to calculate his gross salary. [Though his basic salary is given, do this program where basic is taken
through keyboard].
[Hint: Gross salary = Basic + DA + HRA; where Basic is given through keyboard.
DA will be calculated from basic. DA= 74% of Basic
HRA= 15% of Basic ]
6. Write a C program to calculate and print electricity bill for consumer @Rs.3.75 per unit, given the
following information: previous meter reading and current reading.
[ Hint:
Step 1: Two meter reading will be given through the keyboard.
Step 2: Total consumption of electricity can be calculated by the difference of current and
previous electricity reading.
Step 3: To calculate electricity bill amount multiply Calculated difference with 3.75.
Step 4: Print the calculated electricity bill. ]