C-Angeben ob eine Zahl eine Dezimalzahl ist?
Ich habe in der Programmiersprache C eine Hausübung, und dort muss ich so schreiben
if(a/4=eine Ganze Zahl){
aber wie soll ich das im Programm schreiben?
Danke schon im Voraus!
2 Antworten
if(a%4==0){
...
}
//Testdata:
//2000||2016||0||1900||2021
#include <stdio.h>
#include <stdlib.h>
int main(){
const float a;
printf("--------------------");
printf("\nLeap Year Calculator");
printf("\n--------------------");
//input
printf("\n-----------------");
printf("\nType in a Year: ");
scanf("%f", &a);
printf("\n-----------------");
if(a%4==0){
if(a%100==0){
if(%400==0){
printf("\nThe Year %f is a Leap Year!",a);
}else{
printf("\nThe Year %f is not a Leap Year!",a);
}
}else{
printf("\nThe Year %f is a Leap Year!",a);
}
}else{
printf("\nThe Year %f is not a Leap Year!",a);
}
}
das geht aber nicht :(
Wenn die Jahreszahl vom Typ float ist, schreibe fmod(a, 4)==0 (statt a%4==0).
Allerdings solltest Du Deine Entscheidung für den Typ float ausführlich kommentieren. Sonst fasst sich jeder beim Lesen nur an die Stirn.
class Main {
public static void main(String args[]) {
double a = 11.5;
if( (int)a == a ){
System.out.println("Ganze Zahl");
}
else{
System.out.println("Keine Ganze Zahl");
}
}
}
Du kannst den Code testen bei:
Hat leider nicht funktioniert, es rechnet nur ob 0 rauskommt....