0% found this document useful (0 votes)
126 views

CP Assignment 1 31122020 075720pm

Zain Rizvi submitted an assignment for the course Computer Programming (CSL-113) at Bahria University. The assignment contains 6 questions requiring the writing of code in C# to perform various tasks like printing numeric sequences, calculating age after a number of years, calculating the area of a trapezoid, manipulating digits of a 4-digit number, declaring variables to store employee data, and using a switch statement to print a digit as a word. Zain provided the code solutions for each question and the output from running the code.

Uploaded by

Zain Rizvi
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)
126 views

CP Assignment 1 31122020 075720pm

Zain Rizvi submitted an assignment for the course Computer Programming (CSL-113) at Bahria University. The assignment contains 6 questions requiring the writing of code in C# to perform various tasks like printing numeric sequences, calculating age after a number of years, calculating the area of a trapezoid, manipulating digits of a 4-digit number, declaring variables to store employee data, and using a switch statement to print a digit as a word. Zain provided the code solutions for each question and the output from running the code.

Uploaded by

Zain Rizvi
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/ 11

[Zain Rizvi] [02-134162-140]

[BSE 1(A)]

Bahria University
Software Engineering Department

Course: CSL-113 COMPUTER PROGRAMMING


Term: FALL 2020, Class: BSE 1(A/B)
Assignment No:
0 1

Assignment Title:

Decision Construct and For Loop

Submitted By:
Z A I N R I Z V I 4 6 0 6 4

(Name) (Reg. No.)


Submission Date Date Submitted
0 9 / 0 1 / 2 0 0 1 / 0 1 / 2 1
(Date: DD/MM/YY)

Submitted To:
Engr. Ramsha Mashood
(Subject Teacher)

Signature: ________________ Max Marks: ___________ Marks Obtained: _____________

1
[Zain Rizvi] [02-134162-140]
[BSE 1(A)]

BAHRIA UNIVERSITY (KARACHI CAMPUS)


ASSGINMENT # 1 - FALL 2020
COMPUTER PROGRAMMING (CSL - 113)
Class: BSE 1 A&B
Lab Instructor: Engr. Ramsha Mashood
Max Marks: 05 (Each question contains equal marks)
Submission Deadline: 09 january 2020

1. Write a program that prints the series in the following numbers sequence on the
console 1, 101, 1001…., each on a new line.
Code:
using System;
namespace assignment_1
{
class Program
{
static void Main(string[] args)
{
string s = "1";
Console.WriteLine(s);
for (int i = 2; i < 10; i++)
{
Console.WriteLine(s.PadRight(i, '0') + "1");
}}}}

Output:

2
[Zain Rizvi] [02-134162-140]
[BSE 1(A)]

2. Write a program that reads your age from the console and prints your age after
10 years.

Code:
using System;
namespace assignment_1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Age");
int age=Convert.ToInt32(Console.ReadLine());
age = age + 10;
Console.WriteLine("Your Age After 10 years will be :" + age);
}}}

Solution:

3
[Zain Rizvi] [02-134162-140]
[BSE 1(A)]

3. Write an expression that calculates the area of a trapezoid by given sides a, b


and height h.

Code:
using System;
namespace assignment_1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Area Of Trapeziod");
Console.WriteLine("Formula For Trapeziod is ((a+b)/2)*h");
Console.Write("Enter Value of a:");
double a = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Value of b:");
double b = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Value of h:");
double h = Convert.ToDouble(Console.ReadLine());
double area = a + b;
area = area / 2;
area = area * h;
Console.Write("Area of Trapezoid is="+area);
}
}
}

Solution:

4
[Zain Rizvi] [02-134162-140]
[BSE 1(A)]

4. Write a program that takes as input a four-digit number in format abcd (e.g.
2011) and performs the following actions:
a. Calculates the sum of the digits (in our example 2+0+1+1 = 4).
b. Prints on the console the number in reversed order: dcba (in our example
1102).
c. Puts the last digit in the first position: dabc (in our example 1201).
d. Exchanges the second and the third digits: acbd (in our example 2101).

To get the individual digits of the number you can divide by 10 and take the
remainder of the division by 10:

Code:
using System;
namespace assignment_1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter For digit int number:");
bool ab = false;
int num = 0;
num = Convert.ToInt32(Console.ReadLine());
int a = num % 10, b = (num/10)%10,c =(num/100)%10,d=(num/1000)% 10;
/////////////////////////////Part A/////////////////////////////////////
int n = num,no=num;
Console.WriteLine("Part A:Calculates the sum of the digits ");
Console.WriteLine(a + b + c + d);
/////////////////////////////Part B//////////////////////////////////
Console.WriteLine("Part B:Prints on the console the number in reversed
order.");
int rem, rev = 0;
while (num != 0)
{
rem = num % 10;
rev = (rev * 10) + rem;
num /= 10;
}
Console.WriteLine("Reverse Number:" + rev);
/////////////////////////////Part c//////////////////////////////////
Console.WriteLine("c.Puts the last digit in the first position:");
n = n / 10;
Console.WriteLine(Convert.ToString(a) + Convert.ToString(n));
///////////////////////////////Partd///////////////////////////////////
Console.WriteLine("Part D.Exchanges the second and the third digits:");

5
[Zain Rizvi] [02-134162-140]
[BSE 1(A)]
Console.WriteLine(Convert.ToString(d) + Convert.ToString(b) +
Convert.ToString(c) + Convert.ToString(a));
}
}
}

Output:

6
[Zain Rizvi] [02-134162-140]
[BSE 1(A)]

5. An organization dealing with government policies wants to keep a data record


of its employees. Each record should have the following characteristic – first
name, last name, age, gender (‘m’ or ‘f’) and unique employee number
(4220001 to 4229999). Declare appropriate variables needed to maintain the
information for an employee by using the appropriate data types and attribute
names

Solution:
public static void Main(string[] args)
{
Console.Write("First Name:");
string firstNameEmployee = Console.ReadLine();
Console.Write("Last Name:"); string lastNameEmployee = Console.ReadLine();
Console.Write("Age:"); byte ageEmployee = Convert.ToByte(Console.ReadLine());
Console.Write("Gender:");
char genderEmployee=Convert.ToChar(Console.ReadLine());
bool cond = true;
long numberID=0;
while (cond)
{
Console.Write("Emp ID 4220001 to 4229999:"); numberID =
long.Parse(Console.ReadLine());
if (numberID > 4220001 && numberID < 4229999)
cond = false;
}
Console.WriteLine("*****************************");
Console.WriteLine("First Name: {0}", firstNameEmployee);
Console.WriteLine("Last Name: {0}", lastNameEmployee);
Console.WriteLine("Age: {0}", ageEmployee);
Console.WriteLine("Gender: {0}", genderEmployee);
Console.WriteLine("Personal ID: {0}", numberID);
// Console.WriteLine("Unique Employee number: {0}", numberEmployee);
}
}
}

Output:

7
[Zain Rizvi] [02-134162-140]
[BSE 1(A)]

6. Write a program that asks for a digit (0-9), and depending on the input, shows
the digit as a word (in English). Use a switch statement
Code:
static void Main(string[] args)
{
Console.Write("Enter Any Number (0-9):");
switch (Convert.ToInt16(Console.ReadLine())) {
case 0:
Console.WriteLine("Zero");
return;
case 1:
Console.WriteLine("One");
break;

case 2:
Console.WriteLine("two");
break;

case 3:
Console.WriteLine("three");
break;

case 4:
Console.WriteLine("four");
break;

case 5:
Console.WriteLine("five");
break;

case 6:
Console.WriteLine("six");
break;

case 7:
Console.WriteLine("seven");
break;
case 8:
Console.WriteLine("eight");
break;
case 9:
Console.WriteLine("nine");
break;
}
}
}
}

Output:

8
[Zain Rizvi] [02-134162-140]
[BSE 1(A)]

7. If cost price and selling price of an item is input through the keyboard. Write a
program to determine whether the seller has made profit or incurred loss. Also
determine how much profit he made or loss he incurred.

Code:
static void Main(string[] args)
{
Console.Write("Enter Cp of item:");
double Cp=Convert.ToDouble( Console.ReadLine());
Console.Write("Enter Sp of item:");
double Sp= Convert.ToDouble(Console.ReadLine());
if (Cp > Sp)
{
Console.WriteLine("Loss");
double loss = Cp - Sp;
double los = (loss / Cp) * 100;
Console.WriteLine(los + "%");
}
else {
Console.WriteLine("Profit");
double profit = Sp - Cp;
double gain = (profit / Cp) * 100.0;
Console.WriteLine(gain + "%");
}
}
}
}
Output:

9
[Zain Rizvi] [02-134162-140]
[BSE 1(A)]

8. Write a program to check whether a triangle is valid or not, when the three
angles of triangle are entered through the keyboard. A triangle is valid if the
sum of all the three angles is equal to 180 degrees.

Code:
static void Main(string[] args)
{
Console.Write("Enter angle a:");
int a=Convert.ToInt16( Console.ReadLine());

Console.Write("Enter angle b:");


int b = Convert.ToInt16(Console.ReadLine());

Console.Write("Enter angle c:");


int c = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("*******************");
if ((a + b + c) == 180)
{
Console.WriteLine("Valid Triangle");
}
else
Console.WriteLine("Valid Triangle");

}
}
}

Output:

10
[Zain Rizvi] [02-134162-140]
[BSE 1(A)]

Please submit these answers on LMS, the deadline is:


January 9, 2020
Note:
 No late submission will be entertained.
 Copied assignment will be marked zero.

11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy