Assignment 5
1.
#include <stdio.h>
int main() {
int num1, num2;
// Prompt the user to enter the first number
printf("Enter the first number: ");
scanf("%d", &num1);
// Prompt the user to enter the second number
printf("Enter the second number: ");
scanf("%d", &num2);
// Check if the two numbers are equal
if (num1 == num2) {
printf("The two numbers are equal.\n");
} else {
printf("The two numbers are not equal.\n");
}
return 0;
}
2.
#include <stdio.h>
int main() {
int number;
// Prompt the user to enter a number
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is positive or negative
if (number > 0) {
printf("%d is a positive number.\n", number);
} else if (number < 0) {
printf("%d is a negative number.\n", number);
} else {
printf("%d is neither positive nor negative (it is zero).\n", number);
}
return 0;
}
3.
#include <stdio.h>
int main() {
float cost_price, selling_price, profit_or_loss, profit_percent, loss_percent;
// Prompt the user to enter the cost price
printf("Enter the cost price: ");
scanf("%f", &cost_price);
to enter the selling price
printf("Enter the selling price: ");
scanf("%f", &selling_price);
// Calculate the profit or loss
profit_or_loss = selling_price - cost_price;
// Calculate the profit or loss percentage
if (profit_or_loss > 0) {
profit_percent = (profit_or_loss / cost_price) * 100;
} else {
loss_percent = (profit_or_loss / cost_price) * 100;
}
// Print the result
if (profit_or_loss > 0) {
printf("Profit: $%.2f\n", profit_or_loss);
printf("Profit percentage: %.2f%%\n", profit_percent);
} else {
printf("Loss: $%.2f\n", -profit_or_loss);
printf(" %.2f%%\n", -loss_percent);
}
return 0;
}
4.
#include <stdio.h>
int main() {
float angle1, angle2, angle3;
// Prompt the user to enter the first angle
printf("Enter the first angle: ");
scanf("%f", &angle1);
// Prompt the user to enter the second angle
printf("Enter the second angle: ");
scanf("%f", &angle2);
// Prompt the user to enter the third angle
printf("Enter the third angle: ");
scanf("%f", &angle3);
// Check if the sum of the angles is 180 degrees
if (angle1 + angle2 + angle3 == 180) {
printf("A triangle can be formed with the given angles.\n");
} else {
printf("A triangle cannot be formed with the given angles.\n");
}
return 0;
}
5.
#include <stdio.h>
int main() {
int m, n;
// Prompt the user to enter the value of m
printf("Enter the value of m: ");
scanf("%d", &m);
// Calculate the value of n based on the given conditions
if (m > 0) {
n = 1;
} else if (m == 0) {
n = 0;
} else {
n = -1;
}
// Print the value of n
printf("The value of n is %d\n", n);
return 0;
}
6.
#include <stdio.h>
int main() {
int day_number;
// Prompt the user to enter the day number
printf("Enter the day number (1-7): ");
scanf("%d", &day_number);
// Display the corresponding day name
switch (day_number) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Invalid day number\n");
}
return 0;
}
7.
#include <stdio.h>
int main() {
int digit;
// Prompt the user to enter a digit
printf("Enter a digit (0-9): ");
scanf("%d", &digit);
// Display the corresponding word
switch (digit) {
case 0:
printf("Zero\n");
break;
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("Invalid digit\n");
}
return 0;
}
8.
#include <stdio.h>
int main() {
int month_number;
// Prompt the user to enter the month number
printf("Enter the month number (1-12): ");
scanf("%d", &month_number);
// Display the corresponding month name
switch (month_number) {
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Invalid month number\n");
}
return 0;
}
9.
#include <stdio.h>
int main() {
char character;
// Prompt the user to enter a character
printf("Enter a character: ");
scanf(" %c", &character);
// Check if the character is an alphabet
if (character >= 'A' && character <= 'Z' || character >= 'a' && character <= 'z') {
printf("%c is an alphabet.\n", character);
} else {
// Check if the character is a special character
if (character >= '!' && character <= '@' || character >= '#' && character <= '$' || character >=
'%' && character <= '^' || character >= '&' || character <= '*') {
printf("%c is a special character.\n", character);
} else {
printf("%c is neither an alphabet nor a special character.\n", character);
}
}
return 0;
}
10.
#include <stdio.h>
int main() {
char alphabet;
// Prompt the user to enter an alphabet
printf("Enter an alphabet: ");
scanf(" %c", &alphabet);
// Check if the alphabet is a vowel
if (alphabet >= 'A' && alphabet <= 'E' || alphabet >= 'I' && alphabet <= 'O' || alphabet >= 'U'
&& alphabet <= 'Z') {
printf("%c is a vowel.\n", alphabet);
} else {
// Check if the alphabet is a consonant
if (alphabet >= 'a' && alphabet <= 'e' || alphabet >= 'i' && alphabet <= 'o' || alphabet >= 'u'
&& alphabet <= 'z') {
printf("%c is a vowel.\n", alphabet);
} else {
printf("%c is a consonant.\n", alphabet);
}
}
return 0;
}
11.
#include <stdio.h>
int main() {
char grade;
// Prompt the user to enter a grade
printf("Enter a grade (E, V, G, A, F): ");
scanf(" %c", &grade);
// Declare the equivalent description
switch (grade) {
case 'E':
printf("Excellent\n");
break;
case 'V':
printf("Very Good\n");
break;
case 'G':
printf("Good\n");
break;
case 'A':
printf("Average\n");
break;
case 'F':
printf("Fail\n");
break;
default:
printf("Invalid grade\n");
}
return 0;
}
12.
#include <stdio.h>
int main() {
float temperature;
// Prompt the user to enter the temperature
printf("Enter the temperature in centigrade: ");
scanf("%f", &temperature);
// Display a suitable message according to the temperature state
if (temperature < 0) {
printf("Freezing weather\n");
} else if (temperature >= 0 && temperature <= 10) {
printf("Very Cold weather\n");
} else if (temperature > 10 && temperature <= 20) {
printf("Cold weather\n");
} else if (temperature > 20 && temperature <= 30) {
printf("Normal in Temp\n");
} else if (temperature > 30 && temperature <= 40) {
printf("Its Hot\n");
} else {
printf("Its Very Hot\n");
}
return 0;
}