Question 1: Write a program to accept five numbers from keyboard and find their sum and average.
Solution:
Logic--> Here we take five integer number from
user simultaneously and add them after getting input from user and also find average
(average=total sum/total number)
Step 1: Input a,b,c,d,e random number.
Step 2: add them and store in new location
"sum"
Step 3: find average dividing sum by total number
(e.g. sum/5)
Step 4: finally display sum and average to user.
Program -->
#include<stdio.h>
#include<conio.h>
void main()
{int a,b,c,d,e,Sum,Average;
clrscr();
printf("input any five numbers
randomly");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
Sum=a+b+c+d+e;
Average=sum/5;
printf("sum of numbers=%d",Sum);
printf("\n Average=%d",Average);
getch();
}
Output: Input any five number randomly=2 10 5 15
22
Sum of numbers= 54
Average=10.8
Question 2: Write a program to input Principal (P), Time (T) and Rate of interest (R) and calculate the Simple interest.
Solution:
Logic --> Here, we input principal, time and
rate of interest from user in various data type because rate of interest some
time in decimal too and calculate simple interest by formula. (SI=PTR/100).
Program -->
#include<stdio.h>
#include<conio.h>
void main()
{int P,T;
float R;
float SI;
clrscr();
printf("Input value of P,T and R");
scanf("%d,%d,%f",&P,&T,&R);
SI= (float)(P*T*R)/100;
printf("Simple Interest=%d",SI);
getch();
}
Output: Input value of P,T and R...1000 2 3.5
Simple Interest=70
Note: In above program, we used "float"
data type in bracket in step 10. The float we used while encountering a program
is to use change value of result in float.
Question 3: Write a program to find sum of digits of four digit numbers.
Solution:
Logic --> Here, we take four digit number
like1234, 9876, etc. Important thing we consider here is how to find sum of
four digits however we take all four digits input at once. So here we break
each digits individually and store each one in separate location. Here we have
some four digits in "n" location. We break one digit and store in
some location as "a" and store 3 digit in same location "n"
replacing previous one, again we break "n" to get next digit.
once we get next digit, we store in "b" and remaining 2 digit in same
location "n" and step to break digits is go on as describe earlier
and storing location is different. Finally we altogether get four digits individually in different location and step to break we have at most 7...and thus
we find our resultant sum of 4 digits.
Program -->
#include<stdio.h>
#include<conio.h>
void main()
{int n,a,b,c,d,Sum;
clrscr();
printf("Input any four digit numbers");
scanf("%d",&n);
a=n%10;
n=n/10;
b=n%10;
n=n/10;
c=n%10;
n=n/10;
d=n%10;
Sum=a+b+c+d;
printf(sum of digits=%d", Sum);
getch();
}
Output: Input any four digit numbers...1234
Sum of digits=10
Note: "%" Modular division takes
quotient as result in division. "/" Simple Division takes remainder
as result in division.
Question 4: Write a program to input any 5 digit number and find the reverse.
Solution:
Logic --> As we mention in earlier program how
to find digits individually while getting sum and now here we have some extra
more logic behind it. Same process for dividing each digits individually and as
we get it we have to find reverse. Let we have 'abcde" five digit number
and we break each digit individually. Now to find reverse we see that first
digit "a" is in ten thousand position and last digit "e "
in one’s position. We have to do a step as below:
Step 1: Take last digit "e" and
multiply by ten thousand (10000) and now for reverse it comes in first
position.
Step 2: Take second-last digit "d" and
multiply by one thousand (1000) and now for reverse it comes in second
position.
Step 3: Take third-last digit "c" and
multiply by one hundred (100) and now for reverse it comes in third position.
Step 4: Take second digit "b" and
multiply by tens (10) and now for reverse it comes in fourth or second last position.
Step 5: Take first digit "a" and
multiply by ones (1) and now for reverse it comes in fifth or last position.
Once we have done all above step, sum up all
resultant value we get reverse number.
Program -->
#include<stdio.h>
#include<conio.h>
void main()
{int n,a,b,c,d,e,Reverse;
clrscr();
printf("Input any five digit numbers");
scanf("%d",&n);
a=n/10000;
n=n%10000;
b=n/1000;
n=n%1000;
c=n/100;
n=n%100;
d=n/10;
n=n%10;
e=n/1;
Reverse=e*10000+d*1000+c*100+b*10+a*1;
printf("Reverse of five digit
numbers=%d", Reverse);
getch();
}
Output: Input any five digit numbers...12345
Reverse of five digit numbers=54321
No comments:
Post a Comment