146k views
4 votes
C++ Language Use functions to solve all problems in the mentioned assignment

C++ Language Use functions to solve all problems in the mentioned assignment-example-1
User Shiroko
by
3.8k points

1 Answer

4 votes

Answer:

Following are the program to this question:

Q1:

#include <iostream> //including header file

using namespace std;

int main() //defining main method

{

int m,n,i; //defining integer variable

cout<<"Enter starting value: "; //print message

cin>>m; //input m variable value

cout<<"Enter ending value: ";//print message

cin>>n;//input n variable value

for(i=m;i<=n;i++) //defining loop to count even number

{

if(i%2==0) //check even number condition

{

cout<<i<<" "; //print value

}

}

return 0;

}

Q2:

#include<iostream> //include header file

using namespace std;

void menu() //define method menu

{

cout<<"Enter 1 for print your name and id: "<<endl; //print message

cout<<"Enter 2 for sum of odd numbers from 1 to 20: "<<endl;//print message

cout<<"Enter 3 for reverse of any number: "<<endl;//print message

cout<<"Enter 4 for exit"<<endl;//print message

}

void findsum() //define method findsum

{

int s=0,i; //defining integer variable

for(i=1;i<=20;i++) //defining loop to count odd number

{

s=s+i; //add numbers

}

cout<<"Odd number sum from 1 to 20: "<<s; //print value

}

void revers() //defining method revers

{

int n,r,rev=0; //defining integer variable

cout<<"enter any number: "; //print message

cin>>n; //input value

while(n!=0) //define loop to reverse value

{

r=n%10; //store value in r variable

rev=(rev*10)+r; //add reverse value

n=n/10; //divide digit

}

cout<<"The reverse of a given number is: "<<rev; //print reverse value

}

int main() //defining main method

{

int c,rn; //defining integer variable

char n[30]; // defining char variable

menu(); //calling menu method

cout<<"Enter your selected choice: "<<endl; //print message

cin>>c; //input value in char variable

switch(c) //defining switch

{

//defining case

case 1: cout<<"Enter your name: "<<endl; //print message for input name and id

cin>>n;//input name

cout<<"Enter registration number: "<<endl; //print message

cout<<"Name: "<<n<<endl; // print name value

cout<<"Registration number: "<<rn<<endl; // print id value

break;

case 2: findsum(); //calling findsum method

break;

case 3: revers(); //calling reverse method

break;

case 4: exit(1); // calling exit method

break;

default: cout<<"please enter valid choice"; //defining default choice

break;

}

return 0;

}

Q3:

#include <iostream>//defining header file

using namespace std;

void reverse() //defining method readInput

{

char x; //defining char variable

cout<<"Enter a character: "; //print message

cin>>x; //input value

if(x=='.') //defining condition that check value is .

return; //using return keyword

reverse(); //calling readInput method

cout<<x; //print value

}

int main() //defining main method

{

reverse(); //calling readInput method

return 0;

}

Output:

please find the attachment.

Step-by-step explanation:

Description of the given question as follows:

  • In question 1, three integer variable "m, n and i" is declared, in which variable "m and n" is used in user input, and variable "i" is used a loop to print even number from the user input values.
  • In question 2, a menu method is declared that prints a list menus in the next line, two methods "findsum() and revers()"is declared, in which "findsum" method is used to add odd values from 1 to 20 and the revers method is used to reverse value from an input digit. In the last line main method is declared, which uses switch to call the above methods.
  • In question 3, in this, a reverse method is defined, in which a char variable x is declared that input char value and define if block to check if char is equal to (.) if this is correct it will print the value in reverse order.
C++ Language Use functions to solve all problems in the mentioned assignment-example-1
User Hiergiltdiestfu
by
4.8k points