You are on page 1of 13

1. 1 USD = 329.50 Sri Lankan Rupees.

Design a C++ program where the user can input the


amount in rupees and inform the user how much it is in USD. (10 marks)

#include <iostream>
using namespace std;

int main() {
const double conversionRate = 329.50;

double amountInRupees;

cout << "Enter the amount in Sri Lankan Rupees: ";


cin >> amountInRupees;

double amountInUSD = amountInRupees / conversionRate;

cout << "Equivalent amount in USD: " << amountInUSD << endl;

return 0;
}
2. Write a C++ program where a user can input a number (currency) and the program prints
(output) the number of notes to be issued. (10 marks)

In Sri Lankan Rupee, we have 5000, 1000, 500, 100, 50, 20 and sometimes 10 and 5, 2 and 1
rupee coins)

Example
Input
Input amount: 5525

Output

Total number of notes:


5000: 1
100: 0
500: 1
20: 1
10: 0
5: 1
2: 0
1: 0

#include <iostream>
using namespace std;

int main() {
int denominations[] = {5000, 1000, 500, 100, 50, 20, 10, 5, 2, 1};
int notesCount[sizeof(denominations) / sizeof(denominations[0])];

int amount;
cout << "Enter the amount in Sri Lankan Rupees: ";
cin >> amount;

cout << "Number of notes/coins to be issued:" << endl;

for (int i = 0; i < sizeof(denominations) / sizeof(denominations[0]); i++)


{
notesCount[i] = amount / denominations[i];
amount %= denominations[i];
if (notesCount[i] > 0) {
cout << denominations[i] << " LKR notes: " << notesCount[i] <<
endl;
}
}

return 0;
}
If Conditionals
3. Write a C program to input the number of the day of the week (1-7) and print the
corresponding day of week name using if else. (10 marks)
Monday – 1,
Tuesday – 2
Wednesday – 3
Thursday -4
Friday -5
Saturday – 6
Sunday -7

Example
Input
Input day number: 1
Output
Monday

#include <stdio.h>

int main() {
int day;

printf("Enter the number of the day (1-7): ");


scanf("%d", &day);

if (day == 1) {
printf("Monday\n");
} else if (day == 2) {
printf("Tuesday\n");
} else if (day == 3) {
printf("Wednesday\n");
} else if (day == 4) {
printf("Thursday\n");
} else if (day == 5) {
printf("Friday\n");
} else if (day == 6) {
printf("Saturday\n");
} else if (day == 7) {
printf("Sunday\n");
} else {
printf("Invalid input! Please enter a number between 1 and 7.\
n");
}

return 0;
}
4. Explain what the following flowchart does.

It prompts the user to input two numbers. It calculates and displays the Greatest Common
Divisor (GCD) of these two numbers .
5. Translate the following flowchart in to C++ code. (10 marks)

#include <iostream>
using namespace std;

// Function to find GCD using Euclidean algorithm


int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int main() {
int num1, num2;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

int result = gcd(num1, num2);

cout << "GCD of " << num1 << " and " << num2 << " is: " << result << endl;

return 0;
}
6. Ask the user to enter a number and find the sum of the first 10 numbers after it.(loops)
(10 marks)

#include <stdio.h>

int main() {

int number;

printf("Enter a number: ");

scanf("%d", &number);

int sum = 0;

int count = 0;

printf("The first 10 numbers after %d are: ", number);

for (int i = number + 1; count < 10; i++) {

printf("%d ", i);

sum += i;

count++;

printf("\nThe sum of the first 10 numbers after %d is: %d\n", number, sum);

return 0;

}
7. Show the multiplication table of a given integer. (10 marks)

Eg. 1x1=1
1x2=2
1x3=3

#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter an integer to display its multiplication table: ";


cin >> number;

cout << "Multiplication table of " << number << ":\n";


for (int i = 1; i <= 10; ++i) {
cout << number << " * " << i << " = " << number * i << endl;
}

return 0;
}
8. Design a C++ code to check whether a triangle is Equilateral, Isosceles or Scalene.

(10 marks)

#include <iostream>

using namespace std;

int main() {

double side1, side2, side3;

cout << "Enter the lengths of three sides of the triangle: ";

cin >> side1 >> side2 >> side3;

if (side1 == side2 && side2 == side3) {

cout << "It's an Equilateral triangle." << endl;

} else if (side1 == side2 || side1 == side3 || side2 == side3) {

cout << "It's an Isosceles triangle." << endl;

} else {

cout << "It's a Scalene triangle." << endl;

return 0;

}
9. Write a C program to input month number and print total number of days in month using
switch...case. C program to find total number of days in a month using
switch...case. Logic to print number of days in a month using switch...case in C
programming.

#include <stdio.h>

int main() {
int month, days;

printf("Enter month number (1-12): ");


scanf("%d", &month);

switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4: // April
case 6: // June
case 9: // September
case 11:
days = 30;
break;
case 2:
days = 28;
break;
default:
printf("Invalid month number!\n");
return 1;
}

printf("Total number of days in month %d is %d\n", month, days);

return 0;
}
10. Write a C program to input an alphabet and check whether it is vowel or consonant using
switch case.
Example

#include <stdio.h>

int main() {
char alphabet;

printf("Enter an alphabet: ");


scanf(" %c", &alphabet);

switch (alphabet) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", alphabet);
break;
default:
printf("%c is a consonant.\n", alphabet);
break;
}

return 0;
}
11. Write a C program to input number from user and check whether the number is even or
odd using switch case.
Example
Input
Input number: 12
Output
Even number

#include <stdio.h>

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}

return 0;
}
12. If possible a program to add, subtract, divide and multiply based on
the operators entered.

#include <iostream>
using namespace std;

int main() {
char op;
double num1, num2;

cout << "Enter operator (+, -, *, /): ";


cin >> op;

cout << "Enter two numbers: ";


cin >> num1 >> num2;

switch (op) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
cout << "Cannot divide by zero!" << endl;
}
break;
default:
cout << "Invalid operator entered!" << endl;
break;
}

return 0;
}

You might also like