[c]Questions and exercises

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;
}
Advertisements
This entry was posted in General stuffs. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s