Sunday, November 23, 2025

Practice Questions

1. C program that uses comments to display the output using variables

#include <stdio.h> int main() { int a = 10, b = 20; // Storing values in variables // Displaying output using variables printf("Value of a = %d\n", a); printf("Value of b = %d\n", b); return 0; // End of program }

2. C program that uses both constants and variables

#include <stdio.h> int main() { const float PI = 3.14; // constant int radius = 5; // variable float circumference = 2 * PI * radius; printf("Circumference = %.2f", circumference); return 0; }

3. C program using different data types

#include <stdio.h> int main() { int age = 20; float height = 5.7; char grade = 'A'; double salary = 55000.75; printf("Age = %d\n", age); printf("Height = %.2f\n", height); printf("Grade = %c\n", grade); printf("Salary = %.2lf\n", salary); return 0; }

4. Program using putchar() and puts()

#include <stdio.h> int main() { putchar('A'); putchar('\n'); puts("Hello World using puts()"); return 0; }

5. Program using getchar(), getch(), getche()

getch() and getche() work only in <conio.h> and Turbo C/Some compilers.

#include <stdio.h> #include <conio.h> int main() { char a, b, c; printf("Enter a character using getchar(): "); a = getchar(); printf("\nPress a key using getch(): "); b = getch(); // does not show on screen printf("\nPress a key using getche(): "); c = getche(); // shows on screen printf("\nYou entered: %c %c %c", a, b, c); return 0; }

6. Program demonstrating compound assignment operators

#include <stdio.h> int main() { int x = 10; x += 5; // x = 15 x -= 3; // x = 12 x *= 2; // x = 24 x /= 4; // x = 6 printf("Final value of x = %d", x); return 0; }

7. Program to find remainder of two numbers

#include <stdio.h> int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); printf("Remainder = %d", a % b); return 0; }

8. Program using Strings

#include <stdio.h> int main() { char name[30]; printf("Enter your name: "); gets(name); printf("Your name is %s", name); return 0; }

9. Program to check Odd or Even

#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if(num % 2 == 0) printf("Even"); else printf("Odd"); return 0; }

10. Program to print average of 3 numbers

#include <stdio.h> int main() { float a, b, c, avg; printf("Enter three numbers: "); scanf("%f %f %f", &a, &b, &c); avg = (a + b + c) / 3; printf("Average = %.2f", avg); return 0; }

🧪 Lab Exercises (5 Questions)


Q1. Print your name and age

#include <stdio.h> int main() { printf("Name: Ali\nAge: 16"); return 0; }

Q2. Input age and print it

#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); printf("Your age is %d", age); return 0; }

Q3. Input two numbers and print sum

#include <stdio.h> int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); printf("Sum = %d", a + b); return 0; }

Q4. Print formatted table using escape sequences

(Already provided; repeating clean version)

#include<stdio.h> int main() { printf("Name\tAge\tClass\n"); printf("Ali\t15\t8\n"); printf("Sara\t14\t7\n"); printf("Ahmed\t16\t9\n"); return 0; }

Q5. Input marks of 3 subjects and print total

#include <stdio.h> int main() { int m1, m2, m3, total; printf("Enter marks of 3 subjects: "); scanf("%d %d %d", &m1, &m2, &m3); total = m1 + m2 + m3; printf("Total = %d", total); return 0; }

Logical Operators Set


Q1. Gym Entry Condition (OR + NOT)

#include <stdio.h> int main() { int age, fees; printf("Enter age and fees: "); scanf("%d %d", &age, &fees); if(age > 18 || fees > 2000) printf("Allowed in Gym\n"); else printf("Not Allowed\n"); // Proving NOT operator if(!(age > 18 || fees > 2000)) printf("Condition is FALSE now (using NOT)"); return 0; }

Q2. Largest among 3 numbers using &&

#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("Largest = %d", a); else if(b > a && b > c) printf("Largest = %d", b); else printf("Largest = %d", c); return 0; }

Q3. Program using ++ and --

#include <stdio.h> int main() { int x = 10; printf("x = %d\n", x); x++; printf("After increment: %d\n", x); x--; printf("After decrement: %d", x); return 0; }

Q4. Program using relational operators

#include <stdio.h> int main() { int a = 10, b = 20; printf("%d < %d = %d\n", a, b, a < b); printf("%d > %d = %d\n", a, b, a > b); printf("%d <= %d = %d\n", a, b, a <= b); printf("%d >= %d = %d\n", a, b, a >= b); printf("%d == %d = %d\n", a, b, a == b); printf("%d != %d = %d\n", a, b, a != b); return 0; }

Q5. Difference between = and ==

Assignment operator (=)

Used to assign a value to a variable
Example: x = 10;

Equal to operator (==)

Used to compare two values
Example: if (x == 10)

___________________________________________________________________________________

1. Area of Rectangle

(a) Using hard coded input

#include <stdio.h>

 

int main() {

    int length = 10, width = 5;   // Hard coded values

    int area = length * width;

 

    printf("Length = %d\n", length);

    printf("Width = %d\n", width);

    printf("Area of Rectangle = %d\n", area);

 

    return 0;

}

(b) Using user input

#include <stdio.h>

 

int main() {

    int length, width, area;

 

    printf("Enter length of rectangle: ");

    scanf("%d", &length);

    printf("Enter width of rectangle: ");

    scanf("%d", &width);

 

    area = length * width;

    printf("Area of Rectangle = %d\n", area);

 

    return 0;

}


2. Area of Circle and Volume of Cylinder

#include <stdio.h>

#define PI 3.1416

 

int main() {

    float radius, height, area, volume;

 

    printf("Enter radius of circle: ");

    scanf("%f", &radius);

 

    area = PI * radius * radius;

    printf("Area of Circle = %.2f\n", area);

 

    printf("Enter height of cylinder: ");

    scanf("%f", &height);

 

    volume = PI * radius * radius * height;

    printf("Volume of Cylinder = %.2f\n", volume);

 

    return 0;

}


3. Convert Celsius to Fahrenheit

#include <stdio.h>

 

int main() {

    float celsius, fahrenheit;

 

    printf("Enter temperature in Celsius: ");

    scanf("%f", &celsius);

 

    fahrenheit = (celsius * 9 / 5) + 32;

    printf("Temperature in Fahrenheit = %.2f\n", fahrenheit);

 

    return 0;

}

_______________________________________________________________________

 #include <stdio.h>

int main() { int a, b, remainder; printf("Enter two integers:\n"); scanf("%d %d", &a, &b); remainder = a % b; printf("The remainder when %d is divided by %d is %d\n", a, b, remainder); return 0; }

#include <stdio.h> int main() { char name[100]; printf("Enter your name:\n"); scanf("%s", name); printf("Hello, %s!\n", name); return 0; }


_______________________________________________________________________

Assignment Problems (C Programming Basics)

Q1: Personal Information

Write a program to store your name (string), age (int), and grade (char) in variables.
Then print them on the screen using printf().
👉 Hint: use %s%d%c.


Q2: Circle Area (Using Constant)

Declare a constant PI = 3.1416.
Take a radius (int) variable, assign it a value, and calculate the area of a circle.
Formula: Area = PI * radius * radius
👉 Print the radius and area using printf().

int main() {
    const float PI = 3.1416;   // constant value
    int radius = 5;            // radius (integer)
    float area;                // area (decimal value)

    area = PI * radius * radius;   // formula

    printf("Radius: %d\n", radius);
    printf("Area of Circle: %.2f\n", area); // %.2f = 2 decimal places

    return 0;


Q3: Simple Calculator

Declare two integer variables a and b.
Store some values in them (like 10 and 20).
Print the results of:

  • Addition

  • Subtraction

  • Multiplication

  • Division
    👉 Example output:

a + b = 30
a - b = -10
a * b = 200
a / b = 0

Q4: Student Marks

Store marks of 3 subjects in float variables.
Print the total and average marks.
👉 Hint: Use %f and %.2f for decimals.


Q5: Variable Update

Create an integer variable age = 15.
First, print it.
Then update it to 16, and print again.
👉 This will show how variables can change their values.


_______________________________________________________________________
  1. 🧩 1. Check if a number is positive or negative

    #include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num > 0) printf("Number is Positive"); else if (num < 0) printf("Number is Negative"); else printf("Number is Zero"); return 0; }

    Explanation:
    Uses relational operators >, <, and ==.


    🧩 2. 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 number"); else printf("Odd number"); return 0; }

    Explanation:
    Uses % modulus operator and ==.


    🧩 3. Check if a number is divisible by both 3 and 5

    #include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num % 3 == 0 && num % 5 == 0) printf("Divisible by both 3 and 5"); else printf("Not divisible by both"); return 0; }

    Explanation:
    Uses logical AND (&&).


    🧩 4. Check if a number lies between 10 and 50

    #include <stdio.h> int main() { int n; printf("Enter a number: "); scanf("%d", &n); if (n >= 10 && n <= 50) printf("Number is between 10 and 50"); else printf("Number is outside the range"); return 0; }

    Explanation:
    Relational operators >=, <= and logical AND (&&).


    🧩 5. Find the greatest of two numbers

    #include <stdio.h> int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); if (a > b) printf("%d is greater", a); else if (b > a) printf("%d is greater", b); else printf("Both are equal"); return 0; }

    Explanation:
    Compares using relational operators >, <.


    🧩 6. Check if a person is eligible to vote

    #include <stdio.h> int main() { int age; printf("Enter age: "); scanf("%d", &age); if (age >= 18) printf("Eligible to vote"); else printf("Not eligible to vote"); return 0; }

    Explanation:
    Simple >= relational comparison.


    🧩 7. Check if a character is a vowel

    #include <stdio.h> int main() { char ch; printf("Enter a character: "); scanf(" %c", &ch); if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'|| ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') printf("Vowel"); else printf("Consonant"); return 0; }

    Explanation:
    Uses logical OR (||) with multiple relational checks.


    🧩 8. Check if a year is a leap year

    #include <stdio.h> int main() { int year; printf("Enter year: "); scanf("%d", &year); if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) printf("Leap Year"); else printf("Not a Leap Year"); return 0; }

    Explanation:
    Combination of AND (&&) and OR (||) logical operators.


    🧩 9. Check eligibility for admission

    #include <stdio.h> int main() { int marks; printf("Enter marks: "); scanf("%d", &marks); if (marks >= 50 && marks <= 100) printf("Eligible for admission"); else printf("Not eligible"); return 0; }

    Explanation:
    Checks range using relational and logical operators.


    🧩 10. Determine grade based on percentage

    #include <stdio.h> int main() { float per; printf("Enter your percentage: "); scanf("%f", &per); if (per >= 80) printf("Grade A"); else if (per >= 70) printf("Grade B"); else if (per >= 60) printf("Grade C"); else if (per >= 50) printf("Grade D"); else printf("Fail"); return 0; }

    Explanation:
    Uses multiple if-else-if and relational comparisons.

___________________________________________________________________________________

11. Write a Program to check if a student passed or failed
marks >30 is PASS
marks <=30 is FAIL

12. Write a Program to give grades to a student

marks <30 is C
30<= marks <7 0 is B
70< = marks <90 is A
90 <= marks <= 100 is A+

13. Write a Program to find it a character entered by user is upper case or not.


 Group Wise Project

📘 Objective:

Write a C program that:

  • Takes student details (name, roll number, and marks of 3 subjects) using scanf().

  • Calculates total marks and average using arithmetic operators.

  • Uses if–else statements to determine the student’s grade.

  • Displays results using printf() and format specifiers.

  • Includes proper use of escape sequences for clean formatting.

 


 


 




 Prog 1:

#include<stdio.h>

int main(){

    printf("Hello ");

    printf("world\n");

    int a = 1;

    // Loops are used to repeat similar part of a code snippet efficiently

    // (

    // printf("%d\n", a);

    // a++;) =---> 100 times

    return 0;

}

 Prog 2:

#include<stdio.h>

int main(){

    int a;

    scanf("%d", &a);

    while(a<10){

    //     a = 11;

    // while(a>10){ ---> These two lines will lead to an infinite loop

        printf("%d\n", a);

        a++;

    }

    return 0;

}

 Prog 3:

#include<stdio.h>

int main(){

    int i = 5;

    while(i<=20){

        if (i >= 10){ 

        printf("The value of i is %d\n", i);

        }

        i++; // i = i + 1;

    }

    return 0;

}

Prog 4:

#include<stdio.h>

int main(){

    int i = 5;

    printf("The value after i++ is %d\n", ++i);

    i++; // ---> 

    ++i; // ---> 

    printf("The value of i is %d\n", i);

    i+=10; //--> Increments i by 10

    printf("The value of i is %d\n", i);

    return 0;

}

Prog 5:

#include<stdio.h>

int main(){

    int i = 0;

    do{

        printf("The value of i is %d\n", i);

        i++;

    }while(i <10);

    return 0;

}

Prog 6:

#include<stdio.h>

int main(){

    int i=0;

    int n;

    printf("Enter the value of n\n");

    scanf("%d", &n);

    do{

        printf("The number is %d \n",i+1);

        i++;

    }while(i<n);

    return 0;

}

Prog 7:

#include<stdio.h>

#include<conio.h>

main()

{

int x;

do

{

printf("Enter zero to quit");

scanf("%d",&x);

}while(x!=0);

}

Prog 8:

#include<stdio.h>

#include<conio.h>

main()

{

char e;

int p;

do

{

printf("\nEnter your email");

scanf("%c",&e);

printf("\nEnter your passwrod");

scanf("%d",&p);

}while(e!='r' && p!=5);

}

Prog 9:

The Infinite Loop

A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the 'for' loop are required, you can make an endless loop by leaving the conditional expression empty.

#include <stdio.h>
 
int main () {

   for( ; ; ) {
      printf("This loop will run forever.\n");
   }

   return 0;
}

When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the for(;;) construct to signify an infinite loop.

NOTE − You can terminate an infinite loop by pressing Ctrl + C keys.


Prog 10:

#include<stdio.h>

int main()

{

int i = 0;

while(i<10)

{

printf("hellohyd:%d\n",i);

i++;

}

printf("Control reached here");

return 0;

}

MORE PRACTICES ON LOOP






Perform the given programs on Dev C++ and execute to check the output. Write down the Program and Output in your register too.















Note: Write down the program in your register with their outputs.

Prog 1:


Prog 2:


Prog 3:

Prog 4:



Prog 5:

Prog 6:

Prog 7:

Prog 8:

Prog 9:

Prog 10:

Prog 11:













No comments:

Post a Comment