C - Operators

 

C Language

Operators



Fill in the blanks

    1.   How many operator presents in C___7___

    2.   % Symbol Means__Modulo____

    3.   ++ Symbol means Unary Increment Operator

    4.    -- Symbol means Unary Decrement Operator

    5.   Long int a=5; sizeof(a)=   4 

    6.    Int a=8; a++ =   8 

    7.   Int a=9; ++a=  10  

    8.   Write a symbol for bitwise AND  operator   & 

    9.   Write a symbol for bitwise OR  operator   | 

10.Write a symbol for bitwise EX-OR  operator    ^

11.Write a symbol for bitwise Not(1’s complement)                                    operator   ~ 

12.int a=3;int b=2; a^b=    1

13.Ascii value of E =  69

14. Ascii value of z  =  122

15. int a=15;  a<<2 =   60

16. int a=2;   a>>2  =   0

17. int a=5;int b=3;What is the output of this condition ((a==5)&&(b==2)) =  0

18. int a=7; (!a)  =  0

19. How many operator presents in Relational operator   6 

20. Write a syntax of conditional operator (condition)?true statement:false statement;

 

Answer the following Question

                                                                                              (4 marks)

    1.   Write a program for Arithmetic operator


# include<stdio.h>

# include<conio.h>

main()

{

int a, b ;  //declaration

clrscr() ;

printf ("Enter a and b value");

scant ("%d%d", &a,&b);

printf ("a+b = %d",a+b);

printf ("a-b = %d",a-b);

printf ("a*b = %d",a*b);

printf ("a/b = %d",a/b);

printf ("%d",a%b);

getch();

}

    2.   Write a program for Logical operator

#include<stdio.h>

#include<conio.h>

main()

{

int a=5,b=3;

clrscr();

printf("%d\n",((a==5)&&(b==2)));

printf("%d\n",((a==5)||(b==2)));

printf("%d",(!a));

getch();

}

    3.   Write a program for Conditional operator

#include<stdio.h>

#include<conio.h>

main()

{

int m;

clrscr();

printf("enter your mark");

scanf("%d",&m);

printf((m>=50)?"pass":"fail";

getch();

}

    4.   Write a program for Bitwise operator

#include<stdio.h>

#include<conio.h>

main()

{

int a=2,b=3;

clrscr();

printf("a&b=%d",a&b);

printf("a|b=%d",a|b);

printf("a^b=%d",a^b);

printf("~a=%d",a~b);

printf("a<<b=%d",a<<2);

printf("a>>b=%d",a>>2);

getch();

}

    5.   Write a program for Assignment operator

#include<stdio.h>

#include<conio.h>

main()

{

int x;

clrscr();

printf("enter x value");

scanf("%d",& x);

printf("x+=%d",x+=3);

printf("x-=%d",x-=2);

printf("x*=%d",x*=3);

printf("x/=%d",x/=2);

getch();

}

    6.   Write a program for Unary operator

# include<stdio.h>

# include<conio.h>

main()

{

int a = 5, b= ++a;

clrscr();

printf(" b value = %d\n a value = %d";b,a);

getch();

}

    7.   Write a program for Relational operator

# include <stdio.h>

# include <conio.h>

main()

{

int a;

clrscr();

scanf("%d%d",&a,&b);

printf("a<b=%d",a<b);

printf("a<=b=%d",a<=b);

printf("a>b=%d",a>b);

printf("a>=b=%d",a>=b);

printf("a==b=%d",a==b);

printf("a!=b=%d",a!=b);

getch();

}

 

Answer the following Question

                                                                                      (10 marks)

1.   Explain 7 operator .Write a program for 7 operator.

Operators

1. Arithmatic operator

2. Unary operator

3. Relational operator

4. Logical operator

5. Bitwise operator

6. Assignment operator

7. Contional operator

 

                       1.Arithmatic operator

 

+  = a+b = 5+2 = 7

-   = a-b = 5-2 = 3

*  = a*b = 5*2 = 10

/  = a/b = 5/2 = 2

% - remainder           a%b = 5%2 = 1

program:

# include<stdio.h>

# include<conio.h>

main()

{

int a, b ;  //declaration

clrscr() ;

printf ("Enter a and b value");

scant ("%d%d", &a,&b);

printf ("a+b = %d",a+b);

printf ("a-b = %d",a-b);

printf ("a*b = %d",a*b);

printf ("a/b = %d",a/b);

printf ("%d",a%b);

getch();

}

output

Enter a&b value 5  3

a+b=8

a-b=2

a*b=15

a/b=1

2

                      2. Unary operator  

 

(I)Increment                                  (II) Decrement

pre Increment (++a)                 predecrement (--a)

post Increment (a++)               postdecrement ( a--)

 

pre increment                          post Decrement

int  a=5                                         int a= 5

int b=++a                                      int b=a++

                                                             

b = 6                                        b = 5

a = 6                                        a = 6

pre Decrement  

int a =5                                        

int b = --a       

b =4                             

a= 4 

post Decrement    

int a = 5

int b = a--                  

b = 5

a = 4

 

program:

# include<stdio.h>

# include<conio.h>

main()

{

int a = 5, b= ++a;

clrscr();

printf(" b value = %d\n a value = %d";b,a);

getch();

}

3.Relational operator                    true - 1    false - 0

 

A<b

1

<=

A<=b

1

a>b

0

>=

a>=b

0

==

A==b

0

!=

A!=b

1

 

 

 PROGRAM

# include <stdio.h>

# include <conio.h>

main()

{

int a;

clrscr();

scanf("%d%d",&a,&b);

printf("a<b=%d",a<b);

printf("a<=b=%d",a<=b);

printf("a>b=%d",a>b);

printf("a>=b=%d",a>=b);

printf("a==b=%d",a==b);

printf("a!=b=%d",a!=b);

getch();

}

Output

enter a&b value  5 10

a<b=1

a<=b=1

a>b=0

a>=b=0

a==b=0

a!=b=1

              4.  Assignment  operator

      

+=        x+=3

-=         x-=2

*=        x*=3

/=         x/=2

 

  program:

#include<stdio.h>

#include<conio.h>

main()

{

int x;

clrscr();

printf("enter x value");

scanf("%d",& x);

printf("x+=%d",x+=3);

printf("x-=%d",x-=2);

printf("x*=%d",x*=3);

printf("x/=%d",x/=2);

getch();

}

Output

enter x value   5

x+=8

x-=6

x*=18

x/=9

               5.Conditional operator

syntax

(condition)? true statement : false statement ;

(m>=50)?"pass": "fail";

program

#include<stdio.h>

#include<conio.h>

main()

{

int m;

clrscr();

printf("enter your mark");

scanf("%d",&m);

printf((m>=50)?"pass":"fail";

getch();

}

        Output

enter your mark

75>=50      pass

34>=50      fail

                   6. Logical  operator

                

                               &&            AND

                                ||                OR

                                 !                  NOT

                             Truth Table:

A

B

AND

OR

NOT

0

0

0

0

1

0

1

0

1

1

1

0

0

1

0

1

1

1

1

0

      

               program

#include<stdio.h>

#include<conio.h>

main()

{

int a=5,b=3;

clrscr();

printf("%d/n",((a==5)&&(b==2)));

printf("%d/n",((a==5)||(b==2)));

printf("%d",(!a));

getch();

}

 

 

Output

0

1

0

 

                        7.Bitwise operator

 

&    -    AND

|     -    OR

^     -    EX-OR

~    -     NOT

<<   -    LEFT SHIFT

>>   -   RIGHT SHIFT

              program:

#include<stdio.h>

#include<conio.h>

main()

{

int a=2,b=3;

clrscr();

printf("a&b=%d",a&b);

printf("a|b=%d",a|b);

printf("a^b=%d",a^b);

printf("~a=%d",a~b);

printf("a<<b=%d",a<<2);

printf("a>>b=%d",a>>2);

getch();

}

Output

a&b=2

a|b=3

a^b=1

~a=-3

a<<2=8

a>>2=0

 

 

 

 

 

No comments:

Post a Comment