In this article you all are going to learn and remember all the scenarios used by me to learn the programming and achieve the aim so quickly.
So, let's start with the very basic question to print a star pattern.
Q1- Write a Program to print the Sandwich star pattern.
Ans: Here is the answer to this question with the code
Code:
for (int i = 1; i <= 5; i++) // this line tell for-loop to execute it 5 times, this loop is to print upper pyramid of star { for (int j = 0; j < i - 1; j++) // this loop is to print each space between each star(*) in a row { Console.Write(" "); } for (int k = 5; k >= i; k--) // this loop is to print each star(*) in a row { Console.Write("* "); } Console.WriteLine(""); // this line breaks the line to the new row } for (int i = 1; i <= 5; i++) // this loop is executed 5 times, and it is used to print the lower pyramid { for (int j = 4; j >= i; j--) // this loop again print the space between each star(*) in a row. { Console.Write(" "); } for (int k = 1; k <= i; k++) // this loop is to print each star(*) in a row { Console.Write("* "); } Console.WriteLine(""); } Console.ReadLine(); // ReadLine() is used to hold the output window untill user presses enter or return key
Star Sandwich Pattern |
Q2- Write a Program to print a Left aligned Pyramid.
Ans: Here is the answer to this question with the code
Code:
for (int i = 1; i <= 5; i++) // this line tell for-loop to execute it 5 times, this loop is to print upper pyramid of star { for (int j = 5; j >= i; j--) // this loop is to print each star(*) in a row, decreases stars(*) as row increases { Console.Write("*"); } Console.WriteLine(""); } for (int i = 0; i < 5; i++) { for (int j = 0; j <= i; j++) // this loop is to print each star(*) in a row, increases stars(*) as row increases { Console.Write("*"); } Console.WriteLine(""); } Console.ReadLine();
Output:
Star pattern for Left Pyramid |
Q3- Write a Program to reverse a string without using predefined methods of C#.
Ans: Here is the answer to this question with the code
Code:
Console.WriteLine("Please Enter the String"); // this line prints the message to enter a string to be reversed. string input = Console.ReadLine(); // this line is to accept the input from user and store it in a input named variable. char[] rev = new char[input.Length]; // here, i have initialized a variable to type char Array of length of the input string. for (int i = 0; i < input.Length; i++) // this loop runs from 0th index to the (n-1)th index of the string { rev[input.Length - 1 - i] = input[i]; // here, the first character in the input string is stored at the last index of char Array } Console.WriteLine(rev); // this line prints the reversed string Console.ReadLine(); // ReadLine() is used to hold the output window untill user presses enter or return key
Output:
Reversing a string without using predefined methods |
Q4- Write a Program to print a Factorial of a number.
Ans: Here is the answer to this question with the code
Code:
Console.WriteLine("Please Enter the Number"); int number = Convert.ToInt32(Console.ReadLine()); int fact = 1; if (number == 0) { Console.WriteLine("factorial is 1"); } else { for (int i = number; i >= 1; i--) { fact = fact * i; } Console.WriteLine("Factorial is {0}", fact); } Console.ReadLine();
Output:
Factorial of a number |
Q5- Write a Program to sort an Integer Array.
Ans: Here is the answer to this question with the code
Code:
int[] a = { 2, 1, 8, 6, 4 }; for (int i = 0; i < a.Length; i++) { for (int j = i + 1; j < a.Length; j++) { if (a[i] > a[j]) { a[i] = a[i] + a[j]; a[j] = a[i] - a[j]; a[i] = a[i] - a[j]; } } } for (int i = 0; i < a.Length; i++) Console.WriteLine(a[i]); Console.ReadLine();
Output:
Sorting an Integer Array |
Ans: Here is the answer to this question with the code
Code:
Console.WriteLine("Enter First Number"); int a = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter Second Number"); int b = Convert.ToInt32(Console.ReadLine()); if (b > a) { a = a + b; b = a - b; a = a - b; } Console.WriteLine("\nSwapped numbers are {0} and {1}", a, b); Console.ReadLine();
Output:
swaping two numbers |
Ans: Here is the answer to this question with the code
Code:
Console.WriteLine("How many numbers do you want in the fibonacci series"); int Target = int.Parse(Console.ReadLine()); int PreviousNumber = -1, NextNumber = 1; for (int i = 0; i < Target; i++) { int Sum = PreviousNumber + NextNumber; PreviousNumber = NextNumber; NextNumber = Sum; Console.Write(NextNumber + " "); } Console.ReadLine();
Output:
Fibonacci Series |
Ans: Here is the answer to this question with the code
Code:
Console.WriteLine("Please Enter the Number"); int num = Convert.ToInt32(Console.ReadLine()); int count = 0; for (int i = 1; i <= num; i++) { if (num % i == 0) count++; } if (count == 2) { Console.WriteLine("This is the Prime Number"); } else { Console.WriteLine("This is not the Prime Number"); } Console.ReadLine();
Output:
Program to check whether a number is prime or not |
Ans: Here is the answer to this question with the code
Code:
Console.WriteLine("Please Enter the last number"); int num = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i <= num; i++) { if (i % 2 == 0) { Console.Write(i + " "); } } Console.ReadLine();
Output:
Program to print even numbers |
Ans: Here is the answer to this question with the code
Code:
for (char i = 'a'; i <= 'z'; i++) { Console.Write(i + " "); } Console.WriteLine(""); for (char j = 'A'; j <= 'Z'; j++) { Console.Write(j + " "); } Console.ReadLine();
Output:
Alphabets using Char variable |
Ans: Here is the answer to this question with the code
Code:
for (int i = 0; i < 26; i++) { Console.Write(Convert.ToChar(i + (int)'a') + " "); } Console.WriteLine(""); for (int i = 0; i < 26; i++) { Console.Write(Convert.ToChar(i + (int)'A') + " "); } Console.ReadLine();
Output:
Alphabets using ASCII values |
Ans: Here is the answer to this question with the code
Code:
int[] arr = { 1, 2, 3, 4, 5 }; for (int i = 0; i < arr.Length * 2; i++) { if (i < arr[arr.Length - 1]) { Console.Write(arr[i]); } else { Console.Write(arr[(arr.Length * 2 - i) - 1]); } } Console.ReadKey();
Output:
Array in forward and backward order |
Ans: Here is the answer to this question with the code
Code:
string Sentance = "welcome to c# programming"; string[] word = Sentance.Split(' '); string reversSentance = ""; int length = word.Length - 1; while (length >= 0) { reversSentance += word[length] + " "; length--; } Console.WriteLine(reversSentance); Console.ReadKey();
Output:
Reversing a sentence |
Ans: Here is the answer to this question with the code
Code:
int n = 12345; //input int rem = 0; int revers = 0; while (n > 0) { rem = n % 10; revers = revers * 10 + rem; n = n / 10; } Console.WriteLine($"Revers Number Is:={revers}"); Console.ReadKey();
Output:
Revers an integer Number |
Ans: Here is the answer to this question with the code
Code:
string s = "welcome"; string rev = string.Empty; int length = s.Length - 1; while (length >= 0) { rev += s[length]; length--; } Console.WriteLine($"Actual String:={s}"); Console.WriteLine($"Reversed String:={rev}"); if (s == rev) { Console.WriteLine("String is palindrome "); } else { Console.WriteLine("String is not palindrome "); } Console.ReadKey();
Output:
String is palindrome or not |
Ans: Here is the answer to this question with the code
Code:
string s = "google"; string encounter = ""; string result = ""; foreach (char c in s) { if (encounter.IndexOf(c) == -1) { encounter += c; result += c; } } Console.WriteLine($"Actual string is:= {s}"); Console.WriteLine($"Result is:= {result}"); Console.ReadKey();
Output:
Removing Duplicate Characters |
Ans: Here is the answer to this question with the code
Code:
int n = 12345; //input int rem = 0; int revers = 0; int sum = 0; while (n > 0) { rem = n % 10; revers = revers * 10 + rem; sum = sum + rem; n = n / 10; } Console.WriteLine($"sum of an integer Number Is:= {sum}"); Console.ReadKey();
Output:
Sum of an integer Number |
Ans: Here is the answer to this question with the code
Code:
string s = "Abhishek Tomer"; int x = 0; foreach (char c in s) { Console.Write(s[x]); x++; } Console.WriteLine($" result is:= {x}"); Console.ReadKey();
Output:
Length Of String |
Ans: Here is the answer to this question with the code
Code:
string s = "welcome to c#"; string rev = string.Empty; int length = s.Length - 1; for (int i = 0; i <= length; i++) { if (s[i] == ' ') { rev += ""; } else { rev += s[i]; } } Console.WriteLine($"Actual String:={s}"); Console.WriteLine($"String after remove space:={rev}"); Console.ReadKey();
Output:
Remove Spaces From String |
So, Guys this was all about the interview programming questions in C#.
I hope you have enjoyed this post.
Please share this post with your friends and colleagues to help them clearing the interview.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.