1. write a simple program that prints the results of all operators available in c


 1a) Arithmetic Operators

Source Code:

#include<stdio.h>
void main()
{
int a,b,sum,sub,mul,div,rem;
printf("Enter a,b Values\n");
scanf("%d%d",&a,&b);
sum=a+b;
sub=a-b;
mul=a*b;
div=a/b;
rem=a%b;
printf("Addition=%d\n",sum);
printf("Subtraction=%d\n",sub);
printf("Multiplication=%d\n",mul);
printf("Division=%d\n",div);
printf("Remainder=%d\n",rem);                                                                                              
}

OutPut:

Enter a,b Values
20
15
Addition=35
Subtraction=5
Multiplication=300
Division=1
Remainder=5

1b) Relational Operators

Source Code:

#include<stdio.h>
void main()
{
int a,b;
printf("enter the values of  a & b\n");
scanf("%d %d",&a,&b);
printf("a<b=%d\n",a<b);
printf("a>b=%d\n",a>b);
printf("(a<=b)=%d\n",a<=b);
printf("(a>=b)=%d\n",a>=b);
printf("(a==b)=%d\n",a==b);
printf("(a!=b)=%d\n",a!=b);
}

OutPut:

enter the values of  a & b
20
10
a<b=0
a>b=1
(a<=b)=0
(a>=b)=1
(a==b)=0
(a!=b)=1

1c) Increment and Decrement Operators

Source Code:

#include<stdio.h>
void main()
{
int a,b;
a=5;
b=6;
printf("++a=%d ++b=%d \n",++a,++b);
printf("a++=%d b++=%d \n",a++,b++);
printf("(a++ + ++b)=%d \n",a++ + ++b);
printf("--a=%d --b=%d \n",--a,--b);
printf("a--=%d b--=%d \n",a--,b--);
printf("a--+--b=%d \n",a--+--b);
printf("++b-++a=%d \n",++b-++a);
printf("a++ + b++ - ++a=%d \n",a++ + b++ - ++a);
}

OutPut:

++a=6 ++b=7
a++=6 b++=7
(a++ + ++b)=16
--a=7 --b=8
a--=7 b--=8
a--+--b=12
++b-++a=1
a++ + b++ - ++a=5

1d) Increment and Decrement Operators

Source Code:

#include<stdio.h>
void main()
{
int a,b;
printf("Enter values of a,b:\n");
scanf("%d %d",&a,&b);
(a==b)?printf("Both are Equal"):printf("Both are not Equal");
}

Output:

Enter values of a,b:
20
40
Both are not Equal


1e) Assignment Operators

Source Code:

#include <stdio.h>
int main()
{
    int a = 5, c;
    c = a;      // c is 5
    printf("c = %d\n", c);
    c += a;     // c is 10
    printf("c = %d\n", c);
    c -= a;     // c is 5
    printf("c = %d\n", c);
    c *= a;     // c is 25
    printf("c = %d\n", c);
    c /= a;     // c is 5
    printf("c = %d\n", c);
    c %= a;     // c = 0
    printf("c = %d\n", c);
    return 0;
}

OutPut:

c = 5
c = 10
c = 5
c = 25
c = 5
c = 0


1e) Logical Operators

Source Code:

#include <stdio.h>
int main()
{
    int a = 5, b = 5, c = 10, result;
    result = (a == b) && (c > b);
    printf("(a == b) && (c > b) is %d \n", result);
    result = (a == b) && (c < b);
    printf("(a == b) && (c < b) is %d \n", result);
    result = (a == b) || (c < b);
    printf("(a == b) || (c < b) is %d \n", result);
    result = (a != b) || (c < b);
    printf("(a != b) || (c < b) is %d \n", result);
    result = !(a != b);
    printf("!(a != b) is %d \n", result);
    result = !(a == b);
    printf("!(a == b) is %d \n", result);
    return 0;
}

Output:

(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0

1f) Sizeof Operators

Source Code:

#include <stdio.h>
int main()
{
    int a;
    float b;
    double c;
    char d;
    printf("Size of int=%lu bytes\n",sizeof(a));
    printf("Size of float=%lu bytes\n",sizeof(b));
    printf("Size of double=%lu bytes\n",sizeof(c));
    printf("Size of char=%lu byte\n",sizeof(d));
    return 0;
}

OutPut:

Size of int=4 bytes
Size of float=4 bytes
Size of double=8 bytes
Size of char=1 byte

No comments:

Post a Comment