Program 6
#include
#include
#include
using namespace std;
int main()
{
cout << fixed << setprecision(1);
int i1 = 4, i2 = 8;
cout << i1 / i2 << endl;
cout << (double)i1 / i2 << endl;
cout << i1 / (double)i2 << endl;
cout << (double)(i1 / i2) << endl;
double d1 = 5.5, d2 = 6.6;
cout << (int)d1 / i2 << endl;
cout << (int)(d1 / i2) << endl;
d1 = i1;
cout << d1 << endl;
i2 = d2;
cout << i2 << endl;
}
Program 7
#include
#include
#include
using namespace std;
int main()
{
double celsius, fahrenheit;
cout << fixed << setprecision(2);
cout << "Enter the temperature in celsius: ";
cin >> celsius;
fahrenheit = celsius*9/5 + 32;
cout << celsius << "C is " << fahrenheit << "F" << endl;
cout << "Enter the temperature in fahrenheit: ";
cin >> fahrenheit;
celsius = (fahrenheit - 32)*5/9;
cout << fahrenheit << "F is " << celsius << "C" << endl;
return 0;
}
Program 8
#include
#include
#include
using namespace std;
int main()
{
int upperbound;
cout << "Enter the upperbound: ";
cin >> upperbound;
for (int number = 2; number <= upperbound; ++number)
{
int maxFactor = (int)sqrt(number);
for (int factor = 2; factor <= maxFactor; ++factor)
{
if (number % factor == 0)
{
cout << number << " ";
break;
}
}
}
cout << endl;
return 0;
}
Program 9
#include
#include
#include
using namespace std;
int main()
{
int upperbound;
cout << "Enter the upperbound: ";
cin >> upperbound;
for (int number = 2; number <= upperbound; ++number)
{
int maxfactor = (int)sqrt(number);
bool isprime = true;
for (int factor = 2; factor <= maxfactor; ++factor)
{
if (number % factor == 0)
{
isprime = false;
break;
}
}
if (isprime) cout << number << " ";
}
cout << endl;
return 0;
}
Program 10
#include
#include
#include
using namespace std;
int main()
{
int upperbound;
cout << "Enter the upperbound: ";
cin >> upperbound;
for (int number = 2; number <= upperbound; ++number)
{
int maxfactor = (int)sqrt(number);
bool isprime = true;
int factor = 2;
while (isprime && factor <= maxfactor)
{
if (number % factor == 0)
{
isprime = false;
}
++factor;
}
if (isprime) cout << number << " ";
}
cout << endl;
return 0;
}
No comments:
Post a Comment