logics of programming
TRANSCRIPT
-
7/31/2019 Logics of Programming
1/7
c program examplesExample 1 - C hello world program/* A very simple c program printing a string on screen*/
#include
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
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 number5Number entered by you is 5
Example 3 - using if else control instructions
#include
main(){
int x = 1;
if ( x == 1 )printf("x is equal to one.\n");
elseprintf("For comparison use == as = is the assignment operator.\n");
return 0;
}
Output:x is equal to one.
-
7/31/2019 Logics of Programming
2/7
Example 4 - loop example
#include
main(){
int value = 1;
while(value
-
7/31/2019 Logics of Programming
3/7
Example 6 - command line arguments
#include
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 /* print Fahrenheit-Celsius tablefor 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
-
7/31/2019 Logics of Programming
4/7
cout > ftemp;
ctemp = (ftemp-32) * 5 / 9;
cout b)printf("nt The greatest number is : %d ",c);getch();}
-
7/31/2019 Logics of Programming
5/7
Example 11 Do while example
#include
main(){
int i = 10;
do{printf("Hello %d\n", i );i = i -1;
}while ( i > 0 );}
Example 12 Break example
#include
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 #include void main(){
int a;
clrscr();for(i=0; i
-
7/31/2019 Logics of Programming
6/7
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 #include void main(){
int i;clrscr();for(i=1; i
-
7/31/2019 Logics of Programming
7/7
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 #include void main(){
int i=1, j;clrscr();while(i