1. Write a c program to print “Hello World”.
2. Write a c program to print sum of two numbers.
3. Write a c program for exchanging values of two variables.
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
}
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
}
0 Comments