0% found this document useful (0 votes)
21 views11 pages

HW720

The document contains solutions for nine C programming tasks, including printing data types and their ranges, converting Celsius to Fahrenheit, finding the smallest and largest among three numbers, calculating simple and compound interest, finding roots of a quadratic equation, creating a simple calculator, printing natural numbers, calculating factorials, and checking if a number is even or odd. Each task includes code snippets, algorithms, and flowcharts where applicable. The solutions demonstrate fundamental programming concepts in C.

Uploaded by

ap2.brothers3151
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

HW720

The document contains solutions for nine C programming tasks, including printing data types and their ranges, converting Celsius to Fahrenheit, finding the smallest and largest among three numbers, calculating simple and compound interest, finding roots of a quadratic equation, creating a simple calculator, printing natural numbers, calculating factorials, and checking if a number is even or odd. Each task includes code snippets, algorithms, and flowcharts where applicable. The solutions demonstrate fundamental programming concepts in C.

Uploaded by

ap2.brothers3151
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C Practical File Solutions [1 to 9]

1. Write a C Program to print different data types in 'C' and their ranges.
Solution:
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <conio.h>
void main()
{
clrscr();
printf("Data Type Ranges in C:\n\n");
printf("char: \t\t %d to %d\n", CHAR_MIN, CHAR_MAX);
printf("unsigned char: \t 0 to %u\n", UCHAR_MAX);
printf("short: \t\t %d to %d\n", SHRT_MIN, SHRT_MAX);
printf("unsigned short: \t 0 to %u\n", USHRT_MAX);
printf("int: \t\t %d to %d\n", INT_MIN, INT_MAX);
printf("unsigned int: \t 0 to %u\n", UINT_MAX);
printf("long: \t\t %ld to %ld\n", LONG_MIN, LONG_MAX);
printf("unsigned long: \t 0 to %lu\n", ULONG_MAX);
printf("long long: \t %lld to %lld\n", LLONG_MIN, LLONG_MAX);
printf("unsigned long long: 0 to %llu\n", ULLONG_MAX);
printf("\nFloating Point Types:\n");
printf("float: \t\t %e to %e\n", FLT_MIN, FLT_MAX);
printf("double: \t\t %e to %e\n", DBL_MIN, DBL_MAX);
printf("long double: \t %Le to %Le\n", LDBL_MIN, LDBL_MAX);
getch();
}
2. Write an Algorithm & Flowchart to convert temperature from Celsius to
Fahrenheit.
Solution:

Algorithm to Convert Celsius to Fahrenheit

1. Start
2. Declare a variable for Celsius (celsius) and Fahrenheit (fahrenheit).
3. Input the temperature in Celsius.
4. Apply the conversion formula:

fahrenheit=(celsius×9/5)+32

5. Display the converted temperature in Fahrenheit.


6. End

Flowchart

Below is a textual representation of the flowchart. You can visualize it as follows:

Start

Input Celsius

Fahrenheit = (Celsius * 9/5) + 32

Display Fahrenheit

End
3. Write an algorithm & flowchart to find the smallest and largest number among
the three numbers.
Solution:

Algorithm to Find the Smallest and Largest Number Among Three Numbers

1. Start
2. Declare three variables: num1, num2, and num3.
3. Input the three numbers.
4. Initialize smallest and largest with the first number (num1).
5. Compare num2 with smallest and largest, updating them if necessary.
6. Compare num3 with smallest and largest, updating them if necessary.
7. Display the smallest and largest numbers.
8. End

Flowchart Representation

Here’s a textual representation of the flowchart:

Start

Input num1, num2, num3

smallest = num1, largest = num1

If num2 < smallest, update smallest

If num2 > largest, update largest

If num3 < smallest, update smallest

If num3 > largest, update largest

Display smallest and largest

End
4. Write a program to calculate simple and compound interest.

Solution:

#include <stdio.h>
#include <math.h>
#include <conio.h>
void main() { clrscr();
double principal, rate, time, simple_interest, compound_interest;
int n;
// Input principal amount, interest rate, and time
printf("Enter principal amount: ");
scanf("%lf", &principal);
printf("Enter annual interest rate (in percentage): ");
scanf("%lf", &rate);
printf("Enter time (in years): ");
scanf("%lf", &time);
// Calculate Simple Interest
simple_interest = (principal * rate * time) / 100;
// Input number of times interest applied per time period (for compound interest)
printf("Enter number of times interest is compounded per year: ");
scanf("%d", &n);
// Calculate Compound Interest
compound_interest = principal * pow((1 + rate / (100 * n)), n * time) - principal
// Display results
printf("Simple Interest: %.2lf\n", simple_interest);
printf("Compound Interest: %.2lf\n", compound_interest);
getch();
}
5. Write a C program to find the roots of a quadratic equation.

Solution:

#include <stdio.h>
#include <math.h>
#include <conio.h>
void main() { clrscr();
double a, b, c, discriminant, root1, root2, realPart, imagPart;
// Input coefficients a, b, and c
printf("Enter coefficients a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
// Calculate the discriminant
discriminant = b * b - 4 * a * c;
// Check the nature of the roots
if (discriminant > 0) {
// Two distinct real roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different.\n");
printf("Root 1 = %.2lf\n", root1);
printf("Root 2 = %.2lf\n", root2);
}
else if (discriminant == 0) {
// One real root (repeated)
root1 = -b / (2 * a);
printf("Roots are real and the same.\n");
printf("Root = %.2lf\n", root1);
}
else {
// Complex roots
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and different.\n");
printf("Root 1 = %.2lf + %.2lfi\n", realPart, imagPart);
printf("Root 2 = %.2lf - %.2lfi\n", realPart, imagPart);
}
getch();
}
6. Write a C program to make a simple calculator using switch...case.

Solution:

#include<stdio.h>
#include<conio.h>
void main() { clrscr();
char operator;
double num1, num2, result;
// Taking user input
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
// Switch case for calculator operations
switch (operator) {
case '+':
result = num1 + num2;
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result);
break;
case '*':
result = num1 * num2;
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result);
break;
case '/':
if (num2 != 0)
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
else
printf("Error! Division by zero is not allowed.\n");
break;
default:
printf("Invalid operator! Please use +, -, *, or /.\n");
}
getch();
}
7. Write a C program to print natural numbers from 1 to n.

Solution:

#include <stdio.h>
#include <conio.h>
void main() { clrscr();
int n;
// Taking user input
printf("Enter a positive integer: ");
scanf("%d", &n);
// Printing natural numbers from 1 to n
printf("Natural numbers from 1 to %d:\n", n);
for (int i = 1; i <= n; i++) {
printf("%d ", i);
}
printf("\n");
getch();
}
8. Write a C program to find the factorial of a given number.

Solution:

#include <stdio.h>
#include <conio.h>
// Function to calculate factorial
long long factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
void main() { clrscr();
int num;
// Taking user input
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0)
printf("Factorial is not defined for negative numbers.\n");
else
printf("Factorial of %d is %lld\n", num, factorial(num));
getch();
}
9. Write a program in C to check a given number is even or odd using function.

Solution:

#include <stdio.h>
#include <conio.h>
// Function to check even or odd
void checkEvenOdd(int num) {
if (num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
}
void main() { clrscr();
int number;
// Taking user input
printf("Enter an integer: ");
scanf("%d", &number);
// Function call
checkEvenOdd(number);
getch();
}

You might also like