Friday, 6 December 2013

ARITHEMATIC PROGRAM IN C#

Question 1:-  write a program that should accept two numbers from the users and perform the following mathematical operation-
v Addition
v Subtraction
v Multiplication
v Division
Answer :-
class Program
    {
        int num1, num2, result;
        char ch;
        public void accept()
        {
            Console.WriteLine("Main menu");
            Console.WriteLine("1:- addition");
            Console.WriteLine("2:- subtraction");
            Console.WriteLine("3:- Multiplication");
            Console.WriteLine("4:- division");
            Console.WriteLine("pleasee enter our choicee");
            Ch = Convert.ToChar(Console.ReadLine());
        }
        public void display()
        {
            Console.WriteLine("enter the first number");
            num1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter the second number");
            num2 = Convert.ToInt32(Console.ReadLine());
        }
        public void output()
        {
            switch (ch)
            {
                case '1':
                    {
                        result = num1 + num2;
                        Console.WriteLine("result=" + result);
                        break;
                    }
                case '2':
                    {
                        result = num1 - num2;
                        Console.WriteLine("result=" + result);
                        break;
                    }
                case '3':
                    {
                        result = num1 * num2;
                        Console.WriteLine("result=" + result);
                        break;
                    }
                case '4':
                    {
                        result = num1 / num2;
                        Console.WriteLine("result=" + result);
                        break;
                    }
                default:
                    {
                        Console.WriteLine("invalid choice");
                        break;
                    }
            }
        }
        static void Main(string[] args)
        {
            Program cal = new Program();
            cal.accept();
            cal.display();
            cal.output();
            Console.ReadLine();
        }
    }

Question 2:-  write a program to identify the largest of three numbers enter by user. Create a program using methods.
Answer :-
class Program
    {
        int num1, num2, num3;
        public void accept()
        {
            Console.WriteLine("enter the first number");
            num1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter the second number");
            num2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter the third number");
            num3 = Convert.ToInt32(Console.ReadLine());
        }
        public void display()
        {
            if(num1>num2 && num1>num3)
            {
                Console.WriteLine("first number is largest"+num1);
            }
            else if(num2>num1 && num2>num3)
            {
                Console.WriteLine("second number is largest"+num2);
            }
            else
            {
                Console.WriteLine("third number is largest"+num3);
            }
        }
        static void Main(string[] args)
        {
            Program d = new Program();
            d.accept();
            d.display();
            Console.ReadLine();
        }
    }
 Question 3-  predict the output of the following program-
class Program
    {
        int number;
        public void square(int number)
        {
            Console.WriteLine("the numbeer is :{0}", number);
            number *= number;
            Console.WriteLine("the square of 10 is :{0}", number);
        }
        Program()
        {
            number=10;
            square(number);
        }
        static void Main(string[] args)
        {
            Program cal = new Program();
            Console.ReadLine();
        }
    }
Answer :-
The number is :10
The square of 10 is :100
Question 4:- predict the output of the following program-
class Program
    {
       static int number1;
        public void display(int number)
        {
            Console.WriteLine(number);
        }
        Program()
        {
            number1++;
            display(number1);
        }
        static Program()
        {
            number1 = 10;
            number1++;
        }
        static void Main(string[] args)
        {
            Program cal1 = new Program();
            Program cal2 = new Program();
            Console.ReadLine();
        }
    }
Answer :- 12
13Question 5:- As a part of the team that is developing software for library, you have been assigned the task of writing a program, the program should accept the following book detail.
Ø Book number as int
Ø Book category (fiction or notification) as string
Ø Author name as string
Ø Number of copy available as int
Write the program to accept and display the preceding book details.
Answer :-
class Program
    {
        int book_number;
        string book_categary;
        string auther_naame;
        int number_of_copy;
        public void accept()
        {
            Console.WriteLine("enter the book number");
            book_number = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter the bool categary");
            book_categary = Console.ReadLine();
            Console.WriteLine("enter the auther name");
            auther_naame = Console.ReadLine();
            Console.WriteLine("ente the number of copy");
            number_of_copy = Convert.ToInt32(Console.ReadLine());
        }
        static void Main(string[] args)
        {
            Program d = new Program();
            d.accept();
            Console.ReadLine();
        }
    }
Question 6:- write the program that accept distance in kilometer, convert it into meter, and then display the result.
Answer :-
class Program
    {
        int distance, result;
        public void accept()
        {
            Console.WriteLine("enter the distance in kilometer");
            distance = Convert.ToInt32(Console.ReadLine());
        }
        public void display()
        {
            result = distance * 1000;
            Console.WriteLine("after cinvert kilometer into meter" + result);
        }
        static void Main(string[] args)
        {
            Program d = new Program();
            d.accept();
            d.display();
            Console.ReadLine();
        }
    }
Question :- write a program that display “Congratulation You Have Cleared This Level. Entering Level2…..?” in red color.
Answer :-
class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Congratulation! You Have Cleared Level.\n\n\tentring Leveel2.....?");
            Console.ReadLine();
        }}

No comments:

Post a Comment