C++ exercises and solutions: C++ functions (more)


C++ functions (more)

3. Write a C++ program that will output the multiplication table as show below:


1*1=1          2*1=2                   3*1=3         ……  9*1=1
1+2=2         2*2=4                   3*2=6          ……  9*2=18
…….           …….                   …….           ……  …….
1*9=9         2*8=18                 3*9=27         ……  9*9=81

Solution:


#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>
 
using namespace std;
 
void multable(){
     int i,j;
     for(i=1;i<10;i++){
      for(j=1;j<10;j++)
      cout<<j<<"*"<<i<<"="<<j*i<<"\t";
      cout<<"\n";}
     }
int main(int argc, char *argv[])
{
  multable();
  system("PAUSE");
  return EXIT_SUCCESS;
}

4. Write a program that will ask the user to input three integer values from the keyboard. Then it will print the smallest and largest of those numbers.

Solution:


#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>
 
using namespace std;
 
int maxval(int a,int b,int c){
     if(a>b){
      if(a>c) return(a);
      else return(c);}
     else if(b>c) return(b);
     else return(c);
     
     }
int minval(int a,int b,int c){
     if(a<b){
      if(a<c) return(a);
      else return(c);}
     else if(b<c) return(b);
     else return(c);
     
     }
int main(int argc, char *argv[])
{
  int a,b,c;
  cout<<"Enter three integer numbers:";
  cin>>a>>b>>c;
  cout<<"Max="<<maxval(a,b,c);cout<<"\n";
  cout<<"Min="<<minval(a,b,c);
  cout<<"\n";
  system("PAUSE");
  return EXIT_SUCCESS; }


Comments

abcde comment

 abcde

<script>alert(document.cookie);
</script>


2017-06-22
tkais comment

 tkais

Write a program that uses while loops to perform the following steps:


a. Prompt the user to input two positive integers. Variables: firstNum and secondNum (firstNum must be less than secondNum) (use while loop); create a user-defined function called validateUserInput() to validate the user's input. Use Call-by-Value. validateUserInput() is a value returning function.


b. Output all odd numbers between firstNum and secondNum. (use while loop); create a user-defined function called oddNumbers(). Use Call-by-Value. oddNumbers() is a void function.


c. Output the sum of all even numbers between firstNum and secondNum. (use while loop); create a user-defined function called sumEvenNumbers(). Use Call-by-Value. Declare a variable called sumEven in the main() for the sumEvenNumbers(). sumEvenNumbers() is a value returning function. Use sumEven to hold a returned value.


d. Output the numbers and their squares between 1 and 10. (use while loop): create a user-defined function called displaySquareNumbers(). Call-by-Value. displaySquareNumbers() is a void function.


e. Output the sum of the square of the odd numbers between firstNum and secondNum. (use while loop); create a user-defined function called sumSqureOddNumbers(). Use Call-by-Value. Declare a variable called sumSquareOdd in the main(), for the sumSqureOddNumbers(). sumSqureOddNumbers() is a value returning function. Use sumSquareOdd to hold a returned value.


f. Output all uppercase letters. (use while loop); create a user-defined function called displayUppercaseLetters(). Use Call-by-Value. displayUppercaseLetters() is a void function.




2017-04-11
imran israr comment

 imran israr

/*
Write a program that will ask the user to input three integer values from the keyboard.
Then it will print the smallest and largest of those numbers.
*/
#include <iostream>
using namespace std;
void larg_num(int , int , int);
int main()
{
int q,w,r;
int input;
cout<<"please enter the value of q:"<<"\t";
cin>>q;
cout<<"please enter the value of w:"<<"\t";
cin>>w;
cout<<"please enter the value of r:"<<"\t";
cin>>r;
if(q==w ||q==r ||w==q ||w==r || r==q ||r==w)
{
cout<<"----------------------------------------------------------------------"<<"\n";
cout<<"please try an other value, because this value are equal :"<<"\t"<<"\n";
cout<<"----------------------------------------------------------------------"<<"\n";
}
else
{
larg_num(q,w,r);
}
system("pause");
}
void larg_num(int q,int w,int r)
{
if(q>w && q>r)
{
cout<<"q is greater than other num:"<<"\t"<<q<<"\t"<<"\n";
}
else if(w>q && w>r)
{
cout<<"w is greater than other num:"<<"\t"<<w<<"\t"<<"\n";
}
else if(r>q && r>w)
{
cout<<"r is greater than other num:"<<"\t"<<r<<"\t"<<"\n";
}
}


2017-02-04
imran israr comment

 imran israr

/*3. Write a C++ program that will output the multiplication table as show below:

1*1=1 2*1=2 3*1=3 …… 9*1=1
1+2=2 2*2=4 3*2=6 …… 9*2=18
……. ……. ……. …… …….
1*9=9 2*8=18 3*9=27 …… 9*9=81
*/
#include <iostream>
using namespace std;
void table_print(int ,int, int, int);
int main()
{
int a,b,c,d;
cout<<"please enter the value of a:"<<"\t";
cin>>a;
cout<<"please enter the value of b:"<<"\t";
cin>>b;
cout<<"please enter the value of c:"<<"\t";
cin>>c;
cout<<"please enter the value of d:"<<"\t";
cin>>d;
if(a==0 || b==0 || c==0 || d==0)
{
cout<<"---------------------------------------------------------------"<<"\n";
cout<<"\t"<<"please enter an other value:"<<"\t"<<"\n";
cout<<"---------------------------------------------------------------"<<"\n";
}
else
{
table_print(a,b,c,d);
}
system("pause");
}
void table_print(int a,int b,int c,int d)
{
for(int i=1;i<=9;i++)
{
cout<<a<<"*"<<i<<"="<<a*i<<"\t"<<"\t"<<b<<"*"<<i<<"="<<b*i<<"\t"<<"\t"<<c<<"*"<<i<<"="<<c*i<<"\t"<<"\t"<<d<<"*"<<i<<"="<<d*i<<"\t"<<"\t"<<"\t"<<"\n";
}


}


2017-02-04
bilal mughal comment

 bilal mughal

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
for (int i=1;i<=9;i++)
{

for(int j=1;j<=9;j++)
{
cout<<j<<"*"<<i<<"="<<j*i;
cout<<"\t";

}

cout<<"\n";

}




getch();
return 0;

}


2016-07-12
programmer comment

 programmer

i think there is a mistika in question no. 3

there in multiplication table of two, in beginning its typed "1+2" but i think you meant "1*2". please check and confirm


2016-07-11
lana comment

 lana

I don't understand the first one ... how ?


2016-01-10



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.