0% found this document useful (0 votes)
6 views5 pages

Clabb

The document contains multiple C programming examples demonstrating various types of operators, including assignment, logical, relational, conditional, bitwise, and special operators. It also illustrates the use of global and local variables, formatted output functions, and mathematical functions like square root. Each example includes code snippets, expected inputs, and outputs to showcase the functionality.

Uploaded by

nikhilnidhi9021
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Clabb

The document contains multiple C programming examples demonstrating various types of operators, including assignment, logical, relational, conditional, bitwise, and special operators. It also illustrates the use of global and local variables, formatted output functions, and mathematical functions like square root. Each example includes code snippets, expected inputs, and outputs to showcase the functionality.

Uploaded by

nikhilnidhi9021
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Program to demonstrate Assignment Operators


#include <stdio.h>
int main() {
int a = 10, b = 5;

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

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

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

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

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

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

return 0;
}

Output:

Initial value of a = 10
After a += b, a = 15
After a -= b, a = 10
After a *= b, a = 50
After a /= b, a = 10
After a %= b, a = 0

⚙ 2. Program to demonstrate Logical Operators


#include <stdio.h>
int main() {
int a = 10, b = 20, c = 0;

printf("(a < b && b > 0): %d\n", (a < b && b > 0));
printf("(a > b || b > 0): %d\n", (a > b || b > 0));
printf("!(c): %d\n", !c);

return 0;
}

Output:

(a < b && b > 0): 1


(a > b || b > 0): 1
!(c): 1
⚖ 3. Program to demonstrate Relational Operators
#include <stdio.h>
int main() {
int a = 5, b = 10;

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);

return 0;
}

Output:

a == b: 0
a != b: 1
a > b : 0
a < b : 1
a >= b: 0
a <= b: 1

🔢 4. Find the Largest of Two Numbers using Conditional


Operator
#include <stdio.h>
int main() {
int a, b, largest;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

largest = (a > b) ? a : b;
printf("Largest number = %d\n", largest);

return 0;
}

Input:

Enter two numbers: 25 30

Output:

Largest number = 30

💡 5. Demonstrate Bitwise Operators


#include <stdio.h>
int main() {
int a = 5, b = 3;

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


printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("a << 1 = %d\n", a << 1);
printf("a >> 1 = %d\n", a >> 1);

return 0;
}

Output:

a & b = 1
a | b = 7
a ^ b = 6
~a = -6
a << 1 = 10
a >> 1 = 2

6. Demonstrate Special Operators (sizeof, comma)


#include <stdio.h>
int main() {
int a, b, c;
a = (b = 5, c = b + 10);

printf("Using comma operator: a = %d\n", a);


printf("Size of int = %lu bytes\n", sizeof(int));
printf("Size of float = %lu bytes\n", sizeof(float));
printf("Size of char = %lu bytes\n", sizeof(char));

return 0;
}

Output:

Using comma operator: a = 15


Size of int = 4 bytes
Size of float = 4 bytes
Size of char = 1 bytes

🌍 7. Usage of Global and Local Variables


#include <stdio.h>
int global = 10; // Global variable

void display() {
int local = 20; // Local variable
printf("Inside display() -> Global = %d, Local = %d\n", global, local);
}
int main() {
printf("Inside main() -> Global = %d\n", global);
display();
return 0;
}

Output:

Inside main() -> Global = 10


Inside display() -> Global = 10, Local = 20

8. Find the Square Root using sqrt()


#include <stdio.h>
#include <math.h>
int main() {
double num, result;
printf("Enter a number: ");
scanf("%lf", &num);

result = sqrt(num);
printf("Square root of %.2lf = %.2lf\n", num, result);

return 0;
}

Input:

Enter a number: 16

Output:

Square root of 16.00 = 4.00

🖨 9. Show usage of Formatted Output Functions


#include <stdio.h>
int main() {
int age = 20;
float marks = 89.567;
char name[] = "Nikhil";

printf("Name: %s\n", name);


printf("Age: %d years\n", age);
printf("Marks: %.2f\n", marks);

return 0;
}

Output:

Name: Nikhil
Age: 20 years
Marks: 89.57

10. Solve Expression P = (W + X) / (Y + Z)


#include <stdio.h>
int main() {
float W, X, Y, Z, P;

printf("Enter W, X, Y, Z: ");
scanf("%f %f %f %f", &W, &X, &Y, &Z);

P = (W + X) / (Y + Z);
printf("Result: P = %.2f\n", P);

return 0;
}

Input:

Enter W, X, Y, Z: 4 6 2 2

Output:

Result: P = 2.50

You might also like