Sunday, January 24, 2016

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... thumbnail 1 summary

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

No comments

Post a Comment