Professional Documents
Culture Documents
#include <iostream>
using namespace std;
int main() {
const double conversionRate = 329.50;
double amountInRupees;
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
#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;
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;
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;
int main() {
int 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;
scanf("%d", &number);
int sum = 0;
int count = 0;
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;
return 0;
}
8. Design a C++ code to check whether a triangle is Equilateral, Isosceles or Scalene.
(10 marks)
#include <iostream>
int main() {
cout << "Enter the lengths of three sides of the triangle: ";
} else {
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;
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;
}
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;
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;
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;
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;
}