Showing posts with label Determine whether the given year is a leap year C Program. Show all posts
Showing posts with label Determine whether the given year is a leap year C Program. Show all posts

Monday, February 21, 2011

Factorial Program!!

This program computes the factorial of the given number and displays it. The control structure 'for' is used to determine the factorial. When there is a number of iteration is carried out till a desired output or condition is solved , we go for the 'for' statement.

LOGIC:
 Factroial of 'n' is denoted as 'n!' 

 

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
j=1;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
j=j*i;
}
printf("\n Factorial of %d is :%d",n,j);
getch();
}

Choices Program!!

This Program mainly illustrates the use of 'Switch" control structure. When there are varied number of options and when we have to select 1 option , 'switch' is used.

We first get the values of 2 numbers and enter our choice to add,subtract,multiply or divde the numbers.

Important points to remember while using 'switch':
  • The value that is being switched must be an integer [x in this program].
  • There must be a space between the 'case' ant the no. [case 1:]
  • Every caes must end up with the 'break'.
  • 'default' is mandatory. 'break' is not necessary for default.
 PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
 {
  int a,b,x;
  float c;
  clrscr();
  printf("Enter A:");
  scanf("%d",&a);
  printf("Enter B:");
  scanf("%d",&b);
  printf("\n");
  printf("1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide\n");
  printf("Enter your choice:");
  scanf("%d",& x);
  switch(x)
   {
    case 1: c=a+b;
        printf("The Sum is:%f",c);
        break;
    case 2: c=a-b;
        printf("The difference is:%f", c);
        break;
    case 3: c=a*b;
        printf("The Product is:%f", c);
        break;
    case 4: c=a/b;
        printf("The Quotient is:%f",c);
        break;
    default: printf ("Invalid option");
   }
  getch();
 }
 


Sunday, February 20, 2011

Leap Year Program!!

This program determines whether the given year is a leap year or not.This problem also illustrates the use of the control statemen if-else!!

LOGIC:
If the year is divisible by 4 it is a leap year.
If the year is divisible by 100 it is not a leap year.
If the year is divisible by 400 it is a leap year.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
 int year;
 clrscr();
 printf("Enter the year:");
 scanf("%d",&year);
 if(year%100==0)
 {
   if(year%400==0)
  {
   printf("%d Is a leap year",year);
   }
 else
  {
   printf("%d Is a non-leap year",year);
   }
   }
  else
  {
   if(year%4==0)
  {
   printf("%d: It is a leap year",year);
   }
   else
  {
   printf("%d: It is not a leap year",year);
   }
  }
 printf("\n~!Anyway have a nice year!~");
 getch();
}