C#

Control Statements Example Programs

0
Control Statements (if,switch case & loops) example programs
  1. WAP in C# to print even number from 0 – 100

    //print even number from 0 - 100 
    using System;
    class HelloWorld
    {
      static void Main ()
      {
    
        for (int i = 1; i <= 100; i++)
          {
    
    	if (i % 2 == 0)
    	  {
    
    	    Console.WriteLine (i);
    
    	  }
        }
      }
    }
  2. WAP in C# to print factors of a user input number

    //print factors of number
    using System;
    class HelloWorld
    {
      static void Main ()
      {
    
        Console.WriteLine ("Enter Number");
        int n = Convert.ToInt32 (Console.ReadLine ());
    
        for (int i = 1; i <= n; i++)
          {
    	if (a % i == 0)
    	  {
    	    Console.WriteLine (i);
    	  }
        }
      }
    }
  3. WAP in C# to find factorial of a user input number.

    //print factorial of user input number 
    using System;
    class HelloWorld {
      static void Main() {
          
          Console.WriteLine("Enter Number");
          int n = Convert.ToInt32(Console.ReadLine());
          int fact=1;
          
          for(int i=1; i<=n; i++){
              
              fact = fact*i;
              
          }
          Console.WriteLine(fact);
      }
    }
  4. WAP to check if the number is prime or commposite

    //prime check
    using System;
    class HelloWorld
    {
      static void Main ()
      {
    
        Console.WriteLine ("Enter Number");
        int n = Convert.ToInt32 (Console.ReadLine ());
        bool comp = false;
    
    
        for (int i = 2; i <= n/2; i++)
          {
    
    	if (n % i == 0)
    	  {
    	    comp = true;
    	    break;
    	  }
          }
        if (comp == true)
          {
    	      Console.WriteLine ("composite");
          }
        else
          {
            Console.WriteLine ("Prime");  
          }
      }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *