Find number of days in a month in C | Cpp | Java | Go | php

C Program


#include<stdio.h>
int main() {
 int m,y;
 
 printf("Enter any month, say(1-12): ");
 scanf("%d",&m);
 
 switch(m) {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:
   printf("31 days");
   break;
  case 4:
  case 6:
  case 9:
  case 11:
   printf("30 days");
   break;
  case 2:
   printf("Enter the year: ");
   scanf("%d",&y);
   if(y%400 == 0 || y%100 != 0 && y%4 == 0) {
    printf("29 days");
   } else {
    printf("28 days");
   }
  break;
  default:
   printf("Invalid input");
 }
 
}

C++ Program


#include<iostream>
using namespace std;
int main() {
 int m,y;
 
 cout << "Enter any month, say(1-12): ";
 cin >> m;
 switch(m) {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:
   cout << "31 days";
   break;
  case 4:
  case 6:
  case 9:
  case 11:
   cout << "30 days";
   break;
  case 2:
   cout << "Enter the year: ";
   cin >> y;
   if(y%400 == 0 || y%100 != 0 && y%4 == 0) {
    cout << "29 days";
   } else {
    cout << "28 days";
   }
  break;
  default:
   cout<<"Invalid input";
 }
 
}

Java Program


import java.util.Scanner;
class MonthDays {
 public static void main(String args[]) {
 
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter any month,say(1-12): ");
  int m = sc.nextInt();
  
  switch(m) {
   case 1:
   case 3:
   case 5:
   case 7:
   case 8:
   case 10:
   case 12:
    System.out.println("31 days");
    break;
   case 4:
   case 6:
   case 9:
   case 11:
    System.out.println("30 days");
    break;
   case 2:
    System.out.print("Enter the year: ");
    int y = sc.nextInt();
    if(y%400 == 0 || y%100 != 0 && y%4 == 0) {
     System.out.println("29 days");
    } else {
     System.out.println("28 days");
    }
   break;
   default:
    System.out.println("Invalid input");
  }
  
 }
}

Go Lang


package main
import "fmt"

func main() {
 var m,y int
 
 fmt.Print("Enter any month, say(1-12): ")
 fmt.Scan(&m)
 
 switch m {
  case 1,3,5,7,8,10,12:
   fmt.Print("31 days")
  case 4,6,9,11:
   fmt.Print("30 days")
  case 2:
   fmt.Print("Enter the year: ")
   fmt.Scan(&y)
   
   if y%400 == 0 || y%100 != 0 && y%4==0 {
    fmt.Print("29 days")
   } else {
    fmt.Print("28 days")
   }
  default:
   fmt.Print("Invalid input")
 }
}

PHP


$m = $_POST['m'];
$y = $_POST['y'];
switch($m) {
 case 1:
 case 3:
 case 5:
 case 7:
 case 8:
 case 10:
 case 12:
  print("31 days");
  break;
 case 4:
 case 6:
 case 9:
 case 11:
  print("30 days");
  break;
 case 2:
  if($y%400 == 0 || $y%100 != 0 && $y%4 == 0) {
   print("29 days");
  } else {
   print("28 days");
  }
 break;
 default:
  print("Invalid input");
}

Comments

Popular posts from this blog

Overview of Go lang

Arrays in C Language

Data Types in Go language

Creating your first html page

Say Hello world in Go lang

Arrays in Go lang

Installing go lang

String in golang

Decision Making in Go lang

Decision Making Statements