Fazaia College of Education For Women Assignment No II Visual Basic Programming
Fazaia College of Education For Women Assignment No II Visual Basic Programming
Assignment no II
Visual Basic Programming
Department: BSCS-V
Dated: 07-01-2021
a) Write a C# program to print all even numbers between 1 to 100 using while loop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace natural
{
class Program
{
static void Main(string[] args)
{
int i = 0;
Console.Write("Display all even numbers from 1 to 100:\n");
while (i <= 100)
{
i += 2;
Console.Write("{0} ", i);
}
Console.WriteLine();
}
}
}
b) Write a C# program to find sum of all odd numbers between 1 to n.
Code:
using System;
public class program
{
static void Main(string[] args)
{
Console.ReadLine();
}
}
Output:
c) Write a C# program to find all factors of a number.
Code:
using System;
class Factors
{
static void PrintFactors(int num)
{
int iLoop = 0;
Console.WriteLine("The all factors of " + num + " are :");
PrintFactors(num);
Console.WriteLine();
}
}
Output:
d) Write a C# program to enter a number and print its reverse.
Code:
using System;
public class Reverse
{
public static void Main(string[] args)
{
int n, reverse = 0, rem;
Console.Write("Enter a number: ");
n = int.Parse(Console.ReadLine());
while (n != 0)
{
rem = n % 10;
reverse = reverse * 10 + rem;
n /= 10;
}
Console.Write("Reversed Number: " + reverse);
}
}
Output:
e) Write a C# program to print multiplication table of any number.
Code:
using System;
public class table
{
public static void Main()
{
int i, n;
Console.Write("Display the multiplication table:\n");
Console.Write("Input the number: ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("\n");
for (i = 1; i <= 10; i++)
{
Console.Write("{0} X {1} = {2} \n", n, i, n * i);
}
}
}
Output:
f) Write a C# program to find sum of all natural numbers between 1 to n.
Code:
using System;
public class natural
{
public static void Main()
{
int i,n,sum=0;
Console.Write("\n\n");
Console.Write("Display n terms of natural number and their sum:\n");
Console.Write("\n\n");
Output:
g) Write a C# program to print all alphabets from a to z. - using while loop
Program:
using System;
namespace a_to_z
{
class Program
{
static void Main(string[] args)
{
char ch = 'a';
Console.Write("Alphabets from a - z are: \n");
while (ch <= 'z')
{
Console.Write("{0} ", ch);
ch++;
}
Console.ReadKey();
}
}
}
Output:
h) Write a C# program to find LCM of two numbers.
PROGRAM:
using System;
public class LCM
{
public static void Main()
{
int i, n1, n2, max, lcm = 1;
Console.Write("Determine the LCM of two numbers:\n");
Console.Write("Input 1st number for LCM: ");
n1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input 2nd number for LCM: ");
n2 = Convert.ToInt32(Console.ReadLine());
max = (n1 > n2) ? n1 : n2;
for (i = max; ; i += max)
{
if (i % n1 == 0 && i % n2 == 0)
{
lcm = i;
break;
}
}
Console.Write("\nLCM of {0} and {1} = {2}\n\n", n1, n2, lcm);
}
}
Output:
i) Write a C# program to calculate factorial of a number.
PROGRAM:
using System;
public class Factorial
{
public static void Main(string[] args)
{
int i, fact = 1, num;
Console.Write("Enter any Number: ");
number = int.Parse(Console.ReadLine());
for (i = 1; i <= num; i++)
{
fact = fact * i;
}
Console.Write("Factorial of " + num + " is: " + fact);
}
}
Output:
j) Write a C# program to find power of a number using for loop.
Code:
using System;
public class powernum
{
static void Main(string[] args)
{
int basenumber, exponent, i, power;
Console.Write("Enter any number: ");
basenumber = Convert.ToInt32(Console.ReadLine());
power = 1;
for (i = 1; i <= exponent; i++)
power = power * basenumber;
Console.ReadLine();
}
}
Output:
k) Write a C# program to check whether a number is Prime number or not.
Code:
using System;
namespace Prime
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number");
int num = Convert.ToInt32(Console.ReadLine());
int result = Check_Prime(number);
if (result == 0)
{
Console.WriteLine("{0} is not a prime number", num);
}
else
{
Console.WriteLine("{0} is a prime number", num);
}
Console.Read();
}
Output:
Code:
using System;
namespace prime
{
public class GFG
{
public static void prime(int n)
{
while (n % 2 == 0)
{
Console.Write(2 + " ");
n /= 2;
}
if (n > 2)
Console.Write(n);
}
public static void Main()
{
int n = 415;
primeFactors(n);
}
}
}
OUTPUT:
m) Write a C# program to find the sum of all prime numbers between 1 to n.
PROGRAM:
using System;
public class Program
{
public static void Main(string[] args)
{
int n, i, p, k, s = 0;
Console.Write("Enter a number:");
n = Convert.ToInt32(Console.ReadLine());