CS201 Assignment 1 ...

Share:
Clear all

# [Solved] CS201 Assignment 1 Fall 2021

(@zero)
Estimable Member
Joined: 1 year ago
Posts: 125
Topic starter

CS201 Assignment 1 Solution Fall 2021 | CS201 Assignment Solution 2021

This topic was modified 2 months ago by Vu Expert

Joined: 3 years ago
Posts: 6751

CS201 Assignment 1 Fall 2021 solution idea:

Code:

```#include <iostream>
#include <string>

using namespace std;

void printnameid(string studentid, string studentname);
int calculatelastthreedigits(string studentid);

int main()
{
string studentid="MC12345678"; // Student id
string studentname="QueryVU"; // Student name

printnameid(studentid,studentname);
int TotalLastThreeDigits=calculatelastthreedigits(studentid);
int counter=1;
int a,b,c;
// Students id last 3 digits
a=4;
b=3;
c=1;
cout<<""<<endl;
cout<<"Sum of Last 3 digits is="<<a+b+c<<endl;
cout<<""<<endl;
if ( TotalLastThreeDigits % 2 == 0)   // Divide by 2 and see if the reminder is zero? then it is even otherwise it is odd number
{
cout<< "   Sum is an even value "<<endl;
cout<<""<<endl;
cout<<"++++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<""<<endl;
while(counter <= TotalLastThreeDigits)
{
cout << " Iteration: " <<counter <<endl;
cout << "My student id is:" <<studentid<<endl;
counter++;
}
}
else
{
cout << "   Sum is an odd value:"<<endl;
while(counter <= TotalLastThreeDigits)
{
cout<< " Iteration: "<<counter<<endl;
cout<< "My name is "<<studentname<<endl;
counter++;
}
}
return 0;
}

void printnameid(string studentid, string studentname){
cout<<" My name "<<studentname <<endl;
cout<<" My student id is "<<studentid<<endl;
}

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';
}