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

C# Programs

The document contains 10 code examples of simple C# programs that demonstrate basic programming concepts like printing output, performing mathematical operations, taking user input, and calculating averages. The programs cover topics such as printing text, calculating sums, differences, products, quotients and remainders of numbers, generating multiplication tables, finding averages of input values, and evaluating mathematical expressions.

Uploaded by

raaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views

C# Programs

The document contains 10 code examples of simple C# programs that demonstrate basic programming concepts like printing output, performing mathematical operations, taking user input, and calculating averages. The programs cover topics such as printing text, calculating sums, differences, products, quotients and remainders of numbers, generating multiplication tables, finding averages of input values, and evaluating mathematical expressions.

Uploaded by

raaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SIMPLE C# PROGRAMS

Write a C# Sharp program to print Hello and your name in a separate line.
public class Exercise1

public static void Main( )

System.Console.WriteLine("Hello");

System.Console.WriteLine("Alexandra Abramov!");

Hello
Alexandra Abramov!
Write a C# Sharp program to print the sum of two numbers.
public class Exercise2
{
public static void Main()
{
System.Console.WriteLine(15+17);
}
}

Sample Output:
32

Write a C# Sharp program to print the result of dividing two numbers.


public class Exercise3
{
public static void Main()
{
System.Console.WriteLine(36/6);
}
}

Sample Output:
6
Write a C# Sharp program to print the output of multiplication of three numbers
which will be entered by the use
using System;
public class Exercise6
{
public static void Main()
{
int num1, num2, num3;

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


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

Console.Write("Input the second number to multiply: ");


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

Console.Write("Input the third number to multiply: ");


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

int result = num1 * num2 * num3;


Console.WriteLine("Output: {0} x {1} x {2} = {3}",
num1, num2, num3, result);
}
}

Sample Output:
Input the first number to multiply: 2
Input the second number to multiply: 8
Input the third number to multiply: 5
Output: 2 x 8 x 5 = 80
using System;

public class Exercise7

public static void Main()

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

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

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


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

Console.WriteLine("{0} + {1} = {2}", num1, num2, num1+num2);

Console.WriteLine("{0} - {1} = {2}", num1, num2, num1-num2);

Console.WriteLine("{0} x {1} = {2}", num1, num2, num1*num2);

Console.WriteLine("{0} / {1} = {2}", num1, num2, num1/num2);

Console.WriteLine("{0} mod {1} = {2}", num1, num2, num1%num2);

Sample Output:
Enter a number: 10
Enter another number: 2
10 + 2 = 12
10 - 2 = 8
10 x 2 = 20
10 / 2 = 5
10 mod 2 = 0
using System;

public class Exercise8

public static void Main()

int x;

int result;

Console.WriteLine("Enter a number:");

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

result = x * 1;
Console.WriteLine("The table is : {0} x {1} = {2}", x, 1, result);

result = x * 2;

Console.WriteLine(" : {0} x {1} = {2}", x, 2, result);

result = x * 3;

Console.WriteLine(" : {0} x {1} = {2}", x, 3, result);

result = x * 4;

Console.WriteLine(" : {0} x {1} = {2}", x, 4, result);

result = x * 5;

Console.WriteLine(" : {0} x {1} = {2}", x, 5, result);

result = x * 6;

Console.WriteLine(" : {0} x {1} = {2}", x, 6, result);

result = x * 7;

Console.WriteLine(" : {0} x {1} = {2}", x, 7, result);

result = x * 8;

Console.WriteLine(" : {0} x {1} = {2}", x, 8, result);

result = x * 9;

Console.WriteLine(" : {0} x {1} = {2}", x, 9, result);

result = x * 10;

Console.WriteLine(" : {0} x {1} = {2}", x, 10, result);

Sample Output:
Enter a number:
5
The table is : 5 x 1 = 5
: 5 x 2 = 10
: 5 x 3 = 15
: 5 x 4 = 20
: 5 x 5 = 25
: 5 x 6 = 30
: 5 x 7 = 35
: 5 x 8 = 40
: 5 x 9 = 45
: 5 x 10 = 50

using System;

using System.IO;

public class Exercise9

public static void Main()

double number1,number2,number3,number4;

Console.Write("Enter the First number: ");

number1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the Second number: ");

number2 = Convert.ToDouble(Console.ReadLine());

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

number3 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the fourth number: ");

number4 = Convert.ToDouble(Console.ReadLine());

double result = (number1 + number2 + number3 + number4) / 4;

Console.WriteLine("The average of {0}, {1}, {2}, {3} is: {4} ",

number1, number2, number3, number4, result);

}
}

Sample Output:
Enter the First number: 17
Enter the Second number: 17
Enter the third number: 517
Enter the four number: 51
The average of 17, 17, 517, 51 is: 150.5

using System;

public class Exercise10

public static void Main()

int number1, number2, number3;

Console.Write("Enter first number - ");

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

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

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

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

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

Console.Write("Result of specified numbers {0}, {1} and {2}, (x+y)·z is {3} and
x·y + y·z is {4}\n\n",

number1, number2, number3, ((number1+number2)*number3),


(number1*number2+number2*number3));

}
}

Sample Output:
Enter first number - 4
Enter second number - 6
Enter third number - 2
Result of specified numbers 4, 6 and 2, (x+y)·z is 20 and x·y +
y·z is 36

You might also like