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

Programmeren1 Tentamen Code

The document contains code for 5 C# programming assignments: 1) A program that calculates credits needed for a student to progress to the second year of university. 2) A program that factors numbers into their prime factors. 3) A program that generates random numbers and colors them based on value. 4) A program that performs integer division and displays remainders. 5) A program for a train ticket calculator form.

Uploaded by

projectjr2 Profs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views5 pages

Programmeren1 Tentamen Code

The document contains code for 5 C# programming assignments: 1) A program that calculates credits needed for a student to progress to the second year of university. 2) A program that factors numbers into their prime factors. 3) A program that generates random numbers and colors them based on value. 4) A program that performs integer division and displays remainders. 5) A program for a train ticket calculator form.

Uploaded by

projectjr2 Profs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming 1 exam (Wednesday, 30 October 2019)

using System;

namespace Assignment1
{
class Program
{
const int MaxYear1Credits = 60; // number of ECs in first year

static void Main(string[] args)


{
// read name
Console.Write("enter your name: ");
string studentName = Console.ReadLine();

// read number of exemption credits


Console.Write("enter your number of exemption ECs: ");
int exemptionCredits = int.Parse(Console.ReadLine());

// read students total credits of first year


Console.Write("enter your total number of ECs (of the first year): ");
int studentYear1Credits = int.Parse(Console.ReadLine());

float minimumCredits = exemptionCredits + (0.75f * (MaxYear1Credits -


exemptionCredits));
Console.WriteLine("(minimum required ECs to go to the 2nd year: {0})", minimumCredits);
Console.WriteLine();

// student has enough credits to go to 2nd year?


if (studentYear1Credits >= minimumCredits)
{
Console.WriteLine("{0}, you have enough ECs to go to the 2nd year.", studentName);
}
else
{
Console.WriteLine("{0}, you don't have enough ECs (yet) to go to the 2nd year.",
studentName);
}
}
}
}
using System;

namespace Assignment2
{
class Program
{
static void Main(string[] args)
{
// read (first) number
Console.Write("Enter a number: ");
int number = int.Parse(Console.ReadLine());

while (number > 0)


{
// divide number by its prime factors until number becomes 1
int factor = 2;
while (number > 1)
{
if ((number % factor) == 0)
{
number = number / factor;
Console.Write($"{factor} ");
}
else
{
factor++;
}
}
Console.WriteLine();

// read (next) number


Console.Write("Enter a number: ");
number = int.Parse(Console.ReadLine());
}

Console.WriteLine();
Console.WriteLine("end of program");
}
}
}
using System;

namespace Assignment3
{
class Program
{
const int ARRAY_SIZE = 20;
const int MIN_VALUE = 1;
const int MAX_VALUE = 99;

static void Main(string[] args)


{
// create array
int[] numbers = new int[ARRAY_SIZE];

// fill array with random numbers


Random rnd = new Random();

int minimum = MAX_VALUE + 1;


int maximum = MIN_VALUE - 1;
for (int i = 0; i < numbers.Length; i++)
{
int value = rnd.Next(MIN_VALUE, MAX_VALUE + 1);
numbers[i] = value;

if (value < minimum)


minimum = value;
if (value > maximum)
maximum = value;
}

Console.WriteLine($"minimum value: {minimum}");


Console.WriteLine($"maximum value: {maximum}");

// display array values (with different colors)


for (int i = 0; i < numbers.Length; i++)
{
int value = numbers[i];

if ((value >= minimum + 20) && (value <= maximum - 20))


Console.ForegroundColor = ConsoleColor.Green;
else if ((value >= minimum + 10) && (value <= maximum - 10))
Console.ForegroundColor = ConsoleColor.Yellow;
else
Console.ForegroundColor = ConsoleColor.Red;

Console.Write("{0:00} ", numbers[i]);


Console.ResetColor();
}
}
}
}
using System;

namespace Assignment4
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
for (int i = 1; i <= 10; i++)
{
int number1 = rnd.Next(1, 51); // 1..50
int number2 = rnd.Next(1, 6); // 1..5

int remainder;
int result = IntegerDivision(number1, number2, out remainder);
Console.WriteLine($"{number1,2} / {number2} = {result,2} ({remainder})");
}

Console.WriteLine();
Console.WriteLine("end of program");
}

static int IntegerDivision(int number1, int number2, out int remainder)


{
int quotient = 0;

while (number1 >= number2)


{
number1 = number1 - number2;
quotient++;
}

remainder = number1;
return quotient;
}
}
}
using System;
using System.Windows.Forms;

namespace Assignment5
{
public partial class NSDayTicketForm : Form
{
const double DayTicketSecondClass = 53;
const double DayTicketFirstClass = 87.46;
const double RailRunner = 2.50; // 4..11 years

public NSDagkaartForm()
{
InitializeComponent();
}

private void btnCalculate_Click(object sender, EventArgs e)


{
int nrOfDayTickets = int.Parse(txtNrOfTickets.Text);

double price = 0;

if (nrOfDayTickets > 0)
{
if (rbSecondClass.Checked)
{
price = nrOfDayTickets * DayTicketSecondClass;
}
else // if (rbFirstClass.Checked)
{
price = nrOfDayTickets * DayTicketFirstClass;
}
}

int nrOfRailRunners = int.Parse(txtNrOfRailRunners.Text);


prijs += nrOfRailRunners * RailRunner;

txtPrice.Text = price.ToString("0.00");
}
}
}

You might also like