Monday, March 17, 2014

Generating_Random_Value_Ex_3 (Wk-4), Jan 27-31, 2014

Question: Generate 2 random values from 100-999.
Then, ask the user to enter the sum.
Verify if it is correct and tell the user whether the result is correct or wrong.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Generate_Random_Value_Ex_3
{
    class Program
    {
        static void Main(string[] args)
        {
            repeat:
            Random rand = new Random();
            int n1 = rand.Next(100, 1000);
            int n2 = rand.Next(100, 1000);

            Console.WriteLine("Two random value are: n1 = {0} n2 = {1}", n1, n2);
            Console.Write("Enter sum: ");
            int sum = int.Parse(Console.ReadLine());
            if(sum == n1 + n2)
            {
                Console.WriteLine("Correct");
            }
            else
            {
                Console.WriteLine("Incorrect");
            }
            Console.ReadLine();
            Console.Clear();
            goto repeat;
        }
    }
}

No comments:

Post a Comment