modules:composer.user_said_in, @cyberian, CS201 Assignment 1 Solution and Discussion
// C201 Assignment No: 1 Fall2021 #include <iostream> #include <string> using namespace std; void printnameid(string studentid, string studentname); int calculatelastthreedigits(string studentid); int main() { string studentid="bc123456789"; string studentname="ZAHID"; printnameid(studentid,studentname); int TotalLastThreeDigits=calculatelastthreedigits(studentid); int counter=1; int a,b,c; a=6; b=4; c=0; cout<<"Sum of Last Three Numbers ="<<a+b+c; cout<<"\n\n"; if ( TotalLastThreeDigits % 2 == 0) // Divide by 2 and see if the reminder is zero? then it is even otherwise it is odd number { cout << "the sum is an even value: \n\n"; cout<<"++++++++++++++++++++++++++++++++++++++++++++ \n\n"; while(counter <= TotalLastThreeDigits) { cout << " Iteration: " << counter << "\n"; cout << "My student id is:" << studentid << "\n"; counter++; } } else { cout << "the sum is an odd value: \n\n"; while(counter <= TotalLastThreeDigits) { cout << " Iteration: " << counter << "\n"; cout << "My name is " << studentname << "\n"; counter++; } } return 0; } void printnameid(string studentid, string studentname){ cout<<"My name is " << studentname << "\n\n\n"; cout<<"My student id is:" << studentid << "\n\n\n"; } int calculatelastthreedigits(string studentid) { int end=studentid.length(); // Ending point that is total length of string int start=end-3; // Starting point string lastthreedigits=studentid.substr(start,end); // Trim the last three digits; int total=0; //Calculate the sum of last three digits for ( int index=0; index < lastthreedigits.length(); index++) { total += lastthreedigits[index] - '0'; } return total; // return the total to calling statement. }Write a C++ program that performs the following tasks:
1-Print your name and VU id.
2-Add last 3 digit of your VU id.
3-Display the result of sum on screen.
4-Use if-else statement ::
a) If sum is odd then print your name using while loop. Number of iterations of while loop should be equal to the sum.
b) If sum is even then print your VU id using while loop. Number of iterations of while loop should be equal to the sum.
[use remainder operator on sum value to determine the odd and even value for if condition]
For example, suppose the student id is BC123456781. Then by adding last 3 digits of vu id, we get 16 which is an even number. In this case, program should print your VU ID for 16 times using while loop.