0% found this document useful (0 votes)
26 views5 pages

Advanced Application Programming Assignment

assignment solution

Uploaded by

Jackson Obat
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
26 views5 pages

Advanced Application Programming Assignment

assignment solution

Uploaded by

Jackson Obat
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 5

Advanced Application Programming

Lecturer John Maina

Advanced Application Programming Assignments


1) Using nested loops, write code to produce the following output. (8 marks)
NB: Use only two loops.
12345678
1234567
123456
12345
1234
123
12
1
2) Write a program that accepts a set of integers (the user decides how many) and then
stores them in an array. The main function then passes these values one by one to a
method called get_even which returns 1 if the integer is even and 0 if it is odd. The
main function should then specify which numbers were even, which ones were odd
and their respective totals. It should also specify how many numbers were odd and
how many were even. For example, if the user enters 25 34 56 17 14 20, the output
should be: -
25 is an odd number
34 is an even number
56 is an even number
17 is an odd number
14 is an even number
20 is an even number

There is a total of 2 odd numbers and their sum is 42.


There is a total of 4 even numbers and their sum is 124.
NB: All data input and output should be done in main. Don’t use % any where in the
main function (% can be used in the method). (10 marks)
using System;

class EvenOdd
{
static void Main()
{
Console.Write("Enter the number of integers: ");
int n = int.Parse(Console.ReadLine());

int[] numbers = new int[n];


Advanced Application Programming
Lecturer John Maina
Console.WriteLine("Enter the integers:");

for (int i = 0; i < n; i++)


{
numbers[i] = int.Parse(Console.ReadLine());
}

int evenCount = 0;
int oddCount = 0;
int evenSum = 0;
int oddSum = 0;

Console.WriteLine("The numbers are:");

for (int i = 0; i < n; i++)


{
int result = get_even(numbers[i]);

if (result == 1)
{
Console.WriteLine($"{numbers[i]} is an even number");
evenCount++;
evenSum += numbers[i];
}
else
{
Console.WriteLine($"{numbers[i]} is an odd number");
oddCount++;
oddSum += numbers[i];
}
}
Advanced Application Programming
Lecturer John Maina
Console.WriteLine($"There are {oddCount} odd numbers and their sum is
{oddSum}");
Console.WriteLine($"There are {evenCount} even numbers and their sum is
{evenSum}");
}

static int get_even(int number)


{
if (number % 2 == 0)
{
return 1;
}
else
{
return 0;
}
}
}
3) Write a program that accepts the amount of money deposited in a bank account, the
annual interest rate and the target amount (The amount of money the account holder
wants to have in the account after a period of time) and then calculates the number of
years it will take for the money to accumulate to the targeted amount. NB: 1) The
interest being earned is Compound Interest. 2) Don’t use the formula for calculating
compound interest.
For example if the money deposited is 10000 and the target amount is 20000 and the
account earns an interest rate (compound) of 10% pa, then the output should be: -
It will take 8 years for your money to reach your target.
By the end of this period, the amount in your account will be 21435.89 (10 marks)

4) Making use of object orientation write a program that stores and evaluates the total
cost for items bought from a supermarket. The cashier should enter the following: -
Product code, Price and Quantity. The total price should be evaluated as follows: -
Total cost = Price * Quantity
If the total cost per item is more than 20,000 there is a discount of 14% on that item
and a discount of 10% on an item whose total cost is between 10,000 and 20,000. No
discount is given on items whose total cost is less than 10,000
NB: The cashier should decide how many Items he/she wants to work with. If he/she
chooses 3, for example, the output should take the format shown below.
Advanced Application Programming
Lecturer John Maina
Item Code Price Quantity Total Cost Discount Net
109 6000 4 24000 3360 20640
201 900 8 7200 0 7200
127 600 20 12000 1200 10800
The total amount payable is 38640
NB: Your class should have constructors (including default constructor) and a
destructor. It should also have properties to set and return price, quantity and total
cost. Set item code to read only. (15 marks)
5) Rewrite the program above making use of arrays only i.e. don’t use object
orientation your program. (10 marks)

6) Write a program that accepts a set of numbers (The user decides how many) and
stores them in array. The program should the sort all the numbers in the array in
ascending order. It should output the numbers in the array before sorting and then
again after sorting. (10 marks)

7) Write a program that accepts two numbers and swaps them. Both input and output
should be done in the main method and the swapping (actual swapping) should be
done in a method called swapNumbers. Output the two numbers in the main method
before and after swapping. (7 marks)

8) Write a (unique) C# program using the concept of object orientation to illustrate the
following concept in Object Oriented programming. Indicate with comments where
each of these concepts/techniques is used in your program. (15 marks)
a) Inheritance.
b) Overloading.
c) Overriding.
d) Encapsulation.
e) Constructors (Including default constructors)
f) Destructor.
g) Properties.
h) Error handling – try catch

* * * * * All the best – JM * * * * *


using System;

class pattern
{
static void Main()
{
int n = 8;

for (int i = 1; i <= n; i++)


{
for (int j = 1; j <= n - i + 1; j++)
{
Console.Write(j + " ");
}
Advanced Application Programming
Lecturer John Maina

Console.WriteLine();
}
}
}

You might also like