Home » C# » C# programs » C# basic programs

C# program to demonstrate example of simple if else statement

C# simple if else statement example: Here, we are writing a C# program to demonstrate example of simple if else statement.
Submitted by IncludeHelp, on April 07, 2019

Like other programming languages, simple if else statement in C# is used to execute one code block written in two different blocks based on the condition.

Syntax:

    if(test_condition){
	    //code section 1
    }
    else{
	    //code section 2
    }

If test_codition is true, statements written in "code section 1" will be executed, if test_condition is false, statements written in "code section 2" will be executed.

C# example for simple if else statement

In this program, there are two different codes – 1) Input age and check whether age is teenage or not, and 2) Input two numbers and check whether first number is divisible by second number or not

// C# program to demonstrate example of 
// simple if else statement
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            //example 1: Input age and check it's teenage or not
            int age = 0;
            Console.Write("Enter age: ");
            age = Convert.ToInt32(Console.ReadLine());

            //checking condition
            if (age >= 13 && age <= 19)
                Console.WriteLine("{0} is teenage", age);
            else
                Console.WriteLine("{0} is teenage", age);

            //example 2: Input two integer numbers and check 
            //whether first number is divisible by second or not?
            int a, b;
            Console.Write("Enter first number : ");
            a = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter second number: ");
            b = Convert.ToInt32(Console.ReadLine());

            //checking condition
            if (a % b == 0)
                Console.WriteLine("{0} is divisible by {1}", a, b);
            else
                Console.WriteLine("{0} is not divisible by {1}", a, b);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

Enter age: 17
17 is teenage
Enter first number : 120
Enter second number: 20
120 is divisible by 20





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.