Week-4 & 5 Programs
Week-4 & 5 Programs
#include <stdio.h>
int main() {
int num1, num2, num3, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
max = num1;
if (num2 > max) {
max = num2;
}
if (num3 > max) {
max = num3;
}
printf("The maximum of the three numbers is: %d\n", max);
return 0;
}
if (number % 2 == 0) {
printf("%d is Even.\n", number);
} else {
printf("%d is Odd.\n", number);
}
return 0;
}
3. Write a program that declares class awarded for a given percentage of marks,
where mark = 70% = Distinction. Read percentage from standard input.
Solution:
#include <stdio.h>
int main() {
float percentage;
4. Write a program that prints a multiplication table for a given number and the
number of rows in the table.
For example, for a number 5 and rows=3, the output should be:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
Solution:
#include <stdio.h>
int main() {
int number, rows;
return 0;
}
5. Write a C program to input a number and find sum of first and last digit of the
number using for loop (e.g Input 5642, Output=7)
Solution:
#include <stdio.h>
int main() {
int number, lastDigit, firstDigit;
firstDigit = number;
for (; firstDigit >= 10; firstDigit /= 10);
Week-5:
#include <stdio.h>
int main() {
int choice;
float num1, num2, result;
while(1) {
// Display the menu
printf("\nSimple Calculator Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
if (choice == 5) {
printf("Exiting the program.\n");
break;
}
switch (choice) {
case 1:
result = num1 + num2;
printf("Result: %.2f + %.2f = %.2f\n", num1, num2, result);
break;
case 2:
result = num1 - num2;
printf("Result: %.2f - %.2f = %.2f\n", num1, num2, result);
break;
case 3:
result = num1 * num2;
printf("Result: %.2f * %.2f = %.2f\n", num1, num2, result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f / %.2f = %.2f\n", num1, num2, result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
return 0;
}
2. Write a C program to input month number and print total number of days in month
using switch case.
Solution:
#include <stdio.h>
int main() {
int month;
int days;
return 0;
}
Solution:
#include <stdio.h>
int main() {
int number, digit;
int frequency[10] = {0}; // Array to store the frequency of digits (0-9)
return 0;
}
#include <stdio.h>
void decimalToBinary(int number) {
int binary[32];
int i = 0;
int main() {
int choice;
int number;
while (1) {
// Display the menu
printf("\nMenu:\n");
printf("1. Convert Decimal to Binary\n");
printf("2. Convert Decimal to Hexadecimal\n");
printf("3. Exit\n");
printf("Enter your choice (1-3): ");
scanf("%d", &choice);
return 0;
}
Solution:
#include <stdio.h>
int main() {
int sum = 0;
return 0;
}