0% found this document useful (0 votes)
41 views19 pages

Lab Task 4 - 2

The document contains 10 coding problems and their solutions in C programming language. Each problem is related to basic programming concepts like input/output, conditional statements, functions etc. The problems cover topics such as determining quadrant of coordinates, checking temperature conditions, identifying triangle types, converting number to day name, checking triangle validity and more. The solutions provide the full code to implement the logic for each problem.

Uploaded by

2023-3-60-047
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)
41 views19 pages

Lab Task 4 - 2

The document contains 10 coding problems and their solutions in C programming language. Each problem is related to basic programming concepts like input/output, conditional statements, functions etc. The problems cover topics such as determining quadrant of coordinates, checking temperature conditions, identifying triangle types, converting number to day name, checking triangle validity and more. The solutions provide the full code to implement the logic for each problem.

Uploaded by

2023-3-60-047
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/ 19

1.

Write a C program to accept a coordinate point in a XY coordinate system and determine in


which quadrant the coordinate point lies.

Solution:
#include <stdio.h>
#include <stdlib.h>

int main()
{
double a,b;
printf("Enter the coordinates(x,y):");
scanf("%lf %lf", &a,&b);

if (a>0 && b>0){


printf("This is first quadrant.");
}
else if (a<0 && b>0){
printf("This is second quadrant.");
}
else if (a<0 && b<0){
printf("This is third quadrant.");
}
else if (a>0 && b<0){
printf("This is fourth quadrant.");
}
else if (a>0 && b ==0){
printf("On X axis.");
}
else {
printf("On Y axis.");
}
return 0;
}

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:

3. Write a C program to check whether a triangle is Equilateral, Isosceles or Scalene.


Solution:
#include <stdio.h>
#include <stdlib.h>

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>

bool check(int n){


if(n<=0) return false;
while (n%2==0) n/=2; return n==1;
}
int main()
{
int n;
printf("Enter the number:");
scanf("%d", &n);
if (n>0){
if (check(n)){
printf("%d is the power of 2.\n",n);
}
else {
printf("%d is not power of 2.\n",n);
}
}
else if (n==0){
printf("Zero is not a valid input.\n",n);
}
else {
printf("Negative input is not valid.\n");
}
return 0;
}

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.

Marks Letter Grade Marks Letter Grade Marks Letter Grade

90-100 A 70-73 C+ Less Than 55 F

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

printf("Enter your choice (1 to 4):");


scanf("%d", &choice);
switch(choice){
case 1:
add(a,b);
break;
case 2:
sub(a,b);
break;
case 3:
mul(a,b);
break;
case 4:
divi(a,b);
break ;
default:
printf("Invalid choice, Please choice from 1 to 4.\n");
}
if (choice==4 && b!=0){
printf("Enter your choice (1 for quotient and 2 for reminder):");
scanf("%d", &cas);
}
if (cas == 1){
printf("The quotient of the numbers is: %lf.\n", a/b);
}
else {
printf("the remeinder of the numbers is: %lf.\n", (int)a%(int)b);
}
return 0;
}
Output:

10.Program for “Guessing Game”:


Player-1 picks a number X and Player-2 has to guess that number within N = 3 tries. For
each wrong guess by Player-2, the program prints “Wrong, N-1 Chance(s) Left!” If Player-2
successfully guesses the number, the program prints “Right, Player-2 wins!” and stops
allowing further tries (if any left). Otherwise after the completion of N = 3 wrong tries, the
program prints “Player-1 wins!” and halts.
[ Restriction: Without using loop/break/continue
Hint: Use flag ]

Solution:
#include <stdio.h>

void guess(int p1, int chances){

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(){

int p1, chances=3;


printf("Player 1 enter your number:");
scanf("%d", &p1);
guess(p1, chances);
return 0;
}

Output:

You might also like