Task to send 100 codes
// 1. Write a program to input two numbers and display their sum.
#include <stdio.h> int main() { int a, b, sum; printf("Enter two numbers:"); scanf("%d %d", &a, &b);
sum = a + b;
printf("sum = %d\n", sum);
return 0;
}
// 2. Write a program to input two numbers and display their sum, difference, product, and quotient.
#include <stdio.h>
int main() { float num1, num2; float sum, differnce, product, quotient;
printf("Enter first number:");
scanf("%f", &num1);
printf("Enter second number:");
scanf("%f", &num2);
sum = num1 + num2;
differnce = num1 - num2;
product = num1 * num2;
printf("\nResults:\n");
printf("Sum = %.2f\n", sum);
printf("Difference = %.2f\n", difference);
printf("Product = %.2f\n", product);
if (num2 != 0)
{
Quotient = num1 / num2;
printf("Quotient= %.2f\n", quotient);
}
else
{
printf("Quotient= Undefined (division by zero)\n");
}
return 0;
}
// 3. Write a program to calculate the area and perimeter of a rectangle given its length and breadth.
#include <stdio.h> int main() { int length, breath, area, perimeter; printf("Enter length and breadth of rectangle:"); scanf("%d %d", &length, &breath); area = length * breath; perimeter = 2 * (length + breath); printf("Area = %d\n", area); printf("perimeter = %d\n", perimeter); return 0; }
// 4. Write a program to calculate the area and circumference of a circle given its radius.
#include <stdio.h> #include <math.h>
int main() { float radius, area, circumference; printf("Enter the radius of the circle:");
scanf("%f", &radius);
area = M_PI * radius * radius;
circumference = 2 * M_PI * radius;
printf("Area of the circle: %.2f\n", area);
printf("circumference of the circle : %.2f\n", circumference);
return 0;
}
// 5. write a program to convert temperature from celsius to Fahrenheit
#include <stdio.h> int main() { float celsius, fahrenheit; printf("Enter temperature in celsius:"); // Input temperature in celsius scanf("%f", &celsius); // conversion formula fahrenheit = (celsius * 9 / 5) + 32; // output printf(" Fahrenheit: %.2f\n", fahrenheit); return 0; }
// 6. write a program to swap two numbers using a third variable.
#include <stdio.h> int main() { int a, b, x; printf("Enter two numbers to swap;");
scanf(" %d %d", &a, &b);
x = a;
a = b;
b = x;
printf("After swapping: a=%d, b=%d\n", a, b);
return 0;
}
// 7. write a program to swap two numbers without using third variable.
#include <stdio.h> int main() { int a, b; printf("Enter two numbers to swap;");
scanf(" %d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a=%d b=%d \n", a, b);
return 0;
}
// 8.write a program to find and display the sum of the first n natural numbers.
#include <stdio.h> int main() { int n, sum = 0, i = 1; printf("Enter a postive intger:"); scanf("%d", &n);
for (i = 1; i <= n; i++)
{
sum += i;
}
printf("sum=%d\n", sum);
return 0;
}
// 10. Write a program to input time in seconds and convert it to hours:minutes:seconds format. #include <stdio.h> int main() { int totalseconds, hours, minutes, seconds;
printf("Enter time in seconds \n");
scanf("%d", &totalseconds);
hours = totalseconds / 3600;
minutes = (totalseconds % 3600) / 60;
seconds = totalseconds % 60;
printf("%d:%d:%d\n", hours, minutes, seconds);
return 0;
}
//11. Write a program to input an integer and check whether it is even or odd using if–else. #include <stdio.h> int main() { int num; printf("Enter an integer :"); scanf("%d", &num); if (num % 2 == 0) { printf("%d is even .\n", num); } else { printf("%d is odd .\n", num); } return 0; }
// 12. Write a program to input an integer and check whether it is positive, negative or zero using nested if–else.
#include <stdio.h> int main() { int num; printf("Enter an integer :"); scanf("%d", &num);
if (num >= 0)
{
if (num == 0)
{
printf("The number is zero .\n");
}
else
{
printf("%d is positive .\n", num);
}
}
else
{
printf("%d is negative .\n", num);
}
return 0;
}
// 13. Write a program to input a year and check whether it is a leap year or not using conditional statements.
#include <stdio.h> int main() { int year; printf("Enter a year:"); scanf(" %d ", &year);
if (year % 2020 == 0)
{
printf(" leap year \n");
}
else if (year % 1900 == 0)
{
printf(" not a leap year \n");
}
else
{
printf(" leap year \n");
}
return 0;
}
// 14. write a program to input a character and check whether it is a vowel or consonant using if- else.
#include <stdio.h> int main() { char ch; printf("Enter a character:"); scanf(" %c", &ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
printf("%c is a vowel \n", ch);
}
else
{
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("%c is a consonant \n", ch);
}
else
{
printf("%c is not an alphabet \n", ch);
}
}
return 0;
}
// 15. Write a program to input a character and check whether it is an uppercase alphabet, lowercase alphabet, digit, or special character.
#include <stdio.h> int main() { char ch; printf("Enter a alphabet :"); scanf("%c", &ch); if (ch >= 'A' && ch <= 'z') { printf("%c is an upper case alphabet .\n", ch); } else if (ch >= 'a' && ch <= 'z') { printf("%c is a lower case alphabet .\n", ch); } else if (ch >= '0' && ch <= '9') { printf("%c is a digit .\n", ch); } else { printf("%c is a special character .\n", ch); } return 0; }
// 16. Write a program to input three numbers and find the largest among them using if-else. #include <stdio.h>
int main() { int a, b, c; printf("Enter three numbers :"); scanf(" %d %d %d ", &a, &b, &c); if (a >= b && a >= c) { printf("%d is the largest number .\n", a); } else if (b >= a && b >= c) { printf("%d is the largest number .\n", b); } else { printf("%d is the largest number .\n", c); }
return 0;
}
// 17. Write a program to find the roots of a quadratic equation and categorize them. #include <stdio.h> #include <math.h> int main() { int a, b, c; float root 1, root 2, discriminant; printf("Enter coefficients a b c :"); scanf("%d %d %d ", a, b, c); discriminant = b * b - 4 * a * c; if (discriminant > 0) { root 1 = (-b + sqrt(discriminant)) / (2 * a); root 2 = (-b - sqrt(discriminant)) / (2 * a); printf("Roots are real and differnet .\n"); printf("Root 1 = %.2f\n", root 1); printf("Root 2 = %.2f\n", root 2); } else if (discriminant == 0) { root 1 = root 2 = -b / (2 * a); printf("Roots are real and same .\n"); printf("Root 1 = Root 2 = %.2f\n", root 1); } else { float realPart = -b / (2 * a); float imagPart = sqrt(-discriminant) / (2 * a); printf("Roots are complex and differnt .\n"); printf("Root 1 = %.2f + %.2fi\n", realPart, imagPart); printf("Root 2 = %.2f - %.2fi\n", realPart, imagPart); }
return 0;
}
// 18.c Write a program that accepts a percentage (0-100) and assigns a grade based on the following criteria. // 90-100: Grade A ,80-89: Grade B,70-79: Grade C,60-69: Grade D,below 60: Grade F. #include <stdio.h> int main() { int percentage; printf("Enter the percentage (0-100):"); scanf("%d", &percentage); if (percentage >= 90 && percentage <= 100) printf("Grade A\n"); else if (percentage >= 80 && percentage <= 89) printf("Grade B\n"); else if (percentage >= 70 && percentage <= 79) printf("Grade C\n"); else if (percentage >= 60 && percentage <= 69) printf("Grade D\n"); else if (percentage < 60) printf("Grade F\n");
return 0;
}
// 19. Write a program to classify a triangle as Equilateral, Isosceles, or Scalene based on its side lengths.
#include <stdio.h> #include <math.h>
int main() { printf("Enter the lengths of the three sides of the triangle :"); int a, b, c; scanf("%d %d %d", &a, &b, &c); if (a == b && b == c) printf(" The equilateral triangle"); else if (a == b || b == c || c == a) printf("The isosceles triangle"); else printf("The scalene triangle"); return 0; }
// 20. Write a program to display the day of the week based on a number (1–7) using switch-case #include <stdio.h> int main() { int day; printf("Enter a number (1-7) to get the corresponding day of the week:"); scanf("%d", &day); switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday \n"); break; case 6: printf("Saturday \n"); break; case 7: printf("Sunday \n"); break; default: printf("Invalid input! please enter a number between 1 and 7.\n"); break; } return 0; }