Respuesta :

Answer:  

Here is the C++ program to find the largest of three integers.

#include <iostream> //to use input output functions

using namespace std; //to identify objects like cin cout

int main(){ //start of main function

int integer1, integer2, integer3;  // declare three integers

   cin>>integer1>>integer2>>integer3;  //reads values of three integers variables from user

   if (integer1 >= integer2 && integer1 >= integer3) //if value of integer1 variable is greater than or equal to both integer2 and integers3 values

       cout<<integer1;   //then largest number is integer1 and it is displayed

   else if (integer2 >= integer1 && integer2 >= integer3) //if value of integer2 variable is greater than or equal to both integer1 and integers3 values

       cout<<integer2;  //then largest number is integer2 and it is displayed

   else //in case value of integer3 variable is greater than or equal to both integer1 and integers2 values

       cout<<integer3; } //then largest number is integer3 and it is displayed

Here is the JAVA program to find the largest of three integers.

import java.util.Scanner; //to take input from user

public class Main{

public static void main(String[] args) {//start of main function

Scanner input= new Scanner(System.in); //a standard input stream.

int integer1= input.nextInt();//declare and read value of first integer

int integer2= input.nextInt();//declare and read value of second integer

int integer3= input.nextInt();//declare and read value of third integer

   if (integer1 >= integer2 && integer1 >= integer3) //if value of integer1 variable is greater than or equal to both integer2 and integers3 values

       System.out.println(integer1);   //then largest number is integer1 and it is displayed

   else if (integer2 >= integer1 && integer2 >= integer3) //if value of integer2 variable is greater than or equal to both integer1 and integers3 values

       System.out.println(integer2); //then largest number is integer2 and it is displayed

   else //when value of integer3 variable is greater than or equal to both integer1 and integers2 values

               System.out.println(integer3);  } } //then largest number is integer3 and it is displayed

Explanation:

The program is well explained in the comments attached with each statement of the program. I will explain the program with the help of an examples. Suppose

integer1 = 7

integer2 = 15

integer3 = 3

first if condition  if (integer1 >= integer2 && integer1 >= integer3) checks if value of integer1 variable is greater than or equal to both integer2 and integers3 values. Since integer1 = 7 so it is greater than integer3 = 3 but less than integer2. So this statement evaluates to false because in && ( AND logical operator) both of the conditions i.e integer1 >= integer2 and integer1 >= integer3 should be true. So the program moves to the else if part.

The else if condition else if (integer2 >= integer1 && integer2 >= integer3) checks if value of integer2 variable is greater than or equal to both integer1 and integer3 values. Since integer2 = 15 so it is greater than integer1 = 7 and also greater than integer3 = 3. So this statement evaluates to true as both parts of the else if condition holds true. So the else if part executes which has the statement: cout<<integer2; which prints the value of integer2 i.e. 15 on output screen.

Output:

15

Ver imagen mahamnasir
Ver imagen mahamnasir

Answer:

#include <stdio.h>

int main(void) {

 

 int num1;

 int num2;

 int num3;

 printf("Enter three integers: ");

 scanf("%d", &num1);

 scanf("%d", &num2);

 scanf("%d", &num3);

 if (num1 == 0 || num2 == 0 || num3 == 0)

 {

   printf("please input a number greater than zero :)\n");

 }

 if (num1 <= num2 && num1 <= num3)

 {

   printf("%i is the smallest number!\n", num1);

 }

 else if (num2 <= num1 && num2 <= num3)

 {

   printf("%i is the smallest number!\n", num2);

 }

 else

 {

   printf("%i is the smallest number!\n", num3);

 }

 return 0;

}

Explanation:

Alright so let's start with the requirements of the question:

  1. must take 3 integers from user input
  2. determine which of these 3 numbers are the smallest
  3. spit out the number to out

So we needed to create 3 variables to hold each integer that was going to be passed into our script.

By using scanf("%i", &variableName) we were able to take in user input and  store it inside of num1, num2, and num3.

Since you mentioned you were new to the C programming language, I threw in the first if statement as an example of how they can be used, use it as a guide for future reference, sometimes it's better to understand your code visually.

Basically what this if statement does is, it checks to see if any of the integers that came in from user input was the number zero, it told the user it does not accept that number, please input a number greater than zero.

if (num1 == 0 || num2 == 0 || num3 == 0)

 {

   printf("please input a number greater than zero :)\n");

 }

I used this methodology and implemented the heart of the question,

whichever number is smaller, print it out on the shell (output).

if (num1 <= num2 && num1 <= num3)

^^ here we're checking if the first variable we created is smaller than the second variable and the third ^^

 {

   printf("%i is the smallest number!\n", num1);

   ^^ if it is smaller, then print integer and then print a new line so the next line looks neat ^^

   ( incase if your wondering what "\n" is, its a special character that allows you so print a new line on the terminal, kind of like hitting the return or enter key )

 }

else if (num2 <= num1 && num2 <= num3)

^^ else if is used when your checking for more than one thing, and so for the second variable we checked to see if it was smaller than the first and third variable we created ^^

 {

   printf("%i is the smallest number!\n", num2); < -- and we print if it's smaller

 }

Last but not least:

else

^^ if it isn't num1 or num2, then it must be num3 ^^

 {

   printf("%i is the smallest number!\n", num3);

  we checked the first two options, if its neither of those then we have only one variable left, and thats num3.

 }

I hope that helps !!

Good luck on your coding journey :) ‍

ACCESS MORE
EDU ACCESS
Universidad de Mexico