اعداد کامل بین 1 تا 1000 را نشان دهد. مثالی از عدد کامل 1 + 2 + 3 = 6
#include<iostream>
#include<conio.h>
using namespace std;
bool perfect(int);
int main()
{
cout << "For the integers from 1 to 1000:\n";
for(int j = 2; j <= 1000; ++j)
if(perfect(j))
cout << j << " is perfect\n";
cout << endl;
getch();
return 0;
}
bool perfect(int value)
{
int factorSum=1;
for(int i=2; i <= value / 2; ++i)
if(value % i == 0)
factorSum += i;
return factorSum == value ? true : false;
}
بزرگ ترین مقسوم علیه مشترک(GCD) دو عدد صحیح عبارت است از بزرگ ترین عدد صحیحی که هر دو عدد بر ان بخش پذیر باشند. تابعی به نام gcd بنویسید که بزرگ ترین مقسوم علیه مشترک دو عدد را برگرداند.
#include<iostream>
using std::cout;
using std::cin;
int gcd(int, int);
int main()
{
int a, b;
for(int j=1; j=5; ++j) {
cout << "Enter two integers: ";
cin >> a >> b;
cout << "The greatest common divisor of " << a << " and "
<< b << " is " << gcd(a, b) << "\n\n";
}
return 0;
}
//+_+_+_+_+_+_+_+_+_+
int gcd (int x, int y)
{
int greatest = 1;
for(int i=2; i <= ((x < y) ? x: y); ++i)
if(x % i == 0 && y % i == 0)
greatest = i;
return greatest;
}
تابعی بنویسید که یک عدد صحیح از ورودی دریافت کرده و عددی با ارقام معکوس برگرداند. مثلا اگر عدد 7631 وارد تابع شود باید تابع عدد 1367 را برگرداند.
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int reverseDigits(int);
int width(int);
int main()
{
int number;
cout << "Enter a number between 1 to 9999: ";
cin >> number;
cout << "The number with its digits reversed is: "
<< setw((width(number))) << setfill('0')
<< reverseDigits(number)
<< endl;
getch();
return 0;
}
//++++++++++++++++++++++++++++++++++
int reverseDigits(int n)
{
int reverse = 0, divisor = 1000, multiplier = 1;
while(n>10) {
if(n >= divisor) {
reverse += n / divisor * multiplier;
n %= divisor;
divisor /= 10;
multiplier *= 10;
}
else
divisor /= 10;
}
reverse += n * multiplier;
return reverse;
}
//-------------------------------
int width(int n)
{
if(n /= 1000)
return 4;
else if(n /= 100)
return 3;
else if(n /= 10)
return 2;
else
return 1;
}
تابعی بنویسید که زمان را به صور ارگومان صحیح (ساعت دقیق ثانیه) از ورودی دریافت کند و تعداد ثانیه هایی را که از اخیرن ساعت ۱۲ گذشته است را برگرداند. با استفاده از این تابع مدت بین دو زمان را برحسب ثانیه محاسبه کنید. فرض کنید این زمان ها هر دو درون یک دوره ۱۲ ساعتی قرار دارند.
#include<iostream>
#include<conio.h>
using namespace std;
unsigned seconds(unsigned, unsigned, unsigned);
int main()
{
unsigned hours, minutes, secs, temp;
cout << "Enter the first time as three integers: ";
cin >> hours >> minutes >>secs;
temp = seconds(hours, minutes, secs);
cout << "Enter the second time as three integers: ";
cin >> hours >> minutes >> secs;
cout << "The difference between the time is "
<< seconds(hours, minutes, secs) - temp
<< " seconds" << endl;
getch();
return 0;
}
unsigned seconds(unsigned h, unsigned m, unsigned s)
{
return 3600 * (h >= 12 ? h - 12 : h) + 60 * m + s;
}
#include<iostream>
#include<conio.h>
using namespace std;
int celcius(int);
int fahrenheit(int);
int main()
{
cout << "Fahrenheit equivalents of celcius temperatures:\n"
<< "Celcius\t\tFahrenheit\n";
for(int i = 0; i <= 100; ++i)
cout << i << "\t\t" << fahrenheit(i) << '\n';
cout << "\nCelcius equivalents of Fahrenheit temperatures:"
<< "n\Fahrenheit\tCelcius\n";
for(int j=32; j<= 212; ++j)
cout << j << "\t\t" << celcius(j) << '\n';
cout << endl;
getch();
return 0;
}
int celcius(int fTemp)
{
return static_cast < int > (5.0 / 9.0 * (fTemp - 32));
}
int fahrenheit(int cTemp)
{
return static_cast < int > (9.0 / 5.0 * cTemp + 32);
}