923. Season
Determine the
season name by the month number using the compound conditions.
Input. The number of the month.
Output. For spring
months print “Spring”, for summer print “Summer”, for autumn print “Autumn”
and for winter print “Winter”.
Sample
input |
Sample
output |
5 |
Spring |
conditional statement
Using the
conditional statement, print the season of year. The months with
numbers are
given below:
·
12, 1 è 2 – winter,
·
3, 4 è 5 – spring,
·
6, 7 è 8 – summer,
·
9, 10 è 11 – autumn.
Algorithm realization
Read
the input data.
scanf("%d",&n);
Print the aswer.
if ((n == 12) || (n == 1) || (n == 2))
printf("Winter\n"); else
if ((n >= 3) && (n <= 5))
printf("Spring\n"); else
if ((n >= 6) && (n <= 8))
printf("Summer\n"); else printf("Autumn\n");
Algorithm realization – optimal
Reduce the number of conditions.
#include <stdio.h>
int n;
int main(void)
{
scanf("%d",&n);
if ((n == 12)
|| (n == 1) || (n == 2)) printf("Winter\n");
else
if (n <=
5) printf("Spring\n"); else
if (n <=
8) printf("Summer\n"); else
printf("Autumn\n");
return 0;
}
Algorithm realization – switch
#include <stdio.h>
int n;
int main(void)
{
scanf("%d",
&n);
switch (n)
{
case 1:
case 2:
case 12:
puts("Winter");
break;
case 3:
case 4:
case 5:
puts("Spring");
break;
case 6:
case 7:
case 8:
puts("Summer");
break;
default:
puts("Autumn");
}
return 0;
}
Java realization
import java.util.*;
public class Main
{
public static void
main(String[] args)
{
Scanner con = new
Scanner(System.in);
int n = con.nextInt();
if ((n ==
12) || (n == 1) || (n == 2))
System.out.println("Winter"); else
if ((n
>= 3) && (n <= 5))
System.out.println("Spring"); else
if ((n
>= 6) && (n <= 8))
System.out.println("Summer");
else
System.out.println("Autumn");
}
}
Java realization – switch
import java.util.*;
public class Main
{
public static void
main(String[] args)
{
Scanner con = new
Scanner(System.in);
int n = con.nextInt();
switch (n)
{
case 1:
case 2:
case 12:
System.out.println("Winter");
break;
case 3:
case 4:
case 5:
System.out.println("Spring");
break;
case 6:
case 7:
case 8:
System.out.println("Summer");
break;
default:
System.out.println("Autumn");
}
con.close();
}
}