Showing posts with label C-programs. Show all posts

C Program for checking a Leap year entered by user

No comments
In this tutorial we will write a program which will ask the user to enter a year through keyboard and then print the result about that year whether it is a leap year or not.Before starting to code ,we must know in detail what a leap year is and what are the criteria a year must satisfy to be a leap year. It will help us in applying our logic and writing a perfect program without any error.
What is a leap year A leap is year is a year which has 366 days instead of 365 which a common year has. We have a February of 29 days in a leap year. A leap year occurring once every four Years.Now it's clear that a leap year is divisible by 4, but wait there is more to know about a leap year.
Criteria a year must satisfy for being a leap year The C program which we are going to write for checking a leap year is based on the rules defined by the Gregorian calendar for a leap year.
According to Georgian calendar these criteria must be satisfied for a year to be a leap year –
  • The year must be divisible by 4.

  • A year is not a leap year if it is evenly divisible by 100 unless it is also divisible by 400.
    For example-2400 and 2000 are lep years but 1800,2100,2300 etc are not leap years.
Now we will use above criteria for writing our C program for checking a leap year.
Note-Take help of the comments for a better understanding.
Program-
#include <stdio.h>

void main()
{
  int year; //year to be entered by user
 
//asking user to enter a year ,which he/she want to check  
printf("Enter the year to check\n");

//scan the entered value and put it into year variable.  
scanf("%d", &year);
 
//checking for a leap year according to the criteria described and printing the results.  

if (year%400 == 0)
    printf(" year %d is a leap year.\n", year);
  
else if (year%100 == 0)
    printf(" year %d is not a leap year.\n", year);
  
else if (year%4 == 0 )
    printf(" year %d is a leap year.\n", year);
  
else
    printf("year %d is not a leap year.\n", year);  
 
}


C - Programs

No comments
Here you will get almost all the basic and advanced c programs which will improve your logical strength and help you in solving your own programming problems.All the programs are explained in detail which will help you in understanding the logic and techniques used for writing the programs.We have tried to solve various problems by using more than one way.





C program for checking a number even or odd

No comments
Here we will write a C program to check a user input number, whether it is odd or even. Before we start writing the actual code we should know what is an odd number and what is an even number so that we can apply our logic. An odd number is that integer which is not a multiple of 2 and on dividing by 2 the output is a fraction.Odd numbers are of form n=2e+1
An even number is an integer of form n=2e, where e could be any integer(positive or negative). An even number can be completely divided by 2 ie. remainder is zero.

Method One:Writing a C program for checking an even or odd number using modulus operator


So we can use modulus operator (%) for checking the evenness or oddness of any integer. If some integer divided by 2 gives any remainder then it is an odd number or if the remainder is zero then it will be an even number.
Remember if any integer is not even then it will be odd and vice versa. So we will check only for one condition, if that condition is true then other one will be false and if the first condition is false then the other one will be true.
Program:-
   #include <stdio.h>
   #include <conio.h>  
      
    void main(){  
    
 //declare an integer n.
 int n;
 
 //ask the user for an integer to check
 printf("Enter the integer you want to check\n");
 
 //scan the value entered from keyboard and put it in the variable n.
 scanf("%d",&n );
 
 
 if(n%2==0)
 printf("%d is an even integer" ,n );
 else
 printf("%d is an odd integer" ,n );
 
 }