Showing posts with label C. Statement. Show all posts
Showing posts with label C. Statement. Show all posts

Saturday, 18 August 2018

Difference between if else statement an switch statement in C using a program

Program To Find Maximum Of Three Number Using 'If else' Statement 

/*program to find maximum of 3 number*/
#include <stdio.h>
void main ()
{
int a,b,c;
clrscr ();
printf ("/n Enter value of a :");
scanf ("%d", & a);
printf ("/n Enter value of b :");
scanf ("%d", & b);
printf ("/n Enter value of c :");
scanf ("%d", & c);
if ((a>>b)&&(a>>c))
printf ("/n a is maximum number:");
else if ((b>>a)&&(b>>c))
printf ("/n b is maximum number:");
else
printf ("/n c is maximum number:");
getch ();
}

Here we use if else statement for finding maximum of three number.

Output :: Enter value of a 
               a = 25                                                                                                                                                                                           Enter value of b
               b = 35
               Enter value of c
               c = 45 

               Maximum number is c.

Program To Displays Color According To The Enter Character From User in C

/*program to print color*/
# include <stdio.h>
void main ()
{
char ch ;
clrscr () ;
printf ("/n enter a character :") ;
scanf ("%c", & ch) ;
switch (ch)
{
case 'r' ;
printf ("\n colour is red :") ;
break ;
case 'w' ;
printf ("\n color is white :") ;
break ;
case 'y' ;
printf ("\n color is yellow :") ;
break ;
case 'g' ;
printf ("\n color is green :") ;
break ;
case 'b' ;
printf ("\n color is blue :") ;
break ;
default :
printf ("\n invalid character :") ;
}
getch () ;
}

Difference in points :

IF else statement in C

1. Here we can use more than one statement for execution.

2. Here we can use all types of variable.

3. If else statement contains two types of statement means one in if and other in else.

4. If condition written inside if is false then compiler will shift to the statement written in else statement.

5. syntax :  @  if statement 
   
1. syntax :  if (condition)
                   statement :

2. syntax :  if (condition)
                  {
                  statement 1 ;
                  statement 2 ;
                  '
                  '
                 }

@  if else statement 

1. syntax;  if (condition)
      {
      statement ;
      '
      '
      }
      else
      {
      statement ;
       '
       '
       }


Switch statement in C

 1. Here we can use only one statement for execution.

 2. Here we can use only character types of variable.

 3. switch statement contains two types of statement means one in switch and other in default statement.

4. If condition written inside switch is false then compiler will shift to the statement written in default statement.

5. @  switch statement 
    
   syntax ;   switch ( conditional variable )
                  {
                  case value 1 ;
                  statement ;
                  break ;
                  case value 2 ;
                  statement ;
                  break ;
                  '
                  '
                  '
                 default :
                 statement ;
                 }












Monday, 30 July 2018

Difference between while loop statement and for loop statement in C programming

While statement ::


Syntax :: (1)  while ( condition )
                 statement ;

               (2) while ( condition )
                    {
                     statement 1 :
                        "
                        "
                    }

For loop statement

Syntax ::  for ( variable initialization ; condition ; variable increment or decrement )
                {
                statement 1 ;
                "
                "
                "
                }

Here as from the syntax property both of them having different syntax property. In case of while loop statement only condition of program is written with while loop statement and variable initialization is done outside the while loop statement and the variable increment or decrement is done inside loop of condition.
                 In case of for loop statement all of them is written within the statement. The working is similar to the while loop statement. First of all variable initialization is done and then conditional statement is execute then the variable increment or decrement is execute.

Program to difference while and for loop statement ::

Using for loop statement ::

#include <stdio.h>
void main ()
{
int n , a , i = 0 ;
clrscr () ;
printf ("\n Enter how many terms you want to print:") ;
scanf ("%d", &n) ;
for ( a = 2 ; i <= n : i++ )
printf ("\n %d", a) ;
a = a + 2 ;
getch () ;
}

Output :: Enter how many terms you want to print
                Enter number  = 10
                Even number are = 2 , 4 , 6 , 8

Using while loop statement ::

#include <stdio.h>
void main ()
{
int n , a = 2 , i = 0 ;
clrscr () ;
printf ("\n Enter how many terms you want to print:") ;
scanf ("%d", &n) ;
while ( i < n )
{
printf ("\n %d", a) ;
a = a + 2 ;
i++ ;
}
getch () ;
}

Output :: Enter how many terms you want to print
                Enter number  = 10
                Even number are = 2 , 4 , 6 , 8


Friday, 20 April 2018

Statement Used In C Program

Control Statement ;

> control statement are used to control the flow of the execution of the program .
> control statement can be divided into three categories.

1. Conditional control statement
2. Repetitional control statement
3. Jump control statement

# Conditional control statement ;  

@  if statement 
   
1. syntax :  if (condition)
                   statement :

2. syntax :  if (condition)
                  {
                  statement 1 ;
                  statement 2 ;
                  '
                  '
                 }

@  if else statement 

1. syntax;  if (condition)
      {
      statement ;
      '
      '
      }
      else
      {
      statement ;
       '
       '
       }

@  switch statement 
   
   syntax ;   switch ( conditional variable )
                  {
                  case value 1 ;
                  statement ;
                  break ;
                  case value 2 ;
                  statement ;
                  break ;
                  '
                  '
                  '
                 default :
                 statement ;
                 }
       
#   Repetitional control statement ;

@  while statement

syntax ;   while (condition)
               {
               statement ;
               '
               '
               '
               }


@  for statement

syntax ;  for ( variable initialization : condition ; increment or  decrement )
             {
             statement 1 ;
             '
             '
             '
            }          
         
@  do while statement

syntax :  do
              {
              statement 1 ;
               '
               '
               '
               } while (condition) ;
                    

#  Jump control statement 

@  break statement

syntax ;   break ;


@  continue statement

syntax :   continue ;


@  if go to statement

syntax :   go to lablename ;


Tuesday, 17 April 2018

Program To Find Maximum Of Three Number Using 'If else' Statement

/*program to find maximum of 3 number*/
#include <stdio.h>
void main ()
{
int a,b,c;
clrscr ();
printf ("/n Enter value of a :");
scanf ("%d", & a);
printf ("/n Enter value of b :");
scanf ("%d", & b);
printf ("/n Enter value of c :");
scanf ("%d", & c);
if ((a>>b)&&(a>>c))
printf ("/n a is maximum number:");
else if ((b>>a)&&(b>>c))
printf ("/n b is maximum number:");
else
printf ("/n c is maximum number:");
getch ();
}


Here is the program to find maximum of three number using 'if else ' statement . Here we have to use condition for only two times. First of all we have to declare three number and input the number from the user and then check the condition. These type of statement is also called ' Nested if else'.so we can write program from both of the ways. 

Featured post

Kalgudi Interview Round!!

Those who cleared the second round of kalgudi got an email from exceller to complete their Kalgudi Interview Round. So here we will discuss ...