Subjective Assignment 01 Basic Programs Ans
Subjective Assignment 01 Basic Programs Ans
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);
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;
}
Example
Input
Enter length: 5
Enter width: 10
Output
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);
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
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);
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;
}
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;
return 0;
}
7. Write a C program to input temperature in Centigrade and convert to
Fahrenheit.
Example
Input
Enter temperature in Celsius = 100
Output
Solution -
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
return 0;
}
Example
Input
Temperature in fahrenheit = 205
Output
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
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
Solution -
#include <stdio.h>
int main()
{
int days, years, weeks;
/* Conversion */
years = (days / 365); // Ignoring leap year
weeks = (days % 365) / 7;
days = days - ((years * 365) + (weeks * 7));
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;
/* Calculates base^expo */
power = pow(base, expo);
return 0;
}