Skip to content

Latest commit

 

History

History
553 lines (455 loc) · 8.84 KB

conditions.md

File metadata and controls

553 lines (455 loc) · 8.84 KB

Conditions

Conditions-Related Problems and Solutions

Problem-1

#include<stdio.h>
int main()
{
   double number ;
   printf("Enter a number:");
   scanf("%lf",&number);
   if(number>=0)
    printf("Positive");
   else
    printf("Negative");
   return 0;
}

Problem-2

#include<stdio.h>
int main()
{
   int number ;
   printf("Enter a number:");
   scanf("%d",&number);
   if(number%2==0)
    printf("Even");
   else
    printf("Odd");
   return 0;
}

Problem-3

#include <stdio.h>

int main() {
int digit;

printf("Enter a single digit (0-9): ");
scanf("%d", &digit);

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 input.\n");
}
return 0;
}

Problem-4

#include<stdio.h>
int main()
{
 int a,b,c;
 printf("Enter Three angles:");
 scanf("%d%d%d",&a,&b,&c);
 if(a>0 && b>0 && c>0 && a+b+c==180)
    printf("Yes");
 else
    printf("No");
 return 0;
}

Problem-5

#include <stdio.h>
int main() {
unsigned int a;
printf("Enter a positive nonzero number: ");
scanf("%u", &a);
if (a > 0 && (a & (a - 1)) == 0)
printf("YES");
else
printf("NO");
return 0;
}

Problem-6

#include <stdio.h>

int main() {
int number;

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

if (number > 0) {
if ((number & (number - 1)) == 0) {
printf("The number is a positive power of 2.\n");
} else {
printf("The number is positive but not a power of 2.\n");
}
} else if (number == 0) {
printf("Zero is not a valid input.\n");
} else {
printf("Negative input is not valid.\n");
}

return 0;
}

Problem-7

#include<stdio.h>
int main()
{
    int X,Y;
    printf("Enter Two Number:");
    scanf("%d%d",&X,&Y);
    if(X>Y)
    printf("%d is greater than %d",X,Y);
    else if(X<Y)
    printf("%d is less than %d",X,Y);
    else
    printf("%d is equal to %d",X,Y);
    return 0;
}

Problem-8

#include <stdio.h>

int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 
printf("YES");
else 
printf("NO");
return 0;
}

Problem-9

#include<stdio.h>
int main()
{
    char ch;
    printf("Enter Character:");
    scanf("%c",&ch);
    if(ch>='A'&& ch<='Z'||ch>='a'&& ch<='z')
        printf("Alpabet\n");
    else if (ch>='0'&& ch<='9')
        printf("Digit\n");
    else
        printf("Special");
    return 0 ;
}

Problem-10

#include <stdio.h>
int main() {
double num1, num2;
char operator;

printf("Enter an expression ( 7 + 3): ");
scanf("%lf %c %lf", &num1, &operator, &num2);

switch (operator) {
case '+':
printf("ADD: %lf\n", num1 + num2);
break;
case '-':
printf("SUB: %lf\n", num1 - num2);
break;
case '*':
printf("MULTI: %lf\n", num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("DIVI: %lf\n", num1 / num2);
} else {
printf("Zero as divisor is not valid!.\n");
}
break;
default:
printf("Invalid operator.\n");
}

return 0;
}

Problem-11

#include <stdio.h>
int main() {
double finalScore;

printf("Enter the final score: ");
scanf("%lf", &finalScore);

if (finalScore >= 90) {
printf("Grade: A\n");
}
else if (finalScore >= 86) {
printf("Grade: A-\n");
}
else if (finalScore >= 82) {
printf("Grade: B+\n");
}
else if (finalScore >= 78) {
printf("Grade: B\n");
}
else if (finalScore >= 74) {
printf("Grade: B-\n");
}
else if (finalScore >= 70) {
printf("Grade: C+\n");
}
else if (finalScore >= 66) {
printf("Grade: C\n");
}
else if (finalScore >=62) {
printf("Grade: C-\n");
}
else if (finalScore >= 58) {
printf("Grade: D+\n");
}
else if (finalScore >= 55) {
printf("Grade: D\n");
}
else {
printf("Grade: F\n");
}

return 0;
}

Problem-12

#include <stdio.h>
int main() {
double a, b, result;
int choice;

printf("Enter two real numbers a and b: ");
scanf("%lf %lf", &a, &b);

printf("Choose an operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division (quotient)\n");
scanf("%d", &choice);

switch (choice) {
case 1:
result = a + b;
break;
case 2:
result = a - b;
break;
case 3:
result = a * b;
break;
case 4:
if (b != 0) {
result = a / b;
} else {
printf("Division by zero is invalid.\n");
return 1;
}
break;
default:
printf("Invalid choice.\n");
return 1;
}

printf("Result: %lf\n", result);

return 0;
}

Problem-13

#include <stdio.h>

int main() {
double a, b, result;
int choice, caseChoice;

printf("Enter two real numbers a and b: ");
scanf("%lf %lf", &a, &b);

printf("Choose an operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division (quotient and remainder)\n");
scanf("%d", &choice);

switch (choice) {
case 1:
result = a + b;
break;
case 2:
result = a - b;
break;
case 3:
result = a * b;
break;
case 4:
printf("Choose a case:\n");
printf("1. Quotient\n");
printf("2. Remainder\n");
scanf("%d", &caseChoice);
if (b != 0) {
if (caseChoice == 1) {
result = a / b;
} 
else if (caseChoice == 2) {
result = fmod(a, b);
}
 else {
printf("Invalid case choice.\n");
return 1;
}
} 
else {
printf("Error: Division by zero.\n");
return 1;
}
break;
default:
printf("Invalid choice.\n");
return 1;
}

printf("Result: %lf\n", result);

return 0;
}

Problem-14

#include <stdio.h>

int main() {
double a, b, result;
int choice, caseChoice;

printf("Enter two real numbers a and b: ");
scanf("%lf %lf", &a, &b);

printf("Choose an operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
scanf("%d", &choice);

switch (choice) {
case 1:
result = a + b;
printf("Result: %lf\n", result);
break;
case 2:
result = a - b;
printf("Result: %lf\n", result);
break;
case 3:
result = a * b;
printf("Result: %lf\n", result);
break;
case 4:
if (b != 0) {
printf("Choose a case:\n");
printf("1. Quotient\n");
printf("2. Remainder\n");
scanf("%d", &caseChoice);
if (caseChoice == 1) {
result = a / b;
printf("Quotient: %lf\n", result);
} 
else if (caseChoice == 2) {
result = fmod(a, b);
printf("Remainder: %lf\n", result);
} 
else {
printf("Invalid case choice.\n");
}
} 
else {
printf("Error: Divisor is zero.\n");
}
break;
default:
printf("Invalid choice.\n");
}

return 0;
}

Problem-15

#include <stdio.h>

int main() {
int player1Number, player2Guess, chances = 3, flag = 0;

printf("Player-1, enter a number: ");
scanf("%d", &player1Number);

printf("Player-2, you have 3 chances to guess the number.\n");

while (chances > 0) {
printf("Enter your guess: ");
scanf("%d", &player2Guess);

if (player2Guess == player1Number) {
printf("Right, Player-2 wins!\n");
flag = 1;
break;
} else {
chances--;
if (chances > 0) {
printf("Wrong, %d Chance(s) Left!\n", chances);
}
}
}

if (flag == 0) {
printf("Player-1 wins!\n");
}

return 0;
}