AWP-1
PRACTICAL NO.1
a) Aim : Enter 4 number and find the product of the same.
⮚ Code:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num1, num2, num3, num4, prod;
Console.Write("Enter number 1:");
num1 = Int32.Parse(Console.ReadLine());
Console.Write("Enter number 2:");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number 3:");
num3 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number 4:");
num4 = Convert.ToInt32(Console.ReadLine());
prod = num1 * num2 * num3 * num4;
Console.WriteLine(num1 + "*" + num2 + "*" + num3 + "*" + num4 + "=" + prod);
Console.ReadKey();
}}}
⮚ Output:
b) Aim: Create an application to demonstrate String operation
⮚ Code:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Concatenation
Console.Write("Enter string 1:");
string words = Console.ReadLine();
Console.Write("Enter string 2:");
string words2 = Console.ReadLine();
string words3 = words+" "+words2;
Console.Write(words3);
//Length of given string
Console.Write(" Enter name:");
string name = Console.ReadLine();
int length = +name.Length;
Console.WriteLine("Length: " + length);
//Comparison
string str1 = "hello";
string str2 = "hello";
string str3 = "no hello";
// compare str1 and str2
Boolean result1 = str1.Equals(str2);
Console.WriteLine("str1 and str2 are equal: " + result1);
//compare str1 and str3
Boolean result2 = str1.Equals(str3);
Console.WriteLine("str1 and str3 are equal: " + result2);
object[] array = {"Neha", "Jyoti" };
// Using Join method
// Here separator is ', '( comma )
string s1 = string.Join(", ", array);
Console.WriteLine("Value of string s1 is : " + s1);
Console.ReadKey();
}}}
⮚ Output:
c) Create an application to demonstrate the following operations:
⮚ Aim: Write program using conditional statements & loops:
i. Aim: Generate Fibonacci series.
⮚ Code:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num1=0, num2=1, num3, num, counter;
Console.Write("Enter the Number:");
num = int.Parse(Console.ReadLine());
counter = 2;
Console.Write(num1+"\n"+num2);
while(counter<=num)
{
num3 = num1 + num2;
if (counter >= num)
break;
Console.Write("\n" + num3);
num1 = num2;
num2 = num3;
counter++;
}
Console.ReadKey();
}}}
⮚ Output:
ii. Aim: Check whether the given number is “Prime” or not.
⮚ Code:
int num, counter;
Console.Write("Enter number:");
num = int.Parse(Console.ReadLine());
for (counter =1; counter <= num / 2; counter++)
{
if ((num % counter) == 0)
break;
}
if (num == 1)
Console.WriteLine(num + " is neither prime nor composite");
else if (num / 2 == 0)
Console.WriteLine(num + " is not prime number");
else
Console.WriteLine(num + " is prime number");
Console.ReadKey();}}}
⮚ Output:
iii. Aim: Check Whether the given character is Vowel or Not.
⮚ Code:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char ch;
Console.Write("Enter a character:");
ch = (char)Console.Read();
switch(ch) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
Console.WriteLine(ch +" is vowel");
break;
default:
Console.Write(ch +" is not a vowel");
break;}
Console.ReadKey(); }}}
⮚
Output:
iv. Aim: USe of foreach loop with arrays.
⮚ Code:
using System;
class Program {
public static void Main() {
string[] str ={ "Neha", "Jyoti", "TYIT" };
foreach (Strings in str)
{Console.WriteLine(s);}
Console.ReadKey();}}
⮚ Output:
v. Aim : Reverse given number & Find the sum of it.
⮚ Code:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num, actualnumber, revnum = 0, digit, sumDigits = 0;
Console.Write("Enter a number:");
num = int.Parse(Console.ReadLine());
actualnumber = num;
while (num > 0)
{
digit = num % 10;
revnum = revnum * 10 + digit;
sumDigits = sumDigits + digit;
num = num / 10;
}
Console.WriteLine("Reverse of" + actualnumber + "=" + revnum);
Console.WriteLine("Sum of its digits:" + sumDigits);
Console.ReadKey();
}}}
⮚ Output:
Comments
Post a Comment