Saturday 17 November 2012

Programming with C Language - More simple C programs

Programming with C1.4 More simple C programs
More simple C programs Program 1.1 Area of a circle Here is an elementary C program that reads in the radius of a circle, calculates the area and then writes the calculated result. #include <stdio.h>    /* Library file access */ /* program to calculate the area of a circle */    /* Title (Comment) */ main()    /* Function heading */ { float radius, area;    /*Variable declarations */ printf("Radius=?");    /* Output statement(prompt) */ scanf("%f", &radius);    /* Input statement */ area=3.14159*radius*radius;    /* Assignment statement */ printf("Area=%f",area);    /* Output statement */ } Program 1.2 Print a few numbers Here is a program to illustrate a simple loop #include <stdio.h> /* print a few numbers, to illustrate a simple loop */ main() { int i; for(i = 0; i < 10; i = i + 1)                /* Looping statement */     printf("i is %d\n", i); return 0; } Program 1.3: Program to add two numbers #include <stdio.h> main() { int i,j,k; // Defining variables i = 6; // Assign values j = 8; k = i + j; printf("sum of two numbers is %d \n",k); // Printing results }

No comments:

Post a Comment