Breaking News
Loading...
Saturday, December 28, 2013

Basic C# Programms with Solutions

11:18 AM



As last year i share all Programs of Assembly language which i study in last Semester Now in this semester i am on C# programming learning it practicing it so i want to share all my programs of C# which are coded by me,so i am going to share that programs so that other peoples also got benefit from them and don't hesitate to discuss C# programming with me as you all discuss a lot  C and C++ languages which we studied last year.

C# programs List:

Problem#1:

Write a program which converts the 1 lower case letter (‘a’ – ‘z’) in its corresponding upper case letter 
(‘A’ – ‘Z’). E.g. if user enter c then program will show C on the screen.
View Solution
public class armstrong
    {
        int x, a, res, sum = 0;
        public void find()
        {
            Console.WriteLine("\nAll Armstrong Numbers between 1 and 500 \n");
            for (x = 1; x <= 500; x++)
            {
                a = x;
                while (a != 0)
                {
                    res = a % 10;
                    a = a / 10;
                    sum = sum + (res * res * res);
                }
                if (x == sum)
                    Console.Write(" {0} ", x);
                sum = 0;
            }
            Console.WriteLine("\n");
        }
    } 

Problem#2:

Write a program which takes three points (x1, y1), (x2, y2) and (x3, y3) from user and program will check if all the three points fall on one straight line or not.
View Solution
 static void Main(string[] args)
        {
            double x1, x2, x3, y1, y2, y3, m1, m2;
            Console.WriteLine("Enter value of x1");
            x1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter value of x2");
            x2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter value of x3");
            x3 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter value of y1");
            y1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter value of y2");
            y2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter value of y3");
            y3 = Convert.ToInt32(Console.ReadLine());
            m1 = (x2 - x1) / (y2 - y1);
            m2 = (x3 - x2) / (y3 - y2);
            if (x2 - x1 == 0 || x3 - x2 == 0)
                { Console.WriteLine("\t\tInvalid input (Attempted to divide by zero)!");}
            else
                {
                    m1 = (y2 - y1) / (x2 - x1);
                    m2 = (y3 - y2) / (x3 - x2);
                    Console.WriteLine("\t\tSlope 1 = {0}", m1);
                    Console.WriteLine("\t\tSlope 2 = {0}", m2);
                    if (m1 == m2)
                    { Console.WriteLine("\n\t\tSo Given point fall on one straight line.\                      n\n"); }
                    else
                    { Console.WriteLine("\n\t\tSo Given point does not fall on one straig                      ht line.\n\n"); }
                }
              }

Problem#3:

Write a program which takes coordinates (x, y) of a center of a circle and its radius from user, program will determine whether a point lies inside the circle, on the circle or outside the circle.
View Solution
public class circle
    {
        double x, y, c_x, c_y, radius, dis, temp;
        public double square_root(double t)
        {
            double lb = 0, ub = t, temp = 0; int count = 50;
            while (count != 0)
            {
                temp = (lb + ub) / 2;
                if (temp * temp == t)
                { return temp; }
                else if (temp * temp > t)
                { ub = temp; }
                else
                { lb = temp; }
                count--;
            }
            return temp;
        }
        public void cal()
        {
            Console.Write("\n\t\tEnter center point X : ");
            c_x = Convert.ToDouble(Console.ReadLine());
            Console.Write("\t\tEnter center point Y : ");
            c_y = Convert.ToDouble(Console.ReadLine());
            Console.Write("\t\tEnter radius : ");
            radius = Convert.ToDouble(Console.ReadLine());
            Console.Write("\t\tEnter point X : ");
            x = Convert.ToDouble(Console.ReadLine());
            Console.Write("\t\tEnter Y : ");
            y = Convert.ToDouble(Console.ReadLine());
            temp = ((c_x - x) * (c_x - x)) + ((c_y - y) * (c_y - y));
            if (temp < 0)
            { Console.WriteLine("\t\tError negative value!"); }
            else
            { dis = square_root(temp); }
            Console.WriteLine("\t\tDistance : {0}", dis);
            if (dis == radius)
            { Console.WriteLine("\t\tGiven point lies on the circle.\n\n"); }
            else if (dis > radius)
            { Console.WriteLine("\t\tGiven point lies outside the circle.\n\n"); }
            else
            { Console.WriteLine("\t\tGiven point lies inside the circle.\n\n"); }
        }
    }

Problem#4:

Write a program which take one character from user and determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol. The following table shows the range of ASCII values for various characters.
Character
ASCII Values
A – Z
A – z
0 – 9
Special symbols
65 – 90
97 – 122
48 – 57
0 – 47, 58-64, 91-96,123-127
View Solution
static void Main(string[] args)
        {
            char ch;
            int a;
            Console.WriteLine("Enter any Character:");
            ch = Convert.ToChar(Console.ReadLine());
            a= (int)ch;
            if (a >= 65 && a <= 90)
                Console.WriteLine("Entered Character is in Capital Letter");
            else if(a>=97 && a<=122)
                Console.WriteLine("Entered Character is in Low Letter");
            else if(a>=48 && a<=57)
                Console.WriteLine("Entered Character is a digit");
            else
                Console.WriteLine("Entered Character is a Special Symbol");
                Console.ReadLine();
        }

Problem#5:

Write a program using switch statement which takes one value from user and ask about type of conversion and then will perform conversion according to type of conversion.if user enters
I -> convert from inches to centimeters.
G -> convert from gallons to liters.
M -> convert from mile to kilometer.
P -> convert from pound to kilogram.
If user enters any other character then show a proper message.
View Solution
public class con
    {
        int num; char ch;
        float f;
        public void coversion()
        {
            Console.Write("\n\t\tEnter a number to convert : ");
            num = Convert.ToInt32(System.Console.ReadLine());
                Console.WriteLine("\t\tI -> convert from inches to centimeters.");
                Console.WriteLine("\t\tG -> convert from gallons to liters.");
                Console.WriteLine("\t\tM -> convert from mile to kilometer.");
                Console.WriteLine("\t\tP -> convert from pound to kilogram.");
                Console.Write("\n\t\tSelect conversion you want to do: ");
                ch = Convert.ToChar(System.Console.ReadLine());
                switch (ch)
                {
                    case 'I':
                        {
                            f = (float)num * (float)2.5400;
                            Console.WriteLine("\n\t\t{0} inches = {1} cm\n\n", num, f);
                            break;
                        }
                    case 'G':
                        {
                            char ch2;
                            Console.WriteLine("\t\tI=> imperial gallon");
                            Console.WriteLine("\t\tU=> US gallon");
                            Console.Write("\n\t\tWhich one you want to enter : ");
                            ch2 = Convert.ToChar(Console.ReadLine());
                            if (ch2 == 'I')
                            {
                                f = (float)num * (float)4.546;
                                Console.WriteLine("\n\t\t{0} imperial gallons = {1}  litr                                 es\n\n", num, f);
                            }
                            else if (ch2 == 'U')
                            {
                                f = (float)num * (float)3.785;
                                Console.WriteLine("\n\t\t{0} US gallons = {1} litres\n\n"                                 , num, f);
                            }
                            else
                            { Console.WriteLine("\t\tInvalid input!\n\n"); }
                            break;
                        }
                    case 'M':
                        {
                            f = (float)num * (float)1.609344;
                            Console.WriteLine("\n\t\t{0} miles = {1} Kilometers\n\n", num                              , f);
                            break;
                        }
                    case 'P':
                        {
                            f = (float)num / (float)2.2046;
                            Console.WriteLine("\n\t\t{0} lbs = {1} Kilograms\n\n", num, f                                               );
                            break;
                        }
                    default:
                        {
                            Console.WriteLine("\n\t\tInvalid input please try again!\n\n"                                              );
                            break;
                        }
                }
        }

}

Problem#6:

In a company, worker efficiency is determined on the basis of the time required for a worker to complete a particular job. If the time taken by the worker is between 2 – 3 hours, then the worker is said to be highly efficient. If the time required by the worker is between 3 – 4 hours, then the worker is ordered to improve speed. If the time taken is between 4 – 5 hours, the worker is given training to improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has to leave the company. If the time taken by the worker is input through the keyboard, find the efficiency of the worker.
View Solution
static void Main(string[] args)
        {
            double time;
            Console.WriteLine("Enter the Worker's Time to complete Task:");
            time = Convert.ToDouble(Console.ReadLine());
            if (time >= 2 && time <=3)
                Console.WriteLine("Good! Worked Efficiently");
            else if(time>3 && time<=4)
                Console.WriteLine("Improved Your working Speed");
            else if(time>4 && time<=5)
                Console.WriteLine("Required Tranning to improve Speed");
            else if(time>5)
                Console.WriteLine("Leave this company");
            else
                Console.WriteLine("No Result for this input");
            Console.ReadLine();
        }

Problem #7:

Write a program using conditional operators to determine whether a year entered through the keyboard is a leap year or not.
View Solution
static void Main(string[] args)
        {
            int year;
            Console.WriteLine("Enter Year");
            year = Convert.ToInt32(Console.ReadLine());
           Console.WriteLine( year % 4 == 0 ? "Entered year is leap" : "Not leap year");
           Console.ReadLine();}

Problem #8:

Write a program using switch statement which takes one character value from user and check whether the entered value is arithmetic operator, logical operator, conditional operator, relational operator or something else.
View Solution
static void Main(string[] args)
        {
            char ch;
            int a;
            Console.WriteLine("Enter any Character:");
            ch = Convert.ToChar(Console.ReadLine());
            a=(int)ch;
            switch (ch)
            {
                case '+':
                case '-':
                case '*':
                case '/':
                case '%':
                    Console.WriteLine("Entered Character is Arithmetic");
                    break;
                case '&':
                case '|':
                case '!':
                    Console.WriteLine("Entered Character is Logical Operator");
                    break;
                case '?':
                case ':':
                    Console.WriteLine("Entered Character is Condational Operator");
                    break;
                case '<':
                case '>':
                case '=':
                    Console.WriteLine("Entered Character is Relational Operator");
                    break;
                default:
                    Console.WriteLine("Entered Character is something else not included i                     n list");
                    break;

            }
            Console.ReadLine();
        }

Problem #9:

Write a program which prints an identity matrix using for loop i.e. takes value n from user and show the identity table of size n * n.
View Solution
static void Main(string[] args)
        {
            int n,i,j;
            Console.WriteLine("Enter value of n:");
            n = Convert.ToInt32(Console.ReadLine());
              int[,]  arr=new int[n,n];
            
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < n; j++)
                {
                    if (i == j)
                        arr[i, j] = 1;
                    else
                        arr[i, j] = 0;
                    Console.Write(arr[i, j]);
                }
                Console.WriteLine();  
            }
            Console.ReadLine();
        }

Problem #10:

Write a program using for loop which prints the following series.
1 2 4 8 16 21 64 128 …nth iteration
View Solution
static void Main(string[] args)
        {
            int n,i=1,num=1;
            Console.WriteLine("Enter Value of n ");
            n = Convert.ToInt32(Console.ReadLine());
            Console.Write(i+" ");
            for (i = 1; i <= n; i++)
            {
                num = num * 2;
                Console.Write(num + " ");
            }
            
            Console.ReadLine();
        }

Problem #11:

Write a program using for loop which prints the following output. (You have to find a pattern to print alphabetics in this order)
A B D H P
View Solution
static void Main(string[] args)
        {
              int i,j,ch=0,n;
            Console.Write("A ");
            for (i = 1; i <= 4; i++)
            {
                n = 1;
                for (j = 1; j <= i; j++)
                {
                    n = n * 2;
                    ch = 64 + n;
                }
                Console.Write((char)ch + " ");
            }
            
                Console.ReadLine();
        }

Problem #12:

Write a program using loop which prints the following output.
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 . . . nth iteration
View Solution
static void Main(string[] args)
        {
            int i, j, num;
            Console.WriteLine("Enter value of num:");
            num = Convert.ToInt32(Console.ReadLine());
            for (i = 1; i <= num; i++)
            {
                for (j = 1; j <= i; j++)
                    Console.Write(i + " ");
            }
            Console.ReadLine();
        }

Problem #13:

Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255.
View Solution
static void Main(string[] args)
        {
            int i=0;
            Console.WriteLine("ASCII      Char");
            while (i <= 255)
            {
                Console.WriteLine(i+"           "+(char)i);
                i++;
            }
            Console.ReadLine();
}

Problem #14:

Write a program which takes characters from user until user press ENTER and then program will show the number of words with length greater than or equal to 5.
View Solution
public class length
   {
       int count1 = 0, count2 = 0;
       public void check()
       {
           Console.Write("\n\t\tEnter string : ");
           string ch = Console.ReadLine();
           for (int x = 0; x < ch.Length; x++)
           {
               if (ch[x] == ' ')
               {
                   count1++;
                   if (x >= 5)
                   { count2++; }
               }
           }
           Console.WriteLine("\n\t\tNumber of words with length greater than or equal to 5 : {0}", count2);
           Console.WriteLine();
       }
   }

Problem #15:

Write a program to print all the ASCII values and their equivalent characters using a do-while loop. The ASCII values vary from 10 to 255.
View Solution
 static void Main(string[] args)
        {
            int i = 0;
            Console.WriteLine("ASCII      character");
            do
            {
                Console.WriteLine(i+"               "+(char)i);
                i++;
            } while (i <= 255);
            Console.ReadLine();
        }

Problem #16:

Write a program which takes one value from user and check whether the entered value is character, integer or special symbol.
View Solution
 static void Main(string[] args)
        {
            char value;
            int a;
            Console.WriteLine("Enter value to check Character, integer or special symbols ");
            value = Convert.ToChar(Console.ReadLine());
            a=(int)value;
            if (a >= 65 && a <= 90 || a >= 97 && a <= 122)
                Console.WriteLine("Entered Value is Character");
            else if(a>=48 && a<=57)
                Console.WriteLine("Entered Value is Integer");
            else if(a>=0 && a<=47 || a>=58&&a<=64 || a>=91&&a<=96 || a>=123&&a<=127)
                Console.WriteLine("Entered Value is Special Symbols");
            else
                Console.WriteLine("Invlaid input :(");
            Console.ReadLine();
        }

Problem #17:

Write a program that takes an integer as an input from user and prints if it is a prime or composite number.
View Solution
static void Main(string[] args)
        {
            int num, i;
              Console.WriteLine("Enter the number to check number is prime or composite");
                num=Convert.ToInt32(Console.ReadLine());
                i = 2;
                while (i <= num - 1)
                {
                    if (num % i == 0)
                    {
                        Console.WriteLine("composite number: "+num);
                        break;
                    }
                    i++;
                }
                if (i == num)
                    Console.WriteLine("prime number: " + num);
                Console.ReadLine();           
            
        }
 

Problem #18:

Write a program which prints the Fibonacci series using loop.
1 1 2 3 5 8 13 21 34 …
View Solution
 public class _fib
    {
        int f = 0, s = 1, n, fib = 0;
        public void _show()
        {
            Console.WriteLine("\n\t\tPrint fibonacci series 0 1 1 2 3 5 8 13 ...\n\n");
            Console.Write("\n\t\tEnter range of terms of fibonacci series : ");
            n = Convert.ToInt32(Console.ReadLine());
            Console.Write("\n\t\tFirst {0} terms are : ", n);
            for (int x = 0; x < n; x++)
            {
                if (x <= 1)
                { fib = x; }
                else
                {
                    fib = f + s;
                    f = s;
                    s = fib;
                }
                Console.Write("{0} ", fib);
            }
            Console.WriteLine("\n");
        }
    }

Problem #19:

Write a program using for loop which prints the following output on the screen.
*
**
***
****
***
**
*
View Solution

Problem #20:

Write a program using for loop which prints the following output on the screen.
*
***
*****
*******
*********
***********
View Solution

Problem #21:

Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3)
View Solution

Problem #22:

Write a program using for loop which prints the following output on the screen.
$$$$$$$$$$$
$                $
$                $
$                $
$$$$$$$$$$$
View Solution

Problem #23:

Write a program using loop which takes one value n from user and show the factorial of all prime numbers which are less then n. e.g. if n = 10 then program will print the factorial of 2,3,5,7.
View Solution

Problem #24:

Write a program to display the sum of the following series using loop.
1*x + 2*x2 + 3*x3 + 4*x4 + 5*x5 + … + n*xn
View Solution

Problem #25:

Write a program to display the sum of the following series i.e. sum of the factorial of odd series
1! + 3! + 5! + 7! + 9! + . . . + n!
View Solution

Problem #26:

Write a program to display the sum of the following series.
2/1! + 4/3! + 6/5! + 8/7! + 10/9! + . . . + n/(n-1)!
View Solution

Problem #27:

Write a program to display the sum of the following series i.e. sum of the factorial of odd series multiply with x where power of x is square of corresponding number.
X1*1! + X9*3! + X25*5! + X49*7! + X81*9! + . . . + Xn2*n!
View Solution

Problem #28:

Write a program which display the following output on the screen.
1 2 3 4 5
1 4 9 16 25
1 8 27 64 125
View Solution

Problem #29:

Write a program which display the following output on the screen.
####$####
###$#$###
##$###$##
#$#####$#
$#######$
View Solution

Problem #30:

Write a program to produce the following output:
A B C D E F G F E D C B A
A B C D E F     F E D C B A
A B C D E           E D C B A
A B C D                 D C B A
A B C                        C B A
A B                               B A
A                                      A
View Solution

Problem #31:

Write a program to produce the following output:
             1
          2   3
        4   5   6
       7  8  9  10
View Solution

Problem #32:

Write a program which takes 10 values from user in an array and then show the number of prime values in the array.
View Solution

Problem #33:

How many printf statements will be executed by this program and rewrite the following program without using goto statement.
void main( )
{
int i, j, k ;
for ( i = 1 ; i <= 3 ; i++ )
{
for ( j = 1 ; j <= 3 ; j++ )
{
for ( k = 1 ; k <= 3 ; k++ )
{
if ( i == 3 && j == 3 && k == 3 )
goto out ;
else
printf ( "%d %d %d\n", i, j, k ) ;
}
}
}
out :
printf ( "Out of the loop at last!" ) ;
}
View Solution

Problem #34:

Write a program which takes n values from user and then sort them in ascending order.
View Solution

Problem #35:

Write a program which takes n values from user and then sort them using Bubble sort.
View Solution

Problem #36:

Write a program which takes n values in an array and then program should be able to search a value in the array using binary search algorithm. Hint: You have to sort that array first because binary search can be applied only on sorted array.
View Solution

Problem #37:

Write a program which copies the values of one array in second array in reverse order.
View Solution

Problem #38:

Create two arrays student_rollno and student_marks, both of same size. First array will save the rollnos of students and second array will save the marks of students against his rollno. e.g. if student_rollno[0] contains 197 then student_marks[0] will contains the marks of roll no 197.
You have to print the roll no of student with maximum marks.
View Solution

Problem #39:

Write a function which takes one value as parameter and display the sum of digits in the value. e.g. if user has passed 125 then function will print 1+2+5 = 7.
View Solution

Problem #40:

Write a function which takes two values n1 and n2 as arguments and multiply the n1 by itself n2 times i.e. calculate n1n2 and then return the result to main function which will display the result.
View Solution

Problem #41:

Write a program which will take input a charater ‘a’ value from user. You have to use switch statement to decide if value of a is ‘t’ then you have to call table function, if value of a is ‘f’ then call factorial function, if value of a is ‘p’ then call prime function, if value of a is ‘s’ then call search function.
You have to write four functions in your program;
Table(int n1,n2)
Factorial(int n3)
Prime(int n4)
Search(char n5[], char c, char choice)
Table function will print the table of n1 from 1 to n2.
Factorial function will print the factorial of n3 if n3 is multiple of 2.
Prime function will print the n4 if n4 is prime.
Search function will take first argument n5 as an array of characters and second element a character to be search in the array and third element a character to decide which searching algorithm to be used.i.e. if user has passed the value of c as ‘s’ then Search function will perform the sequential search but if value of c is something else then Search function will perform binary search.
Structure of your program will be like this. You have to make it exactly working.
Switch(a)
{
case ‘f’:
factorial();
break;
case ‘p’:
prime();
break;
case ‘t’:
table();
break;
case ‘s’:
search();
break;
}
View Solution

Problem #42:

Write two functions max(int,int) and prime(int). max function will take two arguments and will return the maximum of two numbers in main. Then main function will pass this calculated factorial to prime function which will show that passed value is prime or not.
View Solution

Problem #43:

Write a function which takes four arrays of same size as arguments; array1, array2, array3, array4. The function will multiply the corresponding values of array1 and array2 and will save the result at same index of array3. i.e. array3[0] = array1[0] * array2[0] and so on. Then create another function and pass all four arrays to that function as argument where you have to compare array1, array2 and array3 and save the max value in array4 i.e. if array1[0] = 10, array2[0] = 5, array3[0]= 11 then you have to save max value i.e. array4[0] = 11. In main function, show all values of array4.
View Solution

Problem #44:

If the lengths of the sides of a triangle are denoted by a, b, and c, then area of triangle is given by
Area = SS(S-a)(S-b)(S-c)
where, S = ( a + b + c ) / 2
View Solution

Problem #45:

Create a class of student which stores characteristcs of student like studentID, studentName, studentDOB, studentRollNo, studentEmail, studentGPA of last 5 semesters and other related information of the student. (You may set some properties Boolean and some readonly)
a. Calculate the CGPA of each student using function calculateCGPA(…).
b. Student with highest CGPA will be considerd CR of the class. There should be a function which will compare the CGPA of students and will declare a student having greater CGPA as CR.
c. Program should be able to take input of 5 students from user; definitely there will be a function which will take input from the user.
d. You have to use the Setter for setting the values of the data members of the class and and Getter function for getting the values of the data members of the class. (You can use property as alternative)
e. Default value for each student GPA should be 3.0. (You have to use an array for storing GPAs of last 5 semesters. You may simply initialize the array with 3.0 in constructor).
f. All data members should be private.
g. Member functions can be public.
h. Program must be able to add two objects i.e. if we add two students then all their corresponding data members will be added one by one. (Operator Overloading).
i. Program must contain explicitly definition of copy constructor.
j. StudentID and StudentGPA of last 5 semesters must be constant or readonly.
k. Value of studentGPA of last 5 semesters must be selected from ENUM which may contain values {1.0, 2.0, 3.0, and 4.0}.
View Solution

Main Function for Calling ALL Program Classes
static void Main(string[] args)
        {
            Program obj = new Program();
            obj.Main_Fun();
            Console.ReadKey();

        }

1 comments:

  1. Are you trying to earn cash from your websites/blogs by using popunder advertisments?
    If so, have you tried using PopCash?

    ReplyDelete

 
Toggle Footer