Itp Week 5 and 6
Itp Week 5 and 6
Objective: Explore the full scope of different variants of “if construct” namely if-else, null-
else, if-else if-else, switch and nested-if including in what scenario each one of them can be
used and how to use them. Explore all relational and logical operators while writing
conditionals for “if construct”.
Suggested Experiments/Activities:
Tutorial5:Branching and logical expressions:
Lab5:Problemsinvolvingif-then-elsestructures.
i) Write a C program to find the max and min off our numbers using if-else.
Program:
#include <stdio.h>
int main() {
double num1, num2, num3, num4;
double max, min;
return 0;
}
Output:-
Enter the first number: 10
Enter the second number: 20
Enter the third number: 30
Enter the fourth number: 50
Maximum: 50.000000
Minimum: 10.000000
ii) Write a C program to generate electricity bill.
Program:
#include <stdio.h>
int main() {
double units, tariff, totalBill;
// Define the tariff rate per unit (you can change this rate)
tariff = 0.12; // Example tariff rate, in dollars per unit
return 0;
}
Output:-
Enter the number of units consumed: 10
Electricity Bill: $1.20
Program:
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c;
double discriminant, root1, root2;
// Input coefficients
printf("Enter the coefficients of the quadratic equation (a, b, c): ");
scanf("%lf %lf %lf", &a, &b, &c);
return 0;
}
Output:
Enter the coefficients of the quadratic equation (a, b, c): 1 2 1
Root 1 = Root 2 = -1.000000
iv) WriteaCprogramtosimulateacalculatorusing switchcase.
Program:
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Simple Calculator\n");
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator); // Note the space before %c to consume any leading whitespace
switch (operator) {
case '+':
result = num1 + num2;
printf("Result: %.2lf\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2lf\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2lf\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2lf\n", result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Error: Invalid operator\n");
break;
}
return 0;
}
Output:
Simple Calculator
Enter an operator (+, -, *, /): +
Enter two numbers: 23 345
Result: 368.00
v) WriteaCprogramtofindthegivenyearisaleapyear ornot.
A leap year occurs every 4 years to help synchronize the calendar year with the solar year, which is about
365.2422 days long. In a leap year, an extra day (February 29th) is added to the calendar. To determine if
a given year is a leap year, you can use the following rules:
1. If a year is evenly divisible by 4, go to step 2. Otherwise, it's not a leap year.
2. If a year is divisible by 100, go to step 3. Otherwise, it's a leap year.
3. If a year is divisible by 400, it's a leap year. Otherwise, it's not a leap year.
Program:
#include <stdio.h>
int main() {
int year;
return 0;
}
Output:
WEEK6
Objective:Explorethefullscopeofiterativeconstructsnamelywhileloop,do-whileloopand
forloopinadditiontostructuredjumpconstructslikebreakandcontinueincludingwhen
each of these statements is more appropriate to use.
Suggested Experiments/Activities:
Tutorial6:Loops,whileandfor loops
Lab6:Iterativeproblemse.g.,thesumofseries
i) Findthefactorialofgivennumberusing any loop.
Program:
#include <stdio.h>
int main() {
int num;
unsigned long long factorial = 1; // We use an unsigned long long to accommodate
large factorials
return 0;
}
Output:
ii) Findthegivennumberisaprimeornot.
Program:
#include <stdio.h>
#include <stdbool.h>
int main() {
int num;
return 0;
}
Output:
Program:
#include <stdio.h>
#include <math.h>
return 0;
}
Output:
Cosine series
#include <stdio.h>
#include <math.h>
int main() {
double x, cosX = 0.0;
int n;
return 0;
}
Output:
iv) Checkinganumberpalindrome
Program:
#include <stdio.h>
int main() {
int num, originalNum, reversedNum = 0, remainder;
// Input a number
printf("Enter an integer: ");
scanf("%d", &num);
// Check if the original number and the reversed number are the same
if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
return 0;
}
Output:
Enter an integer: 3456
3456 is not a palindrome.
v) Constructapyramidofnumbers.
Program:
#include <stdio.h>
int main() {
int rows, i, j, number = 1;
printf("\n");
}
return 0;
}
Output: