Professional Documents
Culture Documents
C# Pracs
C# Pracs
displays the product. Hint: you may recall that the Convert.ToDouble()
command was used to convert the input from the console to a double; the
equivalent command to convert from a string to an int is Convert.ToInt32().
Source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication7
{
class Program
{
public int a, b, c, d;
public void get()
{
}
public void show()
{
int product;
product = a * b * c * d;
Console.WriteLine("The product is:" +
product);
Console.ReadLine();
}
static void Main(string[] args)
{
Program p = new Program();
p.get();
p.show();
Console.ReadLine();
}
}
}
Output:
B: 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?
Source Code:
using S
ystem;
using S ystem.Collections.Generic;
using S
ystem.Linq;
using S ystem.Text;
namespace ConsoleApplication7
{
class Program
{
int a, b, c;
public void get()
{
lp:Console.WriteLine("Enter numbers:");
a=Convert.ToInt32(Console.ReadLine());
b=Convert.ToInt32(Console.ReadLine());
if(a>10)
{
Console.WriteLine("Invalid
numbers");
}
else if(b>10)
{
Console.WriteLine("Invalid
numbers");
}
else
{
show();
}
}
public void show()
{
Console.WriteLine("Successful");
}
static void Main(string[] args)
{
Program p = new Program();
p.get();
Console.ReadLine();
}
}
}
Output:
C: 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.
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication7
{
class Program
{
int a, b, c;
public void get()
{
lp:Console.WriteLine("Enter numbers:");
a=Convert.ToInt32(Console.ReadLine());
b=Convert.ToInt32(Console.ReadLine());
if(a>10)
{
Console.WriteLine("Invalid");
goto lp;
}
else if(b>10)
{
Console.WriteLine("Invalid");
goto lp;
}
else
{
show();
}
}
public void show()
{
Console.WriteLine("Successful");
}
static void Main(string[] args)
{
Program p = new Program();
p.get();
Console.ReadLine();
}
}
}
Output:
D: Write a console application that places double quotation marks
around each word in a string
Source code:
using System;
using S
ystem.Collections.Generic;
using S ystem.Linq;
using S ystem.Text;
namespace ConsoleApplication7
{
class Program
{
public void disp()
{
string s;
Console.WriteLine("Enter a sentence");
s = Console.ReadLine();
string[] s1;
s1 = s.Split();
foreach (string w in s1)
{
Console.WriteLine(" \"" + w + "\"
");
}
}
o b.disp();
Console.ReadLine();
}
}
Output:
E: Write an application that uses two command-line arguments to
place values into a string and an integer variable, respectively. Then
display these values.
Source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
String a;
String b;
String c;
a = args[0];
b = args[1];
c = args[2];
Console.WriteLine("My name:" + a);
Console.WriteLine("Class:" + b);
Console.WriteLine("Roll no:" + c);
Console.ReadLine();
}
}
}
Output:
Source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication7
{
public struct s tudent16
{
public int st_id;
public String st_name;
public String co_name;
public String st_dob;
}
class Program
{
static void Main(string[] args)
{
student16[] st = new student16[2];
for( int i = 0; i < 1; i++)
{
Console.WriteLine("Enter id:");
st[i].st_id =
Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter name");
st[i].st_name = C onsole.ReadLine();
Console.WriteLine("Enter Course
name:");
st[i].co_name = C onsole.ReadLine();
Console.WriteLine("Enter dob:");
st[i].st_dob = C onsole.ReadLine();
}
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Your id is:" +
st[i].st_id);
Console.WriteLine("Your name is:" +
st[i].st_name);
Console.WriteLine("Your course name
is:" + st[i].co_name);
Console.WriteLine("Your Birth date
is:" + st[i].st_dob);
Console.ReadLine();
}
}
}
}
Output: