Question 1
A 2 minute telephone call to Lexington, Virginia costs $1.15. Each additional minute costs $0.50. Write a program that takes the total length of a call in minutes as input and calculates and displays the cost.
The sample code below:
#include <stdio.h> int main() { int call_in_mins; float cost; printf("Call in mins: "); scanf("%d",&call_in_mins); if(call_in_mins > 2) { cost = 0.5 * (call_in_mins - 2) + 1.15; } else { cost = 1.15; } printf("Cost on your call will be: %.2f",cost); return 0; }
Question 2
Get the sum of 1^2 + 2^2 + 3^2 + … n^2, where n is the user’s input.
The sample code below:
#include <stdio.h> int main() { int upper_limit,i,sum=0; printf("Enter the upper limit: "); scanf("%d",&upper_limit); for(i=1;i<=upper_limit;i++) { //sum += (i*i); sum += pow(i,2); } printf("Sum = %d",sum); return 0; }