Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

code

The document provides a series of C programming code examples demonstrating various operators, including logical, ternary, increment/decrement, bitwise, and sizeof operators. Each example includes an aim, a concise algorithm, the code itself, and the expected output. The document serves as a tutorial for understanding these operators in C programming.

Uploaded by

lekome5941
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

code

The document provides a series of C programming code examples demonstrating various operators, including logical, ternary, increment/decrement, bitwise, and sizeof operators. Each example includes an aim, a concise algorithm, the code itself, and the expected output. The document serves as a tutorial for understanding these operators in C programming.

Uploaded by

lekome5941
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Skip to content

Chat history

ChatGPT
ChatGPT
GPT Icon
Presentation and Slides GPT: PPTs & PDFs
GPT Icon
AI Humanizer
GPT Icon
Humanize AI
Explore GPTs
Yesterday
Previous 7 Days
Previous 30 Days
2024
Humanizer #1
Humanizer #2
Upgrade plan
More access to the best models
You said:
i will give u some codes and i want u to give me the aim, algorithm, the code(which
i will provide so keep it as it is), the output in this exact order. the algorithm
is like the basic steps like start, initate so and so variables do some operations
on it, etc so the algorithm should be should like just a few words and not more
than 5 steps
ChatGPT said:
Sure! Send me the code, and I'll structure the response exactly as you specified:
Aim, Algorithm, Code (as is), and Output.

You said:
i will first give u all the codes so dont type anything in between
ChatGPT said:
Got it! Send all the codes, and I'll wait until you're done before responding.

You said:
3. Logical Operator
#include < stdio.h >
int main()
{
int a= 10, b = 20, c= 30;
printf ("(a>b) && (a<c) is %d", (a>b) && (a<c));
printf ("(a>b) || (a<c) is %d", (a>b) || (a<c));
printf ("!(a>b) is %d", !(a>b));
return 0;
}
Output: (a>b) && (a<c) is 0
(a>b) || (a<c) is 1
!(a>b) is 1
ChatGPT said:
Aim: To demonstrate the use of logical operators (&&, ||, !) in C.

Algorithm:

Start

Initialize a = 10, b = 20, c = 30


Perform logical operations (&&, ||, !) and print results

Stop

Code:

#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 30;
printf("(a>b) && (a<c) is %d", (a>b) && (a<c));
printf("(a>b) || (a<c) is %d", (a>b) || (a<c));
printf("!(a>b) is %d", !(a>b));
return 0;
}
Output:

(a>b) && (a<c) is 0


(a>b) || (a<c) is 1
!(a>b) is 1
You said:
4. Ternary or Conditional operator
#include
int main()
{
int a=5,b=10;
a>b? printf("a is Greater") : printf("b is Greater");
return 0;
}
ChatGPT said:
Aim: To demonstrate the use of the ternary (conditional) operator in C.

Algorithm:

Start

Initialize a = 5, b = 10

Use the ternary operator to check which number is greater and print the result

Stop

Code:

#include <stdio.h>
int main()
{
int a = 5, b = 10;
a > b ? printf("a is Greater") : printf("b is Greater");
return 0;
}
Output:

b is Greater
You said:
Uploaded image
ChatGPT said:
Aim: To demonstrate the working of increment (++) and decrement (--) operators in
C.

Algorithm:

Start

Initialize a = 5, b = 8, c = 4, d = 2

Apply post-increment, pre-increment, post-decrement, and pre-decrement operations

Print the values of variables after operations

Stop

Code:

#include <stdio.h>
int main()
{
int a = 5, b = 8, c = 4, d = 2;
int a1, b1, c1, d1;

// post increment, first value in a assigns to a1 then a increments


a1 = a++;

// pre increment, first b value increments then assigns to b1


b1 = ++b;

// post decrement, first value in c assigns to c1 then c decrements


c1 = c--;

// pre decrement, first d value decrements then assigns to d1


d1 = --d;

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


printf("\n b1 = %d, b = %d", b1, b);
printf("\n c1 = %d, c = %d", c1, c);
printf("\n d1 = %d, d = %d", d1, d);

return 0;
}
Output:

a1 = 5, a = 6
b1 = 9, b = 9
c1 = 4, c = 3
d1 = 1, d = 1
You said:
6. Bitwise Operator
#include < stdio.h >
int main()
{
a = 9 //equal to 1001
b = 12 //equal to 1100
print ("a & b = ",a & b)
print ("a | b = ",a | b)
print("a ^ b = ",a ^ b)
print("~a = ",~a)
print("a << 2 = ",a << 2)
print("a >> 2 = ",a >> 2)
return(0)
}
ChatGPT said:
There are errors in your code, such as missing int declarations for a and b,
incorrect print statements (C uses printf), and missing semicolons. Below is the
corrected version along with the required details.

Aim: To demonstrate the use of bitwise operators (&, |, ^, ~, <<, >>) in C.

Algorithm:

Start

Initialize a = 9 (1001), b = 12 (1100)

Perform bitwise operations: AND (&), OR (|), XOR (^), NOT (~), Left Shift (<<),
Right Shift (>>)

Print results

Stop

Code:

#include <stdio.h>
int main()
{
int a = 9; // Binary: 1001
int b = 12; // Binary: 1100

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


printf("a | b = %d\n", a | b); // Bitwise OR
printf("a ^ b = %d\n", a ^ b); // Bitwise XOR
printf("~a = %d\n", ~a); // Bitwise NOT
printf("a << 2 = %d\n", a << 2); // Left shift by 2
printf("a >> 2 = %d\n", a >> 2); // Right shift by 2

return 0;
}
Output:

a & b = 8
a | b = 13
a ^ b = 5
~a = -10
a << 2 = 36
a >> 2 = 2
You said:
7. Sizeof Operator
#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)
}
ChatGPT said:
Aim: To demonstrate the use of the sizeof operator in C to determine the size of
different data types.

Algorithm:

Start

Declare variables of types int, float, double, and char

Use sizeof operator to determine the memory size of each data type

Print the results

Stop

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: (May vary depending on system architecture, common values shown below)

Size of int = 4 bytes


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

Search

Reason

You might also like