Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, 23 April 2014

C# program to print pyramid structure 2

b)                 1
                         2   3    4
                    5   6   7    8   9


Source code:
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pyramid1
{
    class pyramidd
    {
        public void pyramid1()
        {
            int no;
            int rw;
            int c;
            int spc;

            Console.WriteLine("Enter number of rows : ");
            no= int.Parse(Console.ReadLine());
           

            for (rw = 1; rw <= no; rw++)
            {
                for (spc = no; spc >= rw; spc--)
                {
                    Console.Write(" ");
                }
                for (c = 1; c <= rw; c++)
                {
                    Console.Write(" " +c);
                }
                Console.Write("\n");
            }
          
            Console.Read();
                
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            pyramidd p = new pyramidd();
            p.pyramid1();

        }
    }

}


Read More

C# program to print pyramid structure.

a)          1
              2  3
              4  5  6
              7  8  9  10


Source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pyramid1
{
    class pyramidd
    {
        public void pyramid1()
        {

            int n;
            int i, c;
            int a=1;

            Console.Write("Enter the number of rows of Floyd's triangle to print\n");
            n = int.Parse(Console.ReadLine());

            for (i = 1; i <= n; i++)
            {
                for (c = 1; c <= i; c++)
                {
                    Console.Write(" "+a);
                    a++;
                }
                Console.Write("\n");
            }

           
            Console.Read();
               
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            pyramidd p = new pyramidd();
            p.pyramid1();

        }
    }

}
Read More

C# program to perform a simple calculator function

Calculator is made to perform some predefined functions on it. We use it to speed up our calculation and provide exact result. Calculator performs varies functions some of the basic functions it does are addition, substation, multiplication and division. These are performed on integers either numbers. The input is provided with specifying operator and hence to get exact output.

The below motioned c# program will perform tasks of a calculator. The program first begins with menu and provides options to user. User will choose desired option to start calculation function. int.Parse(Console.ReadLine())  is used to get an input from the user and stored in a variables. The switch statement performs the its defined tasks or the code as with respect to user input. Case 1 is designed to perform addition, case 2 is to perform subtraction and so on,.  

The basic  calculator functions are :
ADDITION OF TWO NUMBER :
C= A + B
SUBTRACTION OF TWO NUMBER :
C= A – B
MULTIPLICATION OF TWO NUMBER :
C= A * B
DIVISION OF TWO NUMBER
C= A / B


                              SOURCE CODE:
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace calculator  
 {  
   class cal  
   {  
     public void calculator()  
     {  
       int a;  
       int b;  
       int c;  
       Console.WriteLine(" @@@ menu @@@");  
       Console.WriteLine("enter 1 for sum ");  
       Console.WriteLine("enter 2 for sub ");  
       Console.WriteLine("enter 3 for mult ");  
       Console.WriteLine("enter 4 for div ");  
       c = int.Parse(Console.ReadLine());  
       Console.Write("enter a integer A : ");  
       a = int.Parse(Console.ReadLine());  
       Console.Write("enter a integer B : ");  
       b = int.Parse(Console.ReadLine());  
       switch (c)  
       {  
         case 1:  
           c = a + b;  
           Console.WriteLine("sum=" + c);  
           break;  
         case 2:  
           c = a - b;  
           Console.WriteLine("sub=" + c);  
           break;  
         case 3:  
           c = a * b;  
           Console.WriteLine("mul=" + c);  
           break;  
         case 4:  
           if (b == 0)  
           {  
             Console.WriteLine("enter a valid number ");  
           }  
           else  
           {  
             c = a / b;  
             Console.WriteLine("mul=" + c);  
           }  
           break;  
         default:  
           Console.WriteLine("Invalid choice");  
           break;  
       }  
     }  
   }  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       cal c=new cal();  
       c.calculator();  
     }  
   }  
 }  



                   OUTPUT:
@@@ menu @@@
Enter 1 for sum
Enter 2 for sub
Enter 3 for mult
Enter 4 for div

3

Enter a integer A : 80
Enter a integer B : 90
Mult = 7200


Read More

C# program to check whether a input character is vowel or not

There are five vowels in English alphabets there are A,E,I,O,U. All these vowels comes in alphabets where their place differs from each other. Letter A comes in first position, E comes in 5th position of English alphabet so on, now we need to determine only vowels in alphabets. As there are limited number of options available to get result hence we choose switch case to solve this problem. 


Let's start with programming aspect here we write a c# program to check whether a input character is vowel or not using switch statement. Declare a string and get input by asking user so that we get a input. Now we add switch statement where if there any capital letter then we filter those things by using str.Tolower where we only get lower case letter hence it is useful for us to determine a vowel or not. You can do with capital letters if you want but in the next we can see that I have mentioned lower letters in switch cases as case1= a, case2=e,..etc. There are five cases to check input character is vowel of not, if this matches then say "vowel found" or say "vowel not found". For both of statements add break at last so that one can get exact output which is needed.

C# is an object oriented language hence we create an object to call the class switch1 which we have written our program. Call the class in main program as switch1 s = new switch1() in this class we wrote a code inside the method so using "s" object call the method s.switchcase1(). Now the our c# program to check vowel or not program is ready run, if you get any difficulties then post a comment here.

SOURCE CODE :


 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace switchcase1  
 {  
   class switch1  
   {  
     public void switchcase1()  
     {  
       string str = "";  
       Console.WriteLine("\nEnter a character to check vowel or not");  
       str = Console.ReadLine();  
       Console.WriteLine(str);  
       switch (str.ToLower())  
       {  
         case "a":  
         case "e":  
         case "i":  
         case "o":  
         case "u":  
           Console.WriteLine("Vowel");  
           Console.ReadLine();  
           break;  
           default:  
           Console.WriteLine("Not a Vowel");  
           Console.ReadLine();  
           break;  
       }  
     }  
   }  
     class Program  
     {  
       static void Main(string[] args)  
       {  
         switch1 s = new switch1();  
         s.switchcase1();  
       }  
     }  
 }  

OUTPUT:

 Enter a character to check vowel or not  
 a  
 a  
 its a vowel  

Read More

C# program to check whether a input character is vowel or not using switch statement

ENGLISH VOWELS A,E,I,O,U.


SOURCE CODE :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace switchcase1
{

    class switch1
    {
        public void switchcase1()
        {
            string str = "";
            Console.WriteLine("\nEnter a character to check vowel or not");
            str = Console.ReadLine();
            Console.WriteLine(str);
            switch (str.ToLower())
            {
                case "a":
                case "e":
                case "i":
                case "o":
                case "u":

                    Console.WriteLine("Vowel");
                    Console.ReadLine();
                    break;
                default:
                    Console.WriteLine("Not a Vowel");
                    Console.ReadLine();
                    break;
            
            }
        }

    }
        class Program
        {
            static void Main(string[] args)
            {
                switch1 s = new switch1();
                s.switchcase1();

            }
        }
   
}







                        OUTPUT:
Enter a character to check vowel or not
a

a

its a vowel



Read More

C# program to check whether a number is Armstrong or not

Armstrong :

Also known as narcissistic numbers, Armstrong numbers are the sum of their own digits to the power of the number of digits. As that is a slightly brief wording, let me give an example:
153 = 1³ + 5³ + 3³
Each digit is raised to the power three because 153 has three digits.


SOURCE CODE :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace palindrome
{
    class pali
    {
        public void palindrome()
        {
         int n, num, rev = 0, dig;
    
    
     Console.WriteLine("ENTER A NUMBER...: ");
     num = int.Parse(Console.ReadLine());
    
            n = num;
     while(num>0)
     {
          dig = num % 10;
          rev = rev * 10 + dig;
          num = num / 10;
     }
     if (n == rev)
          Console.WriteLine(" GIVEN NUMBER IS A PALINDROME");
     else
           Console.WriteLine(" GIVEN NUMBER NOT A PALINDROME");
     Console.Read();
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
            pali p = new pali();
            p.palindrome();
        }
    }
}




OUTPUT:
ENTER A NUMBER
1231

 GIVEN NUMBER NOT A PALINDROME 
Read More

C# program to find the sum of series 1+3+5+…+100 (adding odd number up to 100)

SOURCE CODE :-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sumodd
{

    class sum
    {
        public void sumodd()
        {
            int i;
            int sam = 0;
             
        for (i = 0; i <= 100; i++)
          {
      
              if (i%2 != 0)
              {
            int count = 0;
            sam = sam + i;
            count++;
              }
        }
      Console.WriteLine("sum of 1+3+5+...100 is " + sam);
   
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            sum s = new sum();
            s.sumodd();

        }
    }
}

OUTPUT:-

SUM OF 1+3+5+… 100 IS 2500
Read More

C# program to find the largest of five number using else if

This C# program is to find the largest of the five numbers using else if statement. There are other things where people searching for largest of two numbers, largest of three numbers etc,. But here we have different aspect where we find the largest of five numbers.

First is to get the five numbers from the user or define in top section of the program. Console.ReadLine().  This method allow one to accept the input from the user and assign them into integer variable a, b, c, d, e. The logic of finding largest of five number is written in if statement and depending on that logic update big variable and at last display the big variable.

C# is an object oriented language so we create an object to call the class bigger which we have written our program. Call the class in main program as bigger s = new bigger() in this class we wrote a code inside the method so using "b" object call the method b.bigger(). Now the our c# program to find the largest of five number using else if statement program is ready run, if you get any difficulties then post a comment here.
 

Source code:-

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace big  
 {  
   class biggest  
   {  
     public void big()  
     {  
       int a, b, c, d, e, big;  
       Console.WriteLine("ENTER ANY FIVE INTEGER NUMBERS");  
       a = int.Parse(Console.ReadLine());  
       b = int.Parse(Console.ReadLine());  
       c = int.Parse(Console.ReadLine());  
       d = int.Parse(Console.ReadLine());  
       e = int.Parse(Console.ReadLine());  
       if (a &gt; b &amp;&amp; a&gt;c &amp;&amp; a&gt;d &amp;&amp; a&gt;e)  
       {  
         big = a;  
       }  
       else if (b &gt; a &amp;&amp; b &gt; c &amp;&amp; b &gt; d &amp;&amp; b &gt; e)  
       {  
         big = b;  
       }  
       else if (c &gt; b &amp;&amp; c &gt; a &amp;&amp; c &gt; d &amp;&amp; c &gt; e)  
       {  
         big = c;  
       }  
       else if (d &gt; b &amp;&amp; d &gt; c &amp;&amp; d &gt; a &amp;&amp; d &gt; e)  
       {  
         big = d;  
       }  
       else  
       {  
         big = e;  
       }  
       Console.WriteLine("largest=" + big);  
       Console.Read();  
    }  
   }  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       biggest b = new biggest();  
       b.big();  
     }  
   }  

  
OUTPUT:-
ENTER ANY FIVE INTEGER NUMBER
666
777
444
555
222

largest = 777
Read More

C# program to perimeter of rectangle and square

The perimeter is equal to the length of the boundary around the rectangle.

Perimeter of rectangle = 2l+2w
Perimeter of square = 4 * sides

SOURCE CODE :-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace perimeter
{
    class peri
    {
        public void perime()
        {
            double l_rectangle, b_rectangle;
            double p_rectangle;
            double sides;
            double p_square;
           
            Console.WriteLine("enter the value of length and breadth of rectangle");
            l_rectangle = double.Parse(Console.ReadLine());
            b_rectangle = double.Parse(Console.ReadLine());

            Console.WriteLine("Enter the sides of the square");
            sides = double.Parse(Console.ReadLine());
            p_rectangle = 2 * l_rectangle + 2 * b_rectangle;
            p_square = 4 * sides;
           
            Console.WriteLine("the perimeter of rectangle" + p_rectangle);
            Console.WriteLine("the perimeter of square" + p_square);

        }
       
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            peri b = new peri();
            b.perime();
        }
    }
}



OUTPUT :-

enter the value of length and breadth of rectangle
3
3
Enter the sides of the square
5
the perimeter of rectangle 12
the perimeter of square 16


Read More

C# program to find the area of rectangle and square

:- The area of a rectangle is equal to the number of unit squares that can be fit between the boundaries the rectangle.
Rectangle
Area = w × h                           where, w = width        h = height

Area = a2
a = length of side
SOURCE CODE :-

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace area  
 {  
   class rectangle  
   {  
     public void are()  
     {  
       double l_rectangle, b_rectangle;  
       double a_rectangle;  
       double sides;  
       double a_square;  
       Console.WriteLine("enter the value of lenght and bredth of rectangle ");  
       l_rectangle =double.Parse(Console.ReadLine());  
       b_rectangle =double.Parse(Console.ReadLine());  
       a_rectangle = l_rectangle * b_rectangle;  
       Console.WriteLine("the area of rect is=" + a_rectangle);  
       Console.WriteLine("Enter the sides of the square");  
       sides = double.Parse(Console.ReadLine());  
       a_square=sides*sides;  
       Console.WriteLine("the area of square is=" + a_square);  
     }  
   }  
   class program  
   {  
     static void Main(string[] args)  
     {  
       rectangle b = new rectangle();  
       b.are();  
     }  
   }  
 }  

Output:-
     enter the value of lenght and bredth of rectangle  
     4  
     4  
     the area of rect is =16  
     Enter the sides of the square  
     5  
     The area of square is =25  





Read More

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2022