Archive for August, 2010


Four Operations

C Code for Addition Mutiplication Subtraction and Division using letters as Inputs.

#include <stdio.h>

int main(void)

{

int num1, num2;

int ans;

char opr;

printf(“\nThis programs calculates for the Sum, Difference, Product and Quotient given two numbers.”);

printf(“\n”);

printf(“\n\t1. A for Addition.”);

printf(“\n\t1. B for Subtraction.”);

printf(“\n\t1. C for Multiplication.”);

printf(“\n\t1. D for Division.”);

printf(“\n”);

printf(“\nEnter first number:”);

scanf(“%i”, &num1);

printf(“\n”);

printf(“\nEnter second number:”);

scanf(“%i”, &num2);

printf(“\n”);

printf(“\nEnter you choice of operation:”);

scanf(“%c”, &opr);

opr = getchar();

switch (opr)

{

case ‘A’: ans = num1 + num2;

printf(“\nThe sum is %i.”, ans);

break;

case ‘B’: ans = num1 – num2;

printf(“\nThe difference is %i.”, ans);

break;

case ‘C’: ans = num1 * num2;

printf(“\nThe product is %i.”, ans);

break;

case ‘D’: if (num2 == 0)

{

printf(“\nUndefined!”);

}

else

{

ans = num1 / num2;

}

break;

default : printf(“\nInvalid Code.”);

}

return 0;

}

#include <stdio.h>

int main(void)

{

float standing = 53;

float score, grade;

printf(“\nA program that determines whether a student passes (or fails) a subject given his/her last exam score.”);

printf(“\n”);

printf(“\nNote:”);

printf(“\n\tThe last exam has a total of 150 points.”);

printf(“\n\tThe student got a class standing of 53% prior to the last exam.”);

printf(“\n”);

printf(“\nEnter score for the last exam:”);

scanf(“%f”, &score);

if ((score < 0) || (score > 150))

{

printf(“\nINVALID SCORE!!”);

}

else

{

grade = (standing * 0.8) + (((score / 150)*100)*0.2);;

if (grade >= 60)

{

printf(“\nGrade = %f”, grade);

printf(“\n”);

printf(“\nPASS!!”);

}

else

{

printf(“\nGrade = %f”, grade);

printf(“\n”);

printf(“\nFAIL!!”);

}

}

return 0;

}

Age (Problem 7)

#include <stdio.h>

int main(void)

{

int month, year;

int agemonth, ageyear;

printf(“This program determines the exact age of a person this month given the person’s birthday.\n”);

printf(“\n”);

printf(” 1.January 4.April 7.July 10.October\n”);

printf(” 2.February 5.May 8.August 11.November\n”);

printf(” 3.March 6.June 9.September 12.December\n”);

printf(“\n”);

printf(“\n”);

printf(“\nPlease enter the number of your birthmonth (ie.1 for January):”);

scanf(“%i”,&month);

printf(“\nPlease enter the year of your birth (ie.1998):”);

scanf(“%i”,&year);

if (year<0)

{

printf(“\nINVALID YEAR!!”);

}

else

{

if ((month<=0) || (month>12))

{

printf(“\nINVALID MONTH!!”);

}

else

{

if ((month <= 8) && (month > 0))

{

agemonth = 8 – month;

ageyear = 2010 – year;

printf(“\n1Your age is %i years and %i months.”, ageyear, agemonth);

}

else

{

agemonth = (12 – month) + 8;

ageyear = 2009 – year;

printf(“\n2Your age is %i years and %i months.”, ageyear, agemonth);

}

}

}

return 0;

}

#include <stdio.h>

int main(void)

{

int distance;

float fare;

printf(“\nThis program determines the appropriate fare given the kilometers travelled by a passenger.”);

printf(“\n”);

printf(“\nEnter your distance travelled:”);

scanf(“%i”, &distance);

if (distance < 0)

{

printf(“\nInvalid Number!”);

}

else

{

if ((distance > 0) && (distance <= 5))

{

printf(“\nJeepney fare is PhP 6.50.”);

}

else

{

if (distance > 5)

{

fare = (6.50+((distance-5)*0.75));

printf(“\nJeepney fare is %.2f.”,fare);

}

else

{

if (distance == 0)

{

printf(“Zero distance = Zero fare.”);

}

}

}

}

return 0;

}

#include <stdio.h>

int main(void)

{

float price,change;

printf(“\nA program that determines how much change a customer will get if he/she buys an item and he/she gives the salesclerk Php1000.”);

printf(“\n”);

printf(“\nNote: All items below 100 has a discount of 8 percent.”);

printf(“\n”);

printf(“\n”);

printf(“\nEnter the original price of the item:”);

scanf(“%f”,&price);

change = 1000-(price -(price*0.08));

if (price >= 1000)

{

printf(“We don’t have discount on items which are PhP 1000.00 and above.”);

}

else

{

if (price < 0)

{

printf(“INVALID NUMBER!”);

}

else

{

if ((price >= 0) && (price < 1000))

{

printf(“Your change is %.2f.”, change);

}

}

}

return 0;

}

Follow

Get every new post delivered to your Inbox.