algorithm

자연수의 합, 진약수의 합

tonirr 2020. 11. 25. 00:18
  • 자연수의 합
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	int i, a, b, sum=0;
	cin>>a>>b;
	for(i=a; i<b; i++){
		cout<<i<<" + ";
		sum = sum + i;
	}
	cout<<i<<" = ";
	cout<<sum+i;
	return 0;
}

 

  • 진약수의 합
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	int a, i, sum=1;
	cin>>a;
	cout<<"1";
	for(i=2; i<a; i++){
		if(a%i==0){
			cout<<" + "<<i;
			sum = sum + i;
		}
	}
	cout<<" = ";
	cout<<sum;
	return 0;
}