Sie sind auf Seite 1von 7

c program examples

Example 1 - C hello world program /* A very simple c program printing a string on screen*/
#include <stdio.h> main() { printf("Hello World\n"); return 0; }

Output of above program: "Hello World" Example 2 - c program to take input from user using scanf
#include <stdio.h> main() { int number; printf("Enter an integer\n"); scanf("%d",&number); printf("Integer entered by you is %d\n", number); return 0; }

Output: Enter a number 5 Number entered by you is 5 Example 3 - using if else control instructions
#include <stdio.h> main() { int x = 1; if ( x == 1 ) printf("x is equal to one.\n"); else printf("For comparison use == as = is the assignment operator.\n"); return 0; }

Output: x is equal to one.

Example 4 - loop example


#include <stdio.h> main() { int value = 1; while(value<=3) { printf("Value is %d\n", value); value++; } return 0; }

Output: Value is 1 Value is 2 Value is 3

Example 5 - c program for prime number


#include <stdio.h> main() { int n, c; printf("Enter a number\n"); scanf("%d", &n); if ( n == 2 ) printf("Prime number.\n"); else { for ( c = 2 ; c <= n - 1 ; c++ ) { if ( n % c == 0 ) break; } if ( c != n ) printf("Not prime.\n"); else printf("Prime number.\n"); } return 0; }

Example 6 - command line arguments


#include <stdio.h> main(int argc, char *argv[]) { int c; printf("Number of command line arguments passed: %d\n", argc); for ( c = 0 ; c < argc ; c++) printf("%d. Command line argument passed is %s\n", c+1, argv[c]); return 0; }

Above c program prints the number and all arguments which are passed to it.

Example 7 - print Fahrenheit-Celsius table


#include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature scale */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } }

Example 8 Convert Fahrenheit to Celsius and vice versa


#include <iostream>
int main() {

int ftemp; int ctemp;

cout << " Enter temperature in Fahrenheit to convert to degrees Celsius: "; cin >> ftemp;

ctemp = (ftemp-32) * 5 / 9; cout << "Equivalent in Celsius is: " << ctemp << endl;

cout <<" Enter temperature in Celsius to convert to degrees Fahrenheit: ";

cin >> ctemp; ftemp = ctemp*9/5 + 32;

cout << "Equivalent in Fahrenheit is: " << ftemp << endl; }

Example 9 - count characters in input


#include <stdio.h> /* count characters in input; */ main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); }

Example 10 Greatest of three numbers


#include <stdio.h> void main() { clrscr(); int a,b,c; printf("nt Enter the first number : "); scanf("%d",&a); printf("nt Enter the second number : "); scanf("%d",&b); printf("nt Enter the third number : "); scanf("%d",&c); if(a>b && a>c) printf("nt The greatest number is : %d ",a); if(b>a && b>c) printf("nt The greatest number is : %d ",b); if(c>a && c>b) printf("nt The greatest number is : %d ",c); getch(); }

Example 11 Do while example


#include <stdio.h> main() { int i = 10; do{ printf("Hello %d\n", i ); i = i -1; }while ( i > 0 ); }

Example 12 Break example


#include <stdio.h> main() { int i = 10; do{ printf("Hello %d\n", i ); i = i -1; if( i == 6 ) { break; } }while ( i > 0 ); }

Example 13 For loop

#include

<stdio.h> #include <conio.h> void main() { int a; clrscr(); for(i=0; i<5; i++) { printf("\n\t HAI"); } getch(); }

// 5 times

Example 14 Switch case


#include <stdio.h> int main() { int color = 1; printf("Please choose a color(1: red,2: green,3: blue):\n"); scanf("%d", &color); switch (color) { case 1: printf("you chose red color\n");

break; case 2: printf("you chose green color\n"); break; case 3: printf("you chose blue color\n"); break; default: printf("you did not choose any color\n"); } return 0; }

Example 15 - Continue program


* Program to demonstrate continue statement.

Creation Date : 09 Nov 2010 07:44:43 PM Author : www.technoexam.com [Technowell, Sangli] */ #include <stdio.h> #include <conio.h> void main() { int i; clrscr(); for(i=1; i<=10; i++) { if(i==6) continue; printf("\n\t %d",i); } getch(); }

// 6 is omitted

Example 16 - Using comments in a program


#include <stdio.h> main() { // Single line comment in c source code printf("Writing comments is very useful.\n"); /* * Multi line comment syntax * Comments help us to understand code later easily. * Will you write comments while developing programs ? */ printf("Good luck c programmer.\n"); return 0; }

Example 17 - Using goto in a program

/*

Program to demonstrate goto statement.

Creation Date : 09 Nov 2010 08:14:00 PM Author : www.technoexam.com [Technowell, Sangli] */ #include <stdio.h> #include <conio.h> void main() { int i=1, j; clrscr(); while(i<=3) { for(j=1; j<=3; j++) { printf(" * "); if(j==2) goto stop; } i = i + 1; } stop: printf("\n\n Exited !"); getch(); }

Das könnte Ihnen auch gefallen