C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

C Programming Introduction Examples

C Program to Print a Sentence

C Program to Print a Integer Entered by a User

C Program to Add Two Integers Entered by User

C Program to Multiply two Floating Point Numbers

C Program to Find ASCII Value of Character Entered by User

C Program to Find Quotient and Remainder of Two Integers Entered by User

C Program to Find Size of int, float, double and char of Your System

C Program to Demonstrate the Working of Keyword long

C Program to Swap Two numbers Entered by User

index-22_1.jpg

C Programming if, if..else and Nested if...else Statement

Decision making are needed when, the program encounters the situation to choose a particular statement among many statements. In C, decision making can be performed with following two statements.

1. if...else statement

2. switch statement

if statement syntax

if (test expression){

statement/s to be executed if test expression is true;

}

If the test expression is true then, statements for the body if, i.e, statements inside parenthesis are executed. But, if the test expression is false, the execution of the statements for the body of if statements are skipped.

Flowchart of if statement

Example of if statement

Write a C program to print the number entered by user only if the number entered is negative.

#include <stdio.h>

int main(){

int num;

printf("Enter a number to check.\n");

scanf("%d",&num);

if(num<0) /* checking whether number is less than 0 or not. */

printf("Number=%d\n",num);

/*If test condition is true, statement above will be executed, otherwise it will not be

executed */

printf("The if statement in C programming is easy.");

return 0;

}

index-23_1.jpg