Anda di halaman 1dari 13

A: Write a console application that obtains four int values from the user and

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()
{

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());

}
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 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
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 System.Collections.Generic;
using System.Linq;
using System.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 + "\"
");
}
}

static void Main(string[] args)


{
Program ob = new Program();

ob.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:
F: 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.

Source code:

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

namespace ConsoleApplication7
{
public struct student16
{
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 = Console.ReadLine();
Console.WriteLine("Enter Course
name:");
st[i].co_name = Console.ReadLine();
Console.WriteLine("Enter dob:");
st[i].st_dob = Console.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:

Anda mungkin juga menyukai