Write a c program practicle 1to13

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

}




 

7.     Write a c program to convert temperature form Fahrenheit to Centigrade using this formula like (c=(f-32)/1.8). 




#include <stdio.h>


void main() {

    float fahrenheit, celsius;


    // Asking the user to input the temperature in Fahrenheit

    printf("Enter temperature in Fahrenheit: ");

    scanf("%f", &fahrenheit);


    // Converting Fahrenheit to Celsius using the formula

    celsius = (fahrenheit - 32) / 1.8;


    // Displaying the result

    printf("Temperature in Celsius: %f\n", celsius);

}




8.     Write a c program to find square root of given number. 


#include <stdio.h>

#include <math.h>  // for sqrt() function


void main() {

    double num, result;


    // Asking the user to input a number

    printf("Enter a number: ");

    scanf("%lf", &num);


    // Calculating the square root

    result = sqrt(num);


    // Displaying the result

    printf("Square root of %.2lf = %.2lf\n", num, result);

}



9. C Program to Check if a Given Year is a Leap Year or Not.


#include <stdio.h>


void main() {

    int year;


    // Asking the user to input the year

    printf("Enter a year: ");

    scanf("%d", &year);


    // Leap year condition

    if (year % 4 == 0 ) {

        printf("%d is a leap year.\n", year);

    } else {

        printf("%d is not a leap year.\n", year);

    }

}


10. C Program to Check if a Given Number is Odd or Even


#include <stdio.h>


void main() {

    int num;


    // Asking the user to input a number

    printf("Enter a number: ");

    scanf("%d", &num);


    // Checking if the number is even or odd

    if (num % 2 == 0) {

        printf("%d is even.\n", num);

    } else {

        printf("%d is odd.\n", num);

    }

}



11. C Program to Check if a Given Number is Positive, Negative, or Zero.



#include <stdio.h>


void main() {

    int num;


    // Asking the user to input a number

    printf("Enter a number: ");

    scanf("%d", &num);


    // Checking if the number is positive, negative, or zero

    if (num > 0) {

        printf("%d is positive.\n", num);

    } else if (num < 0) {

        printf("%d is negative.\n", num);

    } else {

        printf("The number is zero.\n");

    }

}




12. C Program to Print the Largest Value Among Three Values.


#include <stdio.h>


void main() {

    int num1, num2, num3;


    // Asking the user to input three values

    printf("Enter three numbers: ");

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


    // Finding the largest number

    if (num1 >= num2 && num1 >= num3) {

        printf("%d is the largest number.\n", num1);

    } else if (num2 >= num1 && num2 >= num3) {

        printf("%d is the largest number.\n", num2);

    } else {

        printf("%d is the largest number.\n", num3);

    }

}




13. C Program to Print Percentage and Class of Student by Inputting Three Subject Marks (Using if..else if).



#include <stdio.h>


void main() {

    int subject1, subject2, subject3, totalMarks;

    float percentage;


    // Asking the user to input marks for three subjects

    printf("Enter marks for three subjects: ");

    scanf("%d %d %d", &subject1, &subject2, &subject3);


    // Calculating total marks and percentage

    totalMarks = subject1 + subject2 + subject3;

    percentage = (float)totalMarks / 3;


    // Printing the percentage

    printf("Percentage: %.2f%%\n", percentage);


    // Determining the class based on percentage

    if (percentage >= 75) {

        printf("Distinction\n");

    } else if (percentage >= 60) {

        printf("First Class\n");

    } else if (percentage >= 50) {

        printf("Second Class\n");

    } else if (percentage >= 40) {

        printf("Pass\n");

    } else {

        printf("Fail\n");

    }

}















Post a Comment

0 Comments