Lab Task 4 - 2
Lab Task 4 - 2
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double a,b;
printf("Enter the coordinates(x,y):");
scanf("%lf %lf", &a,&b);
Output:
2. Write a C program to read temperature and display a suitable message according to
temperature state below :
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double t;
printf("Enter the temperature:");
scanf("%lf", &t);
if(t <0){
printf("Freezing weather.\n");
}
else if (t>=0 && t<=10){
printf("Very Cold weather.\n");
}
else if(t>=10 && t<=20){
printf("Cold weather.\n");
}
else if(t>=20 && t<=30){
printf("Normal");
}
else if(t>=30 && t<=40){
printf("Its Hot.\n");
}
else{
printf("Its Very Hot.\n");
}
return 0;
}
Output:
int main()
{
double v,l,o;
printf("Enter the lengths of three sides of the triangle:");
scanf("%lf %lf %lf", &v,&l,&o);
if(v <= 0 || l <= 0 || o <= 0) {
printf("Invalid Input! Sides of a triangle must be greater than zero.\n");
}
else if(v==l && l==o){
printf("The triangle is Equilateral.");
}
else if (v==l || l==o || v==o){
printf("The triangle is Isosceles.");
}
else {
printf("The triangle is Scalane.");
}
return 0;
}
Output:
4. Write a program in C to read any day number in integer and display day name in the word.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int day;
printf("Enter the day number (1-7):");
scanf("%d", &day);
switch(day){
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wedday\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.");
}
return 0;
}
Output:
5. Program that will check whether a triangle is valid or not, when the three angles (angle value
should be such that, 0 < value < 180) of the triangle are entered through the keyboard.
[Hint: A triangle is valid if the sum of all the three angles is equal to 180 degrees.]
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ang1,ang2,ang3,sum;
printf("Enter the three angles of a triangle:");
scanf("%d %d %d", &ang1,&ang2,&ang3);
sum=ang1+ang2+ang3;
switch(sum){
case 180:
printf("Valid triangle");
break;
default:
printf("Invalid Triangle");
}
return 0;
}
Output:
6. Program that will read from the console a random number and check if it is a nonzero
positive number. If the check is yes, it will determine if the number is a power of 2.
If the check fails the program will check for two more cases. If the number is zero, the
program will print “Zero is not a valid input”. Else it will print “Negative input is not valid”.
Solution:
#include <stdio.h>
#include <stdbool.h>
Output:
7. Program that will categorize a single character that is entered at the terminal, whether it is an
alphabet, a digit or a special character.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
printf("Enter your character:");
scanf("%c", &c);
if ((c>='a' && c<='z') || (c>='A' && c<='Z')){
printf("This an alphabet.\n");
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U'){
printf("This is also a vowel.\n");
}
else if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
printf("This is also a vowel.\n");
}
else{
printf("This is also a consonant.\n");
}
}
else if (c>='0'&& c<='9'){
printf("This is a digit.\n");
}
else {
printf("This a special character.\n");
}
return 0;
}
Output:
8.Program that will take the final score of a student in a particular subject as input and find
his/her grade.
86-89 A- 66-69 C
82-85 B+ 62-65 C-
78-81 B 58-61 D+
74-77 B- 55-57 D
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m;
printf("Enter your mark:");
scanf("%d", &m);
if(m<=100 && m>=90){
printf("You got %d and your grade is A", m);
if(m<=89 && m>=86){
printf("You got %d and your grade is A-", m);
}
}
else if(m<=85 && m>=82){
printf("You got %d and your grade is B+", m);
}
else if(m<=81 && m>=78){
printf("You got %d and your grade is B", m);
}
else if(m<=77 && m>=74){
printf("You got %d and your grade is B-", m);
}
else if(m<=73 && m>=70){
printf("You got %d and your grade is C+", m);
}
else if(m<=69 && m>=66){
printf("You got %d and your grade is C", m);
}
else if(m<=65 && m>=62){
printf("You got %d and your grade is C-", m);
}
else if(m<=61 && m>=58){
printf("You got %d and your grade is D+", m);
}
else if(m<=57 && m>=55){
printf("You got %d and your grade is D", m);
}
else {
printf("You got %d and your grade is F", m);
}
return 0;
}
Output:
9.Program that will construct a menu for performing arithmetic operations. The user will give
two real numbers (a, b) on which the arithmetic operations will be performed and an integer
number (1 <= Choice <= 4) as a choice. Choice-1, 2, 3, 4 are for performing addition,
subtraction, multiplication, division respectively.
If Choice-4 is selected, the program will check if b is nonzero.
If the check is true, the program will ask for another choice (1 <= Case <=2), where Case-1,
2 evaluate quotient and reminder respectively. If the check is false, it will print an error
message “Error: Divisor is zero” and halt.
Solution:
#include <stdio.h>
#include <stdlib.h>
void add(double a, double b){
printf("The sum of the numbers is: %lf.\n", a+b);
}
void sub(double a, double b){
printf("The difference of the numbers is: %lf.\n", a-b);
}
void mul(double a, double b){
printf("The multiplication of the numbers is: %lf.\n", a*b);
}
void divi(double a, double b){
if(b==0){
printf("Error: Divisor is zero.\n");
}}
int main() {
double a,b;
int choice, cas;
printf("Enter your numbers:");
scanf("%lf %lf", &a,&b);
Solution:
#include <stdio.h>
if (chances==0){
printf("Player 1 wins.\n");
return ;
}
else {
int p2;
printf("Player 2 guess the number:");
scanf("%d", &p2);
if (p2==p1){
printf("Yes, you are right. Player 2 wins!\n");
}
else{
printf("Wrong, %d Chances Left!\n", chances - 1);
guess(p1, chances - 1);
}
}
}
int main(){
Output: