0% found this document useful (0 votes)
5 views

Subjective Assignment 01 Basic Programs Ans

The document contains solutions to 10 programming problems in C language. Each problem asks the user to write a C program to perform a specific task like inputting numbers, calculating operations on numbers, converting between units of measurement, and finding power of a number. The solutions provide the full source code to solve each problem.

Uploaded by

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

Subjective Assignment 01 Basic Programs Ans

The document contains solutions to 10 programming problems in C language. Each problem asks the user to write a C program to perform a specific task like inputting numbers, calculating operations on numbers, converting between units of measurement, and finding power of a number. The solutions provide the full source code to solve each problem.

Uploaded by

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

Assignment of subjective questions

1. Write a C program to input two numbers from user and calculate their sum.

Example
Input
Input first number: 20
Input second number: 10
Output

Sum = 30

Solution -

#include <stdio.h>

int main()
{
int num1, num2, sum;

/*
* Read two numbers from user
*/
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number:");
scanf("%d", &num2);

/* Adding both number is simple and fundamental */


sum = num1 + num2;

/* Prints the sum of two numbers */


printf("Sum of %d and %d = %d", num1, num2, sum);

return 0;
}
2. Write a C program to input two numbers and perform all arithmetic operations.

Example
Input
First number: 10
Second number: 5
Output

Sum = 15
Difference = 5
Product = 50
Quotient = 2
Modulus = 0

Solution -

#include <stdio.h>

int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div;

/*
* Input two numbers from user
*/
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);

/*
* Perform all arithmetic operations
*/
sum = num1 + num2;
sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
mod = num1 % num2;

/*
* Print result of all arithmetic operations
*/
printf("SUM = %d\n", sum);
printf("DIFFERENCE = %d\n", sub);
printf("PRODUCT = %d\n", mult);
printf("QUOTIENT = %f\n", div);
printf("MODULUS = %d", mod);

return 0;
}

3. Write a C program to input length and width of a rectangle and calculate


perimeter of the rectangle.

Example
Input
Enter length: 5
Enter width: 10
Output

Perimeter of rectangle = 30 units

Solution -

#include <stdio.h>

int main()
{
float length, width, perimeter;

/*
* Input length and width of rectangle from user
*/
printf("Enter length of the rectangle: ");
scanf("%f", &length);
printf("Enter width of the rectangle: ");
scanf("%f", &width);

/* Calculate perimeter of rectangle */


perimeter = 2 * (length + width);
/* Print perimeter of rectangle */
printf("Perimeter of rectangle = %f units ", perimeter);

return 0;
}

4. Write a C program to input length and width of a rectangle and find area of the
given rectangle.

Example
Input
Enter length: 5
Enter width: 10
Output

Area of rectangle = 50 sq. units

Solution -

#include <stdio.h>

int main()
{
float length, width, area;

/*
* Input length and width of rectangle
*/
printf("Enter length of rectangle: ");
scanf("%f", &length);
printf("Enter width of rectangle: ");
scanf("%d", &width);

/* Calculate area of rectangle */


area = length * width;

/* Print area of rectangle */


printf("Area of rectangle = %f sq. units ", area);
return 0;
}

5. Write a C program to input radius of a circle from user and find diameter,
circumference and area of the circle.

Example
Input
Enter radius: 10
Output

Diameter = 20 units
Circumference = 62.79 units
Area = 314 sq. units

Solution -

#include <stdio.h>

int main()
{
float radius, diameter, circumference, area;

/*
* Input radius of circle from user
*/
printf("Enter radius of circle: ");
scanf("%f", &radius);

/*
* Calculate diameter, circumference and area
*/
diameter = 2 * radius;
circumference = 2 * 3.14 * radius;
area = 3.14 * (radius * radius);

/*
* Print all results
*/
printf("Diameter of circle = %.2f units \n", diameter);
printf("Circumference of circle = %.2f units \n", circumference);
printf("Area of circle = %.2f sq. units ", area);

return 0;
}

6. Write a C program to input length in centimeter and convert it to meter and


kilometer.

Example
Input
Enter length in centimeter = 1000
Output

Length in meter = 10 m
Length in kilometer = 0.01 km

Solution -
#include <stdio.h>

int main()
{
float cm, meter, km;

/* Input length in centimeter from user */


printf("Enter length in centimeter: ");
scanf("%f", &cm);

/* Convert centimeter into meter and kilometer */


meter = cm / 100.0;
km = cm / 100000.0;

printf("Length in Meter = %.2f m \n", meter);


printf("Length in Kilometer = %.2f km", km);

return 0;
}
7. Write a C program to input temperature in Centigrade and convert to
Fahrenheit.

Example
Input
Enter temperature in Celsius = 100
Output

Temperature in Fahrenheit = 212 F

Solution -

#include <stdio.h>

int main()
{
float celsius, fahrenheit;

/* Input temperature in celsius */


printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);

/* celsius to fahrenheit conversion formula */


fahrenheit = (celsius * 9 / 5) + 32;

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);

return 0;
}

8. Write a C program to input temperature in degree Fahrenheit and convert it to


degree Centigrade.

Example
Input
Temperature in fahrenheit = 205
Output

Temperature in celsius = 96.11 C


Solution -

#include <stdio.h>

int main()
{
float celsius, fahrenheit;

/* Input temperature in fahrenheit */


printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);

/* Fahrenheit to celsius conversion formula */


celsius = (fahrenheit - 32) * 5 / 9;

/* Print the value of celsius */


printf("%.2f Fahrenheit = %.2f Celsius", fahrenheit, celsius);

return 0;
}

9. Write a C program to input number of days from user and convert it to years,
weeks and days.

Example
Input
Enter days: 373
Output

373 days = 1 year/s, 1 week/s and 1 day/s

Solution -

#include <stdio.h>

int main()
{
int days, years, weeks;

/* Input total number of days from user */


printf("Enter days: ");
scanf("%d", &days);

/* Conversion */
years = (days / 365); // Ignoring leap year
weeks = (days % 365) / 7;
days = days - ((years * 365) + (weeks * 7));

/* Print all resultant values */


printf("YEARS: %d\n", years);
printf("WEEKS: %d\n", weeks);
printf("DAYS: %d", days);

return 0;
}

10. Write a C program to input two numbers from user and find their power using
pow() function.

Example
Input
Enter base: 5
Enter exponent: 2
Output

5 ^ 2 = 25

Solution -

#include <stdio.h>
#include <math.h> // Used for pow() function

int main()
{
double base, expo, power;

/* Input two numbers from user */


printf("Enter base: ");
scanf("%lf", &base);
printf("Enter exponent: ");
scanf("%lf", &expo);

/* Calculates base^expo */
power = pow(base, expo);

printf("%.2lf ^ %.2lf = %.2lf", base, expo, power);

return 0;
}

You might also like