Heim  >  Artikel  >  Backend-Entwicklung  >  Schreiben Sie ein Programm in der Sprache C, um zu prüfen, ob ein bestimmtes Jahr ein Schaltjahr ist oder nicht

Schreiben Sie ein Programm in der Sprache C, um zu prüfen, ob ein bestimmtes Jahr ein Schaltjahr ist oder nicht

王林
王林nach vorne
2023-09-20 15:33:101415Durchsuche

Schreiben Sie ein Programm in der Sprache C, um zu prüfen, ob ein bestimmtes Jahr ein Schaltjahr ist oder nicht

Ein Schaltjahr hat 366 Tage, während ein gewöhnliches Jahr 365 Tage hat. Die Aufgabe besteht darin, durch ein Programm zu überprüfen, ob ein bestimmtes Jahr ein Schaltjahr ist.

Die Logik des Urteils lässt sich realisieren, indem man prüft, ob das Jahr durch 400 oder 4 teilbar ist. Wenn es jedoch nicht durch diese beiden Zahlen teilbar ist, handelt es sich um ein gewöhnliches Jahr.

Beispiel

Input-: year=2000
Output-: 2000 is a Leap Year

Input-: year=101
Output-: 101 is not a Leap year

Algorithmus

Start
Step 1 -> declare function bool to check if year if a leap year or not
bool check(int year)
   IF year % 400 = 0 || year%4 = 0
      return true
   End
   Else
      return false
   End
Step 2 -> In main()
   Declare variable as int year = 2000
   Set check(year)? printf("%d is a Leap Year",year): printf("%d is not a Leap Year",year)
   Set year = 10
   Set check(year)? printf("%d is a Leap Year",year): printf("</p><p>%d is not a Leap Year",year);
Stop

Beispiel

#include <stdio.h>
#include <stdbool.h>
//bool to check if year if a leap year or not
bool check(int year){
   // If a year is multiple of 400 or multiple of 4 then it is a leap year
   if (year % 400 == 0 || year%4 == 0)
      return true;
   else
      return false;
}
int main(){
   int year = 2000;
   check(year)? printf("%d is a Leap Year",year): printf("%d is not a Leap Year",year);
   year = 101;
   check(year)? printf("%d is a Leap Year",year): printf("</p><p>%d is not a Leap Year",year);
   return 0;
}

Ausgabe

2000 is a Leap Year
101 is not a Leap Year

Das obige ist der detaillierte Inhalt vonSchreiben Sie ein Programm in der Sprache C, um zu prüfen, ob ein bestimmtes Jahr ein Schaltjahr ist oder nicht. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen