0% found this document useful (0 votes)
31 views17 pages

C# Console Conditional Loop Control Applications

The document provides examples of C# console applications that demonstrate various programming concepts like obtaining user input, performing calculations, conditional logic, and loops. The examples include programs to print "Hello World", calculate the product of four numbers, add quotation marks to each word in a string, determine if a number is prime, reverse a number and sum its digits, check for vowels, calculate simple interest, find the average of three numbers, determine the greatest of three numbers, calculate the area of a rectangle, get the month name from a number, and check if a year is a leap year.

Uploaded by

Balakumar Bk
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)
31 views17 pages

C# Console Conditional Loop Control Applications

The document provides examples of C# console applications that demonstrate various programming concepts like obtaining user input, performing calculations, conditional logic, and loops. The examples include programs to print "Hello World", calculate the product of four numbers, add quotation marks to each word in a string, determine if a number is prime, reverse a number and sum its digits, check for vowels, calculate simple interest, find the average of three numbers, determine the greatest of three numbers, calculate the area of a rectangle, get the month name from a number, and check if a year is a leap year.

Uploaded by

Balakumar Bk
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/ 17

1. ASP.

NET SIMPLE C# CONSOLE APPLICATIONS

FILE-> New-> Project

1
Choose Visual C# -> Console Application

Name Change it HELLO WORLD, Location also changes it then Click OK button.

2
Inside the program, you enter THREE line codes like

string message = "Hello World!!";


Console.WriteLine(message);
Console.ReadLine();

Click Start Debugging F5 BUTTON then get the result

3
CLICK STOP DEBUGGING (SHIFT+F5)

2. Write a console application that obtains four int values from the user and displays the
product.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HELLO_WORLD
{
class Program
{
static void Main(string[] args)
{
int num1, num2, num3, num4, prod;
Console.Write("Enter number 1: ");
num1 = Int32.Parse(Console.ReadLine());
4
Console.Write("Enter number 2: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number 3: ");
num3 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number 4: ");
num4 = Convert.ToInt32(Console.ReadLine());
prod = num1 * num2 * num3 * num4;
Console.WriteLine(num1 + "*" + num2 + "*" + num3 + "*" + num4 + "=" + prod);
Console.ReadLine();
}
}
}

OUTPUT:

***************************************************************************

3. Write a console application that places double quotation marks around each word in a string.

using System;
5
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HELLO_WORLD
{
class Program
{
static void Main(string[] args)
{
string str1;
Console.Write("Enter string 1: ");
str1 = Console.ReadLine();
string[] words = str1.Split(' ');
for (int i = 0; i < words.Length; i++)
{
Console.Write("\" " + words[i] + "\" ");
Console.ReadLine();
}
}
}
}

OUTPUT:

**************************************************************************************
4. Write programs using conditional statements and loops: I) Test for prime numbers.

6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HELLO_WORLD
{
class Program
{
static void Main(string[] args)
{
int num, counter;
Console.Write("Enter number:");
num = int.Parse(Console.ReadLine());
for (counter = 2; counter <= num / 2; counter++)
{
if ((num % counter) == 0)
break;
}
if (num == 1)
Console.WriteLine(num + "is neither prime nor composite");
else if (counter < (num / 2))
Console.WriteLine(num + "is not prime number");
else
Console.WriteLine(num + "is prime number");
Console.ReadLine();
}
}
}

OUTPUT:

7
**************************************************************************************
5. Write programs using conditional statements and loops:

2) Reverse a number and find the sum of a number’s Digits.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HELLO_WORLD
{
class Program
{
static void Main(string[] args)
{
int num,actualnumber,revnum=0,digit,sumDigits=0;
Console.Write("Enter number:");
num = int.Parse(Console.ReadLine());
8
actualnumber = num;
while (num > 0)
{
digit = num % 10;
revnum = revnum * 10 + digit;
sumDigits=sumDigits+digit;
num = num / 10;
}
Console.WriteLine("Reverse of " + actualnumber + "=" + revnum);
Console.WriteLine("Sum of its digits:" + sumDigits);
Console.ReadLine();
}
}
}

OUTPUT:

**************************************************************************************
6. Write programs using conditional statements and loops: 3) Test for vowels

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

9
namespace HELLO_WORLD
{
class Program
{
static void Main(string[] args)
{
char ch;
Console.Write("Enter a character : ");
ch = (char)Console.Read();
switch (ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
Console.WriteLine(ch + "is vowel");
break;
default:
Console.Write(ch + "is not a vowel");
break;
}
Console.ReadKey();
}
}
}

OUTPUT:

**************************************************************************************
7. Program To Calculate the Simple Interest in C#

int P, T;
float R, SI;
10
Console.Write("Enter Amount :");
P = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Rate :");
R = Convert.ToSingle(Console.ReadLine());
Console.Write("Enter Time :");
T = Convert.ToInt32(Console.ReadLine());
SI = P * R * T / 100;
Console.WriteLine("Interest is :{0}", SI);
Console.ReadKey();
Console.ReadLine();

OUTPUT:

**************************************************************************************
9.Program to find the average of 3 numbers in C#

int number1, number2, number3,avarage;

Console.Write("Enter 1st number :");


number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter 2nd number :");
number2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter 3rd number :");
number3 = Convert.ToInt32(Console.ReadLine());

avarage = (number1 + number2 + number3) / 3;

Console.Write("Avarage of three numbers is {0}",avarage);

Console.ReadKey();

OUTPUT:

11
**************************************************************************************
10. Finding the biggest of three numbers in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HELLO_WORLD
{

class Program
{
static void Main(string[] args)
{

int number1, number2, number3;


string result;

Console.Write("Input the first number :");


number1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the second number :");
number2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the third number :");
number3 = Convert.ToInt32(Console.ReadLine());

if (number1 > number2 && number1 > number3)


{
result= "The 1st Number is the greatest among three. \n";
}
else if (number2 > number1 && number2 > number3)
{
result = "The 2nd Number is the greatest among three \n";
}
else
{
result= "The 3rd Number is the greatest among three \n";
}

12
Console.WriteLine(result);

Console.ReadLine();

}
}

OUTPUT:

**************************************************************************************

11. C# Calculate Area of Rectangle

class Program

static void Main(string[] args)

int area, length, width;

Console.Write("Please write the length of your rectangle: ");

length = Convert.ToInt32(Console.ReadLine());

Console.Write("Please write the width of your rectangle: ");

width = Convert.ToInt32(Console.ReadLine());

area = length * width;

Console.WriteLine("The area of rectangle : {0}", area);

Console.ReadKey();

Output:

13
**************************************************************************************
12. Get Month Name From Month Number – C#(Switch Case)

class Program

static void Main(string[] args)

int monthNumber;

Console.Write("Enter Month Number (1 - 12): ");

monthNumber = Convert.ToInt32(Console.ReadLine());

switch (monthNumber)

case 1:

Console.WriteLine("January");

break;

case 2:

Console.WriteLine("February");

break;

case 3:

Console.WriteLine("March");

break;

case 4:

Console.WriteLine("April");

14
break;

case 5:

Console.WriteLine("May");

break;

case 6:

Console.WriteLine("June");

break;

case 7:

Console.WriteLine("July");

break;

case 8:

Console.WriteLine("August");

break;

case 9:

Console.WriteLine("September");

break;

case 10:

Console.WriteLine("October");

break;

case 11:

Console.WriteLine("November");

break;

case 12:

Console.WriteLine("December");

break;

default:

Console.WriteLine("you did not enter correct value for month name");

15
break;

Console.ReadLine();

Output:

**************************************************************************************
13. Program to Find Leap Year in C#

static void Main(string[] args)

int year;

Console.Write("Enter the Year :");

year = Convert.ToInt32(Console.ReadLine());

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

Console.WriteLine("{0} is Leap Year",year);

else

Console.WriteLine("{0} is not a Leap Year",year);

Console.ReadLine();

Output:

16
**************************************************************************************
14. C# Program to Find the Factorial of a Number

Source Code:

static void Main(string[] args)

int i, number, fact;

Console.WriteLine("Enter the Number");

number = int.Parse(Console.ReadLine());

fact = number;

for (i = number - 1; i >= 1; i--)

fact = fact * i;

Console.WriteLine("\nFactorial of Given Number is: "+fact);

Console.ReadLine();

Output:

Enter the Number : 5

Factorial of Given Number is: 120

17

You might also like