0% found this document useful (0 votes)
29 views8 pages

Digital Aptitude Lab

Lab

Uploaded by

satyamprasad2997
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)
29 views8 pages

Digital Aptitude Lab

Lab

Uploaded by

satyamprasad2997
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
You are on page 1/ 8

Digital Aptitude Lab

1- Find the output of C programming print based Aptitude

a. Print the Sum of Two Numbers:

#include <stdio.h>

int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("Sum: %d\n", sum);
return 0;
}

b. Calculate the Area of a Rectangle:

#include <stdio.h>

int main() {
double length, width, area;
printf("Enter the length and width of the rectangle: ");
scanf("%lf %lf", &length, &width);
area = length * width;
printf("Area: %lf\n", area);
return 0;
}

c. Check if a Number is Even or Odd:

#include <stdio.h>

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num % 2 == 0)
printf("Even\n");
else
printf("Odd\n");

return 0;
}
2- Find the output of C programming using with operators.
a. Arithmetic Operations:

#include <stdio.h>

int main() {
int num1 = 10, num2 = 5;

printf("Addition: %d\n", num1 + num2);


printf("Subtraction: %d\n", num1 - num2);
printf("Multiplication: %d\n", num1 * num2);
printf("Division: %d\n", num1 / num2);
printf("Modulus: %d\n", num1 % num2);

return 0;
}

b. Relational Operators:

#include <stdio.h>

int main() {
int num1 = 10, num2 = 5;

printf("Is num1 equal to num2? %d\n", num1 == num2);


printf("Is num1 not equal to num2? %d\n", num1 != num2);
printf("Is num1 greater than num2? %d\n", num1 > num2);
printf("Is num1 less than num2? %d\n", num1 < num2);
printf("Is num1 greater than or equal to num2? %d\n", num1 >= num2);
printf("Is num1 less than or equal to num2? %d\n", num1 <= num2);

return 0;
}

c. Logical Operators:

#include <stdio.h>

int main() {
int x = 1, y = 0;

printf("Logical AND: %d\n", x && y);


printf("Logical OR: %d\n", x || y);
printf("Logical NOT for x: %d\n", !x);
printf("Logical NOT for y: %d\n", !y);

return 0;
}
3- Arrange the operators according to their precedence:+, %,->,=.

Operator precedence in C dictates the order in which operators are evaluated when they appear in
an expression. In the list you provided, the operators are arranged in the following order from
highest to lowest precedence:

% (Modulus Operator): It has the highest precedence among the operators you listed.
-> (Member Access Operator): This is used to access members of a structure or a union through a
pointer. It has lower precedence than %.
= (Assignment Operator): The assignment operator has the lowest precedence among the
operators you mentioned.
+ (Addition Operator): Addition has the lowest precedence in this list, but it is higher than =.

4- Predict the output of following program using with Bitwise Operators.

#include <stdio.h>

int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011

int bitwise_and = a & b;


int bitwise_or = a | b;
int bitwise_xor = a ^ b;
int left_shift = a << 2;
int right_shift = a >> 1;

printf("Bitwise AND (a & b): %d\n", bitwise_and);


printf("Bitwise OR (a | b): %d\n", bitwise_or);
printf("Bitwise XOR (a ^ b): %d\n", bitwise_xor);
printf("Left Shift (a << 2): %d\n", left_shift);
printf("Right Shift (a >> 1): %d\n", right_shift);
return 0;
}

5- Find the output of C programming using with basic input, Output

#include <stdio.h>

int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011

int bitwise_and = a & b;


int bitwise_or = a | b;
int bitwise_xor = a ^ b;
int left_shift = a << 2;
int right_shift = a >> 1;

printf("Bitwise AND (a & b): %d\n", bitwise_and);


printf("Bitwise OR (a | b): %d\n", bitwise_or);
printf("Bitwise XOR (a ^ b): %d\n", bitwise_xor);
printf("Left Shift (a << 2): %d\n", left_shift);
printf("Right Shift (a >> 1): %d\n", right_shift);

return 0;
}

1. bitwise_and = a & b performs a bitwise AND operation:


• Binary 0101 (a)
• Binary 0011 (b)
• Result: 0001 (bitwise_and)
2. bitwise_or = a | b performs a bitwise OR operation:
• Binary 0101 (a)
• Binary 0011 (b)
• Result: 0111 (bitwise_or)
3. bitwise_xor = a ^ b performs a bitwise XOR operation:
• Binary 0101 (a)
• Binary 0011 (b)
• Result: 0110 (bitwise_xor)
4. left_shift = a << 2 performs a left shift operation:
• Binary 0101 (a)
• Result: 10100 (left_shift)
5. right_shift = a >> 1 performs a right shift operation:
• Binary 0101 (a)
• Result: 0010 (right_shift)

6- Find the output of C programming using with Declaration, data types.

Integer Declaration
#include <stdio.h>

int main() {
int num = 42;
printf("The value of num is: %d\n", num);
return 0;
}

Floating-Point Declaration
#include <stdio.h>

int main() {
float pi = 3.14159;
printf("The value of pi is: %f\n", pi);
return 0;
}

Character Declaration
#include <stdio.h>

int main() {
char grade = 'A';
printf("The grade is: %c\n", grade);
return 0;
}

Using Different Data Types


#include <stdio.h>
int main() {
int age = 25;
char gender = 'M';
float height = 175.5;

printf("Age: %d\n", age);


printf("Gender: %c\n", gender);
printf("Height: %.1f\n", height);

return 0;
}
7- Which statement does not require semicolon?

In C and many other programming languages, most statements are terminated with a semicolon
(;) to indicate the end of the statement. However, there is one particular statement that does not
require a semicolon: the "preprocessor directive."
Preprocessor directives are instructions to the C preprocessor, and they typically start with a #
symbol. These directives are not considered regular C statements and do not end with a
semicolon. Common preprocessor directives include:
#include for including header files.

#include <stdio.h> // No semicolon is needed at the end of this #include directive.


8- Dry run program of Declaration and Initialization Aptitude

#include <stdio.h>

int main() {
int a = 5;
float b = 3.14;
char c = 'A';

printf("Value of 'a': %d\n", a);


printf("Value of 'b': %f\n", b);
printf("Value of 'c': %c\n", c);

return 0;
}

Here's a step-by-step dry run of this program:

1. The program starts by including the standard I/O library (<stdio.h>) to use functions like
printf.
2. We declare and initialize three variables:
• a as an integer with the value 5.
• b as a floating-point number with the value 3.14.
• c as a character with the value 'A'.
3. The program then enters the main function.
4. It uses printf to print the values of these variables:
• a is an integer, so it uses the %d format specifier.
• b is a float, so it uses the %f format specifier.
• c is a character, so it uses the %c format specifier.
5. The program prints the values of 'a', 'b', and 'c' using printf.
6. Finally, the return 0; statement ends the program.

9- Find the output of Pseudo code through dry run

#include <stdio.h>

int main() {
int num1, num2, sum;

// Input
printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

// Calculation
sum = num1 + num2;

// Output
printf("The sum of %d and %d is %d\n", num1, num2, sum);

return 0;
}

Dry Run:
1. The program starts, and the standard I/O library is included.
2. Three integer variables are declared: num1, num2, and sum.
3. The program prints "Enter the first number: " using printf.
4. The program waits for user input, expecting an integer, which is stored in the num1 variable.
Let's say the user enters 7.
5. The program then prints "Enter the second number: " using printf.
6. The program waits for the user to enter another integer, which is stored in the num2 variable.
Let's say the user enters 5.
7. Now, the program proceeds to calculate the sum of num1 and num2, which is 7 + 5, resulting
in sum being set to 12.
8. It uses printf to display the result, "The sum of 7 and 5 is 12." It substitutes %d with the
values of num1, num2, and sum in the output.
9. Finally, the program reaches the return 0; statement, indicating successful execution, and it
exits.

10- Find the error of Pseudo code through dry run


The C program we have write is generally correct and should work as intended. However,
there are a few potential issues or areas where you might want to handle errors or edge cases.
Here's an analysis:
1. Input Validation: The program expects the user to enter two integer values. While this
program doesn't explicitly handle non-integer inputs, it's a good practice to include input
validation to ensure that the user enters valid integers.
2. Variable Initialization: In this program, the variables num1 and num2 are used before being
initialized. While C allows reading uninitialized variables, it's better to initialize them to avoid
potential issues. You can set them to default values (e.g., 0) before reading user input.

You might also like