Search This Blog


Monday, September 30, 2019

Looping statement OR Iterative statement



This statement is used to repeat a block of statements multiple times according to the specify condition.


Any loop consists of three parts :-

           I.            Initialization part
         II.            condition part
       III.            increment/ decrement part

⇒Types of Looping statements :-

  There are three loops in C and C++

                I.            for loop
              II.            while loop
            III.            do – while loop ( exit control loop )

➤For Loop :-

  •   In this loop we can specify initialization part , condition  and increment / decrement part at a time .
  •   It is a type of entry control loop because condition is checked first.
  •  At first initialization part is executed.
  •  After that condition is checked and then block of stmts is executed.
  •  And then after increment/ decrement is executed and again condition is checked , and the loop continued.

  Syntax :-

   for( initialization ; condition ; increment / decrement )
    {
      block of stmts;
      }
 
looping statement for loop
syntax of for loop 

  

example 1:-

    
   for ( i=1 ; i <= 100 ; i++ )
    {
      printf( “ %d \n “, i );
    }

  

Example 2:-

    
   i = 1;
    for(   ;  i <= 100 ;  )
   {
     printf( “ %d \n “ , i );
    i++;
    }

⏩Standard form of for loop

         
           I.            Infinity for loop
                for (  ;  ;  )
         {
           printf( “  Saurav “ ) ;
          }
           II.            for ( i=1 , j = 1 , k = 1 ; condition ; i++, j++ ,k++ )
{
  block of stmts ;
  }
         III.            for (  ;  condition  ;  )
{
  block of stmts ;
}

➤While Loop :-

  • In this loop all the three parts are specified separately.
  • It is a type of entry control loop because condition is checked first .
  • At first initialization part is executed.
  • After that condition is checked and then block of stmts is executed.
  • And then after increment/ decrement is executed and again condition is checked , and the loop continued.

Syntax :-

   initialization
  while( condition )
    {
     block of stmts ;
     increment/ decrement;
   }
looping statement whle loop
syntax of while loop

Example:-

   i=1;
  while ( I <= 100 )
  {
    printf( “ %d\n “, &i);
    i++;
  }

➤Do – While loop :-

  • In this loop all the three parts are specified separately.
  • It is a type of exit control loop because block of stmts is executed before the condition .
  • At first block of stmts  part is executed.
  • After that increment/ decrement part is executed .
  • Then after condition is checked , if it is true then the block of stmts is executed again and the loops continued.

Syntax :-

   initialization;
    do
  {
    block of stmts ;
    increment / decrement ;
   }
  while ( condition );
looping statement do while loop
syntax of do-while loop

  

Example :-

   i=1;
   do
   {
     printf( “ %d \n “, &I );
     i++;
    }
   while ( i <= 100 );


Let us know how different these loops are in programming.

P1. Program to print the series of natural no. upto 100 by for loop.

 #include<stdio.h>
#include<conio.h>
  void main()
 {
   clrscr();
   int i;
   printf( “ The series of natural no. upto 100 are \n “);
  for( i =1 ; i <= 100 ; i ++ )
    printf( “ %d \n “, i );
  getch();
 }
For loop programming of natural number
Program to print the 100 natural no.  By for loop

P2. Program to print the series of natural no. upto 100 by while loop.

  #include<stdio.h>
  #include<conio.h>
    void main()
   {
     clrscr();
     int i;
     printf( “The series of natural no. upto 100 are \n “);
      i=1;
      while( i <= 100 )
     {
      printf( “ %d\n “, i );
      i++;
     }
    getch ();
   }
While loop programming of natural number
Program to print the 100 natural no. By while loop

P3. Program to print the series of natural no. upto 100 by do - while loop.

  #include<stdio.h>
  #include<conio.h>
    void main()
   {
     clrscr();
     int i;
     printf( “The series of natural no. upto 100 are \n “);
      i=1;
       do
        {
         printf( “ %d\n “, i );
          i++;
         }
       while( i <= 100 );
    getch ();
   }
Do while programming of natural number
Program to print the 100 natural no. By do while loop


We practice more programs of looping statement further.

Friday, September 27, 2019

Switch case statement


  V. Switch  case statement

·         It is mainly used to match only character and integer type value.
·         Its execution is fast rather than else if ladder stmt.
·         It is generally used to create choice based program.
·         We can not check condition like if condition.

syntax:

switch ( variable / constant / expression )
{
   case < value 1 > :
  block of stmts;
  break ;
  case < value 2 > :
  block of stmts ;
   break ;
   
  
  case < value n > :
  block of stmts ;
  break ;
  default :
   block of stmts ;
 }
Switch case statement with syntax
Syntax of switch case statement 

Note :-

      a)      Case keyword is used to specify value.
b)      break keyword is optional which is used to terminate from switch stmts.
c)       default is optional part. It will be executed if any case does not match.

Example 1:-

  int digit =2;
  swirch ( digit )
 {
   case 0 :
   printf ( “ zero “);
   break;
   case 1:
    printf( “ one “ );
   break ;
  .…
  ….
  case 9 :
   printf ( “ nine “ );
   break ;
  default :
   printf ( “ Invalid digit “ );
  }

Example 2:-

  char ch = ‘E’ ;
  switch ( ch )
  {
   case ‘A’ :
   case ‘a’ :
   case ‘ E’ :
   case ‘e’ :
   case ‘I’ :
   case ‘I’ :
   case ‘O’ :
   case ‘o’ :
   case ‘U’ :
   case ‘u’ :
   printf( “ vowel character “);
   break ;
  default :
     printf( “ consonant character “ );
  }

P1.Program to input two numbers and any arithmetic operator after that performed arithmetic operation using switch case statement.

#include<stdio.h>
#include<conio.h>
  void main()
  {
   clrscr();
   int a , b;
  char opt ;
  printf( “ enter two numbers “ );
  scanf( “ %d %d “, &a ,&b );
   fflush (stdin);
   printf ( “ Enter any arithmetic operator “ );
   scanf( “ %c “, &opt);
   switch ( opt )
   {
     case ‘+’ :
    printf( “ Addition = %d “, a+b);
    break;
     case ‘-‘ :
   printf( “ Subtraction = %d “, a-b);
    break;
    case ‘*’ :
     printf( “ Multiplication = %d “ , a*b );
      break;
    case ‘/’ :
    printf( “ Division = %d “, a/b );
    break;
  default :
   printf( “ Invalid arithmetic operator  “ );
   }
  getch()
  }
Calculator program in C by switch case statement
Program to make simple calculator using switch case

P2. Program to input month by number and after that print only how many days are available in  that month.

#include<stdio.h>
 #include<conio.h>
  void mian()
{
   clrscr();
  int a;
  printf( “ enter any month day no. “);
  scanf( “ %d” , &a );
  switch ( a )
  {
   case 1:
   case 3 :
   case 5 :
   case 7 :
   case 9 :
    case 10 :
    case 12 :
     printf( “ 31 days “ );
   break ;
   case 2 :
   printf( “ 28 or 29 days “);
    break ;
   case 4 :
   case 6 :
    case 9 :
   case 11:
   printf( “ 30 days “ );
   break ;
   default :
    printf( “ Invalid months  no. “ );
   }
 getch () ;
 }
C program to print no. Of days of months
Program to find no. Of days of any month no. Entered 

Wednesday, September 25, 2019

Practice program ( decision making )

Programs of Decision making statement 

For understanding these programs you have to first clear the concept of decision making statements.

P1. Program to find the greatest number among the three no.

#include<stdio.h>
#include<conio.h>
 void main()
 {
  clrscr();
  int a, b, c, gt;
  printf(“ enter the three numbers “);
 scanf(“ %d %d %d “, &a, &b, &c );
  if(a>b && a>c)
  gt= a;
  else if (b>c)
  gt= b;
  else
  gt= c;
   printf(“ greatest number = %d “, gt);
getch();
}
Decision making program
Program to find greatest among three no. 


P2. Program to input marks of five subject after that find total marks, percentage marks and remarks according to given condition.

                                     I.            percentage >= 90   remarks= ‘excellent’
           II.            percentage >=75    remarks=’very good’
         III.            percentage < 75 an percentage >= 60  ‘good’
        IV.            otherwise   remarks =’poor’


#include<stdio.h>
#include<conio.h>
 void main()
 {
  clrscr();
  int a, b, c, d, e, per, T;
  printf(“ enter the marks of five subject” );
  scanf( “%d %d %d %d %d “, &a, &b ,&c ,&d, &e );
  T= a+b+c+d+e; 
  per= T/5;
  if(per >= 90)
   printf(“ Remarks = \”excellent\” );
 else if (per > 75)
   printf( “ Remarks = \”very good\”);
  else if( per > 60)
     printf( “ Remarks =\ “good\”);
  else
     printf( “ Remarks = \”poor\” );
printf(“ Total marks = %d”, T);
printf(“ \n percentage marks = %d “, per);
getch();
}

Decision making program
Program to print marksheet 

P3. Programs of basic salary of an employee after that find growth salary for given extra allowance on basic salary .

Basic salary
HRA
DA
TA
MA
>= 20000
40%
90%
40%
10%
< 20000
30%
70%
30%
8%
< 15000
20%
60%
20%
5%
< 10000
15%
50%




#include<stdio.h>
#include<conio.h>
 void  main()
  {
   clrscr();
  float bs, hra, da, ta, ma, gs;
  printf(“ enter the basic salary of the employee “);
  scanf( “ %f” ,&bs );
   if( bs >= 20000)
  {
     hra= (bs * 40 )/ 100;
    da = ( bs *90 )/ 100;
    ta= ( bs * 40 )/ 100;
   ma= (bs * 10) /100;
   gs = bs+ hra + da + ta + ma ;
  }
 else if (bs < 20000 && bs >= 15000)
  {
    hra= (bs * 30) / 100;
   da = (bs * 70 )/ 100;
   ta = (bs * 30 )/100;
  ma = (bs * 8 )/100;
   gs= bs + hra + da + ta + ma;
  }
else if ( bs < 15000 && bs >= 10000)
   {
    hra = (bs * 20 )/ 100;
   da = (bs * 60 )/ 100;
   ta = ( bs * 20 )/ 100;
   ma =( bs * 5 )/ 100;
   gs = bs + hra + da + ta + ma ;
  }
else
   {
     hra = ( bs * 5 ) /100;
     da = ( bs * 50 )/ 100;
    gs = bs +  hra + da ;
  }
printf( “ basic salary = %f \n hra = %f \n da= %f \n ta= %f \n 
                 ma= %f\n gs= %f”, bs, hra, da, ta, ma, gs );
getch();
}
Decision making program
Program to print salary slip 1

Decision making program
Program to print salary slip 2


P4.  Program to input any character after that check character is lower alphabet , upper alphabet, digit or special character .

#include<stdio.h>
#include<conio.h>
 void main()
{
  clrscr();
  char ch;
  printf(“ enter any character “);
  scanf(“%c”, &ch);
   if( ch >= “A” && ch <= “Z”)
     printf(“  Upper alphabet “);
    else if( ch >= “a” && ch  <= “z”)
      printf(“ lower alphabet “ );
   else if ( ch >= “0” && ch <= “9”)
      printf( “ digit” );
   else
     printf( “ special character “);
getch();
}
Lower upper alphabet  digit and special character program
Program to check form of character 


P5. Program to input any year after that check year is leap or not using nested if.

#include<stdio.h>
#include<conio.h>
 void main()
 {
  clrscr();
   int a;
   printf(“enter any year “ );
   scanf(“ %d “, &a );
   if ( a% 100! = 0)
     {
        if( a% 4 == 0)
         printf( “ leap year “);
       }
  else if( a % 400 == 0 )
    printf( “ leap year “);
    else
     printf( “ not leap year “);
  getch();
}

Leap year program
Program to check leap year



P6. Program to input three nos. after that find smallest no. using nested if.

 #include<stdio.h>
#include<conio.h>
void main()
{
  int a, b, c,st;
  clrscr();
  printf( “ enter the three nos. “);
  scanf( “ %d %d %d “, &a, &b, &c);
    if( a <b)
     {
       if( a < c)
           st=a;
       else
          st=c;
      }
 else if(b<c)
  {
   st= b;
 else
  st=c;
  }
printf(“ smallest no.= %d”, st);
getch();
}
Program of finding Smallest number among three nos.
Program to find smallest number among three numbers