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

C#1

The document contains a series of C# programs that demonstrate various programming concepts such as displaying messages, performing arithmetic operations, checking leap years, calculating grades, finding factorials, and more. Each program is structured with a main method and includes user input and output operations. The examples cover a wide range of topics including string operations, array manipulations, and special number checks like Disarium, Smith, Neon, and Emirp numbers.

Uploaded by

murshad.045100
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C#1

The document contains a series of C# programs that demonstrate various programming concepts such as displaying messages, performing arithmetic operations, checking leap years, calculating grades, finding factorials, and more. Each program is structured with a main method and includes user input and output operations. The examples cover a wide range of topics including string operations, array manipulations, and special number checks like Disarium, Smith, Neon, and Emirp numbers.

Uploaded by

murshad.045100
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

// 1. WCP to display a message "HELLO INDIA" on console.

using System;

class Program {

static void Main() {

Console.WriteLine("HELLO INDIA");

Console.ReadLine();

// 2. WCP to perform arithmetic operation.

using System;

class ArithmeticOperations {

static void Main() {

int a = 10, b = 5;

Console.WriteLine("Addition:"+(a+b));

Console.WriteLine((a-b);

Console.WriteLine(a * b);

Console.WriteLine(a/b);

Console.ReadLine();

// 3. WCP to find whether a year is leap or not.

using System;

class LeapYearCheck {
static void Main() {

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

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

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

Console.WriteLine("Leap Year");

else

Console.WriteLine("Not a Leap Year");

Console.ReadLine();

// 4. WCP to find the grades of a student according to their scores.

using System;

class StudentGrades {

static void Main() {

Console.Write("Enter marks: ");

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

string grade = marks >= 90 ? "A" : marks >= 80 ? "B" : marks >= 70 ? "C" : marks >= 60 ? "D" : "F";

Console.WriteLine(grade);

Console.ReadLine();

// 5. WCP to find factorial using for, while, do…While loops.

using System;
class FactorialProgram {

static void Main() {

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

int num = Convert.ToInt32(Console.ReadLine()), fact = 1;

for (int i = 1; i <= num; i++) fact *= i;

Console.WriteLine(fact);

Console.ReadLine();

// 6. WCP to print tables from 1-10.

using System;

class Program
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("Table of " + i);

for (int j = 1 ; j <= 10; j++)

{
Console.WriteLine(i + " x " + j + " = " + i * j);
}
Console.WriteLine();
Console.ReadLine();
}
}
}

// 7. WCP to perform various string operations.

using System;

class Program

{
static void Main()

Console.WriteLine("String Operations");

Console.WriteLine("------------------");

// 1. Concatenation

string str1 = "Hello";

string str2 = "World";

string result = str1 + " " + str2;

Console.WriteLine("Concatenation: " + result);

// 2. Substring

string str = "Hello World";

string substr = str.Substring(6);

Console.WriteLine("Substring: " + substr);

// 3. IndexOf

string str3 = "Hello World";

int index = str3.IndexOf("World");

Console.WriteLine("IndexOf: " + index);

// 4. LastIndexOf

string str4 = "Hello World Hello";

int lastIndex = str4.LastIndexOf("Hello");

Console.WriteLine("LastIndexOf: " + lastIndex);


// 5. Replace

string str5 = "Hello World";

string replaced = str5.Replace("World", "Universe");

Console.WriteLine("Replace: " + replaced);

// 8. ToUpper

string str8 = "hello world";

string upper = str8.ToUpper();

Console.WriteLine("ToUpper: " + upper);

// 9. ToLower

string str9 = "HELLO WORLD";

string lower = str9.ToLower();

Console.WriteLine("ToLower: " + lower);

// 10. Contains

string str10 = "Hello World";

bool contains = str10.Contains("World");

Console.WriteLine("Contains: " + contains);

Console.ReadLine();

using System;
class Program
{
static void Main()
{
Console.WriteLine("Choose an option:");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");

Console.Write("Enter your choice (1-4): ");


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

switch (choice)
{
case 1:
Console.Write("Enter first number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
double num2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Result: " + (num1 + num2));
break;

case 2:
Console.Write("Enter first number: ");
double num3 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
double num4 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Result: " + (num3 - num4)); // Corrected subtraction
break;

case 3:
Console.Write("Enter first number: ");
double num5 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
double num6 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Result: " + (num5 * num6));
break;

case 4:
Console.Write("Enter first number: ");
double num7 = Convert.ToDouble(Console.ReadLine()); // Fixed variable naming
Console.Write("Enter second number: ");
double num8 = Convert.ToDouble(Console.ReadLine()); // Fixed variable naming
if (num8 != 0)
{
Console.WriteLine("Result: " + (num7 / num8));
}
else
{
Console.WriteLine("Error: Division by zero is not allowed.");
}
break;

default:
Console.WriteLine("Invalid choice. Please choose a valid option.");
break;
}

Console.ReadLine(); // Keep the console open until the user presses Enter
}
}

// 9. WCP to declare & initialize various types of variables.

using System;

class VariableDeclaration {

static void Main() {

int a = 10;

float b = 10.5f;

double c = 20.55;

char d = 'A';

string e = "Hello";

Console.WriteLine($"Integer: {a}, Float: {b}, Double: {c}, Char: {d}, String: {e}");

// 10. WCP to find the largest number among three numbers.

using System;

class LargestNumber {

static void Main() {

Console.Write("Enter three numbers: ");

int a = Convert.ToInt32(Console.ReadLine()), b = Convert.ToInt32(Console.ReadLine()), c =


Convert.ToInt32(Console.ReadLine());

int largest = (a > b && a > c) ? a : (b > c) ? b : c;

Console.WriteLine($"Largest: {largest}");
}

11.WCP to find maximum element in array a of size n.

using System;

class Program
{
static void Main()
{
Console.Write("Enter the size of the array: ");
int n = Convert.ToInt32(Console.ReadLine());

int[] a = new int[n];

Console.WriteLine("Enter the elements of the array: ");


for (int i = 0; i < n; i++)
{
Console.Write("Element " + (i + 1) + ": ");
a[i] = Convert.ToInt32(Console.ReadLine());
}

int max = a[0];


for (int i = 1; i < n; i++)
{
if (a[i] > max)
{
max = a[i];
}
}

Console.WriteLine("The maximum element in the array is: " + max);


Console.ReadLine();
}
}

// 12. WCP to find minimum element in array a of size n.

using System;

class MinElementInArray {

static void Main() {

Console.Write("Enter the size of array: ");

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

int[] arr = new int[n];

Console.WriteLine("Enter the elements of array: ");


for (int i = 0; i < n; i++)

arr[i] = Convert.ToInt32(Console.ReadLine());

int min = arr[0];

for (int i = 1; i < n; i++)

if (arr[i] < min)

min = arr[i];

Console.WriteLine($"Minimum element: {min}");

// 13. WCP to print the Fibonacci Series using Recursion for N Numbers.

using System;
class FibonacciSeries
{
static int Fibonacci(int n)
{
return (n <= 1) ? n : Fibonacci(n - 1) + Fibonacci(n - 2);
}
// Base Case: If n is 0 or 1, the function returns n directly.
//Recursive Case: The function calls itself with n-1 and n-2 and adds the
results.

static void Main()


{
Console.Write("Enter number of terms: ");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
Console.Write(Fibonacci(i) + " ");
Console.ReadLine();
}
}

// 14. WCP to find the smallest required number whose sum of digits equals N

using System;

class Program
{
static void Main()
{
Console.Write("Enter the number M (between 100 and 10000): ");
int M = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number N (less than 100): ");
int N = Convert.ToInt32(Console.ReadLine());

if (M < 100 || M > 10000)


{
Console.WriteLine("Invalid input: M should be between 100 and 10000.");
return;
}

if (N < 1 || N >= 100)


{
Console.WriteLine("Invalid input: N should be less than 100.");
return;
}

int number = M + 1;
while (true)
{
if (SumOfDigits(number) == N)
{
Console.WriteLine("The required number = " + number);
Console.WriteLine("Total number of digits = " + number.ToString().Length);
break;
}
number++;
}
}

static int SumOfDigits(int number)


{
int sum = 0;
while (number > 0)
{
sum += number % 10;
number /= 10;
}
return sum;
}
}

// 15. C# program to check if a number is a Disarium Number or not:

class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());

if (IsDisariumNumber(number))
{
Console.WriteLine(number + " is a Disarium Number.");
}
else
{
Console.WriteLine(number + " is not a Disarium Number.");
Console.ReadLine();
}
}

static bool IsDisariumNumber(int number)


{

string numStr = number.ToString();

int sum = 0;
for (int i = 0; i < numStr.Length; i++)
{

int digit = int.Parse(numStr[i].ToString());

// This line calculates the power of the digit raised to the i + 1 power using the Math.Pow method.
The result is cast to an integer using the (int) cast and added to the sum variable
`
sum += (int)Math.Pow(digit, i + 1);
}
return sum == number;
}

16. C# program to check if a number is an Automorphic Number or not:

using System;

class Program

static void Main()

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

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

int square = num * num;

// Convert both numbers to strings


string numStr = num.ToString();

string squareStr = square.ToString();

// Check if the square ends with the number

if (squareStr.EndsWith(numStr))

Console.WriteLine($"{num} is an Automorphic Number.");

else

Console.WriteLine($"{num} is NOT an Automorphic Number.");

Console.ReadLine(); // Keep console open

//17. Check if a Number is a Smith Number:Smith Number in C#

/*A Smith Number is a composite number whose sum of digits is equal to the sum of the digits
of its prime factors (excluding 1).
For example:

 666 → 6+6+6 = 18
o Prime factorization: 666 = 2 × 3 × 3 × 37
o Sum of digits of factors: 2+3+3+3+7 = 18 ✅ Smith Number
 85 → 8+5 = 13
o Prime factorization: 85 = 5 × 17
o Sum of digits: 5+1+7 = 13 ✅ Smith Number
 23 (Prime Number) ❌ Not a Smith Number */

using System;
class Program

static void Main()

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

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

if (IsPrime(num))

Console.WriteLine($"{num} is a Prime Number, so it's NOT a Smith Number.");

else

int digitSum = GetDigitSum(num);

int factorSum = GetPrimeFactorDigitSum(num);

if (digitSum == factorSum)

Console.WriteLine($"{num} is a Smith Number.");

else

Console.WriteLine($"{num} is NOT a Smith Number.");

}
Console.ReadLine(); // Keep console open

// Function to check if a number is prime

static bool IsPrime(int n)

if (n < 2) return false;

for (int i = 2; i * i <= n; i++)

if (n % i == 0) return false;

return true;

// Function to find the sum of digits of a number

static int GetDigitSum(int n)

int sum = 0;

while (n > 0)

sum += n % 10;

n /= 10;

}
return sum;

// Function to find the sum of digits of prime factors

static int GetPrimeFactorDigitSum(int n)

int sum = 0;

int original = n;

for (int i = 2; i <= n; i++)

while (n % i == 0)

sum += GetDigitSum(i);

n /= i;

return sum;

18. Check if a Number is a Neon Number


/*A neon number is a positive integer where the sum of the digits of its square is equal to the
number itself. This concept is simple yet intriguing, often used in mathematical puzzles and
programming challenges.

Example of Neon Number

Let's take an example to understand this better:


 Input: 9
 Output: Neon Number
 Explanation: The square of 9 is 81. The sum of the digits of 81 is 8 + 1 = 9, which is equal to the
original number.*/

using System;

class Program

static void Main()

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

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

int square = num * num; // Compute square of the number

int sumOfDigits = GetDigitSum(square); // Sum the digits of the square

if (sumOfDigits == num)

Console.WriteLine($"{num} is a Neon Number.");

else

Console.WriteLine($"{num} is NOT a Neon Number.");


Console.ReadLine(); // Keep console open

// Function to get the sum of digits of a number

static int GetDigitSum(int n)

int sum = 0;

while (n > 0)

sum += n % 10; // Get last digit and add to sum

n /= 10; // Remove last digit

return sum;

//19.C# Program to Check if a Number is an Emirp Number

/* An Emirp Number is a prime number that remains prime when its digits are reversed.
(Unlike palindromic primes, the reversed number must be different from the original.)

Examples:

✅ 13 → Reverse: 31 (Both are Prime) → Emirp Number


✅ 17 → Reverse: 71 (Both are Prime) → Emirp Number
❌ 23 → Reverse: 32 (32 is NOT Prime) → Not an Emirp
❌ 101 → Reverse: 101 (Same as original, Palindrome, Not Emirp Number*/

using System;
class Program
{
static void Main()
{
Console.Write("Enter a number: ");
int num = Convert.ToInt32(Console.ReadLine());

if (IsPrime(num))
{
int reversedNum = ReverseNumber(num);
if (reversedNum != num && IsPrime(reversedNum))
Console.WriteLine($"{num} is an Emirp Number.");
else
Console.WriteLine($"{num} is NOT an Emirp Number.");
}
else
{
Console.WriteLine($"{num} is NOT an Emirp Number.");
}

Console.ReadLine(); // Keep console open


}

// Function to check if a number is prime


static bool IsPrime(int n)
{
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0) return false;
}
return true;
}

// Function to reverse a number


static int ReverseNumber(int n)
{
int rev = 0;
while (n > 0)
{
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}
}

//Q20.

using System;
class Program

static void Main()

Console.Write("Enter an amount (up to 5 digits): ");

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

if (amount < 1 || amount > 99999)

Console.WriteLine("INVALID AMOUNT");

else

Console.Write("OUTPUT: ");

Console.WriteLine(ConvertToWords(amount));

Console.WriteLine("\nDENOMINATION:");

int[] denominations = { 1000, 500, 100, 50, 20, 10, 5, 2, 1 };

int totalNotes = 0;

foreach (int denom in denominations)

int count = amount / denom;


if (count > 0)

Console.WriteLine($"{denom} X {count} = {denom * count}");

totalNotes += count;

amount %= denom; // Reduce amount

Console.WriteLine($"Total Notes: {totalNotes}");

Console.ReadLine(); // Keep console open

// Function to convert digits to words

static string ConvertToWords(int num)

string[] words = { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE",

"SIX", "SEVEN", "EIGHT", "NINE" };

string numStr = num.ToString();

string result = "";

foreach (char digit in numStr)

{
result += words[digit - '0'] + " ";

return result.Trim();

You might also like