Digital Aptitude Lab
Digital Aptitude Lab
#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;
}
#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;
}
#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;
return 0;
}
b. Relational Operators:
#include <stdio.h>
int main() {
int num1 = 10, num2 = 5;
return 0;
}
c. Logical Operators:
#include <stdio.h>
int main() {
int x = 1, y = 0;
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 =.
#include <stdio.h>
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
#include <stdio.h>
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
return 0;
}
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;
}
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>
int main() {
int a = 5;
float b = 3.14;
char c = 'A';
return 0;
}
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.
#include <stdio.h>
int main() {
int num1, num2, sum;
// Input
printf("Enter the first number: ");
scanf("%d", &num1);
// 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.