Basic Coding Questions:
Exercise 1:
Q1. Write a programme to calculate area and perimeter of rectangle by accepting its length and breadth.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
float a,p,l,b;
clrscr();
printf("\n enter the length and breadth of rectangle");
scanf("%f%f",&l,&b);
a=l*b;
p=2*(l+b);
printf("\n the area of rectangle is %f and the perimeter is %f",a,p);
getch();
Output:
enter the length and breadth of rectangle
20(press enter)
10(press enter)
Explanation of the above code:
- Declaring of variables in data type float since length and breadth cannot always be integers.
- Taking input of length and breadth together by using scanf function.
- Substituting the values of length and breadth stored in l and b to find out area and perimeter.
- Displaying the result.
Q2. Write a programme to calculate average of 5 numbers by taking input from user.
Ans:
#include<stido.h>
#include<conio.h>
void main()
{
float n1,n2,n3,n4,n5,avg;
clrscr();
printf("\n enter the five numbers");
scanf("%f%f%f%f%f",&n1,&n2,&n3,&n4,&n5);
avg=(n1+n2+n3+n4+n5)/5;
printf("\n the average of five numbers is %f",avg);
getch();
}
Output:
enter the five numbers
10(press enter)
20(press enter)
30(press enter)
40(press enter)
50(press enter)
the average of five numbers is 30
Explanation of the above code:
- Declaring of the variables in data type float since average can be decimals.
- Scanning all five numbers from user together by using format specifier %f .
- Substituting of all five numbers in average formula .
- Displaying the result.
Q3. Write a programme to convert the inches into centimetres.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
float inch,cm;
clrscr();
printf("\n enter the length in inches");
scanf("%f",&inch);
cm=2.54*inch;
printf("\n the length in centimetres is %f",cm);
getch();
}
Output:
enter the length in inches
2(press enter)
the length in centimetres is 5.08
Explanation:
- Declaring of variables in data type float since length cannot always be an integer.
- Taking the input in inches from the user.
- Substituting the length in formula ( 1 inch = 2.54 cm).
- Displaying the result.
Thus now we know how some functions work in software and how to take multiple inputs and display multiple values in the same output.
To learn Basics of C Programming, click on link provided below:
Thanking You.
Hope You Had A Great Time!
Comments
Post a comment
If you have any doubts pls let me know