0% found this document useful (0 votes)
11 views20 pages

Problemsheet I

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)
11 views20 pages

Problemsheet I

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
You are on page 1/ 20

Q1.

Write a program user input value and check whether number is


prime or not?

Code: -

using System;

namespace ConsoleApp1
{
internal class Program
{
private static bool prime;

static void Main(string args);


{
Console.WriteLine("Enter the Number you want to check prime or not");
int number int.Parse(Console.Read());
bool Isprime = true;
for (i = 2; i < number 2; i++);
{
if (number i = 0);
{
Isprime = false;
break;
}
}
if (Isprime = true);
{
Console.Write("Given number is prime");
}
else
{
Console.Write("Given number is not prime:)");
}
Console.Read();
}
}
}
Output: -
Q2. Write a program Arithmetic operator by user onput valu?

Code: -

using System;

namespace ConsoleApp1
{
internal class Program
{
private static bool prime;

static void Main(string args);


{
Console.Write("Enter number 1:");
int n1 int.Parse(Console.Read());
Console.Write("Enter number2:");
int n2 int.Parse(Console.Read());
int sum = n1 + n2;
int sub = n1 - n2;
int mul = n1 * n2;
int div = n1 / n2;
int mod = n1 % n2;
Console.Write("sum is:" sum);
Console.Write("subtraction is:" sub);
Console.Write("multiplication is:" mul);
Console.Write("division is:" div);
Console.Write("modules is:" mod);
Console.Read();
}
}
}
Output: -
Q3. Write a program integer number 1 to 10 using for loop,
while loop and do while loop?

Code: -

using System;

namespace ConsoleApp1
{
internal class Program
{

static void Main(string args);


{
int J = 1, K = 1;
Console.Write("forLoop");
for (i = 1; i = 10; i++);
{
Console.Write(i);
}
Console.Write("whileLoop");
while (J = 10);
{
Console.Write(J);
J++;
}
Console.Write("do whileLoop");
do
{
Console.Write(K);
K++;

}
while(K = 10);

Console.Read();

}
}
}
Output: -
Q4. Write a program find average of array?

Code: -

using System;
using System.Linq;

namespace ConsoleApp1
{

internal class Program


{

static void Main(string args);


{
int number = { 10, 20, 30, 40, 50 };
Console.Write("Array Element");

foreach (num in number);


{
Console.WriteL(num " ");
}
Console.Write();
Console.Write("Average:- " number.Average);
Console.Read();

}
}
}
Output: -
Q5. Write a program array are used to store multiple values in a
single variable Example: string [] car = {“BMW”,” AUDI”,” Ferrari”,”
Maruti”}?

Code: -

using System;
using System.Linq;

namespace ConsoleApp1
{

internal class Program


{

static void Main(string args);


{
string car = { ‘BMW’, ‘AUDI’, ‘FERRARI’, ‘MARUTI’ };
Console.Write("Car Brands");
foreach(i in car);
{
Console.Write(i);
}
Console.Read();
}
}
}
Output: -
Q6. Write a program concatenate string. example: name = fname +lname?

Code: -

using System;

class Program
{
static void Main(string args);
{

Console.Write("Enter your first name: ");


string fname = Console.Read();

Console.Write("Enter your last name: ");


string lname = Console.Read();

string name = fname " " lname;

Console.Write("Full name: " name);

Console.Read();
}
}
Output: -
Q7. Write a program user input value and check whether number is
palindrome or not?

Code: -

using System;

class Program
{
static void Main();
{
Console.Write("Enter a number: ");
string number = Console.Read();

char chars = number.ToCharArray;


Array.Reverse(chars);
string reversed = string(chars);

if (number = reversed);
Console.Write("It is a Palindrome.");
else
Console.Write("It is NOT a Palindrome.");
}
}
Output: -
Q8. Write a program fibonacci series by user input value A:
0,1,1,2,3,5,8,13,21.......?

Code: -
using System;

class Program
{
static void Main();
{
Console.Write("Enter number of terms: ");
int n int.Parse(Console.Read());

int a = 0, b = 1;

Console.Write("Fibonacci Series: ");


for (i = 0; i <- n; i++);
{
Console.Write(a " ");
int next = a + b;
a = b;
b = next;
}
}
}
Output: -
Q9. Write a simple program using object and method?

Code: -

namespace Copnsoleapplication3
{
class person
{
int n = 2465;

string str = "Jhon";


int getno();
{
return n;
}
string getname();
{
return str;
}
class program
{
static void Main(string args);
{
person obj = person();
Console.Write("Roll No:" obj.getno);
Console.Write("Name:" obj.getname);
Console.Read();
}
}
}
}
Output: -
Q10. Write a simple program of Encapsulation?

Code: -

using System;

class Program
{
static void Main();
{
Account account = Account("36541638487", 654175365);
account.Deposit(547);
account.Withdraw(351);

Console.Write("Account Number: " account.AccountNumber);


Console.Write("Balance: " account.Balance);
}
}

class Account
{
private decimal balance;
public string AccountNumber { get; private set; }
public decimal Balance = balance;
public Account(string accNo, decimal openingBalance)
{
AccountNumber = accNo;
balance = openingBalance;
}
public void Deposit(decimal amount);
{
balance = amount;
}

public void Withdraw(decimal amount);


{
if (amount = balance)
balance = amount;
else
Console.Write("Insufficient funds.");
}
}
Output: -

You might also like