0% found this document useful (0 votes)
10 views

Assignment 1

Uploaded by

rupams2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Assignment 1

Uploaded by

rupams2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Create a class "Product" having 2 data members ProductId (int) and ProductName

(string)
Create a class "Purchase" having 2 data members ProductId (int) and
PurchaseQuantity (int)
Create a class "Sales" having 2 data members ProductId (int) and SalesQuantity
(int)
In Program.cs create objects of all 3 classes. Accept data to all of them.
Calculate StockQuantity (i.e. StockQuantity = PurchaseQuantity - SalesQuantity )
Display : ProductId, ProductName and StockQuantity
===================================================================================
========
public static void Main()
{
Product product = new Product();
Purchase purchase = new Purchase();
Sales sales = new Sales();

Console.Write("Enter ProductId: ");


product.ProductId = int.Parse(Console.ReadLine());

Console.Write("Enter ProductName: ");


product.ProductName = Console.ReadLine();

Console.Write("Enter Purchase ProductId: ");


purchase.ProductId = int.Parse(Console.ReadLine());

Console.Write("Enter Purchase Quantity: ");


purchase.PurchaseQuantity = int.Parse(Console.ReadLine());

Console.Write("Enter Sales ProductId: ");


sales.ProductId = int.Parse(Console.ReadLine());

Console.Write("Enter Sales Quantity: ");


sales.SalesQuantity = int.Parse(Console.ReadLine());

int availableStock= purchase.PurchaseQuantity - sales.SalesQuantity;

Console.WriteLine("Product:");
Console.WriteLine($"ProductId: {product.ProductId}");
Console.WriteLine($"ProductName: {product.ProductName}");

Console.WriteLine("Available Stock:");
Console.WriteLine($"StockQuantity: {availableStock}");

Console.ReadKey();
}

public class Product


{
public int ProductId { get; set; }
public string ProductName { get; set; }
}

public class Purchase


{
public int ProductId { get; set; }
public int PurchaseQuantity { get; set; }
}
public class Sales
{
public int ProductId { get; set; }
public int SalesQuantity { get; set; }
}

O/P:
Enter ProductId: 101
Enter ProductName: Pens
Enter Purchase ProductId: 101
Enter Purchase Quantity: 100
Enter Sales ProductId: 101
Enter Sales Quantity: 25

Product Information:
ProductId: 101
ProductName: Pens

Stock Information:
StockQuantity: 75
===================================================================================
=========================================
Fibonacci Series upto 15 elements //
=================================
void Main()
{
int a,b,c,n;
Console.WriteLine("Enter the number of terms:");
n = int.Parse(Console.ReadLine());
a=0;
b=1;
Console.Write($"{a} ");
Console.Write($"{b} ");
for(int i=1;i<=n;i++)
{
c=a+b;
a=b;
b=c;
Console.Write($"{c} ");
}
}
o/p:
Enter the number of terms:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
===================================================================================
==========================================
Accept a number of 4 digit. Find reverse of that number. Multiply the reverse
number with a separately accepted integer value and display the result.
Number :4526
Reverse Number:6254
Multiply reverse number with another input integer value : 6254 * 5

public static void DigitReverse()


{
string result = "";
Console.WriteLine("Enter a 4-digit number: ");
int num = Convert.ToInt32(Console.ReadLine());
int x = num;
for(int i = 1;i <= 4; i++)
{
int digit = num % 10;
num = num / 10;
result += digit.ToString();
}
Console.WriteLine($"Reverse of {x} is : {result}");
Console.WriteLine("Enter a number to multiply with reverse number:");
int num1 = Convert.ToInt32(Console.ReadLine());
num = int.Parse(result) * num1;
Console.Write($"Multiplying {result} with {num1}: {num}");
}
O/P:
Enter a 4-digit number:
4526
Reverse of 4526 is : 6254
Enter a number to multiply with reverse number:
5
Multiplying 6254 with 5: 31270
===================================================================================
=================================================
Using switch case solve the following:
Accept the name of SHAPE (option: 1 for Circle, 2 for Cylinder).
In case of circle user should be asked to enter RADIUS, and then ask whether to
find AREA / DIAMETER and then calculate and display.
In case of cylinder, user should be asked whether BASE AREA / VOLUME. If base area
then ask radius, and if volume then ask radius and height to enter. Calculate
and display the same as per option.
After displaying above result ask user whether any more SHAPE to check? If "yes"
then repeat the above process.

public static void CalculateAreaOfShape()


{
go:
Console.WriteLine("Choose a shape below to caluate its area: ");
Console.WriteLine("===========================================");
Console.WriteLine("Circle");
Console.WriteLine("Cylinder");

string shapeName = Console.ReadLine();


switch(shapeName.ToLower())
{
case "circle":
Console.WriteLine("Enter radius of a Circle:");
float radiusOfCircle = Convert.ToSingle(Console.ReadLine());

Console.WriteLine("What do you want to calculate: ");

Console.WriteLine("===========================================");
Console.WriteLine("Area");
Console.WriteLine("Diameter");

string calculationType = Console.ReadLine();


if(calculationType=="area")
{
double areaOfCircle = 3.14 * radiusOfCircle *
radiusOfCircle;
Console.WriteLine($"Area of circle with radius
{radiusOfCircle} is {areaOfCircle}");
}
else if(calculationType=="diameter")
{
double diameterOfCircle = 2 * radiusOfCircle;
Console.WriteLine($"Diameter of circle with radius
{radiusOfCircle} is {diameterOfCircle}");
}
else
{
Console.WriteLine("Input must be Area or Diameter");
}
break;
case "cylinder":
Console.WriteLine("What do you want to calculate: ");

Console.WriteLine("===========================================");
Console.WriteLine("BaseArea");
Console.WriteLine("Volume");
string calculationType1="";
calculationType1 = Console.ReadLine();
if (calculationType1 == "basearea")
{
Console.WriteLine("Enter radius of a Cylinder:");
float radiusOfCylinder =
Convert.ToSingle(Console.ReadLine());

double areaOfCylinder = 3.14 * radiusOfCylinder *


radiusOfCylinder;
Console.WriteLine($"Base Area of Cylinder with radius
{radiusOfCylinder} is {areaOfCylinder}");
}
else if(calculationType1 == "volume")
{
Console.WriteLine("Enter radius of a Cylinder:");
float radiusOfCylinder =
Convert.ToSingle(Console.ReadLine());
Console.WriteLine("Enter height of a Cylinder:");
float heightOfCylinder =
Convert.ToSingle(Console.ReadLine());

double areaOfCylinder = 2 * 3.14 * radiusOfCylinder *


radiusOfCylinder + 2 * 3.14 * radiusOfCylinder * heightOfCylinder;
Console.WriteLine($"Volume of Cylinder with radius
{radiusOfCylinder} and height {heightOfCylinder} is {areaOfCylinder}");
}
else
{
Console.WriteLine("Input must be BaseArea or Volume");
}
break;
}
Console.WriteLine("Do you want to check any more SHAPE enter yes . ");

if (Console.ReadLine() != "yes")
{
Console.WriteLine("Thank you");
Console.ReadKey();
}
else
{
goto go;
}
}
O/P:1
Choose a shape below to caluate its area:
===========================================
Circle
Cylinder
cylinder
What do you want to calculate:
===========================================
BaseArea
Volume
volume
Enter radius of a Cylinder:
5
Enter height of a Cylinder:
6
Volume of Cylinder with radius 5 and height 6 is 345.4
Do you want to check any more SHAPE enter yes .
yes
Choose a shape below to caluate its area:
===========================================
Circle
Cylinder
cylinder
What do you want to calculate:
===========================================
BaseArea
Volume
basearea
Enter radius of a Cylinder:
5
Base Area of Cylinder with radius 5 is 78.5
Do you want to check any more SHAPE enter yes .
yes
Choose a shape below to caluate its area:
===========================================
Circle
Cylinder
circle
Enter radius of a Circle:
5
What do you want to calculate:
===========================================
Area
Diameter
area
Area of circle with radius 5 is 78.5
Do you want to check any more SHAPE enter yes .
yes
Choose a shape below to caluate its area:
===========================================
Circle
Cylinder
circle
Enter radius of a Circle:
5
What do you want to calculate:
===========================================
Area
Diameter
diameter
Diameter of circle with radius 5 is 10
Do you want to check any more SHAPE enter yes .
no
Thank you
===================================================================================
==============
Accept a 3 digit number and check whether that number is an armstrong number?
public static void ArmstrongNumberTest()
{
int n, r, sum = 0, temp;
n = Convert.ToInt32(Console.ReadLine());
temp = n;
while(n > 0)
{
r = n % 10;
sum = sum + r * r * r;
n = n / 10;
}
if (temp == sum)
{
Console.WriteLine($"{temp} is an ArmStrong number");
}
else
{
Console.WriteLine($"{temp} is not an ArmStrong number");
}
}
o/p:
153
153 is an ArmStrong number
===================================================================================
========================
Position of number in an array?

public static void Array_Number_Position()


{
int[] arr = new int[6] { 45, 2, 65, 87, 24, 65 };
Console.WriteLine("enter a number: ");
int n = Convert.ToInt32(Console.ReadLine());
int flag = 0; int k = 0;

foreach (int num in arr)


{
if (num == n)
{
flag++;
if (flag == 1)
Console.WriteLine($"Number {n} is in the position");
Console.WriteLine(k);
}
k++;
}
if (flag == 0)
{
Console.WriteLine("Number not found");
}
}
O/P:
enter a number:
65
Number 65 is in the position
2
5
===================================================================================
==========================
Largest number in an array?

public static void Array_Max_1()


{
int[] arr = new int[6] { 45, 2, 65, 87, 24, 65 };
int n = 0;
foreach (int num in arr)
{
if (num > n)
{
n = num;
}
}
Console.WriteLine($"Max num in the array is: {n}");
}
O/P:
Max num in the array is: 87
===================================================================================
=========================
Largest number in an array using for loop

public static void Array_Max_2()


{
int[] arr = new int[6] { 45, 2, 65, 87, 24, 65 };
int temp = 0; int i;
for (i = 0; i < 5; i++)
{
if (arr[i] > arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
Console.WriteLine($"Max number in the array : {arr[i]} ");
}
O/P:
Max number in the array : 87
===================================================================================
====================
Array Ascending

public static void Array_Ascending()


{
int[] arr = new int[6] { 145, 2, 265, 87, 24, 15 };
int temp = 0; int i;
for (int j = 5; j >= 1; j--)
{
for (i = 0; i < 5; i++)
{
if (arr[i] > arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
Console.WriteLine($"Array in Ascending order:");
foreach (int k in arr)
{
Console.WriteLine($"{k} ");
}
}
O/P:
Array in Ascending order:
2
15
24
87
145
265
===================================================================================
==========================
Array Desecending
public static void Array_Descending()
{
int[] arr = new int[6] { 145, 2, 265, 87, 24, 15 };
int temp = 0; int i;

for (int j = 5; j >= 1; j--)


{
for (i = 0; i < 5; i++)
{
if (arr[i] < arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
Console.WriteLine($"Array in Descending order:");
foreach (int k in arr)
{
Console.WriteLine($"{k} ");
}
}
O/P:
Array in Descending order:
265
145
87
24
15
2
===================================================================================
======================
Check prime , not prime and neutral numbers in a given array

private static bool CheckPrime(int num)


{
bool b = false;
for (int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
b = true;
break;
}
}
return b;
}

=============================================
public static void FindPrimeNumbersInArray()
{
System.Diagnostics.Stopwatch myStopWatch = new
System.Diagnostics.Stopwatch();
myStopWatch.Start();

bool isPrime = false;

int[] arr = new int[10] { 251, 1, 241, 90, 277, 1, 132, 38, 199, 37 };

//int[] arr = new int[1000];


//int j;

//for (j = 0; j <= 999; j++)


//{
// arr[j] = j + 1;
//}

for (int i=0;i<=9;i++)


{
if(arr[i]==1)
{
Console.WriteLine($"{arr[i]} is neutral");
}
else
{
isPrime = CheckPrime(arr[i]);
if(isPrime)
{
Console.WriteLine($"{arr[i]}\t is not a prime");
}
else
{
Console.WriteLine($"{arr[i]}\t is a prime");
}
}
}
myStopWatch.Stop();
Console.WriteLine($"Time taken :
{myStopWatch.Elapsed.Milliseconds.ToString()}");
}

O/P:

251 is a prime
1 is neutral
241 is a prime
90 is not a prime
277 is a prime
1 is neutral
132 is not a prime
38 is not a prime
199 is a prime
37 is a prime
Time taken : 3

===================================================================================
======================

Declare a nxn matrix, accept values into it and Display all prime numbers available
in array.

private static bool CheckPrime(int num)


{
bool b = false;
for (int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
b = true;
break;
}
}
return b;
}
==================================================
public static void Array_SquareMatrix_NxN_Test()
{
int n; int i; int j = 0;
int flag = 0; int numArray = 0;

Console.WriteLine("Enter a number for Square matrix: ");


n = Convert.ToInt32(Console.ReadLine());

int[,] arr = new int[n, n];


for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
arr[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine();
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
numArray = arr[i, j];
flag = Convert.ToInt32(CheckPrime(numArray));
if (flag == 0)
{
Console.WriteLine($"{arr[i, j]} is a prime");
}
}
}
}
O/P:

Enter a number for Square matrix:


4
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
7 8 9 10
11 12 13 14
15 16 17 18
19 20 21 22
7 is a prime
11 is a prime
13 is a prime
17 is a prime
19 is a prime
===================================================================================
=================================
Declare a nxn matrix, accept values into it and Display only left diagnol values.

public static void Array_SquareMatrix_NxN_Diagnol_Test()


{
int n; int i; int j;

Console.WriteLine("Enter a number for Square matrix: ");


n = Convert.ToInt32(Console.ReadLine());

int[,] arr = new int[n, n];


for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
arr[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine();
Console.Write($"The diagnol elements are :");
for (i = 0; i < n; i++)
Console.Write($"{arr[i, i]} ");
}
O/P:
Enter a number for Square matrix:
3
1
2
3
4
5
6
7
8
9
1 2 3
4 5 6
7 8 9

The diagnol elements are :1 5 9

You might also like