fbpx

C Programing-Questions Basic Questions: Exercises, Practice, Solution.

In this Post we will see some basic questions in C programming. All beginners can go through with these excercise and test there basic skills.

C programming serves as a foundational milestone for many individuals navigating the programming landscape. It is an optimal choice for gaining insights into the intricacies of programming and comprehending the internal workings of computer programs.

Given its proximity to low-level programming, venturing into C can be a daunting prospect for beginners unless approached with deliberate practice. However, fear not! At Codeforwin, we offer a systematic, step-by-step approach to learning and practicing C programming.

Within this programming exercise, our primary focus lies on the fundamentals of C programming. Upon completion, you will have acquired a solid understanding of the basic structure and semantics inherent in C programs. Moreover, you’ll be adept at crafting mathematical programs using the C language.

Basic Questions

1 ) WAP in C to find addition of two numbers by taking input from user.

int main() {
    // Declare variables to store the input numbers
    int num1, num2, sum;

    // Prompt the user to enter the first number
    printf("Enter the first number: ");
    scanf("%d", &num1);

    // Prompt the user to enter the second number
    printf("Enter the second number: ");
    scanf("%d", &num2);

    // Calculate the sum
    sum = num1 + num2;

    // Display the result
    printf("The sum of %d and %d is: %d\n", num1, num2, sum);

    return 0;
}

2 ) WAP in C to find area of rectangle by taking input from user.

int main() {
// Declare variables
float length, width, area; // Prompt user for input
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width); // Calculate the area
area = length * width; // Display the result
printf("The area of the rectangle is: %.2f\n", area);
return 0;
}

3) WAP in C to find area of Square by taking input from user.

int main() {
    // Declare a variable to store the side length of the square
    int sideLength,area;
    // Prompt the user to enter the side length of the square
    printf("Enter the side length of the square: ");
    scanf("%d", &sideLength);
    // Calculate the area of the square
       area = sideLength * sideLength;
    // Display the result
    printf("The area of the square with side length %d is: %dn", sideLength, area);
    return 0;
}

4) WAP in C to calculte simple interest by taking principle, rate and time from user

int main() {
    // Declare variables to store user input
    int principle, rate, time;
    // Prompt the user to enter the principal amount
    printf("Enter the principal amount: ");
    scanf("%d", &principle);
    // Prompt the user to enter the interest rate
    printf("Enter the interest rate (in percentage): ");
    scanf("%d", &rate);
    // Prompt the user to enter the time (in years)
    printf("Enter the time (in years): ");
    scanf("%d", &time);
    // Calculate the simple interest
    float simpleInterest = (principle * rate * time) / 100;
    // Display the result
    printf("Simple Interest: %fn", simpleInterest);
    return 0;
}

5) WAP in C to input cost price and selling price for a product and find profit and loss value

int main() {
    // Declare variables to store user input
    int costPrice, sellingPrice;
    // Prompt the user to enter the cost price
    printf("Enter the cost price: ");
    scanf("%d", &costPrice);
    // Prompt the user to enter the selling price
    printf("Enter the selling price: ");
    scanf("%d", &sellingPrice);
    // Calculate profit or loss
    int profitLoss = sellingPrice - costPrice;
    // Display the result
    if (profitLoss > 0) {
        printf("Profit: %dn", profitLoss);
    } else if (profitLoss < 0) {
        printf("Loss: %dn", -profitLoss);  // Display loss as positive value
    } else {
        printf("No Profit, No Lossn");
    }
    return 0;
}

6) WAP in C to calculate area of circle taking input from user.

int main() {
    // Declare variables to store user input and the area
    int radius;
    float area;
    // Prompt the user to enter the radius of the circle
    printf("Enter the radius of the circle: ");
    scanf("%d", &radius);
    // Calculate the area of the circle
    area = 3.14159 * radius * radius;
    // Display the result
    printf("The area of the circle with radius %d is: %.2fn", radius, area);
    return 0;
}

7) WAP in C to input marks of a student having 5 subjects from user and then calculate total marks and percentage of the student and print them.

int main() {
    // Declare variables to store marks and calculations
    int marksSubject1, marksSubject2, marksSubject3, marksSubject4, marksSubject5;
    int totalMarks;
    float percentage;
    // Prompt the user to enter marks for each subject
    printf("Enter marks for Subject 1: ");
    scanf("%d", &marksSubject1);
    printf("Enter marks for Subject 2: ");
    scanf("%d", &marksSubject2);
    printf("Enter marks for Subject 3: ");
    scanf("%d", &marksSubject3);
    printf("Enter marks for Subject 4: ");
    scanf("%d", &marksSubject4);
    printf("Enter marks for Subject 5: ");
    scanf("%d", &marksSubject5);
    // Calculate total marks
    totalMarks = marksSubject1 + marksSubject2 + marksSubject3 + marksSubject4 + marksSubject5;
    // Calculate percentage
    percentage = (float)totalMarks / 5.0;
    // Display the results
    printf("nTotal Marks: %dn", totalMarks);
    printf("Percentage: %.2f%%n", percentage);
    return 0;
}

8) WAP in C to convert temperature from Celsius to Fahrenheit.

int main() {
    // Declare variables to store temperature in Celsius and Fahrenheit
    float celsius, fahrenheit;
    // Prompt the user to enter temperature in Celsius
    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);
    // Convert Celsius to Fahrenheit
    fahrenheit = (celsius * 9 / 5) + 32;
    // Display the result
    printf("Temperature in Fahrenheit: %fn", fahrenheit);
    return 0;
}

9) WAP in C to input price of 5 products from user and calculate the total price and apply a discount of 10 % to provide a final total for payment.

int main() {
    // Declare variables to store prices and calculations
    float price1, price2, price3, price4, price5;
    float totalPrice, discountedTotal;
    // Prompt the user to enter prices for each product
    printf("Enter price for Product 1: ");
    scanf("%f", &price1);
    printf("Enter price for Product 2: ");
    scanf("%f", &price2);
    printf("Enter price for Product 3: ");
    scanf("%f", &price3);
    printf("Enter price for Product 4: ");
    scanf("%f", &price4);
    printf("Enter price for Product 5: ");
    scanf("%f", &price5);
    // Calculate total price
    totalPrice = price1 + price2 + price3 + price4 + price5;
    // Apply a discount of 10%
    discountedTotal = totalPrice - (0.10 * totalPrice);
    // Display the results
    printf("nTotal Price: %fn", totalPrice);
    printf("Discounted Total: %fn", discountedTotal);
    return 0;
}

10) WAP in C to input radius of circle from user and find its diameter, circumference and area.

int main() {
    // Declare variables to store user input and calculations
    float radius, diameter, circumference, area;
    // Prompt the user to enter the radius of the circle
    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);
    // Calculate diameter, circumference, and area
    diameter = 2 * radius;
    circumference = 2 * 3.14159 * radius;
    area = 3.14159 * radius * radius;
    // Display the results
    printf("nDiameter of the circle: %fn", diameter);
    printf("Circumference of the circle: %fn", circumference);
    printf("Area of the circle: %fn", area);
    return 0;
}
On Key

Related Posts

SQL Quries

A Comprehensive Guide to MySQL Queries

MySQL is a widely-used relational database management system that allows for the creation, modification, and management of databases using SQL (Structured Query Language). In this guide, we’ll cover the essential MySQL queries with examples and tables to help you master the basics. Creating a Database Before you can create tables, you need a database. Use

Java All key Concets

Mastering Java: Key Concepts and Examples for Beginners

Welcome to Pivoteduunit’s comprehensive guide to Java programming! This post is designed to help you understand the essential concepts and provide you with practical examples to kickstart your coding journey. Whether you’re new to Java or looking to solidify your basics, we’ve got you covered. Variables Explanation: Variables are containers for storing data values. Data

Overloading and Overriding

Java- Method Overloading and Overriding

Method Overloading Method overloading in Java allows a class to have more than one method with the same name, as long as their parameter lists are different. Overloading is determined at compile-time and is a form of compile-time polymorphism. Rules for Method Overloading: Explanation: In the MathOperations class, there are three overloaded add methods. The

Java Inheritance Programs

Java Inheritance: Exercises, Practice, Solution

In Java, classes can be derived from other classes, allowing them to inherit fields and methods from the parent classes. Definitions: Except for Object, which has no superclass, every class has exactly one direct superclass (single inheritance). If no other explicit superclass is declared, every class implicitly extends Object. Classes can be derived from other