0% found this document useful (0 votes)
33 views7 pages

Week-4 & 5 Programs

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

Week-4 & 5 Programs

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

Week-4:

1. Write a C program to find the maximum of given three numbers.


Solution:

#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;
}

2. Write a C program to determine whether input number is Odd or Even. Display


appropriate message.
Solution:
#include <stdio.h>
int main() {
int number;

printf("Enter an integer: ");


scanf("%d", &number);

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;

printf("Enter your percentage: ");


scanf("%f", &percentage);

if (percentage >= 70) {


printf("Distinction\n");
} else if (percentage >= 60) {
printf("First Class\n");
} else if (percentage >= 50) {
printf("Second Class\n");
} else if (percentage >= 40) {
printf("Pass\n");
} else {
printf("Fail\n");
}
return 0;
}

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;

printf("Enter the number for the multiplication table: ");


scanf("%d", &number);

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


scanf("%d", &rows);

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


printf("%d x %d = %d\n", number, i, number * i);
}

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;

printf("Enter a number: ");


scanf("%d", &number);

lastDigit = number % 10;

firstDigit = number;
for (; firstDigit >= 10; firstDigit /= 10);

int sum = firstDigit + lastDigit;

printf("Sum of first and last digit: %d\n", sum);


return 0;
}

Week-5:

1. Write a C program for designing a calculator using menu driven Program?


Solution:

#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;
}

printf("Enter two numbers: ");


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

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;

// Read the month number from the user


printf("Enter month number (1-12): ");
scanf("%d", &month);

// Determine the number of days in the month using switch case


switch(month) {
case 1: // January
case 3: // March
case 5: // May
case 7: // July
case 8: // August
case 10: // October
case 12: // December
days = 31;
break;
case 4: // April
case 6: // June
case 9: // September
case 11: // November
days = 30;
break;
case 2: // February
printf("Enter the year: ");
int year;
scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {


days = 29; // Leap year
} else {
days = 28; // Non-leap year
}
break;
default:
printf("Invalid month number.\n");
return 1; // Exit the program with an error code
}
printf("Total number of days: %d\n", days);

return 0;
}

3. Write a C program to count the frequency of digits in a given number.


(Frequency means number of occurrence of the digit in the given number)

Solution:

#include <stdio.h>
int main() {
int number, digit;
int frequency[10] = {0}; // Array to store the frequency of digits (0-9)

// Read the number from the user


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

// Loop to extract digits and count their frequency


while (number > 0) {
digit = number % 10; // Extract the last digit
frequency[digit]++; // Increment the count of the extracted digit
number = number / 10; // Remove the last digit
}

// Print the frequency of each digit


printf("Digit frequency in the given number:\n");
for (int i = 0; i < 10; i++) {
if (frequency[i] > 0) { // Only print digits that appear in the number
printf("Digit %d: %d times\n", i, frequency[i]);
}
}

return 0;
}

4. Write a C program for calculator to include the following in the menu


a. To convert decimal to binary.
b.To convert decimal to hexadecimal.
Solution:

#include <stdio.h>
void decimalToBinary(int number) {
int binary[32];
int i = 0;

while (number > 0) {


binary[i] = number % 2;
number = number / 2;
i++;
}

printf("Binary equivalent: ");


for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}

void decimalToHexadecimal(int number) {


char hexadecimal[32];
int i = 0;

while (number > 0) {


int remainder = number % 16;
if (remainder < 10) {
hexadecimal[i] = remainder + '0';
} else {
hexadecimal[i] = remainder - 10 + 'A';
}
number = number / 16;
i++;
}
printf("Hexadecimal equivalent: ");
for (int j = i - 1; j >= 0; j--) {
printf("%c", hexadecimal[j]);
}
printf("\n");
}

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

// Handle the user's choice


switch (choice) {
case 1:
printf("Enter a decimal number: ");
scanf("%d", &number);
decimalToBinary(number);
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d", &number);
decimalToHexadecimal(number);
break;
case 3:
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}

return 0;
}

5. Write a C program to find the sum of first 100 natural numbers.

Solution:

#include <stdio.h>

int main() {
int sum = 0;

// Loop to calculate the sum of the first 100 natural numbers


for (int i = 1; i <= 100; i++) {
sum += i;
}

// Print the result


printf("The sum of the first 100 natural numbers is: %d\n", sum);

return 0;
}

You might also like