Programa para imprimir la litera del número de asiento de tren dado

Dado un número de asiento de tren, la tarea es verificar si es un número de asiento válido o no. También imprima su tipo de litera, es decir, litera inferior, litera intermedia, litera superior, litera inferior lateral, litera superior lateral según la figura a continuación.
 

Ejemplos: 
 

Entrada: 10 
Salida: litera central
Entrada:
Salida: litera inferior lateral 
 

Acercarse: 
 

  • Compruebe si el número de asiento es un número de asiento válido o no (es decir, en el rango de 1 a 72). 
    1. si (seat_number % 8) es igual a 1 o 4, entonces la litera es una litera más baja
    2. si (seat_number % 8) es igual a 2 o 5, entonces la litera es una litera intermedia
    3. si (seat_number % 8) es igual a 3 o 6, entonces la litera es una litera superior
    4. si (seat_number % 8) es igual a 7, entonces la litera es una litera lateral inferior
    5. si (seat_number % 8) es igual a 0, entonces la litera es una litera lateral superior

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to print
// berth type of a provided
// seat number.
#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;
 
// function to print berth type
void berth_type(int s)
{
    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    if (s > 0 && s < 73)
        if (s % 8 == 1 ||
            s % 8 == 4)
            cout  << s << " is a lower berth\n";
             
        else if (s % 8 == 2 ||
                s % 8 == 5)
            cout  << s << " is a middle berth\n";
             
        else if(s % 8 == 3 ||
                s % 8 == 6)
            cout  << s << " is a upper berth\n";
             
        else if(s % 8 == 7)
            cout  << s << " is a side lower berth\n";
             
        else
            cout  << s << " is a side upper berth\n";
             
    else
        cout  << s << " invalid seat number\n";
}
 
// Driver code
int main()
{
    int s = 10;
     
    // fxn call for berth type
    berth_type(s);
 
    s = 7;
     
    // fxn call for berth type
    berth_type(s);
 
    s = 0;
     
    // fxn call for berth type
    berth_type(s);
    return 0;
}
 
// This code is contributed
// by Amber_Saxena.

C

// C program to print
// berth type of a provided
// seat number.
#include <stdio.h>
 
// function to print berth type
void berth_type(int s)
{
    if (s > 0 && s < 73)
        if (s % 8 == 1 ||
            s % 8 == 4)
            printf("%d is lower berth\n", s);
             
        else if (s % 8 == 2 ||
                 s % 8 == 5)
            printf("%d is middle berth\n", s);
             
        else if(s % 8 == 3 ||
                s % 8 == 6)
            printf("%d is upper berth\n", s);
             
        else if(s % 8 == 7)
            printf("%d is side lower berth\n", s);
             
        else
            printf("%d is side upper berth\n", s);
             
    else
        printf("%d invalid seat number\n", s);
}
 
// Driver code
int main()
{
    int s = 10;
     
    // fxn call for berth type
    berth_type(s);
 
    s = 7;
     
    // fxn call for berth type
    berth_type(s);
 
    s = 0;
     
    // fxn call for berth type
    berth_type(s);
    return 0;
}
 
// This code is contributed
// by Amber_Saxena.

Java

// Java program to print
// berth type of a provided
// seat number.
import java .io.*;
 
class GFG
{
     
// Function for
// printing berth type
static void berth_type(int s)
{
     
    if (s > 0 && s < 73)
        if (s % 8 == 1 ||
            s % 8 == 4)
        System.out.println(s +
                   " is lower berth");
        else if (s % 8 == 2 ||
                 s % 8 == 5)
            System.out.println(s +
                       " is middle berth");
        else if(s % 8 == 3 ||
                 s % 8 == 6)
            System.out.println(s +
                       " is upper berth");
        else if(s % 8 == 7)
            System.out.println(s +
                       " is side lower berth");
        else
            System.out.println(s +
                       " is side upper berth");
    else
        System.out.println(s +
                   " invalid seat number");
}
 
// Driver code
public static void main(String[] args)
{
int s = 10;
berth_type(s); // fxn call for berth type
 
s = 7;
berth_type(s); // fxn call for berth type
 
s = 0;
berth_type(s); // fxn call for berth type
}
}
 
// This code is contributed
// by anuj_67.

Python

# Python program to print berth type
# of a provided seat number.
 
# Function for printing berth type
def berth_type(s):
      
    if s>0 and s<73:
        if s % 8 == 1 or s % 8 == 4:
            print s, "is lower berth"
        elif s % 8 == 2 or s % 8 == 5:
            print s, "is middle berth"
        elif s % 8 == 3 or s % 8 == 6:
            print s, "is upper berth"
        elif s % 8 == 7:
            print s, "is side lower berth"
        else:
            print s, "is side upper berth"
    else:
        print s, "invalid seat number"
 
# Driver code
s = 10
berth_type(s)      # fxn call for berth type
 
s = 7
berth_type(s)     # fxn call for berth type
 
s = 0
berth_type(s)      # fxn call for berth type

C#

// C# program to print
// berth type of a provided
// seat number.
using System;
 
class GFG
{
     
// function to print berth type
static void berth_type(int s)
{
    if (s > 0 && s < 73)
    {
        if (s % 8 == 1 ||
            s % 8 == 4)
            Console.WriteLine(s + " is lower berth");
             
        else if (s % 8 == 2 ||
                 s % 8 == 5)
            Console.WriteLine(s + " is middle berth");
             
        else if(s % 8 == 3 ||
                s % 8 == 6)
            Console.WriteLine(s + " is upper berth");
             
        else if(s % 8 == 7)
            Console.WriteLine(s + " is side lower berth");
             
        else
            Console.WriteLine(s + " is side upper berth");
    }    
    else
        Console.WriteLine(s + " invalid seat number");
    return;
}
 
// Driver code
public static void Main()
{
    int s = 10;
     
    // fxn call for berth type
    berth_type(s);
 
    s = 7;
     
    // fxn call for berth type
    berth_type(s);
 
    s = 0;
     
    // fxn call for berth type
    berth_type(s);
}
}
 
// This code is contributed
// by Amber_Saxena.

PHP

<?php
// PHP program to print
// berth type of a provided
// seat number.
 
// function to print berth type
function berth_type($s)
{
    if ($s > 0 && $s < 73)
    {
        if ($s % 8 == 1 ||
            $s % 8 == 4)
        {
            echo sprintf("%d is lower " .
                          "berth\n", $s);
        }
        else if ($s % 8 == 2 ||
                 $s % 8 == 5)
        {
            echo sprintf("%d is middle ".
                          "berth\n", $s);
        }
        else if($s % 8 == 3 ||
                $s % 8 == 6)
        {
            echo sprintf("%d is upper " .
                          "berth\n", $s);
        }
        else if($s % 8 == 7)
        {
            echo sprintf("%d is side lower ".
                              "berth\n", $s);
        }
        else
        {
            echo sprintf("%d is side upper ".
                              "berth\n", $s);
        }
    }
    else
    {
        echo sprintf("%d invalid seat ".
                        "number\n", $s);
    }
}
 
// Driver Code
$s = 10;
     
// fxn call for berth type
berth_type($s);
 
$s = 7;
     
// fxn call for berth type
berth_type($s);
 
$s = 0;
     
// fxn call for berth type
berth_type($s);
 
// This code is contributed
// by Amber_Saxena.
?>

Javascript

<script>
 
// Javascript program to print
// berth type of a provided
// seat number.
 
// Function for
// printing berth type
function berth_type(s)
{
       
    if (s > 0 && s < 73)
        if (s % 8 == 1 ||
            s % 8 == 4)
        document.write(s +
                   " is lower berth" + "<br/>");
        else if (s % 8 == 2 ||
                 s % 8 == 5)
            document.write(s +
                       " is middle berth" + "<br/>");
        else if(s % 8 == 3 ||
                 s % 8 == 6)
            document.write(s +
                       " is upper berth" + "<br/>");
        else if(s % 8 == 7)
            document.write(s +
                       " is side lower berth" + "<br/>");
        else
            document.write(s +
                       " is side upper berth" + "<br/>");
    else
        document.write(s +
                   " invalid seat number" + "<br/>");
}
 
// driver program
    let s = 10;
berth_type(s); // fxn call for berth type
   
s = 7;
berth_type(s); // fxn call for berth type
   
s = 0;
berth_type(s); // fxn call for berth type
   
  // This code is contributed by susmitakundugoaldanga.
</script>
Producción: 

10 is middle berth
7 is side lower berth
0 invalid seat number

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por anamika9988 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *