as

Wednesday, 12 November 2014

if…else Statement in C


Now let us try to understand the control statements in C Language.

if…else


Above diagram illustrates how if else construct works in c language. Condition is tested in if else construct and if that condition turns out to be true, control goes to true block (if block) otherwise it goes to false block (else block).

Program to find whether number is even or odd

#include<stdio.h>             
void main()
{
 Int n;
 system("cls")
 printf("Enter no");
 scanf("%d",&n);
 if(n%2==0)
  {
    printf("\n%d is even number",n);
  }
 else
  {
    printf("\n%d is odd number",n);
  }
getchar();

}


Program to compare two numbers.

#include<stdio.h>

void main()
{
  int a,b;
  printf("\n Enter 1st number : ");
  scanf("%d: ",&a);
  printf("\n Enter 2nd number : ");
  scanf("%d: ",&b);
  if(a>b)
  {
    printf("\n The greater number is a : %d",a);
  }
  else
  {
    printf("\n The greater number is b : %d",b);
  }
  getchar();
}



Program to check whether Year is leap or not.

#include<stdio.h>

void main()
{
  int year;
  printf("\n Enter Year : ");
  scanf("%d: ",&year);
  if(year%4==0)
  {
    printf("\n The %d is leap year. ",year);
  }
  else
  {
    printf("\n The %d is not leap year.",year);
  }
  getchar();
}


Program to accept character from user and check whether it is vowel or consonant .

#include<stdio.h>

void main()
{
  char ch;
  printf("\n Enter character : ");
  scanf("%c: ",&ch);
  if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
  {
    printf("\n The %c is vowel. ",ch);
  }
  else
  {
    printf("\n The %c is consonant.",ch);
  }
  getchar();
}




Sponsored Links

Popular Posts

Comments

ShareThis

Google+ Followers

Google+ Badge