answers_c
answers_c
1. Identify the valid and invalid identifiers in C, given below. Justify your answer.
2. What will be the output of the following C program? Justify your answer.
void main() {
int i = 1234, j = 0;
while (i > 0) {
j = j * 10 + i % 10;
i = i / 10;
}
printf("%d %d", i, j);
}
Output: 0 4321
Justification:
3. What are the different ways to declare and initialize a single-dimensional array?
Answer:
#include <stdio.h>
int main() {
char str[100];
int i = 0;
printf("Enter a string: ");
scanf("%s", str);
while (str[i] != '\0') {
i++;
}
printf("Length of the string = %d\n", i);
return 0;
}
6. What will be the output of the following C function, if the function was called 5 times
in the main program? Justify your answer.
void fun() {
static int count = 0;
count++;
printf("Count: %d\n", count);
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Justification:
Answer:
Q8: What happens in the following scenarios when using the statement:
fp = fopen("data.txt", "w");
Explanation:
The "w" mode in fopen() is used for writing. If the file exists, it is cleared. If not, a new file
is created.
Concept:
Module 1
Q9a)What will be the output of the following program? Justify your answer.
#include<stdio.h>
void main() {
int a=5, b=8, c=9, d;
d = a++ + --b + c++;
printf("%d %d %d %d\n", a, b, c, d);
}
Answer:
• a = 6 (post-increment)
• b = 7 (pre-decrement)
• c = 10 (post-increment)
• d = 5 + 7 + 9 = 21
Output: 6 7 10 21
Explaination:
int a = 5, b = 8, c = 9, d;
Step-by-step:
So,d = 5 + 7 + 9 = 21
a = 6, b = 7, c = 10
Output: 6 7 10 21
Concepts:
Logic:
Code:
#include <stdio.h>
#include <math.h>
int main() {
int num, original, rem, sum = 0, n = 0;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
temp = num;
while (temp != 0) {
rem = temp % 10;
sum += pow(rem, n);
temp /= 10;
}
if (sum == original)
printf("Armstrong number\n");
else
printf("Not an Armstrong number\n");
return 0;
}
Q10a)
C program to count number of 1s in the binary representation using bitwise operators:
Logic:
Example:
5 in binary = 101 → 2 ones
Code:
#include <stdio.h>
int main() {
int num, count = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
if (num & 1)
count++;
num = num >> 1;
}
Q10b)
Check if a number is prime:
Logic:
Concept:
Efficient check up to sqrt(n) is enough for large numbers.
Code:
#include <stdio.h>
int main() {
int num, i, flag = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1)
flag = 0;
else {
for (i = 2; i <= num/2; i++) {
if (num % i == 0) {
flag = 0;
break;
}
}
}
if (flag)
printf("Prime\n");
else
printf("Not Prime\n");
return 0;
}
Module 2
Q11a)
Sum of odd numbers in an array:
Logic:
• Input array
• Traverse each element: if (element % 2 != 0)
• Add it to sum
Concept:
Odd numbers leave remainder 1 when divided by 2.
Code:
#include <stdio.h>
int main() {
int arr[100], n, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
Q11b)
Arrange array in descending order using bubble sort:
Logic:
Concept:
Bubble sort has n-1 passes. In each pass, largest/smallest bubbles up to correct position.
Example:
Array: [5, 2, 9]
Pass 1: [5, 9, 2] → [9, 5, 2]
Code:
#include <stdio.h>
int main() {
int arr[100], n, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
Q12a)
Count number of words and lines in a string:
Logic:
Concepts:
Note: Uses getchar() to read multi-line input until a stop character like #
Code:
#include <stdio.h>
int main() {
char str[1000];
int lines = 0, words = 0, i = 0;
Q12b)
Check if string is palindrome (without string functions):
Logic:
Concept:
A palindrome reads the same forward and backward.
Code:
#include <stdio.h>
int main() {
char str[100];
int i, j, flag = 1;
if (flag)
printf("Palindrome\n");
else
printf("Not a Palindrome\n");
return 0;
}
Module 3
Logic:
• Initialize sum = 1
• Loop from 1 to n
• Add x^i using pow(x, i)
Concept:
This is a geometric series.
Manual calculation also possible if needed.
Example:
x = 2, n = 3 → 1 + 2 + 4 + 8 = 15
Code:
#include <stdio.h>
#include <math.h>
int main() {
int x, n;
printf("Enter x and n: ");
scanf("%d%d", &x, &n);
printf("Sum = %d\n", series(x, n));
return 0;
}
Concept Explanation:
#include <stdio.h>
int main() {
int a = 36, b = 60;
printf("GCD of %d and %d is %d", a, b, gcd(a, b));
return 0;
}
Concept Explanation:
• struct allows grouping of related data types (roll number, name, marks, etc.).
• We use an array of structures to store 10 student records.
• We calculate percentage = (total_mark / 500.0) * 100
• Then we assign a grade using if-else conditions.
#include <stdio.h>
#include <string.h>
// Structure definition
struct Student {
int roll_no;
char name[50];
int total_mark;
float percentage;
char grade;
};
int main() {
struct Student s[10]; // Array to store 10 student records
for (int i = 0; i < 10; i++) {
printf("Enter Roll No, Name, and Total Marks (out of 500) for
Student %d: ", i + 1);
scanf("%d %s %d", &s[i].roll_no, s[i].name, &s[i].total_mark);
s[i].percentage = (s[i].total_mark / 500.0) * 100; // Convert to
percentage
s[i].grade = calculateGrade(s[i].percentage); // Assign grade
}
return 0;
}
Explanation:
Example:
int main() {
char str1[100] = "Hello ";
char str2[] = "World!";
concatenate(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
Explanation:
• The function finds the end of s1, then copies characters from s2.
• *s1 = '\0' marks the end of the new string.
int main() {
FILE *fp;
char filename[] = "output.txt";
char ch;
int size = 0;
fp = fopen(filename, "w");
if (fp == NULL) {
printf("File creation failed.\n");
return 1;
}
fp = fopen(filename, "r");
printf("Contents of file:\n");
while ((ch = fgetc(fp)) != EOF) {
putchar(ch); // Display file content
}
fclose(fp);
return 0;
}
Explanation:
struct Item {
int item_code;
char item_name[30];
int quantity;
int reorder_level;
};
int main() {
struct Item items[100];
int n;
return 0;
}
Explanation:
• item_code
• item_name
• quantity
• reorder_level
Task:
1. Structure Definition:
struct Item {
int item_code;
char item_name[30];
int quantity;
int reorder_level;
};
int n;
printf("Enter number of items: ");
scanf("%d", &n);
• The user is prompted to enter how many items they want to input.
• That number is stored in n.
• This condition checks whether the quantity has fallen below the reorder level.
• If true, it means that the item should be reordered.
printf("Item Code: %d, Name: %s, Quantity: %d, Reorder Level: %d\n", ...)
• If the condition is true, all item details are printed in a clear format.
Input:
Output:
Explanation:
Only "pen" has quantity < reorder_level (5 < 10), so it needs to be reordered