Try this (Tutorial 08 & 09 & 10)

Write a program to evaluate the polynomial shown here:  

3x 3 - 5x 2 + 6 for x = 2.55  

#include<stdio.h>
int main()
{
float x = 2.55;
printf("Answer is %.2f\n", (3*(x*x*x)-5*(x*x)+6));
return 0;
}

Write a program that evaluates the following expression and displays the results (remember to use exponential format to display the result):  

(3.31 × 10-8 × 2.01 × 10-7) / (7.16 × 10-6 + 2.01 × 10-8) 

#include<stdio.h>
int main()
{
/* (3.31 × 10-8 × 2.01 × 10-7) / (7.16 × 10-6 + 2.01 × 10-8) */
float result = (3.31 * 10 - 8 * 2.01 * 10 - 7) / (7.16 * 10 - 6 + 2.01 * 10 - 8);
printf("Answer is %.2f\n", result);
return 0;
}

To round off an integer i to the next largest even multiple of another integer j, the following formula can be used:

Next_multiple = i + j - i % j  

#include<stdio.h>
int main()
{
int i, j, next_multiple;
printf("Enter round off an integer : ");
scanf("%i", &i);
printf("Enter integer value : ");
scanf("%i", &j);
next_multiple = i + j - i % j;
printf("Answer is %i\n", next_multiple);
return 0;
}

Write a program to input the radius of a sphere and to calculate the volume of the sphere.

Volume = 4/3*pi*radius3  

#include<stdio.h>
int main()
{
const float PI = 3.1415;
float radius, volume = 0;
printf("Enter Radius : ");
scanf("%f", &radius);
volume = 1.333*PI*(radius*radius*radius);
printf("Volume of the sphere : %.2f\n", volume);
return 0;
}

Write a program to input your mid term marks of a subject marked out of 30 and the final exam marks out of 100. Calculate and print the final results. Final Result = Mid Term + 70% of Final Mark  

#include<stdio.h>
int main()
{
float midExam, finalExam, finalMarks;
printf("Enter your MID Examination Marks : ");
scanf("%f", &midExam);
printf("Enter your Final Examination Marks : ");
scanf("%f", &finalExam);
finalMarks = midExam + (finalExam * 0.7);
printf("Your Final Examination Marks : %6.2f\n", finalMarks);
return 0;
}

Input the source file and destination file name as command line arguments and print the following message to standard output: “Copy the details in to at 23.59.59”  

#include<stdio.h>
#include<time.h>

int main(int argc, char *argv[])
{
time_t t = time(NULL);
    struct tm tm = *localtime(&t);
    printf("Copy the details in %s to %s at %02d:%02d:%02d\n", argv[1], argv[2], tm.tm_hour, tm.tm_min, tm.tm_sec);
return 0;
}



Part ii - Tutorial 9 

Swap two values stored in two different variables.

#include<stdio.h>
int main()
{
int val1, val2, temp;
printf("Enter Two Values\n");
scanf("%i %i", &val1, &val2);
temp = val1;
val1 = val2;
val2 = temp;
printf("After the swap values %i %i", val1, val2);
return 0;
}

Check whether an entered number is negative, positive or zero. 

#include<stdio.h>
int main()
{
int val1;
printf("Enter Value : ");
scanf("%i", &val1);
if(val1 > 0)
printf("You entered number is greater than 0.\n");
else if(val1 < 0)
printf("You entered number is less than 0.\n");
else
printf("You entered 0.\n");    
return 0;
}


Check whether an entered year is leap year or not. 

#include<stdio.h>

int main()
{
int year;
printf("Enter year : ");
scanf("%i", &year);
printf("%i is %s\n", year, ((year%4==0)?"Leap year":"not a leap year"));
return 0;
}

Write a program that asks the user to type in two integer values at the terminal. Test these two numbers to determine if the first is evenly divisible by the second, and then display an appropriate message at the terminal.  

#include<stdio.h>

int main()
{
int n1, n2;
printf("Type in two integer values\n");
scanf("%i %i", &n1, &n2);
if(n1%n2==0)
{
printf("The first number is evenly divisible by the second number");
}
else
{
printf("The first number isn't evenly divisible by the second number");
}  
return 0;
}

Write a program that accepts two integer values typed in by the user. Display the result of dividing the first integer by the second, to three-decimal-place accuracy. Remember to have the program check for division by zero.

#include<stdio.h>

int main()
{
float n1, n2;
printf("Type in two integer values you want to divide\n");
scanf("%f %f", &n1, &n2);
if(n2==0)
{
printf("Undefine divide by 0\n");
}
else
{
printf("Answer is %.3f\n",(n1/n2));
}  
return 0;
}

Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types in 932, the program should display nine three two. Remember to display “zero” if the user types in just a 0. 

#include<stdio.h>

int main()
{
int n, counter=0;
int number[20];
printf("Type in Number : ");
scanf("%i", &n);
while(n!=0)
{
number[counter] = n % 10;
n /= 10;
counter++;
}
for(--counter; counter>=0; --counter)
{
switch(number[counter])
{
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
default:
printf("Zero ");
break;
}
}
printf("\n");
return 0;
}

Input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: 
a. Percentage >= 90% : Grade A 
b. Percentage >= 80% : Grade B 
c. Percentage >= 70% : Grade C 
d. Percentage >= 60% : Grade D 
e. Percentage >= 40% : Grade E 
f. Percentage < 40% : Grade F  

#include<stdio.h>
int main(void)
{
float physics, chemistry, bio, mathematic, computer, avg;
printf("Physics marks : ");
scanf("%f", &physics);

printf("Chemistry marks : ");
scanf("%f", &chemistry);

printf("Biology marks : ");
scanf("%f", &bio);

printf("Mathematics marks : ");
scanf("%f", &mathematic);

printf("Computer marks : ");
scanf("%f", &computer);

avg = (physics + chemistry + bio + mathematic + computer)/5;
printf("Grade %c\n", (avg>=90? 'A' : avg>=80? 'B' : avg>=70? 'C' : avg>=60? 'D' : avg>=40? 'E' : 'F' ));

/*
if(avg >= 90)
printf("Grade A\n");
else if(avg >= 80)
printf("Grade B\n");
else if(avg >= 70)
printf("Grade C\n");
else if(avg >= 60)
printf("Grade D\n");
else if(avg >= 40)
printf("Grade E\n");
else
printf("Grade F\n");
*/

return 0;
}

Input basic salary of an employee and calculate its Gross salary according to following: (note: HRA and DA are allowances) 
a. Basic Salary <= 10000 : HRA = 20%, DA = 80% 
b. Basic Salary <= 20000 : HRA = 25%, DA = 90% 
c. Basic Salary > 20000 : HRA = 30%, DA = 95%  

#include<stdio.h>
int main(void)
{
float basic, gross;
printf("Type in Basic Salary : ");
scanf("%f", &basic);
printf("Gross Salary %.2f\n", basic +(basic <= 10000? (basic*0.2)+(basic*0.8) : basic <= 20000? (basic*0.25)+(basic*0.9) : (basic*0.3)+(basic*0.95)));
return 0;
}


Input electricity unit charges and calculate total electricity bill according to the given condition: 

a. For first 50 units Rs. 0.50/unit 
b. For next 100 units Rs. 0.75/unit 
c. For next 100 units Rs. 1.20/unit 
d. For unit above 250 Rs. 1.50/unit 
e. An additional surcharge of 20% is added to the bill  

#include<stdio.h>
int main(){

int units;
float bill;
printf("Enter the units : ");
scanf("%i", &units);
bill = (units<=50?units*0.5:units<=150?((units-50)*0.75)+25:units<=250?((units-150)*1.2)+100:((units-250)*1.5)+220)*1.2;
printf("Total bill : %.2fLKR\n", bill);

return 0;
}

Find the number of separate Notes and coins required to represent a given monetary value. 

E,g, 2700 required 1  2000 note, 1 500 note and 2100 notes.

#include<stdio.h>
int main(){
int value;
printf("Enter Monetary Value : ");
scanf("%i", &value);
if(value>=5000){
printf("%i -> 5000 Notes\t", (value/5000));
value%=5000;
}
if(value>=2000){
printf("%i -> 2000 Notes\t", (value/2000));
value%=2000;
}
if(value>=1000){
printf("%i -> 1000 Notes\t", (value/1000));
value%=1000;
}
if(value>=500){
printf("%i -> 500 Notes\t", (value/500));
value%=500;
}
if(value>=100){
printf("%i -> 100 Notes\t", (value/100));
value%=100;
}
if(value>=50){
printf("%i -> 50 Notes\t", (value/50));
value%=50;
}
if(value>=20){
printf("%i -> 20 Notes\t", (value/20));
value%=20;
}
if(value>=10){
printf("%i -> 10 Coins\t", (value/10));
value%=10;
}
if(value>=5){
printf("%i -> 5 Coins\t", (value/5));
value%=5;
}
if(value>=2){
printf("%i -> 2 Coins\t", (value/2));
value%=2;
}
if(value==1){
printf("%i -> 1 Coins\t", value);
}
printf("\n");
return 0;
}


Comments

Popular posts from this blog

Tutorial 11 & 12

Format Modifiers (Format Specifiers)

Simple and basic C program code