Question

Program written in C ++ to print the largest number among 100 integers using the conditional Ternary operator
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello dear...

Here is the C++ program to print the largest number among 100 integers using the conditional Ternary operatot. I have also written comments for better understanding. Also attached images of output and code for comfort of reading. Hope you will like it.

I have tested the program with an array of 100 random integers. Maximum value in the array is 295.

main.cpp

//import required libraries
#include <iostream>
using namespace std;

//main method
int main(){
        //declare and initislise required variables
        int n = 100, largest=0, i;
        //creating an array of 100 random integers 
        int array[n] = { 240, 128, 239, 29, 232, 198, 294, 194, 136, 59, 173, 119, 241, 20, 16, 100, 17, 267, 267, 188, 268, 280, 285, 287, 84, 117, 142, 203, 6, 255, 281, 217, 275, 110, 22, 122, 289, 226, 128, 66, 1, 140, 212, 223, 176, 152, 186, 137, 295, 71, 255, 121, 215, 282, 124, 97, 49, 116, 137, 288, 291, 218, 42, 117, 81, 104, 258, 175, 21, 33, 12, 94, 110, 142, 149, 28, 158, 213, 107, 135, 293, 51, 233, 94, 160, 269, 238, 239, 98, 197, 245, 138, 280, 127, 257, 65, 21, 178, 97, 184 };
                
        //for loop traverse though all the 100 integers
        for(i=0; i<n; i++){
                //conditional terinary operator
                //it updates value of 'largest' if 'largest' is less than current number
                //else 'largest' is 'largest'
                largest = (largest < array[i]) ? array[i] : largest;
        }
        //print the largest number in the array to console ouput
        cout << "largest number in all 100 integers is : " << largest;
        //end
        return 0;
}

Output:

largest number in all 100 integers is : 295 Process exited after 0.03536 seconds with return value o Press any key to continu

Image of Code:

1 2 3 4 5 6 7 8 9 10 //import required libraries #include <iostream> using namespace std; //main method int main() { //declar

Hope you like it.

If you have any doubts ask me in comment section.

If you like my work, please give me a like and feedback. That helps me a lot. Thank you.

All the best.

Add a comment
Know the answer?
Add Answer to:
Program written in C ++ to print the largest number among 100 integers using the conditional...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT