Write a C# program to compute the sum of the first 100 prime numbers.
using System;  
public class Exercise26
{  
    public static void Main() 
      {     
          Console.WriteLine("\nSum of the first 100 prime numbers: ");
           long sum = 0;
            int ctr = 0;
            int n = 2;
            while (ctr < 100)
              {
                if (isPrime(n))
                {
                    sum += n;
                    ctr++;
                }
                n++;
            }
            Console.WriteLine(sum.ToString());
                    
    }
     public static bool isPrime(int n)
        {
            int x = (int)Math.Floor(Math.Sqrt(n));
            if (n == 1) return false;
            if (n == 2) return true;
            for (int i = 2; i <= x; ++i)
            {
                if (n % i == 0) return false;
            }
            return true;
        }
}
}
using System;
public class Exercise27 {
 public static void Main() {
  Console.Write("Input  a number(integer): ");
  int n = Convert.ToInt32(Console.ReadLine());
  int sum = 0;
  while (n != 0) {
   sum += n % 10;
   n /= 10;
  }
  Console.WriteLine("Sum of the digits of the said integer: " + sum);
 }
}
write a c# program to sort an array in ascending order
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int i,j,temp, n;
            int[] arr = new int[15];
            Console.WriteLine("Give Array Range");
            n =int.Parse(Console.ReadLine());
            Console.WriteLine("Enter The Array Elements");
            for(i=0; i<n; i++)
            {
                arr[i] = int.Parse(Console.ReadLine());
            }
        
        
            for(i=0; i<n; i++)
            {
               for(j=i+1; j<n; j++)
               {
                   if(arr[i]>arr[j])
                   {
                       temp = arr[i];
                       arr[i] = arr[j];
                       arr[j] = temp;
                   }
               }
            }
            Console.WriteLine("After Ascending Numbers: ");
            for(i=0; i<n; i++)
            {
                Console.WriteLine(arr[i]);
            }
             
            Console.ReadLine();
        }   
    }
}
Write a C# program about Single inheritance
using System;
namespace Studytonight
{
    public class Parent
    {
        public void DisplayParentsAB()
        {
            Console.WriteLine("A and B are my parents");
        }
    }
    public class Son: Parent
    {
        public void DisplaySonC()
        {
            Console.WriteLine("I am the son C");
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            Son s = new Son();
            s.DisplaySonC();
            s.DisplayParentsAB();
        }
    }
}
Write a C# program about Multilevel inheritance
using System;
namespace Studytonight
{
    public class Grandparent
    {
        public Grandparent()
        {
            Console.WriteLine("Constructor called at run-time");
        }
        public void DisplayGrandParentsAB()
        {
            Console.WriteLine("A and B are my grandparents");
        }
    }
    public class Parents: Grandparent
    {
        public void DisplayParentsCD()
        {
            Console.WriteLine("C and D are my parents");
        }
    }
    public class Child: Parent
    {
        public void DisplayChildZ()
        {
            Console.WriteLine("I am the child Z");
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            child cd = new Child();
            cd.DisplayChildZ();
            cd.DisplayParentsCD();
            cd.DisplayGrandParentsAB();
        }
    }
Post a Comment