Q:

C program to find century for a year

belongs to collection: Calendar Programs in C

0

In this exercise, we learn how to write a C program to find century for a year?. We will write the C program to find a century for the given years. Write a C program to input the number of years from the user and find the century. How to find century for given years in C programming. Logic to find century for a given year.

Input: 1970
Output : 20 century
 
 
Input: 1800
Output : 18 century

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
void find_century(int years)
{
    // No negative value is allow for years
    if (years <= 0)
    {
        printf("0 and negative is not allow for a years\n");
    }
    // If years is between 1 to 100 it
    // will come in 1st century
    else if (years <= 100)
    {
        printf("1st century\n");
    }
    else if (years % 100 == 0)
    {
        printf("%d%s",years/ 100, " century");
    }
    else
    {
        printf("%d%s",years/ 100 +1, " century");
    }
}
int main()
{
    int years;
    //Ask user to input number of years
    printf("Enter Years: ");
    scanf("%d", &years);
    find_century(years);
    return 0;
}

 

Output:

Enter Years: 2020
21 century

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Check expiry date using c language...