Saturday, January 21, 2023

Takes four numbers as input to calculate and print the average

 Write a C# Sharp program that takes four numbers as input to calculate and print the average.

C# Sharp: Average of numbers

An average is the sum of a list of numbers divided by the number of numbers in the list. In mathematics and statistics, this would be called the arithmetic mean. The term "arithmetic mean" is preferred in some contexts in mathematics and statistics because it helps distinguish it from other means, such as the geometric mean and the harmonic mean.

Method:

Average is found by taking input from the user into different variables. These variables are then added. Then theresult is divided by the number of numbers ie. Total values are counted and assigned to a different variable or used directly. The sum of numbers is then divided by the count of numbers.

Psuedocode:

Here is the psuedocode

define average(a,b,c,d)

    Average = a+b+c+d/4 

Sample solution:

C# Code:

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 first number: 17

Enter second number: 18

Enter third number: 20

Enter fourth number: 25

The average of 17,18,20,25 is : 20 

Flowchart:

Here is the flowchart to find the average of given numbers 





 

Takes a number as input and print its multiplication table.

 Write a C# Sharp program that takes a number as input and print its multiplication table.

 

In mathematics, a multiplication table is a mathematical table used to define a multiplication operation for an algebraic system.

 Method:

Here are two methods of writing code for the table of given number. One is for the beginners and very simple. The other is for experts and use complex method I.e. for loop.

Method 1:

In this method two variables are declared as int. One is to take input from user and other to put result after calculation. After declaration of variables, the input is taken from user and then that input is used to process. Result is printed on the screen line by line and so is calculated.

Method 2:

In this method, just like method 1, two variables are declared as int. One is to take input from user and other to put result after calculation. After declaration of variables, the input is taken from user and then that input is used to process. A for loop is used to calculate and print the output.

Psuedocode:

Here is the psuedocode for table

define table(x)

    For (I=0; I <= 10; I++){

Int result = x*I;

Print (" : {0} x {1} = {2}", x,  I, result);)

Sample Solution:

Method 1:

C# Sharp Code:

using System; public class Exercise1

{ 

public static void Main() 

{

int x, 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); 

} 

}

 Method 2:

using System; public class Exercise1

{ 

public static void Main() 

{

int x, result; 

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

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

For(int I=1; I<=10; I++;)

result = x * I;  

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

} 

}


Sample Output: 

Enter a number:

5

The table is: 5 * 1   = 5

5 * 2   = 10

5 * 3   = 15

5 * 4   = 20

5 * 5   = 25

5 * 6   = 30

5 * 7   = 35

5 * 8   = 40

5 * 9   = 45

5 * 10 = 50

 Flowchart:

Here is the flowchart of the table.





C# Sharp program to swap two numbers

 Write a C# Sharp program to swap two numbers.

Swapping:

Swapping in computer science means shuffling or interchanging. Mutually exchanging two values is called swapping.

Using a temporary variable :

The simplest method to swap two variables is to use a third temporary variable. The temporary variable is used to keep the value of one of the variables temporarily. The values in variables may be a numeric or alphabetic or any other characters I.e. alphanumeric, symbols etc. Temporary variable is a third variable along with two other variables carrying values. Temporary variable is empty but initialized at declaration and no value is assigned. The value is assigned at the time of shuffling of variables. For this , the value of one variable carrying values is assigned to the temporary variable thus that variable becomes empty. Now the value of the other value variable is assigned to the first recently emptied variable. At the end the value of the temporary variable is assigned to the second variable. Now the temporary variable is again empty and both the numbers are swapped. The same is shown in figure.

 

Psuedo code:

Here is the psuedo code for swapping two numbers.

define swap(x, y)

    temp := x

    x := y

y := temp

Code:

C# code for swapping the two numbers is here:

using System; 

public class Exercise1 { 

public static void Main(string[] args) 

{

int number1, number2, temp; 

Console.Write("\nInput the First Number : "); 

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

Console.Write("\nInput the Second Number : "); 

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

temp = number1; 

number1 = number2; 

number2 = temp; 

Console.Write("\nAfter Swapping : ");

Console.Write("\nFirst Number : "+number1);

Console.Write("\nSecond Number : "+number2); 

Console.Read(); 

} 

}

Sample output:

Input the first number: 5

Input the second number: 4

After swapping:

First number: 4

Second number: 5

Here is the flowchart of the above program:












 

Popular Posts