In this blog post, 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
C program to find century for a year:
The below program ask the user to enter the number of years. After getting the years from the user program find the century for the corresponding years.
#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