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
}
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");
}
}
0 Comments