














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















Write a program which asks for length and width of a rectangle and calculates the area and perimeter.
## include using namespace std; int main() { float l, w, a, p; cout << "Enter length : "; cin >> l; cout << "Enter width : "; cin >> w; a = l * w; p = 2 * (l + w); cout << "\nArea = " << a; cout << "\nPerimeter = " << p; return 0; } ``` ## Question No. 2 Write a program which asks for a number input from the keyboard. Add 40 to the number and then divide it by 5. Now, take modulus of that number with 2 and then multiply the resultant value by 10. Display the result. ```cpp ## include using namespace std; int main() { int num, x; cout << "Enter a number: "; cin >> num; x = num + 40; x = x / 5; x = x % 2; x = x * 10; cout << "The final result is " << x; return 0; } ``` ## Question No. 3 Write a program to input an integer value. If the entered value is divisible by 2, print "Divisible by 2" otherwise print "Not divisible by 2" on the computer screen using conditional operator. ```cpp ## include using namespace std; int main() { int num; cout << "Enter a number : "; cin >> num; (num % 2 == 0)? cout << "Divisble by 2" : cout << "Not divisible by 2"; return 0; } Write a program which asks for names of subjects (minimum 7), and then ask for marks in each subject one by one. After the input, it should display the marks of subjects against their name (use setw() manipulator). At the end, it should display the total calculated percentage marks (assume each subject has a maximum of 100 marks).
## include ## include ## include using namespace std; int main() { string s1, s2, s3, s4, s5, s6, s7; int m1, m2, m3, m4, m5, m6, m7; float percentage; cout << "Enter name for Subject 1: "; cin >> s1; cout << "Enter name for Subject 2: "; cin >> s2; cout << "Enter name for Subject 3: "; cin >> s3; cout << "Enter name for Subject 4: "; cin >> s4; cout << "Enter name for Subject 5: "; cin >> s5; cout << "Enter name for Subject 6: "; cin >> s6; cout << "Enter name for Subject 7: "; cin >> s7; cout << "Enter marks for " << s1 << ": "; cin >> m1; cout << "Enter marks for " << s2 << ": "; cin >> m2; cout << "Enter marks for " << s3 << ": "; cin >> m3; cout << "Enter marks for " << s4 << ": "; cin >> m4; cout << "Enter marks for " << s5 << ": "; cin >> m5; cout << "Enter marks for " << s6 << ": "; cin >> m6; cout << "Enter marks for " << s7 << ": "; cin >> m7; percentage = ((m1 + m2 + m3 + m4 + m5 + m6 + m7) * 100) / 700; cout << endl; cout << setw(40) << s1 << setw(40) << m1 << endl; cout << setw(40) << s2 << setw(40) << m2 << endl; cout << setw(40) << s3 << setw(40) << m3 << endl; int main() { float len, x, y, in; int ft; cout << "Enter length in meters: "; cin >> len; x = len * 3.281; ft = x; y = x - ft; in = y * 12; cout << len << " meters = " << ft << " feet " << in << " inches"; return 0; } ``` ## Question No. 1 Write a program which takes three integer values as user input and displays the maximum value as output. Use if-else statement. ```cpp ## include using namespace std; int main() { int a, b, c; cout << "Enter first number: "; cin >> a; cout << "Enter second number: "; cin >> b; cout << "Enter third number: "; cin >> c; if (a > b && a > c) { cout << "Maximum = " << a; } else if (b > a && b > c) { cout << "Maximum = " << b; } else { cout << "Maximum = " << c; } return 0; } ``` ## Question No. 2 Write a program which takes the value from the user as input electricity unit charges and calculate total electricity bill according to the given condition. Use if-else statement. a. For the first 50 units Rs. 0.50/unit b. For the next 100 units Rs. 0.75/unit c. For the next 100 units Rs. 1.20/unit d. For unit above 250 Rs. 1.50/unit e. An additional surcharge of 20% is added to the bill ```cpp ## include using namespace std; int main() { int unit; float amount, surcharge, total; cout << "Input your electricity unit charges: "; cin >> unit; if (unit <= 50) { amount = unit * 0.50; } else if (unit <= 150) { amount = (50 * 0.50) + ((unit - 50) * 0.75); } else if (unit <= 250) { amount = (50 * 0.50) + ((unit - 150) * 1.20); } else { amount = (50 * 0.50) + ((unit - 250) * 1.50); } surcharge = amount * 0.20; total = amount + surcharge; cout << "The total electricity bill is " << total; return 0; } ``` ## Question No. 3 Write a program which takes two integer values as user input and then check if both are equal and tells user the result. If not equal, then it should also display the maximum value as output. Use switch statement. ```cpp ## include using namespace std; int main() { int a, b; cout << "Enter first value: "; cin >> a; cout << "Enter second value: "; cin >> b; switch (a == b) { case 1: cout << "Values are equal"; break; case 0: switch (a > b) { case 1: cout << "Maximum = " << a; break; case 0: cout << "Maximum = " << b; break; } } return 0; } ``` ## Question No. 4 Write a program which takes three integer values as user input and check if all three or any two are equal and display to user that which values are equal (i.e., suppose you have three variables a,b,c then the output should be like: a and b or equal / All are equal / None of the values are equal). For the case when all three or any two values are unequal, it should check for maximum value and print it as output. Use if-else statement. ```cpp ## include using namespace std; int main() { int a, b, c; cout << "a = "; cin >> a; cout << "b = "; cin >> b; cout << "c = "; cin >> c; if (a == b && a == c) { cout << "All are equal"; } else if (a == b) { cout << "a and b are equal" << endl; if (a > c) { cout << "Maximum = " << a; } else { cout << "Maximum = " << c; } } else if (a == c) { cout << "a and c are equal" << endl; if (a > b) { cout << "Maximum = " << a; } else { cout << "Maximum = " << b; } } else if (b == c) { cout << "b and c are equal" << endl; if (b > a) { cout << "Maximum = " << b; } else { cout << "Maximum = " << a; } } else { cout << "None of the values are equal" << endl; if (a > b && a > c) { cout << "Maximum = " << a; } else if (b > a && b > c) { cout << "Maximum = " << b; } else { cout << "Maximum = " << c; } } return 0; } ``` ## Question No. 5 The code mentioned under heading 3 (the if-else if ladder), takes two integer values and required arithmetic operation as user inputs and then displays the output after that operation. Write a program which asks user to input two float values and required arithmetic operation to be done and then perform that operation on those two values. Now, it should take ask user to input another float value and an arithmetic operation and then it should perform that arithmetic operation on the result of operation on first two values and third value. Use if-else statement. int main() { float a, b, c, r1, r2, disc, real, imag; cout << "a = "; cin >> a; cout << "b = "; cin >> b; cout << "c = "; cin >> c; disc = pow(b, 2) - 4 * a * c; if (disc > 0) { r1 = (-b + sqrt(disc)) / (2 * a); r2 = (-b - sqrt(disc)) / (2 * a); cout << "Roots are : " << endl; cout << r1 << endl; cout << r2 << endl; } else if (disc < 0) { real = -b / (2 * a); im ## Grade System ## Grade A+ Percentage >= 90% ## Grade A Percentage >= 85% ## Grade B+ Percentage >= 80% ## Grade B Percentage >= 70% ## Grade C+ Percentage >= 60% ## Grade C Percentage >= 40% ## Grade D Percentage < 40% ## Grade F ```cpp ## include using namespace std; int main() { float perc; cout << 'Enter percentage: '; cin >> perc; switch (perc >= 95) { case 1: cout << 'Grade A+'; break; case 0: switch (perc >= 90 && perc < 95) { case 1: cout << 'Grade A'; break; case 0: switch (perc >= 85 && perc < 90) { case 1: cout << 'Grade B+'; break; case 0: switch (perc >= 80 && perc < 85) { case 1: cout << 'Grade B'; break; case 0: switch (perc >= 70 && perc < 80) { case 1: cout << 'Grade C+'; break; case 0: switch (perc >= 60 && perc < 70) { case 1: cout << 'Grade C'; break; case 0: switch (perc >= 40 && perc { case 1: cout << 'Grade D'; break; case 0: cout << 'Grade F'; break; } } } } } } } return 0; } ``` int main() { int h, m; cout << 'Input The time in 24 hours format ' << endl; cout << 'Input Hours : '; cin >> h; cout << 'Input Minutes : '; cin >> m; if (h >= 12 && h <= 24) { cout << 'The time is in PM ' << endl; } else if (h >= 0 && h <= 11) { cout << 'Time is in AM ' << endl; } else { cout << 'Input is Wrong ' << endl; } return 0; } ``` ## Problem 4 Write a program which takes an integer as user input and if it is odd then convert it to its upper even number. ```cpp ## include using namespace std; int main() { int num; cout << 'Enter a number = '; cin >> num; if (num % 2 != 0) { cout << 'Rounded off to nearest upper even = ' << num + 1; } else { cout << 'Number is already even = ' << num; } return 0; } ``` ## Problem 5 Write a program which takes two values as user input and then swap the values of the two variables to which those are assigned. ```cpp ## include using namespace std; int main() { int a, b, temp; cout << 'a = '; cin >> a; cout << 'b = '; cin >> b; cout << endl << 'Before swapping.' << endl; cout << 'a = ' << a << ', b = ' << b << endl; temp = a; a = b; b = temp; cout << endl << 'After swapping.' << endl; cout << 'a = ' << a << ', b = ' << b << endl; return 0; } ``` ## Problem 6 Write a program which checks whether an input is an alphabet, digit, or special character. ```cpp ## include using namespace std; int main() { char ch; cout << 'Enter any character'; cin >> ch; if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { cout << ch << ' is an Alphabet'; } else if (ch >= '0' && ch <= '9') { cout << ch << ' is a Digit'; } else { cout << ch << ' is a Special Character'; } return 0; } ``` ## Problem 7 Write a program which checks whether an input character is a vowel or not. ```cpp ## include using namespace std; int main() { char c; cout << 'Enter a character : '; cin >> c; if (c == 'a' || c == 'u' || c == 'A' || c == 'U') cout << c << ' is a vowel.'; else cout << c << ' is a consonant.'; return 0; } ``` ## Question No. 1 Write a program which checks whether an input integer is prime number or not. ```cpp ## include using namespace std; int main() { int num, flag=0; cout<<"Enter any number:"; cin>>num; for (int i=2; i<=num/2; i++) { if(num%i == 0) { flag=1; break; } i++; } if(flag == 0) cout<<num<<" is a Prime Number"<<endl; else cout<<num<<" is Not a Prime Number"<<endl; return 0; } ``` ## Question No. 2 Write a program takes an integer base (say x) and integer exponent (say y) as user input, calculates $x^y$ and display it. Do not use power function (cmath library). The program should also be able to provide correct output for negative value inputs. ```cpp ## include using namespace std; ## Question No. 5 Write a program which takes an integer value as user input and calculates its multiplication table and store value at each iteration (use arrays). At the end, it should display the values. ```cpp ## include using namespace std; int main() { int n, limit; cout << "Input a number: "; cin >> n; cout << "Enter limit : "; cin >> limit; int table[limit][3]; for (int i = 0; i < limit; i++) { table[i][0] = n; table[i][1] = i+1; table[i][2] = table[i][0] * table[i][1]; } for (int i = 0; i < limit; i++) { cout << table[i][0] << " * " << table[i][1] << " = " << table[i][2]<<endl; } return 0; } ``` ## Question No. 6 Write a program which converts a user-input decimal number to binary and octal. Use arrays. ```cpp ## include using namespace std; int main() { int n, octal[100]={0}, bin[100]={0}; int temp1, temp2, i, j, k, l; cout << "Enter decimal number : "; cin >> n; temp1 = n; temp2 = n; for(i=0; temp1!=0; i++) { bin[i] = temp1%2; temp1 = temp1/2; } cout<<endl<<"Binary number = "; for(j=(i-1); j>=0; j--) { cout<<bin[j]; } for(k=0 ; temp2!=0; k++) { octal[k] = temp2 % 8; temp2 = temp2 / 8; } cout<<endl<<"Octal number = "; for (l = k - 1; l >= 0; l--) { cout << octal[l]; } return 0; } ``` ## Question No. 7 Write a program which takes total number of entities n as user input and prints Fibonacci series. Fibonacci series is a series where the current number is the sum of previous two terms i.e., 0, 1, 1, 2, 3, 5, 8, 13, 21,………, ((n-1)+(n-2)). Use arrays. ```cpp ## include using namespace std; int main () { int n; cout << "Enter total number of entities : "; cin >> n; int f[n]; int i; f[0] = 0; f[1] = 1; for (i = 2; i < n; i++) { f[i] = f[i-1] + f[i-2]; } for (i = 0; i < n; i++) { cout<<f[i]<<" "; } return 0; } ``` ## Question No. 8 Write a program which takes array size and elements as user input and then sort the elements in ascending and descending order. ## Question No. 1 Write a program which takes two numbers and an arithmetic operator as user input and displays the result. The program should pass these inputs to a function which do the specified operation and return the answer. ## Function Definitions ```cpp float sum(float num1, float num2) { return num1 + num2; } float sub(float num1, float num2) { return num1 - num2; } float mul(float num1, float num2) { return num1 * num2; } float divi(float num1, float num2) { return num1 / num2; } ``` ## Main Program ```cpp ## include using namespace std; int main() { float num1, num2, res; char op; cout << "Enter the 1st number = "; cin >> num1; cout << "\nEnter the 2nd number = "; cin >> num2; cout << "\nEnter your desired operator = "; cin >> op; if (op == '+') res = sum(num1, num2); cout << "\nSum = " << sum(array, n) << endl; cout << "\nAverage = " << avg(array, n) << endl; cout << "\nLargest = " << max(array, n) << endl; cout << "\n Smallest = " << min(array, n) << endl; return 0; } ``` The program takes an array size as input from the user, then prompts the user to enter the array elements. It then calls the appropriate functions to calculate the sum, average, largest, and smallest values of the array and displays the results. ## Question No. 3 Write a program that takes as input your gross salary and your total saving and uses another function named to calculate your tax. This tax calculator function should take as parameters the gross salary as well as the total savings amount. The tax calculator function should use separate sub- functions for each tax slab. ## Function Definitions ```cpp float slab0() { return 0; } float slab1(float x) { return (0.10 * (x - 100000)); } float slab2(float x) { return (slab0() + slab1(x) + (0.20 * (x - 200000))); } float tax(float gross, float savings) { float x; if (savings <= 100000) { x = gross - savings; } else { x = gross - 100000; } if (x > 0 && x <= 100000) { return slab0(); } else if (x > 100000 && x <= 200000) { return slab1(x); } else if (x > 200000 && x <= 500000) { return slab2(x); } else { return (slab0() + slab1(x) + slab2(x) + (0.30 * (x - 500000))); } } ``` ## Main Program ```cpp ## include using namespace std; int main() { float gross, savings; cout << "Enter your gross salary: "; cin >> gross; cout << "Enter total savings: "; cin >> savings; cout << "The calculated amount of tax is " << tax(gross, savings); return 0; } ``` The program takes the user's gross salary and total savings as input, and then calls the tax() function to calculate the tax amount. The tax() function uses the slab0(), slab1(), and slab2() functions to calculate the tax based on the given tax slabs. ## Question No. 4 Write a function that takes your date of birth in YYYY, MM and DD format (separated by spaces) as input as well as the current date, in same format, and calculates your age in years, months and days. Use separate function for age calculation. ## Function Definitions ```cpp void calculateAge(int dob_year, int dob_month, int dob_day, int curr_year, int curr_month, int curr_day) { int years, months, days; years = curr_year - dob_year; months = curr_month - dob_month; days = curr_day - dob_day; if (months < 0) { years--; months += 12; } if (days < 0) { months--; days += 30; } cout << "Your age is: " << years << " years, " << months << " months, and } ``` ## Main Program ```cpp ## include using namespace std; int main() { int dob_year, dob_month, dob_day, curr_year, curr_month, curr_day; cout << "Enter your date of birth (YYYY MM DD): "; cin >> dob_year >> dob_month >> dob_day; cout << "Enter the current date (YYYY MM DD): "; cin >> curr_year >> curr_month >> curr_day; return 0; } ``` **Explanation** The program starts by taking the total number of subjects and the number of students as input from the user. It then creates three arrays: marks, total, and perc. The marks array is a 2D array that stores the marks of each student in each subject. The total array stores the total marks of each student, and the perc array stores the percentage marks of each student. The program also creates a subject array to store the names of the subjects. The user is then prompted to enter the names of the subjects. For each student, the program prompts the user to enter the marks in each subject, calculates the total marks, and stores the percentage in the perc array. Finally, the program displays the percentage marks of each student. **Notable Examples or Evidence** The program demonstrates the use of 2D arrays to store the marks of each student in each subject, and the use of separate arrays to store the total marks and percentage marks of each student. The program also demonstrates the use of nested loops to iterate through the students and subjects, and the use of string variables to store the names of the subjects. **Student Data Management** ## Student Information The program takes the data of students as user input. The data includes the following information for each student: Name Age Roll Number Marks in 3 subjects The program stores this information in a structure variable for each student. ## Functions The program includes the following functions: input_student_data(): This function takes the user input for a single student and stores it in a structure variable. display_student_data(): This function takes a structure variable as input and displays the student's information. ## Main Function In the main() function, the program performs the following steps: Prompts the user to enter the number of students. Dynamically allocates memory for an array of student structure variables. Calls the input_student_data() function for each student to store their information. Calls the display_student_data() function for each student to display their information. Frees the dynamically allocated memory. ## Example Output ``` Enter the number of students: 2 Student 1: Name: John Doe Age: 20 Roll Number: 101 Marks in Subject 1: 85 Marks in Subject 2: 92 Marks in Subject 3: 78 Student 2: Name: Jane Smith Age: 19 Roll Number: 102 Marks in Subject 1: 90 Marks in Subject 2: 88 Marks in Subject 3: 82 ``` ## Conclusion The program demonstrates the use of structures, dynamic memory allocation, and function calls to manage student data. It provides a basic framework for storing and displaying student information. **Student Data Management System** ## User Input and Data Structure The program begins by prompting the user to enter the total number of students. It then creates a structure variable student to store the data for each student, which includes the following fields: Name Registration number Section (either 'A' or 'B') Marks in the following subjects: Calculus Introduction to Aeronautics ICP Physics English Pakistan Studies