Download presentation
Presentation is loading. Please wait.
1
MT262A Review
2
Q1 Write a C++ program to print the sequence 0 , 100 , 200 , 300 , …………….., n, where the value of n is entered by the user.
3
int main(int argc, char* argv[]) { int N , int=I, cout << "Enter N : " ; cin >> N ; For (I=0; I<=N, I=I+100) cout << I <<“ “; getchar(); return 0; }
4
Q1 Write a C++ program to compute and print the average of the sequence 0 + 100 ……………..+ n, where the value of n is entered by the user.
5
int main(int argc, char* argv[]) { int N , I=1, C = 0; float SUM = 0, AV; cout << "Enter N : " ; cin >> N ; For (I=0; I=N, I=I+100) SUM =SUM + I ; C = C + 1 ; } AV = SUM / C ; cout << "\n Average = " << AV ; getchar(); return 0;
6
Q2 Write a C++ program to calculate the following equation and then print the result of the A A = 1.5 * (B - C) /( D + E)
7
int main(int argc, char* argv[]) { float A , B , C , D , E ; cout << "Enter B : " ; cin >> B ; cout << "Enter C : " ; cin >> C ; cout << "Enter D : " ; cin >> D ; cout << "Enter E : " ; cin >> E ; A = 1.5 * (B - C) /( D + E); cout << "\n A = " << A ; getchar(); return 0; }
8
Q3 Write a program that will perform the following tasks: a. Display the following menu: 1) Add 2) Delete 3) Update 4) Exit b. Ask the user to enter his choice. c. The program will print “You want to ” + the needed task. For example, if the user enter the value 1 then the program will print "You want to add" d. If the user enter a number which is not available then the program will display the message "Invalid number" e. The program will keep running until the user input 4 for exit.
9
default: cout <<"Invalid number"; } }while (op !=4);
int main(int argc, char* argv[]) { int op ; do { cout << "\n" << "1) Add" ; cout << "\n" << "2) Delete" ; cout << "\n" << "3) Update" ; cout << "\n" << "4) Exit" ; cout << "\nEnter your choice, please:\n" ; cin >> op; switch(op) { case 1: { cout << "\n Your want to Add“; break; } case 2: { cout << "\n Your want to Delete“; break; } case 3: { cout << "\n Your want to Update“; break; } default: cout <<"Invalid number"; } }while (op !=4); }
10
Q4 Write a program that will perform the following tasks: a. Ask the user to enter total amount of sales. b. The program will calculate the discount based on the following categories: 20% : amount of sales > = % : amount of sales from 5000 to < % : amount of sales from 1 to <5000 c. Display the total amount of sales before and after discount in addition to the amount of discount. d. The program will keep running until the user input zero.
11
int main(int argc, char* argv[]) { int amtofsals , amtofdis ; cout << "\n Enter the amount of your purchases:" ; cin >> amtofsals; while (amtofsals !=0) { if (amtofsals >= 10000) amtofdis= amtofsals*0.2; else if (amtofsals >= 5000) amtofdis= amtofsals*0.15; else amtofdis= amtofsals * 0.1; cout << "\n Amount before discount: " << amtofsals ; cout << "\n Discount: " << amtofdis; cout << "\n Amount after discount: " << amtofsals - amtofdis ; cout << "\n \n Enter the amount of your purchases:" ; cin >> amtofsals; } getchar();
12
Q5 Write a C++ program which reads two integer numbers and calculate their product using function.
13
#include <iostream>; int mult ( int x, int y ); int main() { int x; int y; cout<<"Please input two numbers to be multiplied: "; cin>> x >> y; cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n"; } int mult ( int x, int y ) { return x * y;
14
Q6 Write a C++ program which store the values of two arrays {5, 10, 15}; {2, 4, 6, 8, 10}; and then print them using function.
15
// arrays as parameters #include <iostream> void printarray (int arg[], int length) int main () { int firstarray[] = {5, 10, 15}; int secondarray[] = {2, 4, 6, 8, 10}; printarray (firstarray,3); printarray (secondarray,5); return 0; } { for (int n=0; n<length; n++) cout << arg[n] << " "; cout << "\n";
16
Q7 Write a C++ program to initialize the data of an employee which has (id number = 1, age=22, salary= ) using structure.
17
struct database { int id_number; int age; float salary; }; int main() database employee; //There is now an employee variable that has modifiable // variables inside it. employee.age = 22; employee.id_number = 1; employee.salary = ; }
18
Q8 Write a C++ program to store the values:11, 22,33 in a file named data.txt.
19
// - Write out to a file. #include <iostream> #include <fstream> int main() { int num1 = 11; int num2 = 22; int num3 = 33; // Step #1 - Declare an output file object file. ofstream outFile; // Step #2 - Open the output file destination. outFile.open(“data.txt"); // Check for success. if (outFile.fail()) { cout << "Error opening \"data.txt.\" for output.\n"; return 1; } // Step #3 - Write to file. outFile << num1 << endl << num2 << endl << num3 << endl; // Successful message. cout << "The data has been written to the file.\n\n"; // Step #4 - Close the output file object. outFile.close(); return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.