Uploaded by Mehboob Arshad

WEEK-02-PART02

advertisement
ENTERPRISE SYSTEMS
LAB-01 DATED: 06-06-2024
Note: If VS is not installed in lab, you can use online compiler or VS code.
Online compilers
1. https://www.programiz.com/csharp-programming/online-compiler/
2. https://www.w3schools.com/cs/cs_compiler.php
3. https://www.onlinegdb.com/online_csharp_compiler
Sample Programs in C#
Compile and run these programs to understand the working.
Problem1: Checking Leap Year
Write a C# program to check if a given year is a leap year or not.
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a year: ");
int year = Convert.ToInt32(Console.ReadLine());
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
Console.WriteLine($"{year} is a leap year.");
else
Console.WriteLine($"{year} is not a leap year.");
}
}
Problem2: Reverse a String
Write a C# program to reverse a given string.
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
string reversed = "";
for (int i = input.Length - 1; i >= 0; i--)
{
reversed += input[i];
}
Console.WriteLine("Reversed string: " + reversed);
}
}
3. Problem: Sum of Digits
Write a C# program to calculate the sum of digits of a given number.
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int sum = 0;
while (number > 0)
{
sum += number % 10;
number /= 10;
}
Console.WriteLine("Sum of digits: " + sum);
}
}
Problem4: Count Vowels in a String
Write a C# program to count the number of vowels in a given string.
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
int vowelCount = 0;
foreach (char c in input)
{
if ("aeiouAEIOU".Contains(c))
vowelCount++;
}
Console.WriteLine("Number of vowels: " + vowelCount);
}
}
Problem5: Factorial Calculation Write a C# program to calculate the factorial of a given
number.
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
int factorial = 1;
for (int i = 1; i <= number; i++)
{
factorial *= i;
}
Console.WriteLine("Factorial of " + number + " is " + factorial);
}
}
Tasks
Q1: Write a C# program to check if a given string is a palindrome or not. (A palindrome is a
string that reads the same backward as forward.)
Q2: Write a C# program to calculate the sum of all even or odd numbers from 1 to a given
number. Allow the user to choose whether to calculate the sum of even or odd numbers.
Q3: Write a C# program to display the Fibonacci series up to a given number 'n'. The Fibonacci
series is a series of numbers in which each number is the sum of the two preceding ones.
Q4: Write a C# program to calculate the sum of the factorial of all numbers from 1 to a given
number 'n'. The factorial of a number is the product of all positive integers less than or equal to
that number.
Q5: Write a C# program to count the frequency of each character in a given string and display
the result. Ignore case sensitivity. (For example, in the string "hello", the frequency of 'l' is 2.)
Download