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

CSharp_Programs

The document contains simple C# programs demonstrating four functionalities: calculating the factorial of a number, reversing a string, checking if a number is prime, and generating a Fibonacci series. Each program prompts the user for input and displays the result accordingly. The code is structured within a single Main method of a C# class.
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)
4 views

CSharp_Programs

The document contains simple C# programs demonstrating four functionalities: calculating the factorial of a number, reversing a string, checking if a number is prime, and generating a Fibonacci series. Each program prompts the user for input and displays the result accordingly. The code is structured within a single Main method of a C# class.
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/ 2

Simple C# Programs

using System;

class Program
{
static void Main()
{
// Program 1: Calculate Factorial
Console.Write("Enter a number to find its factorial: ");
int num = Convert.ToInt32(Console.ReadLine());
int factorial = 1;
for (int i = 1; i <= num; i++)
{
factorial *= i;
}
Console.WriteLine("Factorial of " + num + " is " + factorial + "\n");

// Program 2: Reverse a String


Console.Write("Enter a string to reverse: ");
string inputString = Console.ReadLine();
char[] charArray = inputString.ToCharArray();
Array.Reverse(charArray);
Console.WriteLine("Reversed string: " + new string(charArray) + "\n");

// Program 3: Find Prime Number


Console.Write("Enter a number to check if it's prime: ");
int primeNum = Convert.ToInt32(Console.ReadLine());
bool isPrime = true;
if (primeNum <= 1)
{
isPrime = false;
}
else
{
for (int i = 2; i < primeNum; i++)
{
if (primeNum % i == 0)
{
isPrime = false;
break;
}
}
}
Console.WriteLine(primeNum + " is " + (isPrime ? "a Prime Number" : "not a Prime Number") + "\n");

// Program 4: Fibonacci Series


Console.Write("Enter the number of terms for Fibonacci series: ");
int terms = Convert.ToInt32(Console.ReadLine());
int first = 0, second = 1, next;
Console.WriteLine("Fibonacci Series:");
for (int i = 0; i < terms; i++)
{
Console.Write(first + " ");
next = first + second;
first = second;
second = next;
}
Console.WriteLine();
}
}

You might also like