C# Pracs

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

C# Practicals

Practical number:01

Write a console application that obtains four int values from the user and displays the product.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
int a, b, c, d, e;
void get_no()
{
Console.WriteLine(" Enter four numbers: ");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
d = Convert.ToInt32(Console.ReadLine());
}
void product()
{
e = a * b * c * d;
Console.WriteLine("Product of numbers is: " + e);
Console.ReadLine();
}
static void Main(string[] args)
{
Program p = new Program();
p.get_no();
p.product();
}
}
}
Practical number:02

If you have two integers stored in variables var1 and var2, what Boolean test can you perform to

see if one or the other (but not both) is greater than 10?

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int var1, var2;
Console.WriteLine("Enter First number:");
var1=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second number:");
var2=Convert.ToInt32(Console.ReadLine());
bool c = false;
c=(var1>10 && var2<10);
{
Console.WriteLine("First no is greater than 10 and second number is less
than 10");
Console.ReadLine();
}
c=(var1<10 && var2>10);
{
Console.WriteLine("First no is less than 10 and second number is greater
than 10");
Console.ReadLine();
}

}
}
}

Practical number:03

Write an application that includes the logic from Exercise 1, obtains two numbers from the user, and
displays them, but rejects any input where both numbers are greater than 10 and asks for two new
numbers.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{

class Program
{
int var1,var2,var3;
void add()
{
lp: Console.WriteLine("Enter two Numbers:");
var1 = Convert.ToInt32(Console.ReadLine());
var2 = Convert.ToInt32(Console.ReadLine());

if (var1 > 10 && var2 < 10)


{
Console.WriteLine("First no is greater than 10 and second number is less
than 10");
Console.ReadLine();
goto lp;
}

if (var1 < 10 && var2 > 10)


{
Console.WriteLine("First no is less than 10 and second number is greater
than 10");
Console.ReadLine();
goto lp;
}
else
{
var3=var1+var2;
Console.WriteLine("Addition is: " +var3);
Console.ReadLine();
}

static void Main(string[] args)


{
Program p=new Program();
p.add();
}
}
}
Practical number:04

Write a console application that places double quotation marks around each word in a string

Code:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication7

class Program

static void Main(string[] args)

start:

string myString;

char[] separator = { ' ' };

Console.WriteLine("Type any string please");


myString = Console.ReadLine();

string[] myWords;

myWords = myString.Split(separator);

foreach (string word in myWords)

Console.Write("\"{0}\" ", word);

Console.WriteLine();

Console.ReadLine();

Console.Clear();

goto start;

}
Practical number:06
Write an application that receives the following information from a set of students: Student Id: Student
Name: Course Name: Date of Birth: The application should also display the information of all the
students once the data is entered. Implement this using an Array of Structs

Code:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication6

struct student

public String s_id, s_name, c_name, dob;

class Program

static void Main(string[] args)

student[] arr = new student[4];

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

Console.WriteLine("Please enter StudentId:");

arr[i].s_id = Console.ReadLine();

Console.WriteLine("Please enter Student_Name:");

arr[i].s_name = Console.ReadLine();

Console.WriteLine("Please enter Course_name:");


arr[i].c_name = Console.ReadLine();

Console.WriteLine("Please enter DOB:");

arr[i].dob = Console.ReadLine();

Console.WriteLine("----------Student Details-----------");

Console.WriteLine("Student Id: " + arr[i].s_id);

Console.WriteLine("Student Name: " + arr[i].s_name);

Console.WriteLine("Student Course_Name: " + arr[i].c_name);

Console.WriteLine("Student DOB: " + arr[i].dob);

Console.ReadLine();

You might also like