0% found this document useful (0 votes)
9 views

1 Conditional

Conditional statements
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)
9 views

1 Conditional

Conditional statements
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/ 4

1.

Check if a Number is Positive, Negative, or Zero

using System;

class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());

if (number > 0)
Console.WriteLine("Positive");
else if (number < 0)
Console.WriteLine("Negative");
else
Console.WriteLine("Zero");
}
}

2. Check if a Number is Even or Odd

using System;

class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());

if (number % 2 == 0)
Console.WriteLine("Even");
else
Console.WriteLine("Odd");
}
}

3. Find the Largest of Two Numbers

using System;

class Program
{
static void Main()
{
Console.Write("Enter first number: ");
int a = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter second number: ");


int b = Convert.ToInt32(Console.ReadLine());

if (a > b)
Console.WriteLine($"{a} is larger");
else
Console.WriteLine($"{b} is larger");
}
}

4. Check if a Character is a Vowel or Consonant

using System;

class Program
{
static void Main()
{
Console.Write("Enter a character: ");
char ch = Convert.ToChar(Console.ReadLine().ToLower());

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')


Console.WriteLine("Vowel");
else
Console.WriteLine("Consonant");
}
}

5. Check if a Person is Eligible to Vote

using System;

class Program
{
static void Main()
{
Console.Write("Enter your age: ");
int age = Convert.ToInt32(Console.ReadLine());

if (age >= 18)


Console.WriteLine("Eligible to vote");
else
Console.WriteLine("Not eligible to vote");
}
}

6. Check if a Number is Divisible by 5 and 11

using System;

class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());

if (number % 5 == 0 && number % 11 == 0)


Console.WriteLine("Divisible by both 5 and 11");
else
Console.WriteLine("Not divisible by both 5 and 11");
}
}

7. Find the Smallest of Three Numbers

using System;

class Program
{
static void Main()
{
Console.Write("Enter first number: ");
int a = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter second number: ");


int b = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter third number: ");


int c = Convert.ToInt32(Console.ReadLine());

if (a < b && a < c)


Console.WriteLine($"{a} is the smallest");
else if (b < c)
Console.WriteLine($"{b} is the smallest");
else
Console.WriteLine($"{c} is the smallest");
}
}

8. Grade Calculation Based on Marks

using System;

class Program
{
static void Main()
{
Console.Write("Enter your marks: ");
int marks = Convert.ToInt32(Console.ReadLine());

if (marks >= 90)


Console.WriteLine("Grade A");
else if (marks >= 80)
Console.WriteLine("Grade B");
else if (marks >= 70)
Console.WriteLine("Grade C");
else if (marks >= 60)
Console.WriteLine("Grade D");
else
Console.WriteLine("Grade F");
}
}

9. Check if a Year is a Leap Year

using System;
class Program
{
static void Main()
{
Console.Write("Enter a year: ");
int year = Convert.ToInt32(Console.ReadLine());

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


Console.WriteLine("Leap year");
else
Console.WriteLine("Not a leap year");
}
}

10. Basic Calculator Using switch

using System;

class Program
{
static void Main()
{
Console.Write("Enter first number: ");
double num1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter an operator (+, -, *, /): ");


char op = Convert.ToChar(Console.ReadLine());

Console.Write("Enter second number: ");


double num2 = Convert.ToDouble(Console.ReadLine());

switch (op)
{
case '+':
Console.WriteLine("Result: " + (num1 + num2));
break;
case '-':
Console.WriteLine("Result: " + (num1 - num2));
break;
case '*':
Console.WriteLine("Result: " + (num1 * num2));
break;
case '/':
if (num2 != 0)
Console.WriteLine("Result: " + (num1 / num2));
else
Console.WriteLine("Cannot divide by zero");
break;
default:
Console.WriteLine("Invalid operator");
break;
}
}
}

You might also like