0% found this document useful (0 votes)
2 views

Class Examples 02

The document contains multiple C programming examples demonstrating various concepts such as calculating compound interest, factorials, basic arithmetic operations, and grade counting. Each example includes user input and output, showcasing loops, conditionals, and functions. The programs are designed for educational purposes, illustrating fundamental programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Class Examples 02

The document contains multiple C programming examples demonstrating various concepts such as calculating compound interest, factorials, basic arithmetic operations, and grade counting. Each example includes user input and output, showcasing loops, conditionals, and functions. The programs are designed for educational purposes, illustrating fundamental programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CE111 2015-2016

CLASSROOM EXAMPLES-2

#include <stdio.h>
#include <math.h>
#include <conio.h>

main(){
double amount;
double principal=1000.00;
double rate=0.05;
unsigned int year;

printf("%4s%21s\n", "Year", "Amount on deposit");

for(year=1;year<=10;++year){
amount=principal*pow(1.0+rate, year);
printf("%4u%21.2f\n", year, amount);
}
getch();
}

#include <stdio.h>
#include <conio.h>
#include <math.h>

main(){
int number;
int factorial=1;
int count;

printf("Enter a number(99 to end) : ");


scanf("%d", &number);
while(number!=99){
factorial=1;
for(count=1;count<=number;count++){
factorial*=count;
}
printf("%d! = %d", number,factorial);

printf("\n\nEnter a number(99 to end) : ");


scanf("%d", &number);
}
printf("\nYour program is ended!");
getch();
}
#include <stdio.h>
#include <conio.h>
main(){
int x,y;
int row, column;

printf("Enter row number: ");


scanf("%d", &x);
while(x!=-1){
printf("Enter column number: ");
scanf("%d", &y);

for(row=1;row<=x;row++){
for(column=1;column<=y;column++){
printf("*");
}
printf("\n");
}
printf("Enter row number: ");
scanf("%d", &x);
}
}

#include <stdio.h>
#include <conio.h>
main(){
int operation;
int x=5, y=10;

printf("Enter an operation (EOF to end) : ");

while( (operation=getchar())!=EOF ){
switch(operation){
case '+':
printf("You chosed additon!\nThe result is: %d\n", x+y);
break;
case '-':
printf("You chosed subtraction!\nThe result is: %d\n", x-y);
break;
case '*':
printf("You chosed multiplication!\nThe result is: %d\n", x*y);
break;
case '/':
printf("You chosed division!\nThe result is: %d\n", x/y);
break;
case '\n':
case '\t':
case ' ':
break;
default:
printf("Invalid entry. Try again!\n");
break;
}
}
}
#include <stdio.h>
#include <conio.h>

main()
{
int number;
int factorial=1;
int count;

for(number=1;number<=10;number++){
factorial=1;
for(count=1;count<=number;count++){
factorial=factorial*count;
}
printf("%d! = %d\n",number, factorial);
}
getch();
}

#include <stdio.h>
#include <conio.h>

int main( void )


{
unsigned int i; // outer counter
unsigned int j; // inner counter

printf( "%6s%11s", "Number", "Histogram" );


for(i=1;i<=10;i++){
printf("\n%6d ", i);
for(j=1;j<=i;j++){
printf("*");
}
}
getch();
}

#include <stdio.h>
#include <conio.h>

int main( void )


{
int grade; // current grade
unsigned int aCount = 0; // total a grades
unsigned int bCount = 0; // total b grades
unsigned int cCount = 0; // total c grades
unsigned int dCount = 0; // total d grades
unsigned int fCount = 0; // total f grades
double averageGrade; // average grade for class

// prompt user for grades


puts( "Enter the letter grades.");
puts( "Enter the EOF character to end input." );
// loop while not end of file
while ( ( grade = getchar() ) != EOF ) {

// determine which grade was input


switch ( grade ) {

case 'A': // grade was uppercase A


case 'a': // grade was lowercase a
++aCount; // update grade A counter
break; // exit switch

case 'B': // grade was uppercase B


case 'b': // grade was lowercase b
++bCount; // update grade B counter
break; // exit switch

case 'C': // grade was uppercase C


case 'c': // grade was lowercase c
++cCount; // update grade C counter
break; // exit switch

case 'D': // grade was uppercase D


case 'd': // grade was lowercase d
++dCount; // update grade C counter
break; // exit switch

case 'F': // grade was uppercase F


case 'f': // grade was lowercase f
++fCount; // update grade C counter
break; // exit switch

case '\n': // ignore newlines,


case '\t': // tabs,
case ' ': // and spaces in input
break; // exit switch

default: // catch all other characters


printf( "%s", "Incorrect letter grade entered." );
puts( " Enter a new grade." );
break; // optional, will exit switch anyway
} // end switch

} // end while

// output totals for each grade


puts( "\nThe totals for each letter grade are:" );
printf( "A: %u\n", aCount );
printf( "B: %u\n", bCount );
printf( "C: %u\n", cCount );
printf( "D: %u\n", dCount );
printf( "F: %u\n", fCount );

// calculate average grade


averageGrade =
( double )( 4 * aCount + 3 * bCount + 2 * cCount + dCount ) /
( aCount + bCount + cCount + dCount + fCount );

// output appropriate message for average grade


if ( averageGrade > 3.5 ) {
puts( "Average grade is A" );
} // end if
else if ( averageGrade > 2.5 ) {
puts( "Average grade is B" );
} // end else if
else if ( averageGrade > 1.5 ) {
puts( "Average grade is C" );
} // end else if
else if ( averageGrade > 0.5 ) {
puts( "Average grade is D" );
} // end else if
else {
puts( "Average grade is F" );
} // end else
getch();
} // end main

You might also like