Today i am going to tell you some of the basic programming questions that are frequently asked in interview for fresher and experienced as well.
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:
Here comes another question to print the star pattern in the different form
Q2- Write a Program to print a Left aligned Pyramid.
Ans: Here is the answer to this question with the code
Code:
Output:
This is the very important question for freshers to learn, and this question is asked 9 out of 10 interviews.
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:
Output:
So here are more similar questions as these are also asked in the interview
Q4- Write a Program to print a Factorial of a number.
Ans: Here is the answer to this question with the code
Code:
Output:
Q5- Write a Program to sort an Integer Array.
Ans: Here is the answer to this question with the code
Code:
Output:
Q6- Write a Program to swap two numbers without using third variable.
Ans: Here is the answer to this question with the code
Code:
Output:
Q7- Write a Program to print Fibonacci Series.
Ans: Here is the answer to this question with the code
Code:
Output:
Q8- Write a Program to check whether a number is prime or not.
Ans: Here is the answer to this question with the code
Code:
Output:
Q9- Write a Program to print even numbers up to the entered number
Ans: Here is the answer to this question with the code
Code:
Output:
Q10- Write a Program to print small and capital Alphabets using Char variable.
Ans: Here is the answer to this question with the code
Code:
Output:
Q11- Write a Program to print small and capital Alphabets using ASCII values.
Ans: Here is the answer to this question with the code
Code:
Output:
Q12- Write a program to print all the elements of an array in forward and backward order using single loop. ARRAY is 1,2,3,4,5 output should be 1,2,3,4,5,5,4,3,2,1.
Ans: Here is the answer to this question with the code
Code:
Output:
Q13- Write a program to Revers a sentence word without any built-in function.
Ans: Here is the answer to this question with the code
Code:
Output:
Q14- Write a program to Revers an integer Number.
Ans: Here is the answer to this question with the code
Code:
Output:
Q15- Write a program to Check given string is palindrome or not.
Ans: Here is the answer to this question with the code
Code:
Output:
Q16- Write a program to Remove Duplicate Characters from a given string.
Ans: Here is the answer to this question with the code
Code:
Output:
Q17- Write a program to Sum of an integer Number.
Ans: Here is the answer to this question with the code
Code:
Output:
Q18- Write a program to Find Length Of String Without using Length Function.
Ans: Here is the answer to this question with the code
Code:
Output:
Q19- Write a program to Remove Spaces From String.
Ans: Here is the answer to this question with the code
Code:
Output:
Summary:
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 😉
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 😉