Sei sulla pagina 1di 22

Cube Volume #include <stdio.h> /* This is a program that computes the volume of a cube based on its side length.

Ideas to demonstrate to the students: variable, integer types, integer range, de claration, assignment, printf, etc. -- Y. Mao */ int main(void) { long side; /* declare a short-typed integer */ long volume; /* declare a short-typed integer */ side=100; /* assignment */ volume=side*side*side; /*assignment */ /* volume=32768; */ printf("\n\n\nThe volume of the cube with side %d is %d\n\n\n", side, volume); return 0; } Cube Volume 2 #include <stdio.h> /* This is an modification of the earlier code, to demonstrate the use of scanf and float point numbers. */ int main(void) { /* declare necessary variables */ double side; double volume;

printf("\nplease input the side length of the cube: \n"); scanf("%lf", &side); /* for double typed number, we need %lf */ volume=side*side*side;

printf("The volume of the cube with side length %f is %f. \n\n\n", side, volume); return 0; } Print Float Point Numbers #include <stdio.h> int main(void) { float floatA=333.345; /* declaration and assignment */ float floatB=3.1e2; float floatC=33333333333; double doubleA=333.345; double doubleB=3.1e2; double doubleC=33333333333;

printf("\n\n\nfloatA is %152.4f\nfloatB is %15.4f\nfloatC is %15.4f\n\n\n\n", floatA, floatB, floatC); printf("\n\n\nfloatA is %5.2f\nfloatB is %5.2f\nfloatC is %5.2f\n\n\n\n", floatA, floatB, floatC);

printf("\n\n\ndoubleA is %15.4f\ndoubleB is %15.4f\ndoubleC is %15.4f\n\n\n\n", doubleA, doubleB, doubleC); printf("\n\n\ndoubleA is %5.2f\ndoubleB is %5.2f\ndoubleC is %5.2f\n\n\n\n", doubleA, doubleB, doubleC);

return 0; } Size of Types #include <stdio.h>

int main(void) { printf("The size is %d bytes\n", (int) sizeof(int)); return 0; } Arithmetics #include <stdio.h> #define PI 3.14

int main(void) { int myInt; double myDouble; double myDouble2; double myDouble3; double myDouble4; double myDouble5; double myDouble6; double myDouble7; myInt=10/4; myDouble=10.0/4; myDouble2=10/4; myDouble3= ((double) 10) /4; /* Casting int value 10 to a double type */ myDouble4= 2*5.0/4; myDouble5=2*3+5-4*2; myDouble6= 24 % 5; myDouble7=2*PI;

printf("myInt has value %d\n", myInt); printf("myDouble has value %f\n", myDouble); printf("myDouble2 has value %f\n", myDouble2); printf("myDouble3 has value %f\n", myDouble3); printf("myDouble4 has value %f\n", myDouble4); printf("myDouble5 has value %f\n", myDouble5); printf("myDouble6 has value %f\n", myDouble6); myInt++; /* this is the same as myInt=myInt+1 */ printf("myInt has value %d\n", myInt);

myInt--; printf("myInt has value %d\n", myInt); printf("myDouble7 has value %f\n", myDouble7);

return 0; } Play With Char #include <stdio.h> /* ASCII (American Standard Code for Information Interchange). */ int main(void) { char myChar; char myChar2; char myChar3, myChar4; char myChar5='\n'; /* \n is ONE char */ int myInt=98; myChar='a'; myChar2='b'; myChar3='A'; myChar4='B';

printf("myChar is: %c\n", myChar); printf("myChar is: %d\n", myChar); printf("myChar2 is: %d\n", myChar2); printf("myChar3 is: %d\n", myChar3); printf("myChar4 is: %d\n", myChar4); printf("myChar5 is: %d\n", myChar5); printf("myChar5 is: %c\n", myChar5); printf("myInt is:%c\n", (char)myInt); /* cautious on casting from a more precise number to a less precise one */ return 0; } Compute Pass Faill Grade #include <stdio.h>

/* Suppose that in an oral exam, there are three examiners, each will give a mark to the student taking the exam. The average of the marks will converted to a Pass/Fail grade: if the average is higher than 60, it is a Pass; otherwise it is a Fail. Note that if any mark is lower than 10 or higher than 100, it will be disregarded. This programs gets the three marks as the input from the keyboard, and prints out the corresponding Pass/Fail grade. */

int main(void) { float mark1; float mark2; float mark3; float avgMark; int numberOfValidMarks=3;

printf("Please enter the first mark: "); scanf("%f", &mark1); printf("Please enter the second mark: "); scanf("%f", &mark2); printf("Please enter the third mark: "); scanf("%f", &mark3);

if ((mark1<10)||(mark1>100)) { mark1=0; numberOfValidMarks--; } if ((mark2<10)||(mark2>100)) { mark2=0; numberOfValidMarks--; }

if ((mark3<10)||(mark3>100)) { mark3=0; numberOfValidMarks--; }

if (numberOfValidMarks!=0) { avgMark=(mark1+mark2+mark3)/numberOfValidMarks; printf("The average is %f\n", avgMark); if (avgMark>=60) { printf("The grade is Pass.\n"); printf("Congrats!\n"); } else printf("The grade is Fail.\n");

} else { printf("All three marks are invalid, you can take the exam again!\n"); } return 0; }

Check if Fibonacci #include <stdio.h> /* This program checks if a positive int is a Fibonacci number. */ int main(void) {

int N; /* the number to input */ int found=0; int curF=1; int prevF=1; int newVal; printf("input N\n"); scanf("%d", &N); while(curF<N) { newVal=curF+prevF; prevF=curF; curF=newVal; if (curF==N) { found=1; break; /* jump out of the loop ! */ } } /* end of while loop */ if (found==1) printf("It is a Fibonacci number\n"); else printf("It is not a Fibonacci number\n"); return 0; }

For Loop Basic #include <stdio.h> int main(void) { int i; for (i=0;i<3;i++) { printf("hello\n"); } return 0;

} While Loop Basic #include <stdio.h> int main(void) { int i=0; while (i<3) { printf("hello\n"); i++; } return 0; } Sum of 1 to N #include <stdio.h> /* This program computes the sum of integers from 1 to N, where N is the input from the keyboard. */ int main(void) { int N; int i; int sumOfAllNumbers;

sumOfAllNumbers=0; printf("please input N\n"); scanf("%d", &N); for (i=0;i<N;i++) { sumOfAllNumbers=sumOfAllNumbers+(i+1); } printf("The sum is %d\n", sumOfAllNumbers);

return 0; } Right Angle Obtuse or Acute #include <stdio.h> #include <math.h> /* This program takes as input the coordinates of three points on the x-y plane, and determines if the triangle formed by the three points is right-angled/acute/obtuse triangle. it is assumed that the three points indeed form a triangle. */ int main(void) { /* variable declaration */ double x1, y1, x2, y2, x3, y3, d12, d23, d31, a, b, c, d, eps; eps=1e-8; /* compute side lengths */ printf("input x1/n"); scanf("%lf", &x1); printf("input y1/n"); scanf("%lf", &y1); printf("input x2/n"); scanf("%lf", &x2); printf("input y2/n"); scanf("%lf", &y2); printf("input x3/n"); scanf("%lf", &x3); printf("input y3/n"); scanf("%lf", &y3); d12=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); d23=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)); d31=sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));

/* find the largest side, and assign it to c; assign the other sides to a and b */ if (d12>d23) /* compare a^2 + b^2= c^2 */ d=a*a+b*b-c*c; if ((d>eps)&&(d<eps)) { printf("this is a right-angled triangle. \n"); } else { if (d>0) printf("this is an acute triangle. \n"); else printf("this ") } }

Check if Prime Do While #include <stdio.h> #include <math.h> /* This program checks if a positive integer N (no less than 3) entered from keyboard is a prime number, by (less optimally) using every integer smaller than sqrt of N to divide N. */ int main(void) { int divisor=1; int divisible=0; int N; double quotient;

printf("input a positive integer N\n"); scanf("%d", &N);

do { divisor=divisor+1; quotient=((double) N)/divisor; if (quotient==(double)(N/divisor)) { divisible=1; break; }

}while(divisor<=sqrt(N)); /* check if divisor is smaller than sqrt(N) and if N is divisible by divisor */ if (divisible) printf("N is not a prime.\n"); else printf("N is a prime.\n"); return 0; } Do while Loop basic #include <stdio.h> int main(void) { int i=0; do { printf("hello\n"); i=i+1; }while(i<-1);

return 0; } Continue Example

#include <stdio.h> int main(void) { int i; for (i=0;i<10; i++) { if ((i>=5)&&(i<=7)) continue; /* also check what happens when I change "continue" to "break". */ printf("Value of i is %d\n", i); } return 0; } City Hall #include <stdio.h> /* This program simulates the services offerred in City Hall. The user is prompt to enter an integer based on the service he need. The program then gives instructions to the user. */ int main(void) { int go=1; int i; while (go) { printf("Enter an integer: 1-pay parking ticket; 2-renew driver license; 3renew sticker\n"); scanf("%d", &i);

switch(i) { case 1: printf("Get your ticket and credit card ready, go to counter 1\n"); go=0; break; case 2: printf("Fill Drivers License Renewal Form, go to counter 10\n"); go=0; break; case 3: printf("Bring your ownership, fill form XXX, go to counter 16\n"); go=0; break; default: printf("No such servcice.\n"); go=1; } } return 0; } Function Example #include <stdio.h> int increase(int x) { x=x+1; printf("inside the function, x=%d\n", x); return x; }

int main(void) { int x=10; printf("before calling the function, x=%d\n", x);

printf("The output of function increase is:%d\n", increase(x)); printf("after calling the function, x=%d\n", x);

return 0; } Print Sinc Table #include <stdio.h> #include <math.h> #define PI 3.1415926535897932

/* This program prints a table for function sinc(x), which is defined as sin (PI*x)/(PI*x) */

double sinc(double x) { double y; if (fabs(x)<0.00001) y=1; else y=sin(PI*x)/(PI*x); return y; }

int main(void) { /* double sinc(double); */ /* declaring the prototype of function sinc The declaration is not necessary if the definition of the function is before the main function */ int i;

printf("Table Entry\t\tTable Value\n"); for (i=0;i<30; i++) { printf("%f\t\t%f\n", i*0.1, sinc(i*0.1)); } return 0; } Function Example 2 #include <stdio.h> void func1(int i){ printf("Entered func1; the value of i is %d\n", i); i=i+3; printf("Leaving func1; the value of i is %d\n", i); }

int main(void) { int i=0; void func2(int); printf("Entered main; the value of i is %d\n", i); func1(i); func2(i); printf("Leaving main; the value of i is %d\n", i); return 0; }

void func2(int i){ printf("Entered func2; the value of i is %d\n", i); i=i+5; func1(i); printf("Leaving func2; the value of i is %d\n", i); }

Lab Registration #include <stdio.h>

/* This function registers a student to Lab 1 if it is not full. A flag for success registration is returned. A student number is entered as the argument. The returned int value flags the success of registration. */

int registerLab1(int studentNumber) { static currentNumOfStudents=0; int success=0; if (currentNumOfStudents<5){ /* add student number to this lab, supposedly */ currentNumOfStudents++; success=1; printf("You have successfully registered for lab 1. Currently there are %d students in this lab\n", currentNumOfStudents); } else printf("This lab is full\n"); return success; }

int registerLab2(int studentNumber) { static currentNumOfStudents=0; int success=0; if (currentNumOfStudents<5){ /* add student number to this lab, supposedly */

currentNumOfStudents++; success=1; printf("You have successfully registered for lab 2. Currently there are %d students in this lab\n", currentNumOfStudents); } else printf("This lab is full\n"); return success; }

int registerLab3(int studentNumber) { static currentNumOfStudents=0; int success=0; if (currentNumOfStudents<5){ /* add student number to this lab, supposedly */ currentNumOfStudents++; success=1; printf("You have successfully registered for lab 3. Currently there are %d students in this lab\n", currentNumOfStudents); } else printf("This lab is full\n"); return success; }

/* This program simulates the registrar for the students to register for lab sessions. There are in total 3 lab sessions, each can accommodate 5 stuents. A student is prompted to enter a number indicating the lab session he wish to sign up. If the lab is not full, he will be registered to that lab; otherwise he is prompted to do it again. The program stops when all 12 students have registered.

*/ int main(void) { int studentNumber, sessionNumber; int numberOfRegisteredStudents=0;

while(numberOfRegisteredStudents<12) { printf("enter your student number\n"); scanf("%d", &studentNumber); printf("enter the lab session number\n"); scanf("%d", &sessionNumber); switch(sessionNumber) { case 1: numberOfRegisteredStudents=numberOfRegisteredStudents+registerLab1(stude ntNumber); break; case 2:

numberOfRegisteredStudents=numberOfRegisteredStudents+registerLab2(stude ntNumber); break; case 3:

numberOfRegisteredStudents=numberOfRegisteredStudents+registerLab3(stude ntNumber); break; default: printf("Wrong Lab session entered.\n");

} return 0; }

Function example 3 #include <stdio.h> void func1(void){ static int counter=0; counter++; printf("Enter func1 for the %d-th time\n", counter);

int main(void) { void func2(void);

printf("Entered main\n"); func1(); func2(); printf("Leaving main\n"); return 0; }

void func2(void){

static int counter=0; counter++; printf("Enter func2 for the %d-th time\n", counter);

func1(); printf("Leaving func2\n"); }

Check if Fibonacci #include <stdio.h> /* This program checks if a positive int is a Fibonacci number. */ int main(void) { int N; /* the number to input */ int found=0; int curF=1; int prevF=1; int newVal; printf("input N\n"); scanf("%d", &N); while(curF<N) { newVal=curF+prevF; prevF=curF; curF=newVal; if (curF==N) { found=1; break; /* jump out of the loop ! */ } } /* end of while loop */ if (found==1) printf("It is a Fibonacci number\n"); else

printf("It is not a Fibonacci number\n"); return 0; } Function Example 4 #include <stdio.h> void dumFunc(void) { static int counter=0; int copyOfCounter; counter++; copyOfCounter=counter; printf("entered dumFunc: counter = %d\n", counter); if (counter<3) dumFunc(); printf("leaving dumFunc: copy of counter= %d\n", copyOfCounter); } int main(void) { dumFunc(); return 0;

} Compute Factorial #include <stdio.h> int main(void) { int n; int factorial(int); printf("enter integer n\n"); scanf("%d", &n); printf("The factorial of %d is %d\n", n, factorial(n));

return 0; }

int factorial(int n) {

int out=1; int i; /* if (n>1) out=n*factorial(n-1); else out=1; */ for (i=n; i>1; i--) { out=out* i; } return out;

Potrebbero piacerti anche