C# Programs - c# - c# tutorial - c# net



C# String Programs

  1. C# Program to Reverse a string
  2. C# Program To Find The Count Of Sub-string "SHL" In A Given String
  3. C# Program to Find Palindrome or not
  4. C# Program to Reverse a number
  5. C# Program to Find duplicate characters in a string
  6. C# Program to Reverse each word of the sentence (string)
  7. C# Program to Remove characters in a string
  8. C# Program to Replace a character
  9. C# Program to Print pattern
  10. C# Program to Number into string

How to write programs in C# ?

  • C# programs are frequently asked in the interview.
  • These programs can be asked from basics, array, string, control statements, file handling etc. Let's see the list of top C# programs.
learn c# tutorials - class-object-contructor-member-function-in-csharp in c#

class-object-contructor-member-function-in-csharp in c# Example

C# program to enter number and print Fibonacci series upto that nth number:

using System;
class Program
{
static void Main(string[] args)
{
int n, first = 0, second = 1, third = 0;
Console.Write("Enter a number : ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("First {0} Fibonacci numbers {1} {2} ",n, first, second);
for (int i = 3; i <= n; i++)
{
third = first + second;
Console.Write("{0} ", third);
first = second;
second = third;
}
}
}

C# examples - Output :

Enter a number : 10
First 10 Fibonacci numbers : 0 1 1 2 3 5 8 13 21 34

Enter a number : 5
First 5 Fibonacci numbers : 0 1 1 2 3

C# Program to Check Whether the Given Number is a Prime number if so then Display its Largest Factor

/*
 * C# Program to Check Whether the Given Number is a Prime number if so then 
 * Display its Largest Factor
 */
using System;
namespace example
{
    class prime
    {
        public static void Main()
        {
            Console.Write("Enter a Number : ");
            int num;
            num = Convert.ToInt32(Console.ReadLine());
            int k;
            k = 0;
            for (int i = 1; i <= num; i++)
            {
                if (num % i == 0)
                {
                    k++;
                }
            }
            if (k == 2)
            {
                Console.WriteLine("Entered Number is a Prime Number and the Largest Factor is {0}",num);
            }
            else
            {
                Console.WriteLine("Not a Prime Number");
            }
            Console.ReadLine();
        }
    }
}

output:

Enter a Number : 23
Entered Number is a Prime Number and the Largest Factor is 23

Program to Check the Number Is Palindrome Or Not

class Program {  
    static void Main(string[] args) {  
        int num, rem, sum = 0, temp;  
        //clrscr();    
        Console.WriteLine("\n >>>> To Find a Number is Palindrome or not <<<< ");  
        Console.Write("\n Enter a number: ");  
        num = Convert.ToInt32(Console.ReadLine());  
        temp = num;  
        while (num > 0) {  
            rem = num % 10; //for getting remainder by dividing with 10    
            num = num / 10; //for getting quotient by dividing with 10    
            sum = sum * 10 + rem;  
            /*multiplying the sum with 10 and adding  
            remainder*/  
        }  
        Console.WriteLine("\n The Reversed Number is: {0} \n", sum);  
        if (temp == sum) //checking whether the reversed number is equal to entered number    
        {  
            Console.WriteLine("\n Number is Palindrome \n\n");  
        } else {  
            Console.WriteLine("\n Number is not a palindrome \n\n");  
        }  
        Console.ReadLine();  
    }  
}  

C# examples - Output :

To Find A Number is palindrome or not 
Enter a Number: 1221
The Reversed Number is: 1221

Number is Palindrome

C# Program to Generate the Factorial of Given Number

/*
 * C# Program to Generate the Factorial of  Given Number 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, number, fact;
            Console.WriteLine("Enter the Number");
            number = int.Parse(Console.ReadLine());
            fact = number;
            for (i = number - 1; i >= 1; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine("\nFactorial of Given Number is: "+fact);
            Console.ReadLine();
 
        }
    }
}

output:

Enter the Number
6
Factorial of Given Number is: 720

C# Program to Check Whether the Entered Number is an Armstrong Number or Not

/*
 * C# Program to Check Whether the Entered Number is an Armstrong Number or Not
 */ 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            int number, remainder, sum = 0;
            Console.Write("enter the Number");
            number = int.Parse(Console.ReadLine());
            for (int i = number; i > 0; i = i / 10)
            {
                remainder = i % 10;
                sum = sum + remainder*remainder*remainder;
 
            }
            if (sum == number)
            {
                Console.Write("Entered Number is an Armstrong Number");
            }
            else
                Console.Write("Entered Number is not an Armstrong Number");
            Console.ReadLine();
        }
     }
  }

C# examples - Output :

Enter the Number: 371
Entered Number is an Armstrong Number

C# Program to Find Sum of Digits of a Number using Recursion

/*
 * C# Program to Find Sum of Digits of a Number using Recursion
 */
using System;
class program
{
    public static void Main()
{
    int num, result;
    pro pg = new pro();
    Console.WriteLine("Enter the Number : ");
    num=int.Parse(Console.ReadLine());
    result =pg.sum(num);
    Console.WriteLine("Sum of Digits in {0} is {1}", num, result);
    Console.ReadLine();
}
}
class pro
{
    public int sum(int num)
    {
        if (num != 0)
        {
            return (num % 10 + sum(num / 10));
        }
        else
        {
            return 0;
        }
    }
}

output:

Enter the Number :
234
Sum of Digits in 234 is 9

C# Program to Get a Number and Display the Number with its Reverse

/*
 * C# Program to Get a Number and Display the Number with its Reverse
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, reverse = 0;
            Console.WriteLine("Enter a Number : ");
            num = int.Parse(Console.ReadLine());
            while (num != 0)
            {
                reverse = reverse * 10;
                reverse = reverse + num % 10;
                num = num / 10;
            }
            Console.WriteLine("Reverse of Entered Number is : "+reverse);
            Console.ReadLine();
 
        }
    }
}

Output:

Enter a Number : 123
Reverse of Entered Number : 321

C# Program to Print Alphabet Triangle

using System;  
  public class PrintExample  
   {  
     public static void Main(string[] args)  
      {  
       char ch='A';      
       int i, j, k, m;      
       for(i=1; i<=5; i++)      
       {      
        for(j=5; j>=i; j--)      
         Console.Write(" ");      
        for(k=1;k<=i;k++)      
         Console.Write(ch++);      
        ch--;      
        for(m=1;m<i;m++)      
         Console.Write(--ch);      
        Console.Write("\n");      
        ch='A';      
       }      
   }  
  }  

C# examples - Output :

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

Related Searches to C# Programs