Showing posts with label console programming. Show all posts
Showing posts with label console programming. Show all posts

Thursday, November 3, 2011

Console Calculator

Console Application

In the previous blog we saw about sum of two number to see it again CLICK HERE
Now If you want to make console program for subtraction or multiplication then you have to only change the  operator in the previous program.

Now We are going to see how we can make a calculator in Console.

Requirements:-1 User will provide the two numbers on which action to be performed.

2. After it User will select what action(Subtraction,addition,multiplication or division) user want to perform.

Sol:- Write this code .

                Console.WriteLine("**** Enter the first number *****");
                int x = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("**** Enter the second number *****");
                int y = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("**** Enter 1 for addition  *****");
                Console.WriteLine("**** Enter 2 for subtration  *****");
                Console.WriteLine("**** Enter 3 for multification  *****");
                Console.WriteLine("**** Enter 4 for divition  *****");
                int z = Convert.ToInt32(Console.ReadLine());
                switch (z)
                {
                    case 1:
                        Console.WriteLine(x + y);
                        break;
                    case 2:
                        Console.WriteLine(x - y);
                        break;
                    case 3:
                        Console.WriteLine(x * y);
                        break;
                    case 4:
                        Console.WriteLine(x / y);
                        break;
                }

Download Calculator




Wednesday, November 2, 2011

Console Readline

Console

Console Programs

Here we are going to learn how to make some console program. In the previous blog we studied about how to make a very short console program.CLICK HERE to see it again. Now we are going to discuss some concepts of previous console program.
       We have seen Console.WriteLine() method. In it Console is a class and "WriteLine" is an method of that class. So we have a clear concept to how to write in the console but if we want a program which calculate the sum of two  number then how we can do that?
            How we can read the number from Console? There is lot methods but here we are going to use Console.ReadLine() method. We will use like

int x = Console.Readline();

NO!! the above syntex genrate an error because it return the value in the form of string so we need to convert the string value to the int value and How we shall do that?

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

CONSOLE PROGRAM:-

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the first number!");
            int x = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the second number!");
            int y = Convert.ToInt32(Console.ReadLine());
            int z = x + y;
            Console.WriteLine("Result of the sum is");
            Console.WriteLine(z);

           
        }
    }
}


 Now Press CTRL+F5
to bulit without debugging