Jump to content

So I know this is a little off kilter for the LTT forum, but i figured I could find some help here. So I just started getting into coding ( C++ ) and am tasked with making a code that allows the user to type in 3 integers, then tells them the sum, product, average, the smallest and largest number. So I am able to get a code that displays the sum, product and average, yet the smallest and largest are my problems. I know that I have to use an if statement, yet idk how to use it with the numbers ive assigned. My code so far is listed below  and any help would be great. Keep in mind ive started code a few days ago so don't correct me on if its sloppy or not 100% right. It works as of now and does what I want it too and that's all I want as of now.  

 

#include <iostream>
using namespace std;
 
int main()
 
{
int number1;
int number2;
int number3;
 
cout << "Input three different integers \n";
 
cin >> number1 >> number2 >> number3;
 
cout << "The sum is " << number1 + number2 + number3 << "\n";
cout << "The product is " << number1 * number2 * number3 << "\n";
cout << "The average is " << (number1 + number2 + number3) / 3 << "\n";
 
if (number1 <= number2)
cout << "The smallest number is " << number1;
 
}
Link to comment
https://linustechtips.com/topic/442223-c-coding-question/
Share on other sites

Link to post
Share on other sites

I believe your logic is right ,but there is something in your syntacs that seems abit iffy.

|Casual Rig| CPU: i5-6600k |MoBo: ROG Gene  |GPU: Asus 670 Direct CU2 |RAM: RipJaws 2400MHz 2x8GB DDR4 |Heatsink: H100i |Boot Drive: Samsung Evo SSD 240GB|Chassis:BitFenix Prodigy |Peripherals| Keyboard:DasKeyboard, Cherry MX Blue Switches,|Mouse: Corsair M40

|Server Specs| CPU: i7-3770k [OC'd @ 4.1GHz] |MoBo: Sabertooth Z77 |RAM: Corsair Vengeance 1600MHz 2x8GB |Boot Drive: Samsung 840 SSD 128GB|Storage Drive: 4 WD 3TB Red Drives Raid 5 |Chassis:Corsair 600t 

Link to comment
https://linustechtips.com/topic/442223-c-coding-question/#findComment-5927193
Share on other sites

Link to post
Share on other sites

ignore me

|Casual Rig| CPU: i5-6600k |MoBo: ROG Gene  |GPU: Asus 670 Direct CU2 |RAM: RipJaws 2400MHz 2x8GB DDR4 |Heatsink: H100i |Boot Drive: Samsung Evo SSD 240GB|Chassis:BitFenix Prodigy |Peripherals| Keyboard:DasKeyboard, Cherry MX Blue Switches,|Mouse: Corsair M40

|Server Specs| CPU: i7-3770k [OC'd @ 4.1GHz] |MoBo: Sabertooth Z77 |RAM: Corsair Vengeance 1600MHz 2x8GB |Boot Drive: Samsung 840 SSD 128GB|Storage Drive: 4 WD 3TB Red Drives Raid 5 |Chassis:Corsair 600t 

Link to comment
https://linustechtips.com/topic/442223-c-coding-question/#findComment-5927200
Share on other sites

Link to post
Share on other sites

if(number1 <= number2){      if(number1 <= number3)           print 1;      else           print 3;} else if(number2 <= number3)      print 2;else       print 3;

Is there a name for &&? Because this is for school and im going to be asked what that is by the teacher.

It's the sign you use in an if statement to allow the if statement to verify two conditions and require both be true. || would be &&'s counterpart, which allows for either condition to be true.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/442223-c-coding-question/#findComment-5927207
Share on other sites

Link to post
Share on other sites

if ((number1<=number2)&&(number1<=number3))    cout<<"The smallest number is "<<number1;else    {    if (number2<=number3)        cout<<"The smallest number is "<<number2;    else        cout<<"The smallest number is "<<number3;    }

there are better ways to do this involving sorting, but this is the way you started doing it so i just finished in the same way :)

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/442223-c-coding-question/#findComment-5927211
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×