Wednesday, 4 December 2013

Working with Operators

Exercise1
Write a Program to identify whether character entered by a user is a vowel or a consonant.

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VowelConsonant
{
    class Program
    {
        static void Main(string[] args)
        {
            char ip;
            Console.Write("Enter one character to check vowel or consonant ");
            ip = Convert.ToChar(Console.ReadLine());
            switch (ip)
            { 
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                    Console.WriteLine(ip +" is vowel");
                    break;
                default:
                    Console.WriteLine(ip + " is consonant");
                    break;

            }
            Console.ReadLine();
        }
    }
}





Exercise 2
Write a program to identify whether the number entered by a user is even or odd.

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EvenOdd
{
    class Program
    {
        static void Main(string[] args)
        {
            int ip;
            Console.Write("Enter the number ");
            ip = Convert.ToInt32(Console.ReadLine());
            if (ip%2==0)
            {
                Console.WriteLine(ip + " is Even Number\n");
            }
            else
            {
                Console.WriteLine(ip + " is Odd Number");
            }
            Console.ReadLine();
        }
    }
}



Exercise 3
Write a program to accept a number from user and display all the prime numbera from one up to the number entered by the user.

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimeNumber
{
    class Prime
    {
        public void PrimeNo(int num)
        {
            Boolean isPrime = true;
            Console.WriteLine("\n The Prime Number between 1 - {0} is ", num);
            for (int i = 0; i <= num; i++)
            {
                for (int j = 2; j <= num; j++)
                {
                    if (i != j && i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime == true)
                {
                    Console.WriteLine("{0}", i);
                }
                isPrime = true;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int num;
            Prime fpn = new Prime();
            string str;
            Console.Write("Enter the value to print the series of Prime NUmber ");
            str = Console.ReadLine();
            num = Convert.ToInt32(str);
            fpn.PrimeNo(num);
            Console.ReadLine();
        }
    }
}




Exercise 4
Write a program to accept two number and check if the first is divisible by the second. In addition, an error message should be displayed if the second number is zero.

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch2_Ex4
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2, result;
            Console.Write("Enter first number ");
            num1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter second number ");
            num2 = Convert.ToInt32(Console.ReadLine());
            if (num2 == 0)
            {
                Console.Write("You can not divide by zero");
                
            }
            else
            {
                result = num1 % num2;
                if (result == 0)
                    Console.WriteLine("It is divisible");
                else
                    Console.WriteLine("It is not divisible");
            }
            Console.ReadLine();
        }
    }
}


Exercise 5
Write program to accept two numbers and display the quotient as a result. In addition, an error message should be displayed if the second number is zero or greater than the first number.

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch2_Ex5
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2, result;
            Console.Write("Enter first number ");
            num1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter second number ");
            num2 = Convert.ToInt32(Console.ReadLine());
            if (num2 == 0 || num2 >num1)
            {
                Console.Write("Error!! Second number is greater OR it is ZERO");
            }
            else
            {
                result = num1 / num2;
                Console.WriteLine("Quotient is " + result);
            }
            Console.ReadLine();
        }
    }
}



Exercise 6 
Write a program to enter a number from 1 to 7 and display the corresponding day of the week.

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeekDay
{
    class Program
    {
        static void Main(string[] args)
        {
            int weekday;
            Console.Write("Enter the Week Day(1~7)  ");
            weekday = Convert.ToInt32(Console.ReadLine());
            switch(weekday)
            {
                case 1:
                    Console.Write("Monday");
                    break;
                case 2:
                    Console.Write("Tuesday");
                    break;
                case 3:
                    Console.Write("Wednesday");
                    break;
                case 4:
                    Console.Write("Thrusday");
                    break;
                case 5:
                    Console.Write("Friday");
                    break;
                case 6:
                    Console.Write("Saturday");
                    break;
                case 7:
                    Console.Write("Sunday");
                    break;
                default:
                    Console.Write("Invalid Week Day");
                    break;
            }
            Console.ReadLine();
        }
    }
}


Exercise 7
Write a program to display the highest of any 10 number entered.

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch1_Ex7
{
    class Program
    {
        static void Main(string[] args)
        {
            int ctr,num1,max=0;
            Console.Write("Enter the number");
            for(ctr=0;ctr<=9;ctr++)
            {
                num1 = Convert.ToInt32(Console.ReadLine());
           if(num1>max)
           {
           max=num1;
           }
            }
            Console.Write("The largest Number is " + max);
            Console.ReadLine();
        }
    }
}



Exercise 8
Write a program to print the product of the first 10 even numbers.

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch2_Ex8
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 2,even=0;
            double prod = 1;
            while(even<10)
            {
                prod = num * prod;
                num += 2;
                even++;
            }
            Console.Write("Product of first 10 even number is " + prod);
            Console.ReadLine();
        }
    }
}




Exercise 9
Enter a year and determine whether the year is a leap year or not.

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeapYear
{
    class Program
    {
        static void Main(string[] args)
        {
            int year;
            Console.Write("Please enter the year ");
            year = Convert.ToInt32(Console.ReadLine());
            if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
            {
                Console.WriteLine("This is a leap year");
            }
            else
            {
                Console.WriteLine("This is not a leap year");
            }
            Console.ReadLine();
        }
    }
}



Exercise 10
Write a program to implement the following features:
1. If the user enter the option 1, the program shloud display "The applcation will allow you to crete the pattern in pyramid structure".
2. If the user enter the option 2, the program shloud display "This application will allow you to add two primary colors and display the third color".
3. If the user enter the option 3, the program shloud display "This application will allow you to create a meaningful word from jumbled letters".
4. If the user enter the option 4, the program shloud display "This application will allow you to create multiple words from a single word. Each correct word will increase your score by 1."

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch2_Ex10
{
    class ProgramMenu
    {
        int ip;
        public void menu()
        {
            Console.WriteLine("**************APPLICATION MENU************************");
            Console.WriteLine("\t\t\t 1. CREATE PATTERN\n \t\t\t 2. ADD PRIMARY COLORS\n \t\t\t 3. JUMBLED WORDS\n \t\t\t 4. PUZZLE ");
            Console.WriteLine("*******************************************************");
        }

        public void accept()
        {
            Console.Write("Enter your selection ");
            ip = Convert.ToInt32(Console.ReadLine());
            switch (ip)
            { 
                case 1:
                    Console.WriteLine("The applcation will allow you to crete the pattern in pyramid structure");
                    break;
                case 2:
                    Console.WriteLine("This application will allow you to add two primary colors and display the third color");
                    break;
                case 3:
                    Console.WriteLine("This application will allow you to create a meaningful word from jumbled letters");
                    break;
                case 4:
                    Console.WriteLine("This application will allow you to create multiple words from a single word. Each correct word will increase your score by 1");
                    break;
                default:
                    Console.WriteLine("Invalid selection");
                    break;
            }
        }
        static void Main(string[] args)
        {
            ProgramMenu pm = new ProgramMenu();
            pm.menu();
            pm.accept();
            Console.ReadLine();
        }
    }
}


No comments:

Post a Comment