[Solved] Refreshment with Java, C++, Python

Refreshment: The Pan Am 73 flight from Bombay to New York en route Karachi and Frankfurt was hijacked by a few Palestinian terrorists at the Karachi International Airport.

[Solved] Refreshment with Java, C++, Python

 Despite the alarming situation prevailing inside the flight, the senior flight purser Neerja Banhot had to wither her fear and rise to the occasion to take extra care of her passengers on board.
 

[Solved] Refreshment with Java, C++, Python

Firstly, she and her crew decided to provide food and drinks to the passengers. Given n the number of rows of seats in the aircraft and the total number of people in each row, can you determine the total number of passengers in the flight.

Input Format :
The first line of input consists of an integer n, corresponding to the number of rows in the aircraft.
The next n lines of input consist of integers that correspond to the number of people in each row.

Output Format :
The first line of output consists of n integers corresponding to the number of people in each row.
The second line of output consists of an integer corresponding to the total number of people in the aircraft.
Print Invalid Input and terminate the process of getting inputs if any of the inputs is not a positive number.
Refer sample input and output for further specifications.

Sample Input 1:
5
12
28
30
20
45
Sample Output 1:
12 28 30 20 45
135

Sample Input 2:
6
12
28
30
20
-45
Sample Output 2:
Invalid Input

Sample Input 3:
-6
Sample Output 3:
Invalid Input

Solution

import java.util.*;

class Main 
{ 
     public static void main (String[] args) 
    {         

    int n, sum = 0 ,i ,flag=0;
     //sauravhathi
    Scanner s = new Scanner(System.in);
        n = s.nextInt();

        if(n>0)   
      {
 //sauravhathi
    int a[] = new int[n];
        for(i=0 ; i < n; i++)
          {
            a[i] = s.nextInt(); 
        if(a[i]>=0)
        {
            sum = sum + a[i];
        }
        else
        {
        flag=-1;
        break;
        }
           }
        
    if(flag==-1)
         { //sauravhathi
    System.out.println("Invalid Input");
         }        
    else
        {
    for (i = 0; i < n; i++)
        System.out.print(a[i]+" ");    
    System.out.println("\n"+sum);
        }    
     
      //sauravhathi
      }
      else
      {
    System.out.println("Invalid Input");
      } 
       //sauravhathi
    } 
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n,i,sum=0,flag=0;
    cin>>n;
    if(n>0){
        int a[] = new int[n];
        for(i=0 ; i < n; i++)
          {
            cin>>a[i];
        if(a[i]>=0)
        {
            sum = sum + a[i];
        }
        else
        {
        flag=-1;
        break;
        }
    }
    if(flag==-1)
         {
    cout<<"Invalid Input";
         }
    else
        {
    for (i = 0; i < n; i++)
        cout<<a[i]<<" ";
    cout<<"\n"<<sum;
        }
    }
    else
    {
    cout<<"Invalid Input";
    }
    return 0;
}
n = int(input())

passengers = []

neg_flag = 0

if(n>=0):

   for i in range(n):

       x = int(input())

       passengers.append(x)

       if(x<0):

           neg_flag = 1

           break

   if(neg_flag):

       print("Invalid Input")

   else:  

       print(*passengers)

       print(sum(passengers))

else:

   print("Invalid Input")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *