본문 바로가기

PROGRAM/C++

입력한숫자만큼 별 출력하기(4자리수 한정)

입력한 숫자만큼의 별모양을 출력하는 프로그램을 작성하여보자

예를 들어 5914를 입력하면 별이 5, 9 , 1, 4 개가 출력되는 프로그램이다.

#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"정수를 입력하세요(4자리수) : ";
cin >> num;
while(1){
if( (num >= 1000) && (num <= 9999) ){
break;
}
else{
cout << "1000~9999사이의 숫자를 입력하세요"<<endl;
cin >> num;
}
}
int n[4];
n[0] = num % 10;
n[1] = (num / 10) %10;
n[2] = (num / 100)%10;
n[3] = num / 1000;
for (int k = 3; k>=0; k--){
for(int i = 0; i<n[k]; i++){
cout << "*";
}
cout << endl;
}
return 0;
}

'PROGRAM > C++' 카테고리의 다른 글