Write a c program practicle

1.  Write a c program to print “Hello World”.



#include <stdio.h>   // Includes the standard input-output library
#include <conio.h>   // Includes console input-output functions like clrscr() and getch()

void main() {
    clrscr();                    // Clears the screen (specific to Turbo C)

    printf("Hello, World!\n");    // Prints "Hello, World!" to the console

    getch();                     // Waits for a key press before the program exits
}


Output:
Hello, World!

2.     Write a c program to print sum of two numbers. 



#include <stdio.h>   // Includes standard input-output functions like printf, scanf
#include <conio.h>   // Includes console I/O functions like clrscr() and getch()

void main() {
    int num1, num2, sum;  // Variables to store two numbers and their sum

    clrscr();  // Clears the console screen

    // Input: Asking the user to enter two numbers
    printf("Enter the first number: ");
    scanf("%d", &num1);  // Reads the first number

    printf("Enter the second number: ");
    scanf("%d", &num2);  // Reads the second number

    // Process: Adding the two numbers
    sum = num1 + num2;   // Adds the two numbers and stores the result in 'sum'

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

    getch();  // Waits for the user to press a key before closing the program
}


Output:

Enter the first number: 12
Enter the second number: 8
The sum of 12 and 8 is: 20

3.     Write a c program for exchanging values of two variables. 




#include <stdio.h>   // Includes standard input-output functions like printf and scanf
#include <conio.h>   // Includes console input-output functions like clrscr() and getch()

void main() {
    int a, b, temp;  // Declare three integer variables: a, b, and temp

    clrscr();  // Clears the console screen (specific to Turbo C/C++)

    // Input: Asking the user to enter values for a and b
    printf("Enter the value of a: ");
    scanf("%d", &a);  // Read the value of 'a'

    printf("Enter the value of b: ");
    scanf("%d", &b);  // Read the value of 'b'

    // Process: Swapping the values of a and b
    temp = a;  // Store the value of 'a' in 'temp'
    a = b;     // Assign the value of 'b' to 'a'
    b = temp;  // Assign the value stored in 'temp' (original value of 'a') to 'b'

    // Output: Displaying the swapped values
    printf("After swapping, the value of a is: %d\n", a);
    printf("After swapping, the value of b is: %d\n", b);

    getch();  // Waits for the user to press a key before the program terminates
}


Example Output:

Enter the value of a: 12
Enter the value of b: 45
After swapping, the value of a is: 45
After swapping, the value of b is: 12

 

4.     Write a c program to find area of a circle  (a=PI * R * R). using define pi


#include <stdio.h>   // Includes standard input-output functions like printf and scanf

#include <conio.h>   // Includes console input-output functions like clrscr() and getch()


#define PI 3.14159   // Define a constant value for PI


void main() {

    float radius, area;  // Variables to store the radius and the area of the circle


    clrscr();  // Clears the screen


    // Input: Asking the user to enter the radius of the circle

    printf("Enter the radius of the circle: ");

    scanf("%f", &radius);  // Reading the radius from the user


    // Process: Calculating the area of the circle using the formula A = PI * R * R

    area = PI * radius * radius;


    // Output: Displaying the area of the circle

    printf("The area of the circle with radius %.2f is: %.2f\n", radius, area);


    getch();  // Waits for the user to press a key before closing the program

}


Example Output:

Enter the radius of the circle: 5
The area of the circle with radius 5.00 is: 78.54

5.     Write a c program to find volume of a cylinder  (V=PI * R * R * Height). using const pi



#include <stdio.h>   // Includes standard input-output functions like printf and scanf

#include <conio.h>   // Includes console input-output functions like clrscr() and getch()


 

void main() {

    float radius, area,h;  // Variables to store the radius and the area of the circle

    const float PI=3.14159;   // Define a constant value for PI


    clrscr();  // Clears the screen


    // Input: Asking the user to enter the radius of the circle

    printf("Enter the radius of the circle: ");

    scanf("%f", &radius);  // Reading the radius from the user


    // Input: Asking the user to enter the hieght of the circle


    printf("Enter the radius of the circle: ");

    scanf("%f", &h);  // Reading the radius from the user

    // Process: Calculating the area of the circle using the formula A = PI * R * R*h

    area = PI * radius * radius*h;


    // Output: Displaying the area of the circle

    printf("The area of the circle with radius %.2f is: %.2f\n", radius, area);


    getch();  // Waits for the user to press a key before closing the program

}


6.


#include <stdio.h>

#include <conio.h>  // for clrscr() and getch()


void main() {

    char operator;

    int num1, num2, result;


    clrscr();  // Clear the screen


    // Asking user to input the operator

    printf("Enter an operator (+, -, *, /): ");

    scanf(" %c", &operator);  // The space before %c is to consume leftover newline characters


    // Asking user to input two integers

    printf("Enter two integers: ");

    scanf("%d %d", &num1, &num2);


    // Using if-else statements for each operation

    if (operator == '+') {

        result = num1 + num2;

        printf("%d + %d = %d\n", num1, num2, result);

    }

    else if (operator == '-') {

        result = num1 - num2;

        printf("%d - %d = %d\n", num1, num2, result);

    }

    else if (operator == '*') {

        result = num1 * num2;

        printf("%d * %d = %d\n", num1, num2, result);

    }

    else if (operator == '/') {

        if (num2 != 0) {

            result = num1 / num2;  // Integer division

            printf("%d / %d = %d\n", num1, num2, result);

        } else {

            printf("Error! Division by zero is not allowed.\n");

        }

    }

    else {

        printf("Error! Invalid operator.\n");

    }


    getch();  // Wait for a key press before ending the program

}



Post a Comment

0 Comments