0% found this document useful (0 votes)
7 views6 pages

C Practice Answer Sheet

The document contains a series of C programming practice questions that cover various topics including variable manipulation, conditional statements, and basic input/output operations. Each question includes code snippets demonstrating concepts like swapping integers, calculating averages, checking for leap years, and determining grades based on marks. The document serves as a practical guide for learning fundamental programming skills in C.

Uploaded by

rohanbisaria58
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)
7 views6 pages

C Practice Answer Sheet

The document contains a series of C programming practice questions that cover various topics including variable manipulation, conditional statements, and basic input/output operations. Each question includes code snippets demonstrating concepts like swapping integers, calculating averages, checking for leap years, and determining grades based on marks. The document serves as a practical guide for learning fundamental programming skills in C.

Uploaded by

rohanbisaria58
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/ 6

Answer Sheet: C Practice Questions (Variables to Conditional Statements)

1. Swap two integers without using a temporary variable


#include <stdio.h>

int main() {
int a = 5, b = 10;
printf("Before swapping: a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}

2. Student's name, roll number, and grade


#include <stdio.h>

int main() {
char name[50];
int roll;
char grade;

printf("Enter name: ");


scanf("%s", name);
printf("Enter roll number: ");
scanf("%d", &roll);
printf("Enter grade: ");
scanf(" %c", &grade);

printf("Name: %s\nRoll No: %d\nGrade: %c\n", name, roll, grade);


return 0;
}

3. Output of int x = 10.5;


The output is: 10

Explanation: 10.5 is a float, but it's being stored in an int variable, so it gets
truncated to 10.

4. Average of three numbers


#include <stdio.h>

int main() {
int a, b, c;
float avg;
printf("Enter three integers: ");
scanf("%d %d %d", &a, &b, &c);
avg = (a + b + c) / 3.0;
printf("Average = %.2f\n", avg);
return 0;
}

5. Net salary calculation


#include <stdio.h>

int main() {
float basic, hra, da, pf, net;
printf("Enter basic salary: ");
scanf("%f", &basic);
hra = 0.10 * basic;
da = 0.05 * basic;
pf = 0.12 * basic;
net = basic + hra + da - pf;
printf("Net Salary = %.2f\n", net);
return 0;
}

6. a = 5, b = 2; result = a / b;
Output: 2.000000

Explanation: Integer division truncates decimal. a / b = 2, and assigning it to float


doesn't recover the decimal.

7. Largest of three numbers


#include <stdio.h>

int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("%d is the largest\n", a);
else if (b >= a && b >= c)
printf("%d is the largest\n", b);
else
printf("%d is the largest\n", c);
return 0;
}

8. Leap year check


#include <stdio.h>

int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("Leap year\n");
else
printf("Not a leap year\n");
return 0;
}

9. Vowel, consonant, digit or special character


#include <stdio.h>
#include <ctype.h>

int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);

if (isalpha(ch)) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
printf("Vowel\n");
else
printf("Consonant\n");
} else if (isdigit(ch)) {
printf("Digit\n");
} else {
printf("Special character\n");
}
return 0;
}
10. Grade based on marks
#include <stdio.h>

int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);

if (marks >= 90)


printf("Grade A\n");
else if (marks >= 80)
printf("Grade B\n");
else if (marks >= 70)
printf("Grade C\n");
else if (marks >= 60)
printf("Grade D\n");
else
printf("Grade F\n");

return 0;
}

11. Simple calculator using switch


#include <stdio.h>

int main() {
double a, b;
char op;
printf("Enter two numbers and operator (+ - * /): ");
scanf("%lf %lf %c", &a, &b, &op);

switch(op) {
case '+': printf("Result = %.2lf\n", a + b); break;
case '-': printf("Result = %.2lf\n", a - b); break;
case '*': printf("Result = %.2lf\n", a * b); break;
case '/':
if(b != 0)
printf("Result = %.2lf\n", a / b);
else
printf("Division by zero!\n");
break;
default: printf("Invalid operator\n");
}
return 0;
}

12. Even or odd without modulus


#include <stdio.h>

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

if ((num & 1) == 0)
printf("Even\n");
else
printf("Odd\n");

return 0;
}

13. Three-digit palindrome check


#include <stdio.h>

int main() {
int num, rev = 0, temp, rem;
printf("Enter a 3-digit number: ");
scanf("%d", &num);
temp = num;

while (temp != 0) {
rem = temp % 10;
rev = rev * 10 + rem;
temp /= 10;
}

if (num == rev)
printf("Palindrome\n");
else
printf("Not a palindrome\n");

return 0;
}

14. Triangle validity check


#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three sides: ");
scanf("%d %d %d", &a, &b, &c);

if (a + b > c && b + c > a && c + a > b)


printf("Valid triangle\n");
else
printf("Invalid triangle\n");

return 0;
}

You might also like