Anda di halaman 1dari 7

SMALLEST INTEGER

//Write a program that reads in three floating - point numbers


and prints the largest of
//the three inputs.For example :
//Please enter three numbers : 4 9 2.5
//The largest number is 2.5

#include<iostream>

using namespace std;

int main()
{
float input1, input2, input3;

cout << "Enter three numbers: ";


cin >> input1 >> input2 >> input3;

float smallest;

if(input1 < input2 && input1 < input3)


{
smallest = input1;
cout << "The smallest number is: " << smallest;
}

else if(input2 < input1 && input2 < input3)


{
smallest = input2;
cout << "The smallest number is: " << smallest;
}

else
{
smallest = input3;
cout << "The smallest number is: " << smallest;
}

return 0;
}

1
SORTING STRINGS

//Write a program that reads in three strings and sorts them


lexicographically.
//Enter three strings : Charlie Able Baker
//Physics
//Chemsitry
//Biology

#include<iostream>

using namespace std;

int main()
{
string s1, s2, s3;

cout << "Enter three strings: ";


cin >> s1 >> s2 >> s3;

if(s1 < s2 && s1 < s3)


{
cout << s1 << "\n";

if(s2 < s3)


{
cout << s2 << "\n";
cout << s3 << "\n";
}
else
{
cout << s3 << "\n";
cout << s2 << "\n";
}
}

else if(s2 > s1 && s2 < s3)


{
cout << s2 << "\n";

if(s1 < s3)


{
cout << s1 << "\n";
cout << s3 << "\n";
}

2
else
{
cout << s3 << "\n";
cout << s1 << "\n";
}
}

else
{
cout << s3 << "\n";

if(s1 < s2)


{
cout << s1 << "\n";
cout << s2 << "\n";
}

else
{
cout << s2 << "\n";
cout << s1 << "\n";
}
}

return 0;
}

3
INTEGER TO DIGITS

//Write a program that reads an integer and prints how many


digits the number has, by
//checking whether the number is >= 10, >= 100, and so on.
(Assume that all integers are
//less than ten billion.) If the number is negative, first
multiply it with -1.

#include<iostream>

using namespace std;

int main()
{
int input;
cout << "Enter an integer: ";
cin >> input;

// avoid checking if it's >=0 && <= 10


int digits = 1;

if(input < 0)
{
input *= -1;
}

if(input >= 10 && input < 100 )


{
digits = 2;
}

else if(input >= 100 && input < 1000)


{
digits = 3;
}

else if(input >= 1000 && input < 10000)


{
digits = 4;
}

else if(input >= 10000 && input < 100000)


{
digits = 5;
}

4
else if(input >= 100000 && input < 1000000)
{
digits = 6;
}

else if(input >= 1000000 && input < 10000000)


{
digits = 7;
}

else if(input >= 10000000 && input < 100000000)


{
digits = 8;
}

else if(input >= 100000000 && input < 1000000000)


{
digits = 9;
}

else if(input >= 1000000000 && input < 10000000000)


{
digits = 10;
}

else if(input >= 10000000000 && input < 100000000000)


{
digits = 11;
}

else
{
cout << "Input is greater than 10 billion.";
return 0;
}

cout << "Digit = " << digits << "\n";

return 0;
}

5
MONTHS TO NUMBER OF DAYS CONVERSION

//Write a program that asks the user to enter a month(1 for


January, 2 for February,
// and so on) and then prints the number of days in the
month.For February, print 28
// or 29 daysî.
// Enter a month : 5
// 30 days
// Do not use a separate if / else branch for each month.Use
Boolean operators.

#include<iostream>
using namespace std;

int main()
{
cout << "Enter a month(1-12): ";
int month;
cin >> month;

if(month <= 0 || month > 12)


{
cout << "Invalid input. Month less than 1 or greater
than 12";
return 0;
}

int days;
if(month == 4 || month == 6 || month == 9 || month == 11)
{
days = 30;
}

else if(month == 2)
{
days = 28;
}

else
{
days = 31;
}

cout <<days << " days";

return 0;

6
}

Anda mungkin juga menyukai