0% found this document useful (0 votes)
8 views8 pages

Assignment - 3 Program Solution

The document contains a series of C# programming assignments that include tasks such as multiplication of two numbers, calculating the area of a square, reversing a number, and finding the factorial of a number. Each task is accompanied by a complete C# code solution demonstrating various programming concepts. The assignments also cover topics like matrix multiplication, Armstrong numbers, string length, and student details management.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Assignment - 3 Program Solution

The document contains a series of C# programming assignments that include tasks such as multiplication of two numbers, calculating the area of a square, reversing a number, and finding the factorial of a number. Each task is accompanied by a complete C# code solution demonstrating various programming concepts. The assignments also cover topics like matrix multiplication, Armstrong numbers, string length, and student details management.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment 3

Chapter no 3- Introduction to C#
Q.2 4 Mark
1. Write a program in c#.net for multiplication of two numbers.
Multiplication of Two Numbers
Answer:
using System;
class Program
{
static void Main()
{
int a, b, mul;
[Link]("Enter first number: ");
a = Convert.ToInt32([Link]());
[Link]("Enter second number: ");
b = Convert.ToInt32([Link]());

mul = a * b;
[Link]("Multiplication = " + mul);
[Link]();
}
}
2. Write a program in c#.net to calculate area of Square.
Answer:
using System;
class Program
{
static void Main()
{
double side, area;
[Link]("Enter side of square: ");
side = [Link]([Link]());

area = side * side;


[Link]("Area of Square = " + area);
[Link]();
}
}
3. Write a program in c# .net for sum of two number.
Answer:
using System;
class Program
{
static void Main()
{
int a, b, sum;
[Link]("Enter two numbers: ");
a = Convert.ToInt32([Link]());
b = Convert.ToInt32([Link]());

sum = a + b;
[Link]("Sum = " + sum);
[Link]();
}
}
4. Write a program in c# .net to reverse given number.
Answer:
using System;
class Program
{
static void Main()
{
int num, rev = 0, rem;
[Link]("Enter number: ");
num = Convert.ToInt32([Link]());

while (num != 0)
{
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}

[Link]("Reverse Number = " + rev);


[Link]();
}
}
5. Write a program in c# .net to swap two number.
Answer:
using System;
class Program
{
static void Main()
{
int a, b, temp;
[Link]("Enter first number: ");
a = Convert.ToInt32([Link]());
[Link]("Enter second number: ");
b = Convert.ToInt32([Link]());

temp = a;
a = b;
b = temp;

[Link]("After Swapping: a = " + a + " b = " + b);


[Link]();
}
}
6. Write a program in c# .net for multiplication of matrix.
Answer:
using System;
class Program
{
static void Main()
{
int[,] a = { {1,2}, {3,4} };
int[,] b = { {5,6}, {7,8} };
int[,] c = new int[2,2];

for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c[i,j] = 0;
for(int k=0;k<2;k++)
{
c[i,j] += a[i,k] * b[k,j];
}
[Link](c[i,j] + " ");
}
[Link]();
}
[Link]();
}
}
7. Write a program in c#.net to create function to find factorial of given number.
Answer:
using System;
class Program
{
static int Factorial(int n)
{
if (n == 0)
return 1;
else
return n * Factorial(n - 1);
}

static void Main()


{
int num;
[Link]("Enter number: ");
num = Convert.ToInt32([Link]());

[Link]("Factorial = " + Factorial(num));


[Link]();
}
}
8. Write C# program to find Armstrong number of given number.
Answer:
using System;
class Program
{
static void Main()
{
int num, sum = 0, rem, temp;
[Link]("Enter number: ");
num = Convert.ToInt32([Link]());
temp = num;

while (num != 0)
{
rem = num % 10;
sum += rem * rem * rem;
num /= 10;
}

if (temp == sum)
[Link]("Armstrong Number");
else
[Link]("Not Armstrong Number");

[Link]();
}
}
9. Write C# program to find length of string.
Answer:
using System;
class Program
{
static void Main()
{
string str;
[Link]("Enter string: ");
str = [Link]();

[Link]("Length = " + [Link]);


[Link]();
}
}
10. Write a C# program to calculate area of circle.
Answer:
using System;
class Program
{
static void Main()
{
double r, area;
[Link]("Enter radius: ");
r = [Link]([Link]());

area = 3.14 * r * r;
[Link]("Area of Circle = " + area);
[Link]();
}
}
11. Write a C# .net application to display the vowels from a given string.
Answer:
using System;
class Program
{
static void Main()
{
string str;
[Link]("Enter string: ");
str = [Link]();

foreach(char ch in str)
{
if ("aeiouAEIOU".Contains(ch))
[Link](ch + " ");
}
[Link]();
}
}
12. Write a C# program to accept and display ‘n’ students details such as Roll no,
name , marks in three subject using class. Display percentage of each student.
Answer:
using System;

class Student
{
public int roll;
public string name;
public int m1, m2, m3;

public void GetData()


{
[Link]("Enter Roll No: ");
roll = Convert.ToInt32([Link]());
[Link]("Enter Name: ");
name = [Link]();
[Link]("Enter Marks of 3 Subjects: ");
m1 = Convert.ToInt32([Link]());
m2 = Convert.ToInt32([Link]());
m3 = Convert.ToInt32([Link]());
}

public void Display()


{
double per = (m1 + m2 + m3) / 3.0;
[Link]("Roll No: " + roll);
[Link]("Name: " + name);
[Link]("Percentage: " + per + "%");
}
}

class Program
{
static void Main()
{
int n;
[Link]("Enter number of students: ");
n = Convert.ToInt32([Link]());

Student[] s = new Student[n];

for(int i=0;i<n;i++)
{
s[i] = new Student();
s[i].GetData();
}

for(int i=0;i<n;i++)
{
s[i].Display();
}

[Link]();
}
}
13. Write a menu driven program, in C#.net to perform following functionality :
Addition , Multiplication , Subtraction , Division.
Answer:
using System;
class Program
{
static void Main()
{
int choice, a, b;

[Link]("[Link]\[Link]\[Link]\[Link]");
[Link]("Enter choice: ");
choice = Convert.ToInt32([Link]());

[Link]("Enter two numbers: ");


a = Convert.ToInt32([Link]());
b = Convert.ToInt32([Link]());

switch(choice)
{
case 1:
[Link]("Addition = " + (a + b));
break;
case 2:
[Link]("Subtraction = " + (a - b));
break;
case 3:
[Link]("Multiplication = " + (a * b));
break;
case 4:
[Link]("Division = " + (a / b));
break;
default:
[Link]("Invalid Choice");
break;
}
[Link]();
}
}
14. Write a program in C#.net to create a function for the sum of two numbers.
Answer:
using System;

class Program
{
// Function to calculate sum
static int Sum(int a, int b)
{
return a + b;
}

static void Main()


{
int num1, num2, result;

[Link]("Enter first number: ");


num1 = Convert.ToInt32([Link]());

[Link]("Enter second number: ");


num2 = Convert.ToInt32([Link]());

result = Sum(num1, num2);

[Link]("Sum = " + result);


[Link]();
}
}

You might also like