반복문과 If문 그리고 함수

2023. 3. 28. 21:04·C++

선언한 메소드(함수)

- while문을 사용하여 구구단을 출력하는 메소드

- for문을 사용하여 구구단을 출력하는 메소드

- 두 정수를 입력받고 더 큰 정수를 return 해주는 메소드

#include <iostream>
using namespace std; 

//while문으로 구구단 출력 
void printTimesTableByWhile(){
	int i = 1; 
	int j = 1;
	
	while(i<=9){
		while(j<=9){
			cout << j << "X" << i << "=" << i * j << "  ";
			j++;
		}
		cout << endl;
		j=1;
		i++;
	}
	
	cout << endl;
}

//for문으로 구구단 출력 
void printTimesTableByFor(){
	for(int i=1; i<=9; i++){
		for(int j=1; j<=9; j++){
			cout << j << "X" << i << "=" << i * j << "  ";
		}
		cout << endl;
	}
	
	cout << endl;
}

//두 정수를 입력받고 더 큰 정수를 return한다.
int getMaxValueAfterTwoInput(){
	int first, second;
	int return_value;
	
	cout << "두 정수를 입력하세요 : "; 
	cin >> first;
	cin >> second;
	
	if(first>second){
		return_value = first;
	}
	else if(first<second){
		return_value = second;
	}
	else{
		return_value = first;
	}
	
	return return_value;
}
	
int main(int argc, char** argv) {
	printTimesTableByWhile();
	printTimesTableByFor();
	
	int maxValue;
	maxValue = getMaxValueAfterTwoInput();
	cout << "두 정수중에 큰 수는 " << maxValue << "입니다." << endl;
	return 0;
}

출력해보면 잘 돌아가지만 구구단의 줄이 맞지 않는다. 이럴 땐 '\t'으로 포매팅 해주거나 <iomanip> 헤더파일의 setw()함수를 사용하면 정리 할 수 있다. (사용 예 : cout << x << "X" << y << "=" << right << setw(2) << x*y << " "; )

 

 

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

클래스와 객체  (0) 2023.03.28
객체지향언어(Object-Oriented Programming)  (0) 2023.03.28
'C++' 카테고리의 다른 글
  • 클래스와 객체
  • 객체지향언어(Object-Oriented Programming)
switch_user
switch_user
나의 공부 기록
  • switch_user
    while(true)
    switch_user
  • 전체
    오늘
    어제
    • 분류 전체보기
      • C
      • C++
      • Java
      • Python
      • Web
      • App
      • Security
        • Web Hacking
        • Reverse Engineering
      • DB
      • Machine Learning
      • Computer Science
      • Linux
      • Algorithm
      • 진로
      • 기타
  • 블로그 메뉴

    • 홈
    • 태그
    • velog
    • Github
  • 링크

    • velog
    • Github
  • 공지사항

  • 인기 글

  • 태그

    Web 기초
    race condition
    CSS
    cin.getline
    HTML
    리버싱
    SQL
    xss
    클래스 외부에 함수 구현
    웹
    ml
    비트연산
    코드 패치
    생성자와 소멸자
    모델 기반 학습
    인터프리팅
    어셈블리
    HTTP
    사례 기반 학습
    반복문
    x64dbg
    Hacking Process
    SQLi
    머신러닝
    웹해킹
    디컴파일
    배치 학습
    IDA
    어셈블리어
    쿠키
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
switch_user
반복문과 If문 그리고 함수
상단으로

티스토리툴바