Programming in C Language Assignment1
Programming in C Language Assignment1
Project File
Code:
#include <stdio.h>
int main() {
char text[] = "Hello how are you doing ?";
int length;
length = strlen(text);
printf("The number of characters in the message is: %d\n", length);
return 0;
}
Output:
The number of characters in the message is: 25
2.Question:
WAP to count the number of inputs entered by the user.
Code:
#include <stdio.h>
int main() {
int input, count = 0;
while(1) {
scanf("%d", &input);
if(input == -1) {
break;
}
count++;
}
return 0;
}
Output:
Enter numbers (Enter -1 to stop):
2
3
4
5
9
75
3
3
-1
You entered 8 inputs.
Ans:
<stdio.h> searches in standard C library locations, whereas "stdio.h" searches in
the current directory as well.
Code:
#include <stdio.h>
int main() {
char num1, num2, sum;
printf("Enter first number (under 50): ");
scanf("%hhd", &num1);
printf("Enter second number (under 50): ");
scanf("%hhd", &num2);
Output:
Enter first number (under 50): 38
Enter second number (under 50): 18
The sum of 38 and 18 is: 56
int main() {
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
6.Question:
WAP to check whether the number entered by user is even or
odd (using bitwise AND).
Code:
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
if (num & 1) {
} else {
printf("%d is an even number.\n", num);
return 0;
Output:
Enter a number: 33
33 is an odd number.
Enter a number: 22
22 is an even number.
Code:
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
(num & 1) ? printf("%d is an odd number.\n", num) : printf("%d is an even
number.\n", num);
return 0;
Output:
Enter a number: 43
43 is an odd number.
9Q:
9a. 12, 9b. 012, 9f. 12, 9g. 123.46, 9h. 123.256700
10Q:
a: statement inside if executes : because 100 is +ve.
11Q.
a. The logical AND operator returns true (1) only if both operands are non-zero
(true). Since one of the operands (0) is false, the entire expression 5 && 0
evaluates to false, which is represented as 0.
b. Printf(“%d”, 5 || 0);
The logical OR operator returns true (1) if at least one of the operands is non-
zero (true).
c. Printf(“%d”, !0);
Since !0 evaluates to 1 (true), the expression evaluates to 1.
d.0 && printf("Hello");
Nothing is printed.
e. Printf(“%d”, 5|6);
Nothing is printed
f. printf("%d", 5 & 6);
output:4
5 in binary is 0101
6 in binary is 0110
Performing 5 & 6 gives 0100 (which is 4 in decimal).
g. printf("%d", 5 ^ 6);
output:3
5 in binary is 0101
6 in binary is 0110
Performing 5 ^ 6 gives 0011 (which is 3 in decimal).
h. printf("%d", 5 | 6);
output:7
5 in binary is 0101
6 in binary is 0110
Performing 5 | 6 gives 0111 (which is 7 in decimal).
i. printf("%d", 5 << 1);
output:10
5 in binary is 0101.
Shifting left by 1 bit gives 1010 (which is 10 in decimal)
j. printf("%d", 5 >> 1);
output:2
5 in binary is 0101.
Shifting right by 1 bit gives 0010 (which is 2 in decimal).
CODE:
#include <stdio.h>
int main() {
int number;
scanf("%d", &number);
if (number < 1) {
return 0;
if (number % 2 != 0) {
return 0;
number /= 2;
return 0;
Output:
Enter a number: 32
Enter a number: 31
18Q. WAP to check the kth bit in the number entered by the
user is either 0 or 1.
CODE:
#include <stdio.h>
int main() {
int number, k;
scanf("%d", &number);
scanf("%d", &k);
if (bit) {
} else {
return 0;
OUTPUT:
Enter a number: 33
Conclusion
These data types provide the foundation for data manipulation in C. Basic types
represent simple data, while derived types allow for more complex data
structures, enhancing code organization and functionality. Understanding these
types is essential for effective programming in C.