Drawing graphs and bar charts
Posted by Samath
Last Updated: December 30, 2020
  5554

One interesting application of computers is drawing graphs and bar charts. Write a program in C++ that reads five numbers (each between 1 and 30). Assume that the user enters only valid values. For each number that is read, your program should print a line containing that number of adjacent asterisks. For example, if your program reads the number 7, it should print *******.

Code:

#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
    int number[5];
    for (int i = 0; i < 5; i++)
    {
        cout << "Enter a between 1 and 30: ";
        cin >> number[i];
        if (number[i] < 1 || number[i] > 30)
        {
            cout << "Invalid number" << endl;
            i--;
        }


    }
    cout << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << number[i] << ": ";
        for (int j = 0; j < number[i]; j++)
        {
            cout << "*";
        }

        cout << endl;

    }
    system("pause");
    return 0;
}

   
  
 
 
   

 
 
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
	int number1;
	int number2;
	int number3;
	int number4;
	int number5;
	
		   cout<<"Enter a between 1 and 30: ";
	       cin>>number1;
	       
   		   cout<<"Enter a between 1 and 30: ";
	       cin>>number2;
	       
   		   cout<<"Enter a between 1 and 30: ";
	       cin>>number3;
	       
   		   cout<<"Enter a between 1 and 30: ";
	       cin>>number4;
	       
   		   cout<<"Enter a between 1 and 30: ";
	       cin>>number5;
	    
        cout<<endl;
	    cout<<number1<<": ";
	    for(int j = 0;j < number1; j++ )
	    {
	    	cout<<"*";
	    }
	    
	    cout<<endl;
	    cout<<number2<<": ";
	    for(int j = 0;j < number2; j++ )
	    {
	    	cout<<"*";
	    }
	    
	    cout<<endl;
	    cout<<number3<<": ";
	    for(int j = 0;j < number3; j++ )
	    {
	    	cout<<"*";
	    }
	    
	    cout<<endl;
	    cout<<number4<<": ";
	    for(int j = 0;j < number4; j++ )
	    {
	    	cout<<"*";
	    }
	    
	    cout<<endl;
	    cout<<number5<<": ";
	    for(int j = 0;j < number5; j++ )
	    {
	    	cout<<"*";
	    }
	    
	     cout<<endl;
	
system("pause");
	return 0;
}
 
cool code bro!
 
yeah man. respect!