Article

C# and VB.net Interview Programs

In this article we will learn about some of the frequently asked C# and VB.Net Interview Programs in technical interviews. Let’s see the list of top C# programs generally asked in every interview.

Fibonacci series means, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 etc. The first two numbers of Fibonacci series are 0 and 1. Like
0+1=1, 1+1=2, 1+2=3
Here you can see next number is sum of previous two number.

Code in C#
[php]
using System;

namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
int num1 = 0, num2 = 1, num3, i, number;
Console.Write("Enter the number of elements: ");
//i have assign num1 and num2 for printing 0 and 1
Console.Write(num1 + " " + num2 + " ");
//loop will begin from 2 because 0 and 1 are already printed
for (i = 2; i < number; ++i)
{
num3 = num1 + num2;
Console.Write(num3 + " ");
num1 = num2;
num2 = num3;
}
}
}
}

[/php]

Output

Fibonacci Series in VB.Net

[php]
Imports System

Namespace ConsoleApp3
Class Program
Private Shared Sub Main(ByVal args As String())
Dim num3, i, number As Integer, num1 As Integer = 0, num2 As Integer = 1
Console.Write("Enter the number of elements: ")
Console.Write(num1 & " " & num2 & " ")

For i = 2 To number – 1
num3 = num1 + num2
Console.Write(num3 & " ")
num1 = num2
num2 = num3
Next

End Sub
End Class
End Namespace

[/php]
Output

2. Prime Number Program in C#
A prime number is a number that is divisible only by 1 and itself.
For example :
5 is a prime number as it is divisible by 1 and 5.
6 is not a prime number as it is divisible by 1,2,3 and 6.

Program :Prime Number Program in C#

[php]
using System;

namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
int number;
int count = 0;
Console.Write("Check whether a number is Prime or not :" );
Console.WriteLine("—————————————");
Console.WriteLine();
Console.Write("Enter an number : ");

for (int i = 1; i <= number; i++)
{
if (number % i == 0)
count += 1;
}

if (count == 2)
Console.WriteLine(number+ " is a Prime Number.");
else
Console.WriteLine(number+ " is not a Prime Number.");

}
}
}

[/php]

Output

Prime Number Program in VB.Net

[php]
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Namespace ConsoleApp1
Class Program
Private Shared Sub Main(ByVal args As String())
Dim number As Integer
Dim count As Integer = 0
Console.Write("Check whether a number is Prime or not :" & vbLf)
Console.Write("—————————————")
Console.Write(vbLf & vbLf)
Console.Write("Enter an number : ")

For i As Integer = 1 To number

If number Mod i = 0 Then
count += 1
End If
Next

If count = 2 Then
Console.WriteLine("{0} is a Prime Number." & vbLf, number)
Else
Console.WriteLine("{0} is not a Prime Number." & vbLf, number)
End If

End Sub
End Class
End Namespace

[/php]

Result :