About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Unit-2 Bit-wise Operators

Control Statements / Control Structures / Conditional Structures /Decision Statements.

1.    Simple if statement
2.    if else statement
3.    Nested if else
4.    Ladder if else
5.    Switch case statement
6.    Goto statement
7.    Break
8.    Continue

Simple if statement
*   Simple if statement is also known as one way statement because decision in one time . If the condition is true only if block will be executed other wise not.
Syntax :-
If(condition)
{
statement 1;
statement 2;
}
Note –
If single statement is there no need to use bracket and if more than single statement then need of bracket is necessary.

If else statement
*   If else statement is also known as two way decision statement because it has paths to be executed if condition is true then only statements inside if block will be executed otherwise else block will be executed.


Syntax :-
If(condition)
{
statement 1;
statement 2;
               }
else
               {
statement 3;
statement 4;
               }
Nested if else
*   There is no proper syntax for structure for nested if else. The structure of syntax will upon the requirement of the program.
Syntax –
if(condition)
{
if(condition)
           {
statement 1;
statement 2;
           }
else
          {
statement 3;
statement 4;
          }
}


Ladder if else
èLadder if else is also known as multi way decision statement. Its structures seem like a ladder thats why it is known as ladder if else . It does not have fixed syntax or structures. The number of if else statement depends upon the requirement of the program .
Switch case statement
èSwitch case statement is also known as multiway statement decision statement . The switch statement tests the value in the list of “case” value and when match is founf then that case will be executed.
èdefault statement will be executed if no one case is match
èbreak is used to terminate the execution of the case and execute 
next first statement after the block.
Syntax-
switch(case label)
{
case label1:
statement 1;
statement 2;
break;
case label2:
statement 3;
statement 4;
break;
case label3:
statement 5;
statement 6;
break;
default:
statement 7;
break;
}

go to statement
ègo to statement is also known as jumping statement or forward jump and backward jump go to statement.
Syntax –
#include
#include
{
     Statement 1;
     Statement 2;
Goto loop;
     Statement 3;
     Statement 4;
loop:
statement 5;
statement 6;
     }














Operator Precedence and Associativity
Period
Operator
Description
Associativity
1
()
Function Call
Left To Right
1
[]
Array Element Reference
Left To Right
2
+
Unary Plus
Right to Left
2
-
Unary Minus
Right to Left
2
++
Increment
Right to Left
2
--
Decrement
Right to Left
2
!
Negation
Right to Left
2
~
Complement
Right to Left
3
*
Multiplication
Left To Right
3
/
Division
Left To Right
3
%
Mod / Modulus
Left To Right
4
+
Binary Plus
Left To Right
4
-
Binary minus
Left To Right
5
< 
Less than
Left To Right
5
<=
Less than equal to
Left To Right
5
> 
Greater Than
Left To Right
5
>=
Greater than equal to
Left To Right
6
&
Bitwise AND
Left To Right
7
^
Bitwise explosive OR/XOR
Left To Right
8
|
Bitwise OR
Left To Right
9
&&
Logical AND
Left To Right
10
||
Logical OR
Left To Right
11
?:
Conditional Operator
Right To Left
12
,
Comma
Left To Right


Control Loops –
*  Control loops are used where we have to execute same statements from multiple times. Loop reduces the code length also it makes our code more readable .There are three types of control loops and they are :-
·       while loop
·       do while loop
·       for loop
*  while loop :-
èIt is an entry control loop. It checks condition before executing the statements before executing the statements inside while loop if condition is true.
Syntax:-
while(condition)
{
statement 1;
statement 2;
             }

*  do while loop :-
èdo while loop is also known as exit control loop because compiler checks the condition at the time leaving the loop. If condition is false, although statements inside dowhile loop will be executed for once.
Syntax:-
do
{
     Statement 1;
     Statement 2;
}while(condition);

*  for loop :-
èfor loop is also known as entry control loop because compiler checks condition first then executes the statement inside the loop. for loop consist of initialization , condition and increment / decrement with the two semicolon . First compiler will initialize the value then it checks for the condition is true then only statements inside for loop will be executed and after execution the statements increment/decrement occurs.
Syntax :-
for(initialization;condition;increment/decrement)
{
         Statement 1;
         Statement 2;
}







Difference between while loop and do while loop.
1.   In while loop , condition is checked first.
2.   while loop is known as entry controlled loop.
3.   If condition is false, statements inside while loop will not be executed.
4.   Syntax: -
while(condition)
{
     statement 1;
     statement 2;
         }
1.   In do while loop , condition is lastly checked.
2.   do while loop is known as exit controlled loop.
3.   If condition is false, statement will be executed once.

4.   Syntax:-
do
{
     Statement 1;
     Statement 2;
} while(condition);


P- 1.Program to print “Hello” word.
è     #include
    #include
    Void main()
    {
clrscr();
printf(“Hello”);
getch();
    }

Output

HELLO
P - 2 .Program for addition of two numbers.
è #include
    #include
    Void main()
    {
int a=5,b=10,c;
clrscr();
                 c=a+b;
printf(“Additon is %d”,c);
getch();
   }

   Output

  Addition is 15

P - 3   Program for multiplying any two numbers on the number input
given  by the user.
è #include
    #include
    Void main()
    {
inta,b,c;
clrscr();
printf(“Please Enter Number 1 :”);
scanf(“%d”,&a);
printf(“Please Enter Number 2 :”);
scanf(“%d”,&b);
        c=a*b;
printf(“Multiplication of %d and %d is %d”,a,b,c);
getch();
   }

Output
   Please Enter Number 1 : 25                                  \\ Press Enter\\
   Please Enter Number 2 : 20                                  \\ Press Enter\\
   Multiplication of 25 and 20 is 500

P- 4 Program for checking whether the enter year is leap or not
è #include
#include
void main()
{
int year;
          clrscr();
          printf("Enter any year :");
          scanf("%d",&year);
          if(((year%4==0)&&(year%100!=0))||(year%400==0))
           {
          printf("LEAP YEAR");
           }
          else
           {
          printf("NOT A LEAP YEAR");
           }
          getch();     
}
Output
Enter any year : 2011                    \\ Press Enter\\
NOT A LEAP YEAR

P-5 Program to find out maximum number between two numbers.
è #include
#include
void main()
{
int num1;num2;
          clrscr();
          printf("Enter Two Numbers :");
          scanf("%d %d",&num1,&num2);
          if(num1>num2>
           {
          printf("Num1 is greater than Num2");
           }
          else
           {
          printf("Num2 is greater than Num1");
           }
          getch();     
}

Output
Enter Two Numbers : 12                 \\ Press Enter\\
6                                                          \\ Press Enter\\
Num1 is greater than Num2

P-6 Program to find out maximum number between two numbers using ternary operator
#include
#include
void main()
{
int num1;num2;
          clrscr();
          printf("Enter Two Numbers :");
          scanf("%d %d",&num1,&num2);
           (num1>num2>?printf("%d is greater",num1):printf("%d is greater",num2);
          getch();     
}
Output
Enter Two Numbers : 12                 \\ Press Enter\\
6                                                          \\ Press Enter\\
Num1 is greater than Num2


P-7  Program to check whether a person can vote or not using simple if statement
è #include
#include
void main()
{
int age;
          clrscr();
          printf("Enter Your Age :");
          scanf("%d",&age);
          if(age>=18)
           {
          printf("You Can Vote");
           }
          getch();     
}
Output
Enter Your Age : 21            \\ Press Enter\\
You Can Vote

P-8 Program to check whether a person can vote or not using if else statement.
è #include
#include
void main()
{
int age;
          clrscr();
          printf("Enter Your Age :");
          scanf("%d",&age);
          if(age>=18)
           {
          printf("You Can Vote");
           }
          else
           {
          printf("You can't Vote");
           }
          getch();     
}
Output
Enter Your Age : 21            \\ Press Enter\\
You Can Vote

P-9  Program to find greatest number among three numbers.
è #include
#include
void main()
{
int num1,num2,num3;
          clrscr();
          printf("Enter Three Numbers :");
          scanf("%d %d %d",&num1,&num2,&num3);
          if(num1>num2)
           {
          if(num1>num3)
          {
                   printf("Number 1 is greatest");
              }
                   else
              {
          printf("Number 3 is greatest");
              }
           }
          else
          {
          if(num2>num3)
                    {
                   printf("Number 2 is greater");
                    }
                   else
                    {
                   printf("Number 3 is greater");
                    }
          }
          getch();     
}
Output
Enter Three Numbers : 15                 \\ Press Enter\\
21                                                           \\ Press Enter\\
16                                                           \\ Press Enter\\
Number 2 is greater
P-10 Program to find out greatest number among three numbers using ternary operator
è #include
#include
void main()
{
int num1,num2,num3;
          clrscr();
          printf("Enter Three Numbers :");
scanf("%d %d %d",&num1,&num2,&num3);
          ans=(num>num2)?((num1>num2)?num1:num2):((num2>num3)?num2:num3));
          printf("Greatest number is %d",ans);
          getch();     
}
Output
Enter Three Numbers : 15                 \\ Press Enter\\
21                                                           \\ Press Enter\\
16                                                           \\ Press Enter\\
Number 2 is greater

P-11 Program to find out greatest number among three numbers using logical operator
è #include
#include
void main()
{
int num1,num2,num3;
          clrscr();
          printf("Enter Three Numbers :");
          scanf("%d %d %d",&num1,&num2,&num3);
          if((num1>num2)&&(num1>num3))
           {
          printf("%d is the greatest number",num1);
           }
          else
           {
          if((num2>num1)&&(num2>num3))
                     {
                   printf("%d is the greatest number",num2);
                     }
                   else
                     {
                   printf("%d is the greatest number",num3);
                     }
           }
          getch();     
}
Output
Enter Three Numbers : 15                 \\ Press Enter\\
21                                                           \\ Press Enter\\
16                                                           \\ Press Enter\\
Number 2 is greater

P-12 Program to find out a grade of a student upon the following criteria.
                A                     B                     C                    Fail
100                        80                 60                  40                 0
è #include
#include
void main()
{
int per;
          clrscr();
          printf("Enter Your Percentage :");
          scanf("%d",&per);
          if((per>=80)&&(per<=100))
           {
          printf("Grade A");
           }
          else
           {
          if((per>=60)&&(per<80 o:p="">
                     {
                   printf("Grade B");
                     }
                   else
                     {
                   if((per>=40)&&(per<=59))
                               {
                             printf("Grade C");
                               }
                             else
                               {
                             if((per>=0)&&(per<40 o:p="">
                                                 {
                                                printf("Fail");
                                                 }
                                                else
                                                 {
                                                printf("Invalid Percentage");
                                                 }
                               }
                     }
           }
          getch();     
}
Output
Enter Your Percentage : 20                   \\ Press Enter\\
Fail
P-13 Program to print name of day according to the number input.
è #include
#include
void main()
{
int day;
          clrscr();
          printf("Enter Any Number From 1 to 7:");
          scanf("%d",&day);
          switch(day)
           {
          case 1:
                   printf("Today is Monday");
          break;                                    
case 2:
          printf("Today is Tuesday");
          break;
case 3:
                   printf("Today is Wednesday");
break;
          case 4:
                   printf("Today is Thursday");
break;
case 5:
                   printf("Today is Friday");
break;
          case 6:
          printf("Today is Saturday");
break;
case 7:
                             printf("Today is Sunday");
break;
default:
                   printf("Invalid Number");
break;
           }
          getch();     
}
Output
Enter Any Number From 1 to 7: 1                   \\ Press Enter\\
Today is Monday
P-14  Program to find a character whether it a constant or vowel using switch case statement.
è #include
#include
void main()
{
charch;
          clrscr();
          printf("Enter Any Character:");
          scanf("%c",&ch);
          switch(ch)
           {
          case 'a':                                 
                   case 'A':
                   case 'e':
                   case 'E':
                   case 'i':
                   case 'I':
                   case 'o':
                   case 'O':
                   case 'u':
                   case 'U':
                   printf("Vowel");
break;
                   default:
                   printf("Consonant");
break;
           }
          getch();     
}
Output
Enter Any Character: A                   \\ Press Enter\\
Vowel
P-15  Program to find a character whether it a constant or vowel using ladder if else.
è #include
#include
void main()
{
charch;
          clrscr();
          printf("Enter Any Character:");
      scanf("%c",&ch); …..if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U'||)
           {
          printf("Vowel");
           }
          else
           {
          printf("Consonant");
           }
          getch();     
}
Output
Enter Any Character: A                   \\ Press Enter\\
Vowel

P-16  Program To Find ASCII value
è #include
#include
void main()
{
charch;
          clrscr();
          printf("Enter Any Character:");
          scanf("%c",&ch);
          printf("ASCII value is : %i",ch);
          getch();     
}
Output
Enter Any Character: a                   \\ Press Enter\\
97

P-17  Program To Find Number Of days in a month according to the month number entered using switch.
è #include
#include
void main()
{
int cu;
          clrscr();
          printf("Enter Any number:");
          scanf("%d",&cu);
          switch(cu)
           {
          case 1:                                   
                   case 3:
                   case 5:
                   case 7:
                   case 8:
                   case 10:
                   case 12:
          printf("31 days");
break;
                   case 4:
                   case 6:
                   case 9:
                   case 11:
                   printf("30 days");
break;
                   case 11:
                   printf("28 days");
break;                                    
                   default:
                             printf("Invalid Number");
break;
           }
          getch();     
}
Output
Enter Any number: 10                   \\ Press Enter\\
31 days

P-18  Program to check whether entered number is even or odd using ternary operator.
è #include
#include
void main()
{
intnum;
          clrscr();
          printf("Enter Any Number :");
          scanf("%d",&num);
          num%2==0?printf("Even Number"):printf("Odd Number");
          getch();     
}
Output
Enter Any number: 10                   \\ Press Enter\\
Even Number

P-19 Program to check whether entered number is even or odd using if else.
è #include
#include
void main()
{
intnum;
          clrscr();
          printf("Enter Any Number :");
          scanf("%d",&num);
          if(num%2==0)
           {
          printf("Even Number");
           }
          else
           {
          printf("Odd Number");
           }
          getch();     
}
Output
Enter Any number: 10                   \\ Press Enter\\
Even Number

P-20  Program to check whether entered number is even or odd using switch.
è #include
#include
void main()
{
intnum;
          clrscr();
          printf("Enter Any Number :");
          scanf("%d",&num);
          switch(num%2)
           {
                   case 0:
                   printf("Even");
break;
                   case 1:
                   printf("Odd");
break;                                    
                   default:
                   printf("Invalid Number");
break;
           }
          getch();     
}
Output
Enter Any number: 10                   \\ Press Enter\\
Even Number

P-21 Program to check whether enter character is small case , upper case or digit.
è #include
#include
void main()
{
charch;
          clrscr();
          printf("Enter Any Character :");
          scanf("%c",&ch);
          if(ch>=65&&ch<=90)
           {
          printf("Upper Case");
           }
          else
           {  
          if(ch>=97&&ch<=122)
               {
          printf("Lower Case");
               }
else
{
                   if(ch>=48&&ch<=57)
                             {
                             printf("Digit");
                             }
                             else
                             {
                             printf("Special Character");
                             }
          }
           }
         
          getch();     
Output
Enter Any Character: a                   \\ Press Enter\\
Lower Case

P-22  Program to divide two numbers but the denominator should not be zero using goto statement.
è #include
#include
void main()
{
int num1,num2;
          clrscr();
          printf("Enter Number 1 :");
          scanf("%d",&num1);
          printf("Enter Number 2 :");
          scanf("%d",&num2);
          if(num2==0)
           {
          goto back;
           }
          else
           {  
          printf("Division is %d",num1/num2);
           }
          getch();     
}
Output
Enter Number1 : 10                   \\ Press Enter\\
Enter Number2 : 2                     \\ Press Enter\\
Division is 5

P-23  Program to change upper case to lower case and vice-versa.
è #include
#include
void main()
{
charabc;
          clrscr();
          printf("Enter Any Character :");
          scanf("%c",&abc);
          if(abc>=65&&abc<=90)
           {
          printf("%c",abc+32);
           }
          else
           {  
          if(abc>=97&&abc<=122)
                     {
          printf("%c",abc=32);
           }
else
 {
                   printf("Invalid Character");
            }               
           }
          getch();     
}
Output
Enter Any Character : A                   \\ Press Enter\\
a

P-24 Program to change upper case to lower case using tolower.
è #include
#include
#include
void main()
{
char p;
          clrscr();
          printf("Enter Any Character :");
          scanf("%c",&p);
           p=toupper(p);
          printf("%c",p);
          getch();     
}
Output
Enter Any Character : a                   \\ Press Enter\\
A
P-25 Program to change lower case to upper case using toupper.
è #include
#include
#include
void main()
{
char p;
          clrscr();
          printf("Enter Any Character :");
          scanf("%c",&p);
           p=tolower(p);
          printf("%c",p);
          getch();     
}
Output
Enter Any Character : A                   \\ Press Enter\\
A
P-26 Program to print message for 10 times.
è #include
#include
void main()
{
int i;
          clrscr();
           i=1;
          while(i<=10)
           {
          printf("\n Hello Class...");
                   i++;
           }
          getch();     
}
Output
Hello Class…
Hello Class…
Hello Class…
Hello Class…
Hello Class…
Hello Class…
Hello Class…
Hello Class…
Hello Class…
Hello Class…

P-27  Program to print the table of entered number.
è #include
#include
void main()
{
intnum,i;
          clrscr();
     printf("Enter Any Number :"),
          scanf("%d",&num);
           i=1;
          while(i<=10)
           {
          printf("%d\n",num*i);
i++;
           }
          getch();     
}
Output
Enter Any Number : 2                   \\ Press Enter\\
2
4
6
8
10
12
14
16
18
20

P-28  Program to print a table of any entered number in the following format up to 10 times
          5*1=5
          5*2=10
è #include
#include
void main()
{
intnum,i;
          clrscr();
          printf("Enter Any Number :"),
          scanf("%d",&num);
           i=1;
          while(i<=10)
           {
          printf("%d * %d = %d\n",num,i,num*i);
i++;
           }
          getch();     
}
Output
Enter Any Number : 5                   \\ Press Enter\\
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

P-29  Program to print first 15 even numbers.
è #include
#include
void main()
{
inti,j;
          clrscr();
           i=1;
          while(i<=15)
           {
               j=2*I;
          printf("%d\n",j);
          i++;
           }
          getch();     
}
Output
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30

P-30  Program to print first 10 odd numbers.
è #include
#include
void main()
{
inti,j;
          clrscr();
           i=0;
          while(i<=10)
           {
               j=2*i+1;
printf("%d\n",j);
i++;
           }
          getch();     
}
Output
1
3
5
7
9
11
13
15
17
19

P-31  Program to calculate factorial of a number using while loop
è #include
#include
void main()
{
intnum,i,fact=1;
          clrscr();
printf("Enter any number :");
          scanf("%d",&num);
           i=num;
          while(i>=1)
           {
          fact=fact*i;
          i--;
           }
          printf("Factorial is %d",fact);
          getch();     
}
Output
Enter any number : 5               \\ Press Enter\\
120

P-32  Program to calculate the sum of digits of any number using while loop.
è #include
#include
void main()
{
intnum,dig,sum=0;
          clrscr();
printf("Enter any number :");
          scanf("%d",&num);
          while(num!=0)
           {
          dig=num%10;
                   sum=sum+dig;
                   num=num/10;
           }
          printf("Summation is %d",sum);
          getch();     
}
Output
Enter any number : 123          \\Press Enter\\
6
P-33  Program to reverse a number using while loop.
è #include
#include
void main()
{
intnum,dig,rev=0;
          clrscr();
printf("Enter any number :");
          scanf("%d",&num);
          while(num!=0)
           {
          dig=num%10;
                   rev=rev*10+dig;
                   num=num/10;
           }
          printf("Reverse number is %d",rev”);
          getch();     
}
Output
Enter any number : 123          \\Press Enter\\
321

P-34  Program to check whether enter number is Armstrong number or not.
è #include
#include
void main()
{
intnum,dig,sum=0,temp;
          clrscr();
printf("Enter any number :");
          scanf("%d",&num);
          temp=num;
          while(num!=0)
           {
          dig=num%10;
                   sum=sum+(dig*dig*dig);
                   num=num/10;
           }
          if(temp==sum)
           {
          printf("Armstrong Number");
           }
          else
           {
          printf("Not a Armstrong Number");
           }
          getch();     
}
Output
Enter any number : 123          \\Press Enter\\
Not a Armstrong Number

P-35  Program to check whether enter number is prime or not using while loop.
è #include
#include
void main()
{
intnum,i,count=0;
          clrscr();
printf("Enter any number :");
          scanf("%i",&num);
           i=1;
          while(i<=num)
           {
          if(num%i==0)
                    {
                   count++;
                    }
                   i++;
           }
          if(count==2)
           {
          printf("Number is Prime");
           }
          else
           {
          printf("Number is not Prime");
           }
          getch();     
}
Output
Enter any number : 6          \\Press Enter\\
3

P-36 Program to check whether enter number is prime or not using do while loop.
è #include
#include
void main()
{
intnum,i,count=0;
          clrscr();
printf("Enter any number :");
          scanf("%i",&num);
           i=1;
          do
           {
          if(num%i==0)
                    {
                   count++;
                    }
                   i++;
          }while(i<=num);
          if(count==2)
           {
          printf("Number is Prime");
           }
          else
           {
          printf("Number is not Prime");
           }
          getch();     
}
Output
Enter any number : 7          \\Press Enter\\
Number is Prime

P-37 Program to print numbers in fabonacci series using do while loop.
è #include
#include
void main()
{
int a=0,b=1,c,i=1,num;
          clrscr();
printf("Enter any number :");
          scanf("%d %d",&a,&b);
          do
           {
               c=a+b;
                   printf("%d",c);
                    a=b;
                    b=c;
                   i++;
          }while(i<=num-2);
          getch();     
}
Output
Enter any number : 7          \\Press Enter\\
0
1
1
2
3
5
8

P-38 Program to print numbers in fabonacci series using while loop.
è #include
#include
void main()
{
int a=0,b=1,c,i=1,num;
          clrscr();
printf("Enter any number :");
          scanf("%d %d",&a,&b);
          while(i<=num-2)
           {
               c=a+b;
                   printf("%d",c);
                    a=b;
                    b=c;
                   i++;
           }
          getch();     
}
Output
Enter any number : 7          \\Press Enter\\
0
1
1
2
3
5
8

P-39  Program to check whether enter number is Palindrome or not using for loop.
è #include
#include
void main()
{
intnum,sum=0,dig,temp;
          clrscr();
printf("Enter any number :");
          scanf("%d",&num);
          temp=num;
          for( ;num!=0; )
           {
          dig=num%10;
                   sum=sum*10+dig;
                   num=num/10;
           }
          if(temp==rev)
           {
          printf("Palindrome Number");
           }
          else
           {
          printf("Not a Palindrome Number");
           }
          getch();     
}
Output
Enter any number : 12321          \\Press Enter\\
Palindrome Number





No comments: