Write a program to show whether the year is a leap year or not .If it is then ad
ID: 3674618 • Letter: W
Question
Write a program to show whether the year is a leap year or not .If it is then add the digits of the year and write the sum? I know how to do the leap year but i don't know how to do the sum with out loops. The program that I wrote for it produces a "Segmentation fault" please help.
#include<stdio.h>
main()
{
int year;
int t1;
int t2;
int sum1;
int sum2;
int sum3;
int sum4;
int sum;
scanf("%d",year);
sum1=year/1000;
t1=year%1000;
sum2=t1/100;
t2=sum2%100;
sum3=t2/10;
sum4=t2%10;
sum=sum1+sum2+sum3+sum4;
printf("%d",sum);
}
Explanation / Answer
// C code, main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int year;
int t1;
int t2;
int sum1;
int sum2;
int sum3;
int sum4;
int sum;
scanf("%d",&year); // you didnt put & before year here
if(year % 4 == 0) // suppose year is 1992
{
printf("Leap Year ");
sum1 = year % 10; // sum1 will hold 1992 % 10 = 2 and
year = year / 10; // year will be 199 now as it is divided by 10
sum2 = year % 10; // sum2 will hold 199 % 10 = 9 and
year = year / 10; // year will be 19 now as it is divided by 10
sum3 = year % 10; // sum3 will hold 19 % 10 = 9 and
year = year / 10; // year will be 1 now as it is divided by 10
sum4 = year % 10; // sum4 will hold 1 and 1% 10 =1
sum = sum1 + sum2 + sum3 + sum4;
printf("%d ",sum);
}
else printf("Not Leap Year ");
return 0;
}
/*
output:
2016
Leap Year
9
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.