Please write a program that will asks to give an integer number K that will be higher than 20.
If the number is not higher than 20 then make the program to show a relevant message and then ask to insert again the number K.

After that, depending on the users choice:
If he/she presses 1, it will be shown the sum of numbers from 1 to K.
If the number is 2, it will be shown the odd numbers from 1 to K.
If the number is 3, the program will end.

If any other numbers are inserted then make your code to show a warning message (that the user must insert numbers from 1 to 3) and then the program must do again the process of inserting the number 1-3.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{   
    int k;
    cout<<"\n Insert a number greater than 20. "; 
    cin>>k; 
    cout<<"\n The number is "<<k<<" ."<<endl<<endl;
    while ( k<=20 ) {
     cout<<"It must be greater than 20. Give again the number. ";
     cin>>k; 
     }    

    int num, sum, odd;
    cout<<"\n Now,give a number from 1 to 3. ";
    cin>>num; 
    cout<<endl;  

  switch (num)
    { case 1: {sum = 0;
           for ( int i=num ; i<=k ; i++)
           sum+=i;
           cout<<"The sum from "<<num<<" to "<<k<<" is: "<<sum<<endl;
               }
           break;

      case 2: { int i = 1;
           cout<<"\n The odd numbers from 1 to "<<k<<" are:\n";
           for ( int i=1 ; i<=k ; i=i+2 )
               cout<<i<<"  "; 
               cout<<"\n";
               odd=i++;
              }
           break;

      case 3: 
           break;

      default: {cout<<"The number must be 1, 2 or 3.\n";
                cout<<"\n Give again the number "; 
                cin>>num; cout<<"\n";
                }
           break;
    }    
    cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Fill an array of 60 elements with positive and negative numbers that will be random.
The range of numbers is from -10 to 10.

Please make the code to show the percentage (%) of negative and positive numbers of the array.
Show the array on the screen with 20 elements in every row. Then the negative and the positive numbers of the array must be shown in two different rows.

( The zero elements are not included in the calculation of percentage. )

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

int main(int argc, char *argv[])
{
    int array[60];      
    int pos=0, neg=0;
    float pos_perc, neg_perc;  
     srand ( time(0) ); 
    for( int i=0 ; i<60 ; i++ ) 
    {
            array[i] = (rand()%21 +(-10));  
            if ( array[i]>0 ) { pos++; }
            else if ( array[i]<0 ) { neg++; }
    }
     pos_perc=(pos*100)/60;
     neg_perc=(neg*100)/60;
     cout<<"\nPercentage of positive numbers of the array: "<<pos_perc<<"%"<<endl;
     cout<<"\nPercentage of negative numbers of the array: "<<neg_perc<<"%"<<endl<<endl;

    for ( int i=0 ; i<60 ; i+=20 ) 
    { for ( int j=i ; j<(20+i) ; j++ )
            cout<<setw(4)<<array[j];
    }
    cout<<endl;

      cout<<"The positive numbers are: "<<endl;

      for ( int i=0 ; i<60 ; i++ )
      { if ( array[i]>0 ) {
           cout<<setw(4)<<array[i];}
      }
      cout<<endl<<endl;

      cout<<"The negative numbers are: "<<endl;

      for ( int j=0 ; j<60 ; j++ )
      { if ( array[j]<0 ) {
           cout<<setw(4)<<array[j];}
      }
      cout<<endl<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

How do you expect us to help you if you do not describe your problem with the program?

A car rental company stores information using database elements of gas consumption and kilometers that every client had done.
That company wants to calculate gas consumption in every 100Km.
Your code must calculate repeatedly this calculation until the user inserts -1.
Then a message will be shown and the program will close.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int KM;
    float lt, consum;
    cout<<"\n Give Km (else press -1 to exit the program): ";
    cin>>KM;
    cout<<endl;
    consum=0; 
    while ( KM!=-1 ) {    
        cout<<" Give litre that are consumed: ";
        cin>>lt;
        cout<<endl;
        consum=(100*lt)/KM;  
        cout<<" Consumption at 100Km is: "<<consum<<"lt"<<endl;
        cout<<"\n Give Km (else press -1 to exit the program): ";
        cin>>KM;
        cout<<endl;
        }

    system("PAUSE");
    return EXIT_SUCCESS;
}

Your code must return digits of an integer number M. User is giving that number and must be at least with 5 digits. In order to insert a five-digit number use do…while. Then invert the number M and show that on the screen. (You can use modulo %)

**An example: **
The number you gave is: 12345
The invert number is: 54321

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int M,inv;
    do {
       cout<<"\n Give a number that has at least 5 digits: ";
       cin>>M;
       cout<<endl;
       }
    while ( M<=9999 && M>=(-9999) );
       while ( M>1 || M<-1 ) 
       {
         inv=M%10;
         if ( M<0 ){
            inv=inv*(-1); 
            cout<<inv;
            }
            else 
             cout<<anestramenos;
            M=M/10;
       }
       cout<<"\n\n That was the inverted number of the number you had entered."<<endl<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}

A string contains words that are separated by at least one space. Make the code to show the number of letters that every word has and show on screen the lower case letters of each word. Use function islower().

It would be useful if you were using functions that will have as parameters strings or pointers to strings.

#include <cstdlib>
#include <iostream>
const int MAXCHAR=81; 

using namespace std;

int main(int argc, char *argv[])
{   
    char str[MAXCHAR];
    int spaces=0;
    cout<<"Give a sentence. ";
    cin.getline(str,MAXCHAR);  
    for ( int i=0 ; str[i]!='\0' ; i++ )
    {if ( islower(str[i]) )      
    {
        cout<<str[i];
     } 
         else if ( isspace(str[i]) )    
         {
              cout<<" ";
         }                         
    }
    for ( int i=0 ; str[i]!='\0' ; i++ )
    {   if( isspace(str[i]) )    
        spaces++;
    }
    cout<<"\n The sentence has "<<strlen(str)-spaces<<" characters.";  
    cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

The user types in data of basketball players in a game.
Your code must read those and insert them in an array of structures.

Each structure must have the following data elements (members):
1) Last name of player
2) Scores
3) Fouls

After inserting members of each player, user must have the ability to say if he/she wants to be shown on screen players that scored more than 6 scores and had less than 2 fouls.
Use functions for finding players with the above variables and to ask the user to insert data.

#include <cstdlib>
#include <iostream>

using namespace std;

struct player
    {      char Lname[40];     
           int Scores; 
           int Fouls;
    }array[5];     
player inElements()
{      for( int i=0 ; i < 5 ; i++ )       
  {     cout<<"Give players data.";
        cout<<"\n Name: ";
        while ( cin.get()!='\n');
        cin.getline( array[i].Lname, 40 );  
        cout<<"Points: ";
        cin>> array[i].Scores;
        cout<<"Fouls: ";
        cin>> array[i].Fouls;
    }
}
player outElements()
{       for ( int i=0 ; i < 5 ; i++ )
        {   cout<< array[i].Lname <<" with "<< array[i].Scores;
            cout<<" points and "<<array[i].Fouls<<" fouls.\n";
        }       
}
void find()
{    cout<<"Players you've asked for are: ";
     for ( int i=0 ; i < 5 ; i++ )
    {if( (array[i].Scores>6) && (array[i].Fouls<2) )
      {cout<< array[i].Lname <<" who scored ";
       cout<< array[i].Scores <<" points and "<< array[i].Fouls <<" fouls. ";
      }
    }
}

int main(int argc, char *argv[])
{   
    char ans;
    inElements();
    cout<<"Data of players are \n";
    outElements();
    cout<<"Would you like to show players that had less than 2 faouls and scored more than 6 points? \n";
    cout<<"Press N if you want to, else press O.";
    cin>>ans;
    if ( ans=='N' || ans=='n' )   
       {  find();           
       }
    else if(ans=='o' || ans=='O')
         {   
         cout<<" Thank you. Now the program is going to close.";
         }
    cout<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.