Basic C# Programming Problem and Solutions: Part 2

Part 1: Basic C# programming Problem and Solutions

Today I am writing this article for the beginners who have just begun programming in the C# language. I have solved all the basic problems of the C# programming. Today I will show you Part 2 of my article series Basic C# Programming Problems. It contain 15 problems.

Problem 16

Write a program that takes one value from the user and checks whether the entered value is a character, integer or special symbol.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem16

{

classProblem16

    {

staticvoid Main(string[] args)

        {

char a;

int b;

Console.WriteLine("Enter A Value:");

            a = Convert.ToChar(Console.ReadLine());

            b = (int)a;

if (b >= 65 && b <= 90)

            {

                a = (char)b;

 

Console.WriteLine("Entered Value Is Character:" +a);

            }

if (b >= 97 && b <= 122)

            {

 

Console.WriteLine("Entered Value Is Character:"+a);

            }

if (b >= 48 && b <= 57)

            {

 

Console.WriteLine("Entered Value Is  Integer:"+a);

            }

if (b == 0 && b <= 47 || b >= 58 && b <= 64 || b >= 91 && b <= 96 || b >= 123 && b <= 127)

            {

 

Console.WriteLine("Entered Value Is Special Symbols:");

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 17

Write a program that takes an integer as an input from the user and prints if it is a prime or composite number.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem17

{

classProblem17

    {

staticvoid Main(string[] args)

        {

int num;

int i = 2;

Console.WriteLine("Enter A Number:");

            num = Convert.ToInt32(Console.ReadLine());

while ( i < num)

            {

 

if (num % i == 0)

                {

Console.WriteLine("Entered Number Is  A Composite  Number.");

 

break;

 

                }

                i++;

            }

if (i == num)

            {

Console.WriteLine("Entered Number Is  A Prime Number.");

            }

if (num==0||num==1)

            {

Console.WriteLine("Entered Number Is Not A Composite  Number Nor A Prime Number.");

            }

Console.ReadKey();

 

        }

    }

}

Output:



Problem 18

Write a program that prints the Fibonacci series using a loop.

1 1 2 3 5 8 13 21 34 …

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem18

{

classProblem18

    {

staticvoid Main(string[] args)

        {

int num;

int  next;

int first=0;

int second=1;

 

Console.WriteLine("Enter The Number Of Terms Of Fibonacci Series You Want:");

            num = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i < num; i++)

            {

 

if (i <= 1)

                {

                    next = i;

 

                }

else

                {

                    next = first + second;

                    first = second;

                    second = next;

                }

Console.Write(next);

Console.Write(" ");

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 19

Write a program using a for loop that prints the following output on the screen.

*
**
***
****
***
**
*

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem19

{

classProblem19

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

for (int i = 0; i < 4; i++)

            {

 

for (int j = 0; j <= i; j++)

                {

 

Console.Write("*");

 

                }

Console.WriteLine("\n");

            }

for (int i = 0; i <= 2; i++)

            {

 

for (int j = 3; j >i; j--)

                {

 

Console.Write("*");

 

                }

Console.WriteLine("\n");

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 20

Write a program using a for loop that prints the following output on the screen.

*
***
*****
*******
*********
***********
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem20

{

classProblem20

    {

staticvoid Main(string[] args)

        {

int num;

int k = 0;

Console.WriteLine("Enter The Value To Draw A Rectangle Shape:");

            num = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i < num; i++)

            {

for (int j = 0; j <= k;j++)

                {

Console.Write("*");

                }

Console.WriteLine("\n");

                k += 2;

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 21

Write a program to print all Armstrong numbers between 1 and 500. If the sum of the 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).
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem21

{

classProblem21

    {

staticvoid Main(string[] args)

        {

int FirstDigit, SecondDigit, LastDigit,num;

Console.WriteLine("All Armstrong Numbers Between 1 And 500.\n");

for (int i = 1; i <= 500; i++)

            {

                num = i;

                LastDigit = num % 10;

                num = num / 10;

                SecondDigit = num % 10;

                num = num / 10;

                FirstDigit = num % 10;

if ((FirstDigit * FirstDigit * FirstDigit) + (SecondDigit * SecondDigit * SecondDigit) + (LastDigit * LastDigit * LastDigit) == i)

                {

Console.WriteLine("This Is A Armstrong Number:" + i);

                }

 

 

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 22

Write a program using a for loop that prints the following output on the screen.

$$$$$$$$$$$
$ $
$ $
$ $
$$$$$$$$$$

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem22

{

classProblem22

    {

staticvoid Main(string[] args)

        {

int check1=1;

int check2=2;

int check3=3;

for (int a = 1; a <= 11; a++)

            {

Console.Write("$");

            }

Console.WriteLine("\n");

for (int b = 1; b <= 3; b++)

            {

for (int c = 1; c <= 1; c++)

                {

Console.Write("$");

                }

if (b == check1)

                {

for (int d = 1; d <= 9; d++)

                    {

Console.Write(" ");

                    }

                }

if (b == check2)

                {

for (int d = 1; d <= 8; d++)

                    {

Console.Write(" ");

                    }

                }

if (b == check3)

                {

for (int d = 1; d <= 8; d++)

                    {

Console.Write(" ");

                    }

                }

 

Console.Write("$"); 

Console.WriteLine("\n");

            }

for (int e = 1; e <= 10; e++)

            {

Console.Write("$");

            }

Console.ReadKey();

        }

    }

}

Output:



Problem 23


Write a program using a loop that takes one value n from the user and shows the factorial of all prime numbers that are less then n, for example if n = 10 then the program will print the factorial of 2, 3, 5 and 7.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem23

{

classProblem23

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

int j;

int num;

int fact=1;

Console.WriteLine("Enter The Number:");

            num = Convert.ToInt32(Console.ReadLine());

 

for (int i = 1; i <= num; i++)

            {

 

for (j = 2; j <i; j++)

                {

 

 

 

if (i % j == 0)

                        {

 

 

goto outloop;

 

                        }

 

                }

                outloop:

if (j == i)

                    {

                        fact = 1;

for(int k=1;k<=i;k++)

                        {

                        fact=fact*k;

                        }

Console.WriteLine("Factorial Of "+i+" Prime Number Is "+fact);

                    }

//if (i == 0 || i == 1)

//{

//    Console.WriteLine("Entered Number Is Not A Composite  Number Nor A Prime Number.");

//}

              }

Console.ReadKey();

            }

        }

 

}

Output:



Problem 24

Write a program to display the sum of the following series using a loop.

1*x + 2*x2 + 3*x3 + 4*x4 + 5*x5 + … + n*xn

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem24

{

classProblem24

    {

staticvoid Main(string[] args)

        {

int num;

double sum=0;

double x;

Console.WriteLine("Enter The Range Value To Display The Sum Like This Series \n1*^x + 2*x^2 + 3*x^3 + 4*x^4 + 5*x^5 + … + n*x^n");

            num = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= num; i++)

            {

                x = (double)i;

 

                sum = sum +(i * Math.Pow(x,x));

            }

Console.WriteLine("\nThe Sum Is :" + sum);

Console.ReadKey();

        }

    }

}

Output:



Problem 25


Write a program to display the sum of the following series, in other words the sum of the factorial of the odd series.

1! + 3! + 5! + 7! + 9! + . . . + n!
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem25

{

classProblem25

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

int num; 

int fact1=1;

int sum=0;

Console.WriteLine("Enter Number To Display The Sum Of The Following Series i.e. sum of the factorial of odd series\n Like This 1! + 3! + 5! + 7! + 9! + . . . + n!");

            num = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= num; i++)

            {

 

if (i % 2 != 0)

                {

                    fact1 = 1;

for (int j = 1; j <= i; j++)

                    {

                        fact1 = fact1 * j;

 

                    }

 

                    sum = sum + fact1;

                }

 

            }

Console.WriteLine("\nThe Sum Of The Following Series: " + sum);

Console.ReadKey();

        }

    }

}

Output:



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)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem26

{

classProblem26

    {

staticvoid Main(string[] args)

        {

int num;

double fact1 = 1;

double temp;

double sum = 0;

Console.WriteLine("Enter Number To Display The Sum Of The Following Series i.e. sum of the factorial of series\n Like This 2/1! + 4/3! + 6/5! + 8/7! + 10/9! + . . . +
n/(n-1)!"
);

            num = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= num; i++)

            {

 

if (i % 2 != 0)

                {

                    fact1 = 1;

for (int j = 1; j <= i; j++)

                    {

                        fact1 = fact1 * j;

 

                    }

 

                }

if (i % 2 == 0)

                {

                    temp = i / fact1;

 

 

                    sum = sum + temp;

                }

 

 

            }

Console.WriteLine("\nThe Sum Of The Following Series: " + sum);

Console.ReadKey();

        }

    }

}

Output:



Problem 27

Write a program to display the sum of the following series, in other words the sum of the factorial of the odd series multiplied by x where the power of x is the square of the corresponding number.

X1*1! + X9*3! + X25*5! + X49*7! + X81*9! + . . . + Xn2*n!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem27

{

classProblem27

    {

staticvoid Main(string[] args)

        {

int num;

int fact1 = 1;

int sum = 0;

 

int x;

int temp;

Console.WriteLine("Enter Number To Display The Sum Of The Following Series i.e.sum of the factorial of odd series multiply with x \n where power of x is square of corresponding number.\nX1*1! + X9*3! + X25*5! + X49*7! + X81*9! + . . . + Xn2*n!");

            num = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= num; i++)

            {

                fact1 = 1;

if (i % 2 != 0)

                {

for (int j = 1; j <= i; j++)

                    {

                        fact1 = fact1 * j;

 

                    }

 

                        x =i*i;

                        temp = x * fact1;

 

                        sum = sum + temp;

 

                }

 

 

            }

Console.WriteLine("\nThe Sum Of The Following Series: " + sum);

Console.ReadKey();

        }

    }

}

Output:



Problem 28

Write a program that displays the following output on the screen.

1 2 3 4 5
1 4 9 16 25
1 8 27 64 125

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem28

{

classProblem28

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

int temp1;

int temp2;

for (int i = 1; i <= 5; i++)

            {

 

Console.Write(i);

Console.Write(" ");

            }

Console.WriteLine("\n");

for (int i = 1; i <= 5; i++)

            {

                temp1 = i * i;

 

Console.Write(temp1);

Console.Write(" ");

            }

Console.WriteLine("\n");

for (int i = 1; i <= 5; i++)

            {

                temp2 = i * i*i;

 

Console.Write(temp2);

Console.Write(" ");

            }

 

Console.ReadKey();

        }

    }

}

Output:



Problem 29

Write a program that displays the following output on the screen.

####$####
###$#$###
##$###$##
#$#####$#
$#######$

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem29

{

classProblem29

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

int firstdollar = 8;

int seconddollar;

int k;

int x = 8;

int space = 0; int val;

int j;

int i = 1;

while (i <= 5)

            {

                j = 4;

                val = x;

                seconddollar = val;

while (j <= val)                            //This loop is for left side triangle

                {

if (j == firstdollar)

                    {

Console.Write("$");

                    }

else

Console.Write("#");

                    j++;

                }

if (i == 1)

                    val--;

                k = 1;

while (k <= space)                          //This loop is for center triangle

                {

Console.Write("#");

                    k++;

                }

 

                space = 2 * i - 1;

while (val >= 4)                            //This loop is for right side triangle

                {

if (val == seconddollar)

                    {

Console.Write("$");

                    }

else

                    {

Console.Write("#");

                    }

 

                    val--;

 

                }

                i++;

                x--;

                firstdollar--;

Console.WriteLine();

 

 

            }

Console.ReadKey();

        }

    }

}

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Problem30

{

classProblem30

    {

staticvoid Main(string[] args)

        {

Console.WriteLine("\t\tName: Ehtesham Mehmood\n\t\tRoll No: 11014119-131\n\t\tSection: AE\n \t\t        UOG\n");

int i = 1;

int x = 71;

int space = 0;

int j;

int k;

int val;

char ch;

char ch1;

while (i <= 7)

            {

                j=65;

                val = x;

while (j <= val)

                {                                            //This loop is for left side triangle

 

                    ch = (char)j;

Console.Write(ch);

                    j++;

                }

if (i == 1)

                    val--;

                k = 1;

while (k <= space)

                {                                            //This loop is for center side triangle

Console.Write("*");

                    k++;

                }

 

                space = 2 * i - 1;

while (val >= 65)                            //This loop is for Right side triangle

                {

                    ch1 = (char)val;

Console.Write(ch1);

                    val--;

 

                }

                i++;

                x--;

Console.WriteLine();

 

            }

Console.ReadKey();

        }

    }

}


 

Up Next
    Ebook Download
    View all
    Learn
    View all