1. Valid or invalid identifiers and mention the reason for the invalid ones.
void 123_azr hello world return _1101cse vu$123
1. void – Invalid
o Reason: void is a reserved keyword in most programming languages like C,
C++, and Java. Keywords cannot be used as identifiers.
2. 123_azr – Invalid
o Reason: Identifiers cannot start with a digit. It starts with 123, which is not
allowed.
3. hello world – Invalid
o Reason: Identifiers cannot contain spaces. hello world has a space between
hello and world, making it invalid.
4. return – Invalid
o Reason: return is a reserved keyword in most programming languages.
Keywords cannot be used as identifiers.
5. _1101cse – Valid
o Reason: Identifiers can start with an underscore (_) and contain digits and
letters.
6. vu$123 – Invalid
o Reason: The dollar sign ($) is not allowed in most programming languages as a
part of an identifier. Some languages like Java allow it, but in general, it is not
recommended.
Summary:
Identifier Valid/Invalid Reason
void ❌ Invalid Reserved keyword
123_azr ❌ Invalid Cannot start with a digit
hello world ❌ Invalid Contains a space
return ❌ Invalid Reserved keyword
_1101cse ✅ Valid Starts with _, contains letters and digits
vu$123 ❌ Invalid Contains $ (not allowed in most languages)
2. Find the value of the following C expression
Operator Precedence and Associativity in C
1. Multiplication (*), Division (/), and Modulus (%) → Same precedence, evaluated left to right.
2. Addition (+) and Subtraction (-) → Lower precedence, evaluated left to right.
Expression (i):
2%3∗9−57/7+672 \% 3 * 9 - 57 / 7 + 67
Step-by-step evaluation:
1. 2 % 3 → 2 (since 2 divided by 3 gives remainder 2)
2. 2 * 9 → 18
3. 57 / 7 → 8 (integer division; fractional part is discarded)
4. 18 - 8 → 10
5. 10 + 67 → 77
✅ Final result: 77
Expression (ii):
2∗3%9−57/7−672 * 3 \% 9 - 57 / 7 - 67
Step-by-step evaluation:
1. 2 * 3→6
2. 6 % 9 → 6 (since 6 divided by 9 gives remainder 6)
3. 57 / 7 → 8 (integer division)
4. 6 - 8 → -2
5. -2 - 67 → -69
✅ Final result: -69
Final Answers
(i) 2 % 3 * 9 - 57 / 7 + 67 → 77
(ii) 2 * 3 % 9 - 57 / 7 - 67 → -69
3. output of the following code
Code:
#include <stdio.h>
int main() {
int a = 5, b = 7;
printf("%d %d", a++, ++b);
printf("%d %d", ++a, b++);
return 0;
}
Understanding a++ and ++b
• a++ (Post-increment): Returns the current value of a first, then increments a after the
statement execution.
• ++b (Pre-increment): Increments b first, then returns the updated value.
Step-by-step Execution
Step 1: Variable Initialization
• a = 5, b = 7
Step 2: First printf Statement
printf("%d %d", a++, ++b);
• a++ (Post-increment):
o Returns 5, but a becomes 6 after the statement.
• ++b (Pre-increment):
o b increments first, so b = 8, and returns 8.
First Output: 5 8
(Current values: a = 6, b = 8)
Step 3: Second printf Statement
printf("%d %d", ++a, b++);
• ++a (Pre-increment):
o a increments first, so a = 7, and returns 7.
• b++ (Post-increment):
o Returns 8, but b becomes 9 after the statement.
Second Output: 7 8
(Current values: a = 7, b = 9)
Final Output
5 8
7 8
Summary of Execution
Statement Action Taken Value Returned Updated a Updated b
a++ Returns 5, then a = a + 1 5 6 7
++b b = b + 1, then returns 8 8 6 8
First Output: 5 8
++a a = a + 1, then returns 7 7 7 8
b++ Returns 8, then b = b + 1 8 7 9
Second Output: 7 8
4.C program that takes an integer as input and determines whether it is
even or odd.
C Program:
#include <stdio.h>
int main() {
int num;
// Taking user input
printf("Enter an integer: ");
scanf("%d", &num);
// Checking even or odd
if (num % 2 == 0) {
printf("%d is Even.\n", num);
} else {
printf("%d is Odd.\n", num);
}
return 0;
}
Explanation:
1. Takes an integer input from the user using scanf().
2. Checks divisibility by 2 using num % 2 == 0:
o If true, the number is even.
o Else, the number is odd.
3. Prints the result accordingly.
Sample Output:
Example 1 (Even Number)
Enter an integer: 8
8 is Even.
Example 2 (Odd Number)
Enter an integer: 15
15 s Odd.
5.Convert the following flowchart into a C program.
#include <stdio.h>
int main() {
int a;
// Taking user input
printf("Enter a number: ");
scanf("%d", &a);
// Decision-making as per flowchart
if (a > 0) {
printf("Output: Non-Negative\n");
} else {
printf("Output: Negative\n");
return 0;
6.List the parts of a for loop. Write a C program using for loop that prints the
values from 1 to N.
Parts of a for Loop in C
A for loop in C consists of three main parts:
1. Initialization: A variable is initialized before the loop starts (e.g., int i = 1).
2. Condition: The loop runs while the condition is true (e.g., i <= N).
3. Update (Increment/Decrement): The variable is updated after each iteration (e.g., i++).
C Program to Print Values from 1 to N
#include <stdio.h>
int main() {
int N, i;
// Taking user input
printf("Enter the value of N: ");
scanf("%d", &N);
// For loop to print numbers from 1 to N
for (i = 1; i <= N; i++) {
printf("%d ", i);
}
printf("\n"); // New line after printing numbers
return 0;
}
Explanation of the for Loop
for (i = 1; i <= N; i++)
• Initialization (i = 1) → i starts from 1.
• Condition (i <= N) → The loop runs as long as i is less than or equal to N.
• Update (i++) → i is incremented by 1 in each iteration.
Sample Outputs
Example 1 (N = 5)
Enter the value of N: 5
1 2 3 4 5
Example 2 (N = 10)
Enter the value of N: 10
1 2 3 4 5 6 7 8 9 10
Let's analyze the given C code step by step and determine the output.
6. Compute the output of the following code segment:
int x=25;
int y=x%30;
printf(“%d %d”,x++,y);
Given Code:
#include <stdio.h>
int main() {
int x = 25;
int y = x % 30;
printf("%d %d", x++, y);
return 0;
}
Step-by-Step Execution
Step 1: Variable Initialization
• x = 25
• y = x % 30
o Since 25 % 30 = 25 (because 25 divided by 30 gives remainder 25),
o So, y = 25.
Step 2: printf Execution
printf("%d %d", x++, y);
• x++ is post-increment, meaning:
o printf prints the current value of x (25) first.
o Then x is incremented to 26 after printing.
• y remains 25.
Final Output
25 25
Key Takeaways
• Post-increment (x++) prints the original value first before updating.
• Modulus (%) gives the remainder when dividing x by 30.
• y is not affected by the post-increment on x.
7. Differentiate the followings:
i) ‘a’ vs “a”
ii) “=” vs “==”
iii) “m++” vs “++m”
(i) 'a' vs "a"
Feature 'a' (Single Quotes) "a" (Double Quotes)
Type Character constant String constant
Data Type char char[] (character array)
Memory Size 1 byte (stores a single character) 2 bytes ('a' + '\0' for string termination)
Example Usage char ch = 'a'; char str[] = "a";
Explanation Represents a single character Represents a string with a null-terminator (\0)
✅ Example:
char ch = 'a'; // Valid, stores a single character
char str[] = "a"; // Valid, stores a string with 'a' and '\0'
(ii) = vs ==
Operator Meaning Usage
= Assignment operator Assigns a value to a variable (x = 5;)
== Comparison operator Checks if two values are equal (if (x == 5))
✅ Example:
int x = 10; // Assigns 10 to x
if (x == 10) { // Compares x with 10
printf("x is 10");
}
Common Mistake: Using = inside an if condition instead of ==:
if (x = 10) { // WRONG! Assigns 10 to x, which is always TRUE
(iii) m++ vs ++m
Operator Meaning Effect
m++ Post-increment Returns current value of m, then increments m
++m Pre-increment Increments m first, then returns the updated value
✅ Example:
int m = 5;
printf("%d\n", m++); // Prints 5, then m becomes 6
printf("%d\n", ++m); // Increments to 7 first, then prints 7
Key Difference:
• m++ uses the old value first, then increments.
• ++m increments first, then uses the new value.
Summary
Expression Description
'a' vs "a" 'a' is a character, "a" is a string.
= vs == = assigns a value, == checks equality.
m++ vs ++m m++ (post-increment) uses the value first, ++m (pre-increment) increases first.
8. Analysis of Valid and Invalid Variable Declarations
Declaration Valid/Invalid Reason
int x; ✅ Valid Proper variable declaration with a valid identifier.
double 5d; ❌ Invalid Variable names cannot start with a digit.
float Varendra
university; ❌ Invalid Variable names cannot have spaces.
Missing data type before variables, and a comma cannot be used
Varendra, university; ❌ Invalid
without proper declaration.
char is a reserved keyword in C, so it cannot be used as a variable
char char; ❌ Invalid
name.
Variable names are case-sensitive, so CHAR is different from char (a
char CHAR; ✅ Valid
reserved keyword).
Corrected Versions of Invalid Declarations
Here are the corrected versions to make them valid:
int x; // Valid as it is
double d5; // Changed '5d' to 'd5'
float Varendra_university; // Removed space and replaced with '_'
int Varendra, university; // Added data type 'int' before variables
char ch; // Renamed 'char' to 'ch'
char CHAR; // Already valid
Final Verdict
✅ Valid Declarations:
• int x;
• char CHAR;
❌ Invalid Declarations (with reasons):
• double 5d; → Cannot start with a number
• float Varendra university; → Cannot have spaces
• Varendra, university; → Missing data type
• char char; → Uses a reserved keyword
9. Write down the output of the following code snippets:
i. int x, y, a,b,c;
x=5,y=10;
a=x/y;
b=x<y;
printf("%d %d %d",a, b, c);
ii. int x, y;
x=12,y=21;
y=y*(-1);
if(x%3==0&&y)
printf("A");
else
printf("B")
Code Snippet i:
int x, y, a, b, c;
x = 5, y = 10;
a = x / y;
b = x < y;
printf("%d %d %d", a, b, c);
Line-by-line Explanation:
1. int x, y, a, b, c;
o This declares five integer variables: x, y, a, b, and c.
2. x = 5, y = 10;
o This initializes x to 5 and y to 10.
3. a = x / y;
o This calculates the division of x (5) by y (10), which is 5 / 10 = 0 in integer division.
The result (0) is stored in a.
4. b = x < y;
o This checks if x (5) is less than y (10). Since 5 is indeed less than 10, the result is true,
which is represented as 1 in C. The value 1 is stored in b.
5. printf("%d %d %d", a, b, c);
o This prints the values of a, b, and c. The values of a and b are already calculated, but c
has not been initialized, so its value is indeterminate. Printing an uninitialized variable
(c) results in an unpredictable value (garbage value).
o Output: 0 1 <garbage value>
Code Snippet ii:
int x, y;
x = 12, y = 21;
y = y * (-1);
if (x % 3 == 0 && y)
printf("A");
else
printf("B");
Line-by-line Explanation:
1. int x, y;
o This declares two integer variables: x and y.
2. x = 12, y = 21;
o This initializes x to 12 and y to 21.
3. y = y * (-1);
o This multiplies y (21) by -1, changing its value from 21 to -21.
4. if (x % 3 == 0 && y)
o This condition checks two things:
▪ x % 3 == 0: This checks if x is divisible by 3. Since x = 12, 12 % 3 == 0, so
this part of the condition is true.
▪ y: This checks if y is non-zero. Since y = -21, which is non-zero, this part is also
true.
o Since both conditions are true, the overall condition is true.
5. printf("A");
o Since the condition is true, the program prints "A".
o Output: A
Final Outputs:
• Snippet i: 0 1 <garbage value>
• Snippet ii: A
10. Write a C program that takes three integers as input and print
the maximum value.
Here's a simple C program that takes three integers as input and prints the maximum value:
#include <stdio.h>
int main() {
int a, b, c, max;
// Taking input from the user
printf("Enter three integers: ");
scanf("%d %d %d", &a, &b, &c);
// Finding the maximum value
max = a; // Assume 'a' is the maximum initially
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
// Printing the maximum value
printf("The maximum value is: %d\n", max);
return 0;
}
Explanation:
1. Declare three integer variables (a, b, c) to store user input and an integer max to store the
maximum value.
2. Prompt the user to enter three integers and read them using scanf().
3. Assume a is the maximum and compare it with b and c to update max accordingly.
4. Print the maximum value.
Example Output:
Enter three integers: 12 45 32
The maximum value is: 45
This program ensures that the largest of the three numbers is correctly identified and displayed.
11.
Write down the output of the following code snippets:
i. int x=56,y=89; ii. int x=5,i;
int temp=x; for(i=1;i<=5;i++){
x=y; printf("%d X %d = %d\n",x, i, x*i);
y=temp; }
y+=2;
printf("%d %d",x,y);
Let's analyze the output of both code snippets.
Snippet (i)
int x=56, y=89;
int temp = x;
x = y;
y = temp;
y += 2;
printf("%d %d", x, y);
Step-by-step Execution:
1. x = 56, y = 89
2. temp = x → temp = 56
3. x = y → x = 89
4. y = temp → y = 56
5. y += 2 → y = 56 + 2 = 58
6. printf("%d %d", x, y); → prints 89 58
Output:
89 58
Snippet (ii)
int x = 5, i;
for(i = 1; i <= 5; i++) {
printf("%d X %d = %d\n", x, i, x * i);
}
Step-by-step Execution:
The for loop runs from i = 1 to i = 5, printing the multiplication table of 5.
Generated Output:
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
Final Outputs
Snippet (i) Output:
89 58
Snippet (ii) Output:
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
12. Write down a c program that takes year as an input and check weather it is leapyear or
not.
Leap Year Rules:
• A year is a leap year if it is divisible by 4.
• However, if the year is also divisible by 100, it must also be divisible by 400 to be a leap year.
C Program:
#include <stdio.h>
int main() {
int year;
// Taking input from the user
printf("Enter a year: ");
scanf("%d", &year);
// Checking leap year condition
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
Example Runs:
Input:
Enter a year: 2024
Output:
2024 is a leap year.
Input:
Enter a year: 1900
Output:
1900 is not a leap year.