FAZAIA COLLEGE OF EDUCATION FOR WOMEN
Assignment no II
Visual Basic Programming
Department: BSCS-V
Submitted to: Ma’am Ayesha
Submitted by: Rushna Sohail
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)
{
int o, num, sum = 0;
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
for (o = 1; o <= num; o += 2)
{
sum += o;
}
Console.WriteLine("Sum of all odd numbers between 1 to " + num + " = " + sum);
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 :");
for (iLoop = 1; iLoop <= num; iLoop++)
{
if (number % iLoop == 0)
{
Console.Write(iLoop + " ");
}
}
}
static void Main(string[] args)
{
int num = 0;
Console.Write("Enter a number: ");
num = int.Parse(Console.ReadLine());
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");
Console.Write("Input Value : ");
n= Convert.ToInt32(Console.ReadLine());
Console.Write("\nThe first {0} natural number are :\n",n);
for(i=1;i<=n;i++)
{
Console.Write("{0} ",i);
sum+=i;
}
Console.Write("\nThe Sum of Natural Number upto {0} terms : {1} \n",n,sum);
}
}
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());
Console.Write("Enter any number: ");
exponent = Convert.ToInt32(Console.ReadLine());
power = 1;
for (i = 1; i <= exponent; i++)
power = power * basenumber;
Console.Write("Power : " + power);
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();
}
private static int Check_Prime(int num)
{
int i;
for (i = 2; i <= num- 1; i++)
{
if (num % i == 0)
{
return 0;
}
}
if (i == num)
{
return 1;
}
return 0;
}
}
}
Output:
l) Write a C# program to find all prime factors of a number.
Code:
using System;
namespace prime
{
public class GFG
{
public static void prime(int n)
{
while (n % 2 == 0)
{
Console.Write(2 + " ");
n /= 2;
}
for (int i = 3; i <= Math.Sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
Console.Write(i + " ");
n /= i;
}
}
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());
for (i = 2; i <= n; i++)
{
k = 2;
p = 1;
while (k < i)
{
if (i % k == 0)
{
p = 0;
break;
}
k++;
}
if (p == 1)
{
s = s + i;
}
}
Console.WriteLine("Sum of prime numbers:" + s);
}
}
OUTPUT: