You work in a printing office that the machine there can print documents in different colors. It gets an integer number between 1 to 7 and prints the documents in rainbow colors, as shown in the following table:
Number Matched Color
1 ----- red
2 ----- orange
3 ----- yellow
4 ----- green
5 ----- blue
6 ----- indigo
7 ----- violet
Design an algorithm and write a C++ program to do the following:
Algorithm:
Program:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
int code;
string color;
cout << "Enter a number between 1 and 7: ";
cin >> code;
switch (code) {
case 1:
color = "red";
break;
case 2:
color = "orange";
break;
case 3:
color = "yellow";
break;
case 4:
color = "green";
break;
case 5:
color = "blue";
break;
case 6:
color = "indigo";
break;
case 7:
color = "violet";
break;
default:
cout << "Incorrect color number";
exit(1);
}
cout << "Selected colr is " << color;
ofstream fout("color.txt");
fout << color << endl;
fout.close();
return 0;
}
Comments
Leave a comment