[go: up one dir, main page]

0% found this document useful (0 votes)
24 views10 pages

Itp Week 5 and 6

Uploaded by

Tejaswi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views10 pages

Itp Week 5 and 6

Uploaded by

Tejaswi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

WEEK5

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;

// Input four numbers


printf("Enter the first number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
printf("Enter the third number: ");
scanf("%lf", &num3);
printf("Enter the fourth number: ");
scanf("%lf", &num4);

// Assume num1 as both max and min initially


max = num1;
min = num1;

// Compare with num2


if (num2 > max) {
max = num2;
} else if (num2 < min) {
min = num2;
}

// Compare with num3


if (num3 > max) {
max = num3;
} else if (num3 < min) {
min = num3;
}

// Compare with num4


if (num4 > max) {
max = num4;
} else if (num4 < min) {
min = num4;
}

// Output the maximum and minimum


printf("Maximum: %lf\n", max);
printf("Minimum: %lf\n", 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;

// Input the number of units consumed


printf("Enter the number of units consumed: ");
scanf("%lf", &units);

// Define the tariff rate per unit (you can change this rate)
tariff = 0.12; // Example tariff rate, in dollars per unit

// Calculate the total bill


totalBill = units * tariff;

// Output the electricity bill


printf("Electricity Bill: $%.2lf\n", totalBill);

return 0;
}
Output:-
Enter the number of units consumed: 10
Electricity Bill: $1.20

iii) Find the roots of the quadratic equation.

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);

// Calculate the discriminant


discriminant = b * b - 4 * a * c;

// Check the discriminant for real or complex roots


if (discriminant > 0) {
// Real and distinct roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Root 1 = %.2lf and Root 2 = %.2lf\n", root1, root2);
} else if (discriminant == 0) {
// Real and equal roots
root1 = -b / (2 * a);
printf("Root 1 = Root 2 = %.2lf\n", root1);
} else {
// Complex roots
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Root 1 = %.2lf + %.2lfi and Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart, realPart,
imaginaryPart);
}

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

printf("Enter two numbers: ");


scanf("%lf %lf", &num1, &num2);

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;

// Input the year


printf("Enter a year: ");
scanf("%d", &year);

// Check if it's a leap year


if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}

return 0;
}

Output:

Enter a year: 1986


1986 is not a leap year.

Enter a year: 1992


1992 is a leap year.

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

// Input the number


printf("Enter a positive integer: ");
scanf("%d", &num);

// Check if the input is a non-negative integer


if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate the factorial using a loop
for (int i = 1; i <= num; i++) {
factorial *= i;
}

// Output the factorial


printf("Factorial of %d = %llu\n", num, factorial);
}

return 0;
}

Output:

Enter a positive integer: 5


Factorial of 5 = 120

ii) Findthegivennumberisaprimeornot.

Program:

#include <stdio.h>
#include <stdbool.h>

// Function to check if a number is prime


bool isPrime(int n) {
if (n <= 1) {
return false; // 0 and 1 are not prime numbers
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false; // If n is divisible by any number between 2 and the square root of n,
it's not prime
}
}
return true; // If no divisors are found, the number is prime
}

int main() {
int num;

// Input the number


printf("Enter a positive integer: ");
scanf("%d", &num);

// Check if the number is prime and print the result


if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}

Output:

Enter a positive integer: 23


23 is a prime number.

Enter a positive integer: 10


10 is not a prime number.

iii) Find sin and cosine series

Program:

#include <stdio.h>
#include <math.h>

// Function to calculate the factorial of a number


double factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
double x, sinX = 0.0;
int n;

// Input the angle in radians


printf("Enter the angle in radians: ");
scanf("%lf", &x);

// Input the number of terms in the series


printf("Enter the number of terms in the series: ");
scanf("%d", &n);

for (int i = 0; i < n; i++) {


int exponent = 2 * i + 1;
double term = pow(-1, i) * pow(x, exponent) / factorial(exponent);
sinX += term;
}

printf("sin(%lf) = %lf\n", x, sinX);

return 0;
}

Output:

Enter the angle in radians: 0.785


Enter the number of terms in the series: 4
sin(0.785000) = 0.706825

Cosine series

#include <stdio.h>
#include <math.h>

// Function to calculate the factorial of a number


double factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}

int main() {
double x, cosX = 0.0;
int n;

// Input the angle in radians


printf("Enter the angle in radians: ");
scanf("%lf", &x);
// Input the number of terms in the series
printf("Enter the number of terms in the series: ");
scanf("%d", &n);

for (int i = 0; i < n; i++) {


int exponent = 2 * i;
double term = pow(-1, i) * pow(x, exponent) / factorial(exponent);
cosX += term;
}

printf("cos(%lf) = %lf\n", x, cosX);

return 0;
}

Output:

Enter the angle in radians: 0.785


Enter the number of terms in the series: 4
cos(0.785000) = 0.707385

iv) Checkinganumberpalindrome

Program:

#include <stdio.h>

int main() {
int num, originalNum, reversedNum = 0, remainder;

// Input a number
printf("Enter an integer: ");
scanf("%d", &num);

originalNum = num; // Store the original number for comparison

// Reverse the number


while (num > 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}

// 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.

Enter an integer: 343


343 is a palindrome.

v) Constructapyramidofnumbers.

Program:

#include <stdio.h>

int main() {
int rows, i, j, number = 1;

printf("Enter the number of rows for the pyramid: ");


scanf("%d", &rows);

for (i = 1; i <= rows; i++) {


// Print leading spaces
for (j = 1; j <= rows - i; j++) {
printf(" ");
}

// Print numbers in increasing order


for (j = 1; j <= i; j++) {
printf("%d ", number);
number++;
}

printf("\n");
}

return 0;
}

Output:

Enter the number of rows for the pyramid: 5


1
23
456
7 8 9 10
11 12 13 14 15

You might also like