0% found this document useful (0 votes)
10 views12 pages

C# Prac File

The document contains a series of C# programming exercises that cover various basic programming concepts. Each exercise includes a description, the corresponding C# code, and sample output for user interaction. Topics range from printing messages, performing arithmetic operations, string manipulations, to implementing algorithms like factorial and Fibonacci series.

Uploaded by

gamerharshhh
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)
10 views12 pages

C# Prac File

The document contains a series of C# programming exercises that cover various basic programming concepts. Each exercise includes a description, the corresponding C# code, and sample output for user interaction. Topics range from printing messages, performing arithmetic operations, string manipulations, to implementing algorithms like factorial and Fibonacci series.

Uploaded by

gamerharshhh
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/ 12

1. Write a program to print hello in c#.

using System;

class Program
{
static void Main()
{
Console.WriteLine("Hello");
}
}
Output : Hello
2. Write a program to input number from user c#

using System;

class Program

static void Main()

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

string input = Console.ReadLine();

int number = Convert.ToInt32(input);

Console.WriteLine("You entered: " + number);

Output : Enter a number: 25

You entered: 25

3. Write a program to input 2 numbers from user and output of adding, subtracting, multiplying of
numbers in c#

using System

class Program

static void Main()

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

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


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

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

double addition = num1 + num2;

double subtraction = num1 - num2;

double multiplication = num1 * num2;

double division = (num2 != 0) ? num1 / num2 : double.NaN; // Prevent division by zero

Console.WriteLine($"Addition: {num1} + {num2} = {addition}");

Console.WriteLine($"Subtraction: {num1} - {num2} = {subtraction}");

Console.WriteLine($"Multiplication: {num1} * {num2} = {multiplication}");

Output : Enter the first number: 10

Enter the second number: 5

Addition: 10 + 5 = 15

Subtraction: 10 - 5 = 5

Multiplication: 10 * 5 = 50

4. Write a program to swap two numbers without using third variable in c#


using System;
class Program
{
static void Main()
{
Console.Write("Enter the first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"Before swap: num1 = {num1}, num2 = {num2}");
num1 = num1 + num2; // Step 1: num1 becomes the sum of num1 and num2
num2 = num1 - num2; // Step 2: num2 becomes the original num1
num1 = num1 - num2; // Step 3: num1 becomes the original num2

Console.WriteLine($"After swap: num1 = {num1}, num2 = {num2}");


}
}
Output : Enter the first number: 20
Enter the second number: 10
Before swap: num1 = 20, num2 = 10
After swap: num1 = 10, num2 = 20
5. Write a program to input 4 numbers from user and calculate the average in c#
using System;
class Program
{
static void Main()
{
Console.Write("Enter the first number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the second number: ");
double num2 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the third number: ");
double num3 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the fourth number: ");
double num4 = Convert.ToDouble(Console.ReadLine());
double average = (num1 + num2 + num3 + num4) / 4;
Console.WriteLine($"The average of {num1}, {num2}, {num3}, and {num4} is {average}");
}
}
Output : Enter the first number: 10
Enter the second number: 20
Enter the third number: 30
Enter the fourth number: 40
The average of 10, 20, 30, and 40 is 25
6. Write a program to input 3 numbers(x,y,z) from user and print the output of (x+y).z and x.y+y.z
in c#
using System;
class Program
{
static void Main()
{
Console.Write("Enter the value of x: ");
double x = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the value of y: ");
double y = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the value of z: ");
double z = Convert.ToDouble(Console.ReadLine());
double result1 = (x + y) * z;
double result2 = (x * y) + (y * z);
Console.WriteLine($"The result of (x + y) * z is: {result1}");
Console.WriteLine($"The result of x * y + y * z is: {result2}");
}
}

Output : Enter the value of x: 10


Enter the value of y: 20
Enter the value of z: 30
The result of (x + y) * z is: 900
The result of x * y + y * z is: 800
7. Write a program to create a new string from a given string whether first and last character
change their position in c#.
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
if (input.Length > 1)
{
string result = input[input.Length - 1] + input.Substring(1, input.Length - 2) + input[0];
Console.WriteLine("New string after swapping first and last characters: " + result);
}
else
{
Console.WriteLine("The string has only one character: " + input);
}
}
}
Output : Enter a string: hello
New string after swapping first and last characters: oellh
8. Write a converting string into lower case in c#.
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
string lowerCaseString = input.ToLower();
Console.WriteLine("The string in lowercase: " + lowerCaseString);
}
}
Output : Enter a string: HELLO
The string in lowercase: hello
9. Write a program to print reverse string using function in c#.
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
string reversedString = ReverseString(input);
Console.WriteLine("Reversed string: " + reversedString);
}
static string ReverseString(string str)
{
char[] charArray = str.ToCharArray(); // Convert string to character array
Array.Reverse(charArray); // Reverse the character array
return new string(charArray); // Convert the character array back to string
}
}
Output : Enter a string: Hello HNIT
Reversed string: TINH olleH
10. Write a program to convert hexadecimal number to decimal number in c#
using System;
class Program
{
static void Main()
{
Console.Write("Enter a hexadecimal number: ");
string hexNumber = Console.ReadLine();
try
{
int decimalNumber = Convert.ToInt32(hexNumber, 16);
Console.WriteLine($"The decimal equivalent of hexadecimal {hexNumber} is:
{decimalNumber}");
}
catch (FormatException)
{
Console.WriteLine("Invalid hexadecimal number.");
}
}
}
Output : Enter a hexadecimal number: 4B0
The decimal equivalent of hexadecimal 4B0 is: 1200
11. Write a program to multiply corresponding elements of two array of integers in c#.
using System;
class Program
{
static void Main()
{
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 6, 7, 8, 9, 10 };
if (array1.Length != array2.Length)
{
Console.WriteLine("Arrays must have the same length.");
}
else
{
int[] result = new int[array1.Length];
for (int i = 0; i < array1.Length; i++)
{
result[i] = array1[i] * array2[i];
}
Console.WriteLine("Result of multiplying corresponding elements:");
foreach (var res in result)
{
Console.Write(res + " ");
}
}
}
}
Output : Result of multiplying corresponding elements:
6 14 24 36 50
12. Write a program to print largest and lowest value from three integers in c#.
using System;
class Program
{
static void Main()
{
Console.Write("Enter the first integer: ");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second integer: ");
int num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the third integer: ");
int num3 = Convert.ToInt32(Console.ReadLine());
int largest = Math.Max(num1, Math.Max(num2, num3));
int lowest = Math.Min(num1, Math.Min(num2, num3));
Console.WriteLine($"The largest value is: {largest}");
Console.WriteLine($"The lowest value is: {lowest}");
}
}
Output : Enter the first integer: 10
Enter the second integer: 19
Enter the third integer: 15
The largest value is: 19
The lowest value is: 10
13. Write a program to display the cube of number upto given integers in c#
using System;
class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"Cubes of numbers from 1 to {n}:");
for (int i = 1; i <= n; i++)
{
int cube = i * i * i; // Calculate the cube of the number
Console.WriteLine($"The cube of {i} is {cube}");
}
}
}
Output : Enter a number: 5
Cubes of numbers from 1 to 5:
The cube of 1 is 1
The cube of 2 is 8
The cube of 3 is 27
The cube of 4 is 64
The cube of 5 is 125
14. Write a program to make pattern like a pyramid with numbers increased by 1 in c#
1
2 3
4 5 6
7 8 9 10
using System;
class Program
{
static void Main()
{
Console.Write("Enter the number of rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
int number = 1; // Start the number from 1
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (int j = 1; j <= i; j++)
{
Console.Write(number + " ");
number++; // Increment the number by 1
}
Console.WriteLine();
}
}
}
Output : Enter the number of rows: 4
1
2 3
4 5 6
7 8 9 10
15. Write a program to make pattern like a diamond in c#.
using System;
class Program
{
static void Main()
{
Console.Write("Enter the number of rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (int j = 1; j <= (2 * i - 1); j++)
{
Console.Write("*");
}
Console.WriteLine();
}
for (int i = rows - 1; i >= 1; i--)
{
for (int j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (int j = 1; j <= (2 * i - 1); j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
Output : Enter the number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
16. Write a program to display the n terms of harmonic series and their sum in c#
using System;
class Program
{
static void Main()
{
Console.Write("Enter the number of terms (n): ");
int n = Convert.ToInt32(Console.ReadLine());
double sum = 0; // Variable to store the sum of the harmonic series
Console.WriteLine($"The first {n} terms of the harmonic series are:");
for (int i = 1; i <= n; i++)
{
double term = 1.0 / i; // Calculate the i-th term of the harmonic series
Console.WriteLine($"1/{i} = {term}");
sum += term; // Add the term to the sum
}
Console.WriteLine($"The sum of the first {n} terms is: {sum}");
}
}

Output : Enter the number of terms (n): 5


The first 5 terms of the harmonic series are:
1/1 = 1
1/2 = 0.5
1/3 = 0.333333333333333
1/4 = 0.25
1/5 = 0.2
The sum of the first 5 terms is: 2.28333333333333
17. Write a program to check whether given number is perfect or not in c#.
using System;
class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
if (IsPerfectNumber(number))
{
Console.WriteLine($"{number} is a perfect number.");
}
else
{
Console.WriteLine($"{number} is not a perfect number.");
}
}
static bool IsPerfectNumber(int num)
{
if (num <= 0)
return false;

int sum = 0;
for (int i = 1; i <= num / 2; i++)
{
if (num % i == 0)
{
sum += i;
}
}
return sum == num;
}
}
Output : Enter a number: 496
496 is a perfect number.
18. Write a program to find factorial of given number using function.
using System;
class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
if (number < 0)
{
Console.WriteLine("Factorial is not defined for negative numbers.");
}
else
{
long factorial = CalculateFactorial(number);
Console.WriteLine($"The factorial of {number} is: {factorial}");
}
}
static long CalculateFactorial(int num)
{
long result = 1;
for (int i = 1; i <= num; i++)
{
result *= i; // result = result * i
}

return result;
}
}
Output : Enter a number: 5
The factorial of 5 is: 120
19. Write a program to print Fibonacci series upto n terms in c#
using System;
class Program
{
static void Main()
{
Console.Write("Enter the number of terms: ");
int n = Convert.ToInt32(Console.ReadLine());
if (n <= 0)
{
Console.WriteLine("Please enter a positive integer greater than 0.");
}
else
{
Console.WriteLine($"Fibonacci series up to {n} terms:");
int a = 0, b = 1;
if (n == 1)
{
Console.WriteLine(a);
}
else
{
Console.Write(a + " " + b + " ");
for (int i = 3; i <= n; i++)
{
int nextTerm = a + b; // The next term is the sum of the previous two
Console.Write(nextTerm + " ");
a = b;
b = nextTerm;
}
}
Console.WriteLine();
}
}
}
Output : Enter the number of terms: 7
Fibonacci series up to 7 terms:
0112358
20. Write a program to find given number is Armstrong or not in c#
using System;
class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int num = Convert.ToInt32(Console.ReadLine());
if (IsArmstrong(num))
{
Console.WriteLine($"{num} is an Armstrong number.");
}
else
{
Console.WriteLine($"{num} is not an Armstrong number.");
}
}
static bool IsArmstrong(int num)
{
int originalNumber = num;
int sum = 0;
int numDigits = (int)Math.Floor(Math.Log10(num) + 1);
while (num > 0)
{
int digit = num % 10; // Get the last digit
sum += (int)Math.Pow(digit, numDigits); // Add the digit raised to the power
num /= 10; // Remove the last digit
}
return sum == originalNumber;
}
}
Output : Enter a number: 153
153 is an Armstrong number.

You might also like