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.
#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
need an explanation for this answer? contact us directly to get an explanation for this answer21 century