返回列表 發帖

因數分解 (三) - 所有因數的總和

本帖最後由 tonyh 於 2013-12-14 17:52 編輯

所有因數的總和?
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a, b=0, c=0;
  7.     cout<<"請輸入一正整數: ";
  8.     cin>>a;
  9.     cout<<a<<"的因數有: ";
  10.     for(int i=1; i<=a; i++)
  11.     {
  12.          if(a%i==0)
  13.          {
  14.              cout<<i<<" ";   
  15.              b++;     //b=b+1
  16.              c=c+i;   //c+=i
  17.          }
  18.     }
  19.     cout<<endl<<"總共有"<<b<<"個!";
  20.     cout<<endl<<"它們的總合為"<<c<<"!";
  21.     cout<<endl<<endl;
  22.     system("pause");
  23.     return 0;   
  24. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a, b=0,total=0;
  7.     cout<<"請輸入一正整數: ";
  8.     cin>>a;
  9.     cout<<a<<"的因數有: ";
  10.     for(int i=1; i<=a; i++)
  11.     {
  12.        if(a%i==0)
  13.        {
  14.          cout<<i<<" ";
  15.          b++;  
  16.          total=total+i;
  17.        }
  18.     }
  19.     cout<<endl<<"總共有"<<b<<"個!"<<endl;
  20.     cout<<"它們的總和為"<<total;
  21.     cout<<endl<<endl;
  22.     system("pause");
  23.     return 0;   
  24. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;

  4. int main()
  5. {
  6.     int a,b=0,c=0;
  7.     cout<<"請輸入一正整數:";
  8.     cin>>a;
  9.     cout<<a<<"的因數有:";
  10.     for(int i=1;i<=a;i++)
  11.     {
  12.             if(a%i==0)
  13.             {   
  14.                 cout<<i<<" ";
  15.                 b++;
  16.                 c=c+i;
  17.             }   
  18.     }
  19.     cout<<endl<<"總共有"<<b<<"個!";
  20.     cout<<endl<<"它們的總和為"<<c<<"!";   
  21.     cout<<endl<<endl;
  22.     system("pause");
  23.     return 0;
  24. }
複製代碼

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;

  4. int main()
  5. {
  6.     int a, b=0, c=0;
  7.     cout<<"請輸入一正整數: ";
  8.     cin>>a;
  9.     cout<<a<<"的因數有:";
  10.     for(int i=1; i<=a; i++)
  11.     {
  12.       if(a%i==0)
  13.         {   
  14.          cout<<i<<" ";
  15.          b++;
  16.          c=c+i;      
  17.         }     
  18.     }   
  19.     cout<<"總共有"<<b<<"個!"<<endl;
  20.     cout<<"它們的總和為"<<c<<endl;
  21.     system("pause");
  22.     return 0;
  23. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main ()
  5. {
  6. int a, b=0, c=0;
  7. cout<<"請輸入一正整數:";
  8. cin>>a;
  9. cout<<a<<"的因數有" ;
  10. for(int i=1; i<=a; i++)
  11. {
  12.         if(a%i==0)
  13.         {     
  14.              cout<<i<<" ";
  15.              b++;
  16.              c=c+i;
  17.         }
  18. }
  19. cout<<endl<<"總共有"<<b<<"個!";
  20. cout<<"總和為:"<<c;
  21. cout<<endl<<endl;
  22. system("pause");
  23. return 0;   
  24. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.   int a,b=0,c=0;  
  7.   cout<<"請輸入一正整數: ";
  8.   cin>>a;
  9.   cout<<a<<"的因數有:";
  10.   for(int i=1;i<=a;i++)
  11.   {
  12.         if(a%i==0)
  13.         {
  14.                 cout<<i<<" ";
  15.                 b++;
  16.                 c+=i;
  17.         }
  18.   }
  19.   cout<<endl<<"總共有:"<<b<<"個!";
  20.   cout<<endl<<"他們的總合為:"<<c<<endl;
  21.   cout<<endl<<endl;
  22.   
  23.   
  24.   system ("pause");   
  25.   return 0;
  26. }
複製代碼

TOP

返回列表