C Practical
Program 1: Print Hello World
**Hello World (Program 1):** The classic introductory program that prints "Hello World" to the console, serving as a simple starting point for new programmers.
c#include <stdio.h>
int main() {
printf("Hello World\n");
return 0;
}
Program 2: Area of Rectangle
**Area of Rectangle (Program 2):** Takes user input for the length and breadth of a rectangle and calculates its area.
c#include <stdio.h>
int main() {
int l, b;
printf("Enter the length: ");
scanf("%d", &l);
printf("Enter the breadth: ");
scanf("%d", &b);
int area = l * b;
printf("The area of the rectangle is %d\n", area);
return 0;
}
Program 3: Area of Square
**Area of Square (Program 3):** Calculates the area of a square based on the user-input side length.
c#include <stdio.h>
int main() {
int s;
printf("Enter the side length of the square: ");
scanf("%d", &s);
int area = s * s;
printf("The area of the square is %d\n", area);
return 0;
}
Program 4: Area of Circle
**Area of Circle (Program 4):** Computes the area of a circle using the radius entered by the user.
c#include <stdio.h>
int main() {
int r;
printf("Enter the radius of the circle: ");
scanf("%d", &r);
float area = 3.14 * r * r;
printf("The area of the circle is %.2f\n", area);
return 0;
}
Program 5: Convert Fahrenheit to Celsius
**Convert Fahrenheit to Celsius (Program 5):** Converts a temperature value from Fahrenheit to Celsius.
c#include <stdio.h>
int main() {
int fahrenheit, celsius;
printf("Enter the temperature in Fahrenheit: ");
scanf("%d", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("Temperature in Celsius: %d\n", celsius);
return 0;
}
Program 6: Simple Interest
**Simple Interest (Program 6):** Computes simple interest based on user-provided principal amount, rate of interest, and time period.
c#include <stdio.h>
int main() {
int p, r, t;
printf("Enter the principal amount: ");
scanf("%d", &p);
printf("Enter the rate of interest: ");
scanf("%d", &r);
printf("Enter the time period: ");
scanf("%d", &t);
int interest = (p * r * t) / 100;
printf("Simple interest is %d\n", interest);
return 0;
}
Program 7: Check if a number is divisible by 19
**Check if a number is divisible by 19 (Program 7):** Determines whether a given number is divisible by 19.
c#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 19 == 0) {
printf("%d is divisible by 19\n", num);
} else {
printf("%d is not divisible by 19\n", num);
}
return 0;
}
Program 8: Check if a number is even or odd
**Check if a number is even or odd (Program 8):** Identifies whether a given number is even or odd.
c#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even\n", num);
} else {
printf("%d is odd\n", num);
}
return 0;
}
Program 9: Grading system
**Grading system (Program 9):** Implements a basic grading system based on user-input marks.
c#include <stdio.h>
int main() {
int num;
printf("Enter the marks: ");
scanf("%d", &num);
if (num <= 50) {
printf("Pass\n");
} else if (num <= 33) {
printf("Fail\n");
} else if (num >= 34 && num <= 60) {
printf("C grade\n");
} else if (num >= 61 && num <= 80) {
printf("B grade\n");
} else if (num >= 81 && num <= 100) {
printf("A grade\n");
}
return 0;
}
Program 10: Calculate Income Taxes
**Calculate Income Taxes (Program 10):** Computes income tax based on user-provided income using a simple tax rate structure.
c#include <stdio.h>
int main() {
float income, tax;
printf("Enter your income: ");
scanf("%f", &income);
if (income <= 250000) {
tax = 0;
} else if (income <= 500000) {
tax = 0.05
- **Leap Year (Program 11):** Determines whether a given year is a leap year.
Program 11: Leap Year
c#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
Program 12: Multiplication Table
- **Multiplication Table (Program 12):** Generates the multiplication table for a user-input number.
c#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}
return 0;
}
Program 13: Print Prime Numbers
- **Print Prime Numbers (Program 13):** Prints prime numbers up to 100 using a function to check primality.
c#include <stdio.h>
#include <stdbool.h>
bool isPrime(int num) {
if (num < 2)
return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0)
return false;
}
return true;
}
int main() {
printf("Prime numbers up to 100:\n");
for (int i = 2; i <= 100; i++) {
if (isPrime(i))
printf("%d ", i);
}
printf("\n");
return 0;
}
Program 14: Sum of First 10 Natural Numbers
- **Sum of First 10 Natural Numbers (Program 14):** Calculates the sum of the first 10 natural numbers.
c#include <stdio.h>
int main() {
int n = 10;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("Sum of first %d natural numbers is: %d\n", n, sum);
return 0;
}
Program 15: Sum of Numbers in the Multiplication Table of 8
- **Sum of Numbers in the Multiplication Table of 8 (Program 15):** Computes the sum of numbers in the multiplication table of 8
c#include <stdio.h>
int main() {
int number = 8;
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += number * i;
}
printf("Sum of numbers in the multiplication table of 8 is: %d\n", sum);
return 0;
}
Program 16: Factorial of a Number
- **Factorial of a Number (Program 16):** Calculates the factorial of a user-input number.
- cint main() { int number; int factorial = 1; printf("Enter a number: "); scanf("%d", &number); for (int i = 1; i <= number; i++) { factorial *= i; } printf("Factorial of %d is: %d\n", number, factorial); return 0; }
- These programs cover fundamental concepts such as input/output, arithmetic operations, conditionals, loops, functions, and more, making them suitable for learning the basics of C programming.
Tags:
C