INTRODUCTION TO C 1 N.Rajkumar
Steps in learning C Character Set Programs Instructions Tokens 2 N.Rajkumar
C Character Set C Character Set Execution Character Set Source Character Set Special Characters Digits Alphabets Escape Sequence White Spaces 3 N.Rajkumar
C Character Set (Cont) • Source Character Set – It is used to construct the statements in the program. • Executable Character Set – These characters are employed at the time of execution i.e. they have effects only when the program is being executed. 4 N.Rajkumar
Source Character Set Letters a to z ,A to Z Digits 0 to 9 Special Characters ! @ # $ % ^ & * ( ) _ - + = | { } [ ] etc,. White Spaces Blank Space ,Horizontal tab, New line, Vertical tab etc,. 5 N.Rajkumar
Special characters • Comma , • Period or dot . • Semicolon ; • Colon : • Apostrophe ‘ • Quotation mark “ • Exclamation mark ! • Vertical bar | • Back Slash • Tilde ~ • Underscore - • Dollar $ • Question mark ? 6 N.Rajkumar
• Ampersand & • Caret ^ • Asterisk * • Minus - • Addition + • Lesser than < • Greater than > • Parenthesis () • Bracket [] • Braces {} • Percentage % • Hash # • Equal to = • At the rate @ 7 N.Rajkumar
Executable Character Set Characters Escape Sequence Back Space b Horizontal Space t Vertical Space v Newline n 8 N.Rajkumar
C Tokens • The smallest element in the C language is the token. • It may be a single character or a sequence of characters. 9 N.Rajkumar
C Tokens (Cont) C Tokens Identifiers Eg:main, avg Keywords Eg: int, for operators Eg: + - Strings Eg: “ab” spI symbol Eg: # $ % Constants Eg:17, 15.5 10 N.Rajkumar
Executing a C Program Creating the Program Compilation Linking Execution 11 N.Rajkumar
Executing a C Program (Cont) • Enter the program in a C editor. • Save the program (File  Save) or F2. Use the extension .c for saving the file. Eg: sample.c • Compile the program(Compile  Compile) or Alt+F9. • Run the program(Run  Run) or Ctrl+F9. 12 N.Rajkumar
Structure of C program DOCUMENTATION SECTION PREPROCESSOR SECTION DEFINITION SECTION GLOBAL DECLARATION SECTION main() { Declaration part; Executable Part; } sub program section { Body of the subprogram; } 13 N.Rajkumar
• Documentation Section – It contains the comment lines. • Preprocessor Section – It is used to link library files. • Global Declaration Section – The Global declaration section comes at the beginning of the program and they are visible to all parts of the program. • Declaration Section – It describes the data to be used within the function. • Executable Part – It contains the valid statements. 14 N.Rajkumar
C Programs  C program may have many functions.  One and only one of the functions MUST BE named main.  main is the starting point for the program.  main and other functions in a program are divided into two sections, declaration section and statement section. 15 N.Rajkumar
Preprocessor Directives • Special instructions to the preprocessor that tells how to prepare the program for compilation • E.g: include : tells the processor to include information from selected libraries known as header files e.g. <stdio.h> 16 N.Rajkumar
Comments (Program documentation)  The compiler simply ignores comments when it translates the program into executable code.  To identify a comments, C uses opening /* and closing */ comment tokens. 17 N.Rajkumar
Comments (Cont)  Comments can appear anywhere in a program.  Comments are also found wherever it is necessary to explain a point about a code.  Comments cannot be nested in C i.e. you cannot have comments inside comments. 18 N.Rajkumar
C program /* Example program in C*/ Comments # include <stdio.h>Preprocessor Section #include<conio.h> Global Declaration void main () { Local declaration printf (“Hello World! n”); Statements getch(); } Output : Hello World 19 N.Rajkumar
C Tokens • Identifiers • Keywords • Constants • Operators • Special symbols 20 N.Rajkumar
Identifiers • Identifiers are names given to various program elements such as variables, functions and arrays etc,. • Eg: #define N 10 #define a 15 Here N and a are user defined identifiers. 21 N.Rajkumar
Rules for naming identifier • First character must be alphabetic or underscore. • Must consist only of alphabetic characters, digits, or underscores. • Only the first 31 characters of an identifier are significant and are recognized by the compiler. • Cannot use a keywords or reserved word (e.g. main, include, printf & scanf etc.). • No space are allowed between the identifiers etc,. • C is case sensitive, e.g. My_name  my_name. 22 N.Rajkumar
Examples of Valid and Invalid Names Valid Names Invalid Names a a1 $sum /* $ is illegal */ student_name stdntNm 2names /* Starts with 2 */ _aSystemName _anthrSysNm stdnt Nmbr /* no spaces */ TRUE FALSE int /* reserved word */ 23 N.Rajkumar
Variables • Variable is an identifier that is used to represent some specified type of information. • Eg: x=3 • Here x is variable. 24 N.Rajkumar
Keywords • It is a reserved words. • Cannot be used for anything else. • Examples: – int – while – for etc,. 25 N.Rajkumar
Keywords Auto register Continue Double typedef For Int Char signed Struct extern void Break return Default Else union Goto Long Const sizeof Switch Float do Case short If Enum unsigned Static While 26 N.Rajkumar
Constants • It is an entity whose value does not changes during the execution. • Eg: x=3 • Here 3 is a constant. 27 N.Rajkumar
Types • Numeric constants • Character constant 28 N.Rajkumar
Constants Constants Character Constants Numeric Constants Real Constant Integer Constant String Constant Single Character Constant 29 N.Rajkumar
Numeric constants Integer constants • It is formed using a sequence of digits. Decimal - 0 to 9 . Octal - 0 to 7. Hexa - 0 to 9 ,A to F Eg: 10,75 etc. 30 N.Rajkumar
Rules for defining Integer Constant • It must have atleast one digit. • Decimal point are not allowed. • No blank space or commas are allowed. • It can be either positive or negative. Etc,. 31 N.Rajkumar
Numeric constants Real constants • It is formed using a sequence of digits but it contain decimal point. • length, height, price distance measured in real number Eg: 2.5, 5.11, etc. 32 N.Rajkumar
Character constants Single character constant – A character constant is a single character they also represented with single digit or a single special symbol which is enclosed in single quotes. – Eg: ‘a’, ‘8’,’_’etc. 33 N.Rajkumar
Character constants String constants • String constant are sequence of characters enclosed with in double quote. • Eg: “Hello” ,”444”,”a” etc,. 34 N.Rajkumar
Operators • An operator is a symbol that specifies an operation to be performed on the operands. • Eg: a + b + is an operator. a,b are operands. 35 N.Rajkumar
Data Types  A Data type is the type of data that are going to access within the program. 36 N.Rajkumar
DATA TYPES DATA TYPES PRIMARY DERIVED USER DERIVED CHARACTER INTEGER REAL Array, Function, Pointer Structure, union …. char short int, int, long int Float, double long double 37 N.Rajkumar
integer  A number without a fraction part : integral number.  C supports three different sizes of the integer data type :  short int  int  long int 38 N.Rajkumar
Floating Point  A floating-point type is a number with a fractional part, e.g. 56.78  Floating point numbers are stored using 4 Byte.  Types  Float  Double  long double 39 N.Rajkumar
character • Character are generally stored using 8 bits(1 Byte) of the internal storage. 40 N.Rajkumar
void  The void type has no values and no operations.  Both the set of values and the set of operations are empty. 41 N.Rajkumar
Variable Declaration  To create a variable, you must specify the type and then its identifier :  Integer int a; a=10; int a=10; int a,b; a=10; b=5; int a=10,b=5; 42 N.Rajkumar
Variable Declaration  To create a variable, you must specify the type and then its identifier :  Float float a; a=10.5; float a=10.5; float a,b; a=10.5; b=5.5; float a=10.5,b=5.5; 43 N.Rajkumar
Variable Declaration  To create a variable, you must specify the type and then its identifier :  Character char a; a=‘x’; char a=‘x’; char a,b; a=‘x’; b=‘y’; char a=‘x’,b=‘y’; 44 N.Rajkumar
Variable Declaration  To create a variable, you must specify the type and then its identifier :  String char a[6]; a=“NAFEES”; char a[6]=“NAFEES”; Representation of string a N a[0] A a[1] F a[2] E a[3] E a[4] S a[5] 45 N.Rajkumar
Entire Data types in c: Data type Size(bytes) Range Format string Char 1 128 to 127 %c Unsigned char 1 0 to 255 %c Short or int 2 -32,768 to 32,767 %i or %d Unsigned int 2 0 to 65535 %u Long 4 -2147483648 to 2147483647 %ld Unsigned long 4 0 to 4294967295 %lu Float 4 3.4 e-38 to 3.4 e+38 %f or %g Double 8 1.7 e-308 to 1.7 e+308 %lf Long Double 10 3.4 e-4932 to 1.1 e+4932 %lf 46 N.Rajkumar
Basic C programs #include<stdio.h>//program to display given data #include<conio.h> void main() { int a=10; float b=10.88; char c=‘$’; char d[6]=“Nafees”; clrscr(); printf(“Integer value : %dn”,a); printf(“Float value : %fn”,b); printf(“Character : %cn”,c); printf(“String : %s”,d); getch(); } OUTPUT Integer value : 10 Float value : 10.88 Character : $ String : Nafees 47 N.Rajkumar
Basic C programs #include<stdio.h>//program to display given data from user #include<conio.h> void main() { int a; float b; char c; char d[6]; clrscr(); printf(“Enter a integer value : “); scanf(“%d”,&a); printf(“Enter a float value : “); scanf(“%f”,&b); printf(“Enter a character : “); scanf(“%c”,&c); printf(“Enter a string : “); scanf(“%s”,d); OUTPUT Enter a integer value :12 Enter a float value : 5.34 Enter a character : h Enter a string : Nafees Integer value : 12 Float value : 5.34 Character : h String : Nafees printf(“Integer value : %dn”,a); printf(“Float value : %fn”,b); printf(“Character : %cn”,c); printf(“String : %s”,d); getch(); } 48 N.Rajkumar
Types of Operator 1. Arithmetic operator 2. Relational operator 3. Logical operator 4. Assignment operator 5. Short hand assignment operator 6. Increment or decrement operator(unary) 7. Comma operator 8. Conditional operator 49 N.Rajkumar
Arithmetic operator • It is used to carry out arithmetic operations NAME OPERATOR ADDITION + SUBTRACTION - MULTIPLICATION * DIVISION / MODULUS (REMAINDER) % 50 N.Rajkumar
SAMPLE PROGRAM #include<stdio.h> // Header File #include <conio.h> void main ( ) /* main is the starting of every c program */ { int a=5,b=2; //Local Declaration clrscr( ); printf(“Addition : %dn”,(a+b)); printf(“Subtraction : %dn”,(a-b)); printf(“Multiplication : %dn”,(a*b)); printf(“Division : %dn”,(a/b)); printf(“Modulus(remainder): %d”,(a%b)); getch( ); } OUTPUT Addition : 7 Subtraction : 3 Multiplication : 10 Division :2 Modulus(remainder):1 51 N.Rajkumar
Division operator on Different Data Type Operation Result Example int/int int 5/2 = 2 int/real real 5/2.0 = 2.5 real/int real 5.0/2 = 2.5 real/real real 5.0/2.0 = 2.5 52 N.Rajkumar
Relational operator • It is used to compare two or more operands and returns 1 if true else return 0 NAME OPERATOR Less than < Less than or equal to <= Greater than > Greater than or equal to >= Equal to == Not equal to != 53 N.Rajkumar
Relational Operator… #include<stdio.h> #include <conio.h> void main() { clrscr(); printf(“n5!=3:%d”,(5!=3)); printf(“n5<=3:%d”,(5<=3)); printf(“n5>=3:%d”,(5>=3)); printf(“n5<7 :%d”,(5<7)); printf(“n5>4 :%d”,(5>4)); getch(); } OUTPUT 5!=3 : 1 5<=3 : 0 5>=3 : 1 5<7 : 1 5>4 : 1 54 N.Rajkumar
Logical operator • It is used to perform logical operations NAME OPERATOR AND && OR || NOT ! 55 N.Rajkumar
Sample program #include<stdio.h> #include <conio.h> void main ( ) { clrscr( ); printf(“n(5<=8)&&(4>=2):%d”,((5<=8)&&(4>=2))); printf(“n(5>=3)||(6<4):%d”,((5>=3)||(6<4))); printf(“n!(7==7):%d”,!(7==7)); getch( ); } OUTPUT (5<=8)&&(4>=2) : 1 (5>=3)||(6<4) : 1 !(7==7) : 0 56 N.Rajkumar
Assignment operator • It is used to assign a value or expression etc to a variable. • Eg: a =10.(assignment operator ‘=‘ assigns value 10 to variable a. a = b a = b + c etc,. 57 N.Rajkumar
Shorthand Assignment Operator 58 N.Rajkumar
Increment & Decrement Operator NAME OPERATOR Increment operator ++ Decrement operator -- Pre Increment Post Increment int a=10; ++a Now value of a is 11 int a=10; a++ Now value of a is 11 Pre decrement Post decrement int a=10; --a Now value of a is 9 int a=10; a-- Now value of a is 9 59 N.Rajkumar
Increment & Decrement Operator #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf(“Enter a & b value : n”); scanf(“%d%d”,&a,&b); printf(“Value of a & b before increment : %d %dn”,a,b); printf(“Value of a & b after increment : %d %dn”,a++,b++); printf(“Value of a & b after decrement : %d %dn”,a--,b—); getch(); } 60 N.Rajkumar
Increment & Decrement Operator OUTPUT Enter a & b value : 6 9 Value of a & b before increment : 6 9 Value of a & b after increment : 7 10 Value of a & b after decrement : 5 8 61 N.Rajkumar
Conditional Operator (or) Ternary Operator • It is used to checks the condition and execute the statement depending on the condition. • Eg: C = a > b ? a:b 62 N.Rajkumar
Sample Program #include<stdio.h> #include <conio.h> void main ( ) { int a=5,b=8,c; clrscr( ); c = a>b?a:b; //Conditional operator printf(" n The Larger Value is %d",c); getch( ); } OUTPUT The Larger Value is 8 63 N.Rajkumar
Output The Larger Value is 8 OUTPUT The Larger Value is 8 64 N.Rajkumar
Special Operator • comma operator ( , ) EX: int a,b; 65 N.Rajkumar
Expression • An expression represent data item such as variable, constant are interconnected using operators. • Eg: Expression C Expression a + b + c a + b + c a2+b2 a*a + b*b 66 N.Rajkumar
Precedence Operator High * , / , % Low + , - 67 N.Rajkumar
Type Conversion • Converting the type of an expression from one type to another type. Eg: x = (int)10.45 68 N.Rajkumar
Sample Program #include<stdio.h> #include <conio.h> void main ( ) { int c; clrscr( ); c=(int)10.45; printf("nOutput is:%d",c); getch( ); } OUTPUT Output is:10 69 N.Rajkumar
PROGRAM CONTROL STRUCTURES • Depending upon the sequence of the execution of statements, the control structures are categorized as follows: • Categories: • Sequential structure – In which instructions are executed in sequence. • Selection structure – In which instruction are executed based on the result of some condition. • Iteration structure – In which instruction are executed repeatedly. 70 N.Rajkumar
Sequential STRUCTURE • It allows the program to flow in sequence START Statement 1 Statement 2 Statement N STOP 71 N.Rajkumar
SELECTION STRUCTURE • It allows the program to make a choice from alternative paths. • C provide the following selection structures – if statement – if … else statement – Nested if … else statement 72 N.Rajkumar
if Statement Syntax if (condition is true) { Statements; } If condition False True Statements 73 N.Rajkumar
if…else Statement Syntax if (condition) { True statements; } else { False statements; } If Condition True False True statements False statements 74 N.Rajkumar
Nested if…else Condition 1 Statements Condition 2 Statements Condition 3 Statements Statements TRUE TRUE TRUE FALSE FALSE FALSE 75 N.Rajkumar
Nested if…else Syntax IF (condition1) { statements; } else if (condition2) { statements; } else if (condition3) { statements; } else { statements; } 76 N.Rajkumar
Iteration structure • It is used to execute some instructions several time based on some condition. – while – do…while – for 77 N.Rajkumar
while Loop Syntax . while (condition) { . Body of the loop; . } Body of The loop condition False True 78 N.Rajkumar
do…while Loop Syntax do { Body of the loop }while (condition); Body of The loop condition False True 79 N.Rajkumar
for loop Syntax for (initialization; test condition; Increment/Decrement) { Body of the loop } 80 N.Rajkumar
for loop Initialization condition False Body of the loop Inc / Decrement 81 N.Rajkumar
Nested for loop Syntax for (initi; cond; Inc/Dec) { for (initi; cond; Inc/Dec) { Body of the loop } } 82 N.Rajkumar
DECISION MAKING & BRANCHING • if statement • if else statement • Nested if else statement • switch statement • Conditional statement • goto statement • break statement • continue statement Selection statements Unconditional statements 83 N.Rajkumar
if statement (Simple if) Syntax if (condition is true) { Statements; } 84 N.Rajkumar
if statement (Simple if) #include<stdio.h> #include <conio.h> void main ( ) { int a; clrscr( ); printf("nEnter a number:"); scanf("%d",&a); if(a>10) { printf(" n a is greater than 10"); } getch( ); } OUTPUT Enter the number: 12 a is greater than 10 85 N.Rajkumar
if else statement Syntax if (condition is true) { Statements; } else { Statements; } 86 N.Rajkumar
if else statement #include<stdio.h> #include <conio.h> void main ( ) { int a; clrscr( ); printf("nEnter a number:"); scanf("%d",&a); if(a>10) { printf(" n a is greater than 10"); } else { printf(" n a is not greater than 10"); } getch( ); } OUTPUT Enter the number: 12 a is greater than 10 Enter the number: 1 a is not greater than10 87 N.Rajkumar
Nested if else statement Syntax if (condition is true) { Statements; } else if (condition is true) { Statements; } else if (condition is true) { Statements; } else { Statements; } 88 N.Rajkumar
Nested if else statement #include<stdio.h> #include<conio.h> void main() { int m1,m2,m3; float avg; printf("nEnter the marks:"); scanf("%d%d%d",&m1,&m2,&m3); avg=(m1+m2+m3)/3; printf("n The average is:%f",avg); printf("n The Grade is:"); if(avg>=60) { printf("First class"); } 89 N.Rajkumar
else if(avg>=50) { printf("Second class"); } else if(avg>=35) { printf("Third class"); } else { printf("Fail"); } getch(); } OUTPUT Enter the marks:65 75 70 The average is:70.00 The Grade is: First class 90 N.Rajkumar
Nested if else #include <stdio.h> //Program to find leap year #include<conio.h> void main() { int year; printf("Enter a year to check if it is a leap year : n"); scanf("%d", &year); if ( year%400 == 0) printf("%d is a leap year.n", year); else if ( year%100 == 0) printf("%d is not a leap year.n", year); else if ( year%4 == 0 ) printf("%d is a leap year.n", year); else printf("%d is not a leap year.n", year); getch(); } OUTPUT Enter a year to check if it is a leap year :2012 2012 is a leap year 91 N.Rajkumar
switch statement Syntax switch (expression) { case constant 1: block1; break; case constant 2: block2; break; . . default : default block; break; } Case 1 Case 2 Default case Switch 92 N.Rajkumar
switch statement #include<stdio.h> #include<conio.h> void main() { int a,b,c,n; clrscr(); printf("nEnter the value of a,b:n"); scanf("%d%d",&a,&b); printf("nMENU"); printf("n1.ADDn2.SUBn3.MULTIPLYn4.EXIT"); printf("nEnter the choice:"); scanf("%d",&n); 93 N.Rajkumar
switch(n) { case 1: c=a+b; printf("nThe result of Addition is:%d",c); break; case 2: c=a-b; printf("nThe result of Subtraction is:%d",c); break; 94 N.Rajkumar
case 3: c=a*b; printf("nThe result of Multiplication is:%d",c); break; case 4: exit(0); break; default: printf(“Wrong Choice”); } getch(); } OUTPUT Enter the value of a,b: 5 6 MENU 1.ADD 2.SUB 3.MULTIPLY 4.EXIT Enter the choice:1 The result of Addition is:11 95 N.Rajkumar
Conditional Statement • It uses conditional operator (?) #include<stdio.h> #include<conio.h> void main() { int n; printf(“Enter a number : “); scanf(“%d”,&n); (n%2==0)?printf(“even”):printf(“odd”); getch(); } OUTPUT Enter a number : 3 odd 96 N.Rajkumar
goto statement • When a goto statement is encountered, the control is transferred to the label. goto label; ………… ………… ………… label: ………… 97 N.Rajkumar
goto statement #include<stdio.h> #include<conio.h> void main() { int x; clrscr(); printf(“Enter a number to find odd or even no : “); scanf(“%d”,&x); if(x%2==0) goto even; else goto odd; 98 N.Rajkumar
goto statement even: printf(“%d is an even no”,x); return; odd: printf(“%d is an odd no”,x); return; OUTPUT Enter a number to find odd or even no :5 5 is an odd no 99 N.Rajkumar
#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf(“Enter two value to subtract : n”); scanf(“n%d%d”,&a,&b); if(a>b) { printf(“Subtracted value is : %dn”,(a-b)); } else { break; } } OUTPUT Enter two value to subtract : 5 3 Subtracted value is : 2 When break statement is encountered inside any block or loop, control automatically passes to the first statement after the block or loop break statement 100 N.Rajkumar
#include<stdio.h> #include<conio.h> void main() { int a; clrscr(); for(a=1;a<=10;a++) { if((a==5)||(a==7)) continue; else printf(“%dn”,a); } } OUTPUT 1 2 3 4 6 8 9 10 Continue statement terminates the current loop iteration & persist the loop with next iteration continue statement 101 N.Rajkumar
• Loop: –A loop is defined as a block of statements which are repeatedly executed for certain no of times. • Types of Loops in C –while loop –do while loop –for loop(simple & nested) Looping Statements 102 N.Rajkumar
• It is an entry controlled loop works only when the condition is true • Syntax: while (condition) { . Body of the loop; . } while loop 103 N.Rajkumar
#include<stdio.h> //program to print even no #include<conio.h> void main() { int n,i; printf(“Enter the limit : “); scanf(“%d”,&n); i=1; while (i<=n) { if(i%2==0) { printf(“%dt”,i); } i++; } } while loop OUTPUT Enter the limit : 10 2 4 6 8 10 104 N.Rajkumar
#include<stdio.h> //program to calculate factorial of given no #include<conio.h> void main() { int i=1,fact=1,n; printf("nEnter the Number:"); scanf("%d",&n); while(i<=n) { fact =fact *i; i++; } printf("n The value of %d! is:%d",n,fact); getch(); } while loop OUTPUT Enter the Number:3 The value of 3! is: 6 105 N.Rajkumar
#include<stdio.h> //program to reverse a number and to check #include<conio.h>// whether it is palindrome or not void main() { int n,rev=0;` clrscr(); printf(“Enter a number to reverse : “); scanf(“%d”,&n); while(n!=0) { rev=rev*10; rev=rev+(n%10); n=n/10; } printf(“Reverse of given no is : %dn”,rev); while loop OUTPUT Enter a number to reverse : 452 Reverse of given number is : 254 Not Palindrome if(n==rev) { printf(“Palindrome”); } else { printf(“Not Palindrome”); } getch(): } 106 N.Rajkumar
#include<stdio.h> //program to find sum of digits #include<conio.h> void main() { int r=0,sum=0,n; printf("nEnter the no:"); scanf("%d",&n); while(n>0) { r=n%10; sum=sum+r; n=n/10; } printf("sum of the digits is:%d",sum); getch(): } while loop OUTPUT Enter the no:156 sum of the digits is:12 107 N.Rajkumar
#include<stdio.h> //program to find armstrong number #include<conio.h> void main() { int r=0,sum=0,n,a; printf("nEnter the number:"); scanf("%d",&n); a=n; while(n>0) { r=n%10; sum=sum+r*r*r; n=n/10; } if(a==sum) printf("nIt is an armstrong number"); else printf("nIt is not an armstrong number"); getch(); } OUTPUT Enter the number:153 It is an armstrong number 108 N.Rajkumar
• It is an exit controlled loop. It proceeds its first iteration without checking the loop condition and proceeds next iteration by checking the condition. • Syntax: do { . Body of the loop; . }while (condition); do while loop 109 N.Rajkumar
#include<stdio.h> //program to print even no #include<conio.h> void main() { int n,i; printf(“Enter the limit : “); scanf(“%d”,&n); i=1; do { if(i%2==0) { printf(“%dt”,i); } i++; }while (i<=n); } do while loop OUTPUT Enter the limit : 10 2 4 6 8 10 110 N.Rajkumar
#include<stdio.h> //program to calculate factorial of given no #include<conio.h> void main() { int i=1,fact=1,n; printf("nEnter the Number:"); scanf("%d",&n); do { fact =fact *i; i++; }while (i<=n); printf("n The value of %d! is:%d",n,fact); getch(); } do while loop OUTPUT Enter the Number:3 The value of 3! is: 6 111 N.Rajkumar
do while loop //program to generate fibonacci series OUTPUT Enter the range : 10 0 1 1 2 3 5 8 #include<stdio.h> #include<conio.h> void main() { int a,b,c,r; clrscr(); printf(“Enter the range : “); scanf(“%d”,&r); a=0,b=1; printf(“%d”,a); printf(“n%d”,b); printf(“n”); c=0; do { c=a+b; if(c<=r) printf(“%dn”,c); a=b; b=c; }while(c<=r); getch(); } 112 N.Rajkumar
for loop Syntax: for (initialization; test condition; Increment/Decrement) { Body of the loop } 113 N.Rajkumar
#include<stdio.h> //program to generate number from 1 to n #include<conio.h> void main() { int i,n; printf("nEnter the limit : "); scanf("%d",&n); for(i=1;i<=n;i++) { printf(“%dn”,i); } getch(); } for loop OUTPUT Enter the limit : 5 1 2 3 4 5 114 N.Rajkumar
#include<stdio.h> //program to calculate factorial of given no #include<conio.h> void main() { int i,fact=1,n; printf("nEnter the Number:"); scanf("%d",&n); for(i=1;i<=n;i++) { fact =fact *i; } printf("n The value of %d! is:%d",n,fact); getch(); } for loop OUTPUT Enter the Number:3 The value of 3! is: 6 115 N.Rajkumar
#include<stdio.h> //program to generate given multiplication table #include<conio.h> void main() { int i,tab,n,res; printf("nEnter the multiplication table:"); scanf("%d",&tab); printf("nEnter the limit of multiplication table:"); scanf("%d",&n); for(i=1;i<=n;i++) { res=i*tab; printf("%d * %d : %dn”,i,tab,res); } getch(); } for loop OUTPUT Enter the multiplication table : 2 Enter the limit of multiplication table : 4 1 * 2 : 2 2 * 2 : 4 3 * 2 : 6 4 * 2 : 8 116 N.Rajkumar
#include<stdio.h>//program to calculate student marks & average using for loop #include<conio.h> void main() { int n,s1,s2,s3,s4,s5,tot; float avg; clrscr(); printf(“Enter no of students : ”); scanf(“%d”,&n); printf(“Enter marks of five subjects : n”) for(i=1;i<=n;i++) { printf(“Enter student %d marksn”,i); scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5); tot=s1+s2+s3+s4+s5; avg=tot/5; printf(“Total : %dnAverage : %f”,tot,avg); } getch(); } for loop 117 N.Rajkumar
• Definition of prime number: – A natural number greater than one has not any other divisors except 1 and itself. In other word we can say which has only two divisors 1 and number itself. – For example: 5. Their divisors are 1 and 5. for loop 118 N.Rajkumar
#include<stdio.h>//program to check whether the given no is prime or not #include<conio.h> void main() { int n, i, c = 0; printf("Enter a number :"); scanf("%d", &n); for (i = 1; i <= n; i++) { if (n % i == 0) { c++; } } if (c == 2) printf(“Given no is a Prime number"); else printf(“Given no is not a Prime number"); getch(); } for loop OUTPUT Enter a number : 7 Given no is a Prime number 119 N.Rajkumar
Nested for loop Syntax for (initi; cond; Inc/Dec) { for (initi; cond; Inc/Dec) { Body of the loop } } 120 N.Rajkumar
#include<stdio.h> //program to generate numbers in triangle #include<conio.h> void main() { int i`,j,n; printf("nEnter the limit : "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf(“%d”,i); } printf(“n”); } getch(); } Nested for loop OUTPUT Enter the limit : 5 1 22 333 4444 55555 121 N.Rajkumar
#include<stdio.h> //program to generate stars(*) in triangle #include<conio.h> void main() { int I,j,n; printf("nEnter the limit : "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf(“*”); } printf(“n”); } getch(); } Nested for loop OUTPUT Enter the limit : 5 * ** *** **** ***** 122 N.Rajkumar
EXAMPLES 123 N.Rajkumar
Swapping without using third variable #include<stdio.h> #include <conio.h> void main ( ) { int a,b; clrscr( ); printf(" nEnter the value of a:"); scanf("%d",&a); printf(" nEnter the value of b:"); scanf("%d",&b); 124 N.Rajkumar
a=a+b; b=a-b; a=a-b; printf(" nThe value of a is:%d",a); printf(" nThe value of b is:%d",b); getch( ); } Output: Enter the value of a:5 Enter the value of b:6 The value of a is:6 The value of b is:5 125 N.Rajkumar
Quadratic Equation #include<stdio.h> #include <conio.h> #include<math.h> void main ( ) { int a,b,c,d,r1,r2; clrscr( ); printf(" nEnter the value of a:"); scanf("%d",&a); printf(" nEnter the value of b:"); scanf("%d",&b); printf(" nEnter the value of c:"); scanf("%d",&c); d=b*b-4*a*c; 126 N.Rajkumar
if(d>=0) { r1=(-b+sqrt(d))/(2*a); r2=(-b-sqrt(d))/(2*a); printf(" nThe roots are %d,%d",r1,r2); } else { printf(" nThe roots are imaginary"); } getch( ); } 127 N.Rajkumar
Output Enter the value of a:4 Enter the value of b:5 Enter the value of c:6 The roots are imaginary 128 N.Rajkumar

IT22101 Programming for Problem Solvingunit-2.ppt

  • 1.
  • 2.
    Steps in learningC Character Set Programs Instructions Tokens 2 N.Rajkumar
  • 3.
    C Character Set CCharacter Set Execution Character Set Source Character Set Special Characters Digits Alphabets Escape Sequence White Spaces 3 N.Rajkumar
  • 4.
    C Character Set(Cont) • Source Character Set – It is used to construct the statements in the program. • Executable Character Set – These characters are employed at the time of execution i.e. they have effects only when the program is being executed. 4 N.Rajkumar
  • 5.
    Source Character Set Lettersa to z ,A to Z Digits 0 to 9 Special Characters ! @ # $ % ^ & * ( ) _ - + = | { } [ ] etc,. White Spaces Blank Space ,Horizontal tab, New line, Vertical tab etc,. 5 N.Rajkumar
  • 6.
    Special characters • Comma, • Period or dot . • Semicolon ; • Colon : • Apostrophe ‘ • Quotation mark “ • Exclamation mark ! • Vertical bar | • Back Slash • Tilde ~ • Underscore - • Dollar $ • Question mark ? 6 N.Rajkumar
  • 7.
    • Ampersand & •Caret ^ • Asterisk * • Minus - • Addition + • Lesser than < • Greater than > • Parenthesis () • Bracket [] • Braces {} • Percentage % • Hash # • Equal to = • At the rate @ 7 N.Rajkumar
  • 8.
    Executable Character Set CharactersEscape Sequence Back Space b Horizontal Space t Vertical Space v Newline n 8 N.Rajkumar
  • 9.
    C Tokens • Thesmallest element in the C language is the token. • It may be a single character or a sequence of characters. 9 N.Rajkumar
  • 10.
    C Tokens (Cont) CTokens Identifiers Eg:main, avg Keywords Eg: int, for operators Eg: + - Strings Eg: “ab” spI symbol Eg: # $ % Constants Eg:17, 15.5 10 N.Rajkumar
  • 11.
    Executing a CProgram Creating the Program Compilation Linking Execution 11 N.Rajkumar
  • 12.
    Executing a CProgram (Cont) • Enter the program in a C editor. • Save the program (File  Save) or F2. Use the extension .c for saving the file. Eg: sample.c • Compile the program(Compile  Compile) or Alt+F9. • Run the program(Run  Run) or Ctrl+F9. 12 N.Rajkumar
  • 13.
    Structure of Cprogram DOCUMENTATION SECTION PREPROCESSOR SECTION DEFINITION SECTION GLOBAL DECLARATION SECTION main() { Declaration part; Executable Part; } sub program section { Body of the subprogram; } 13 N.Rajkumar
  • 14.
    • Documentation Section –It contains the comment lines. • Preprocessor Section – It is used to link library files. • Global Declaration Section – The Global declaration section comes at the beginning of the program and they are visible to all parts of the program. • Declaration Section – It describes the data to be used within the function. • Executable Part – It contains the valid statements. 14 N.Rajkumar
  • 15.
    C Programs  Cprogram may have many functions.  One and only one of the functions MUST BE named main.  main is the starting point for the program.  main and other functions in a program are divided into two sections, declaration section and statement section. 15 N.Rajkumar
  • 16.
    Preprocessor Directives • Specialinstructions to the preprocessor that tells how to prepare the program for compilation • E.g: include : tells the processor to include information from selected libraries known as header files e.g. <stdio.h> 16 N.Rajkumar
  • 17.
    Comments (Program documentation) The compiler simply ignores comments when it translates the program into executable code.  To identify a comments, C uses opening /* and closing */ comment tokens. 17 N.Rajkumar
  • 18.
    Comments (Cont)  Commentscan appear anywhere in a program.  Comments are also found wherever it is necessary to explain a point about a code.  Comments cannot be nested in C i.e. you cannot have comments inside comments. 18 N.Rajkumar
  • 19.
    C program /* Exampleprogram in C*/ Comments # include <stdio.h>Preprocessor Section #include<conio.h> Global Declaration void main () { Local declaration printf (“Hello World! n”); Statements getch(); } Output : Hello World 19 N.Rajkumar
  • 20.
    C Tokens • Identifiers •Keywords • Constants • Operators • Special symbols 20 N.Rajkumar
  • 21.
    Identifiers • Identifiers arenames given to various program elements such as variables, functions and arrays etc,. • Eg: #define N 10 #define a 15 Here N and a are user defined identifiers. 21 N.Rajkumar
  • 22.
    Rules for namingidentifier • First character must be alphabetic or underscore. • Must consist only of alphabetic characters, digits, or underscores. • Only the first 31 characters of an identifier are significant and are recognized by the compiler. • Cannot use a keywords or reserved word (e.g. main, include, printf & scanf etc.). • No space are allowed between the identifiers etc,. • C is case sensitive, e.g. My_name  my_name. 22 N.Rajkumar
  • 23.
    Examples of Validand Invalid Names Valid Names Invalid Names a a1 $sum /* $ is illegal */ student_name stdntNm 2names /* Starts with 2 */ _aSystemName _anthrSysNm stdnt Nmbr /* no spaces */ TRUE FALSE int /* reserved word */ 23 N.Rajkumar
  • 24.
    Variables • Variable isan identifier that is used to represent some specified type of information. • Eg: x=3 • Here x is variable. 24 N.Rajkumar
  • 25.
    Keywords • It isa reserved words. • Cannot be used for anything else. • Examples: – int – while – for etc,. 25 N.Rajkumar
  • 26.
    Keywords Auto register Continue Doubletypedef For Int Char signed Struct extern void Break return Default Else union Goto Long Const sizeof Switch Float do Case short If Enum unsigned Static While 26 N.Rajkumar
  • 27.
    Constants • It isan entity whose value does not changes during the execution. • Eg: x=3 • Here 3 is a constant. 27 N.Rajkumar
  • 28.
    Types • Numeric constants •Character constant 28 N.Rajkumar
  • 29.
  • 30.
    Numeric constants Integer constants •It is formed using a sequence of digits. Decimal - 0 to 9 . Octal - 0 to 7. Hexa - 0 to 9 ,A to F Eg: 10,75 etc. 30 N.Rajkumar
  • 31.
    Rules for definingInteger Constant • It must have atleast one digit. • Decimal point are not allowed. • No blank space or commas are allowed. • It can be either positive or negative. Etc,. 31 N.Rajkumar
  • 32.
    Numeric constants Real constants •It is formed using a sequence of digits but it contain decimal point. • length, height, price distance measured in real number Eg: 2.5, 5.11, etc. 32 N.Rajkumar
  • 33.
    Character constants Single characterconstant – A character constant is a single character they also represented with single digit or a single special symbol which is enclosed in single quotes. – Eg: ‘a’, ‘8’,’_’etc. 33 N.Rajkumar
  • 34.
    Character constants String constants •String constant are sequence of characters enclosed with in double quote. • Eg: “Hello” ,”444”,”a” etc,. 34 N.Rajkumar
  • 35.
    Operators • An operatoris a symbol that specifies an operation to be performed on the operands. • Eg: a + b + is an operator. a,b are operands. 35 N.Rajkumar
  • 36.
    Data Types  AData type is the type of data that are going to access within the program. 36 N.Rajkumar
  • 37.
    DATA TYPES DATA TYPES PRIMARYDERIVED USER DERIVED CHARACTER INTEGER REAL Array, Function, Pointer Structure, union …. char short int, int, long int Float, double long double 37 N.Rajkumar
  • 38.
    integer  A numberwithout a fraction part : integral number.  C supports three different sizes of the integer data type :  short int  int  long int 38 N.Rajkumar
  • 39.
    Floating Point  Afloating-point type is a number with a fractional part, e.g. 56.78  Floating point numbers are stored using 4 Byte.  Types  Float  Double  long double 39 N.Rajkumar
  • 40.
    character • Character aregenerally stored using 8 bits(1 Byte) of the internal storage. 40 N.Rajkumar
  • 41.
    void  The voidtype has no values and no operations.  Both the set of values and the set of operations are empty. 41 N.Rajkumar
  • 42.
    Variable Declaration  Tocreate a variable, you must specify the type and then its identifier :  Integer int a; a=10; int a=10; int a,b; a=10; b=5; int a=10,b=5; 42 N.Rajkumar
  • 43.
    Variable Declaration  Tocreate a variable, you must specify the type and then its identifier :  Float float a; a=10.5; float a=10.5; float a,b; a=10.5; b=5.5; float a=10.5,b=5.5; 43 N.Rajkumar
  • 44.
    Variable Declaration  Tocreate a variable, you must specify the type and then its identifier :  Character char a; a=‘x’; char a=‘x’; char a,b; a=‘x’; b=‘y’; char a=‘x’,b=‘y’; 44 N.Rajkumar
  • 45.
    Variable Declaration  Tocreate a variable, you must specify the type and then its identifier :  String char a[6]; a=“NAFEES”; char a[6]=“NAFEES”; Representation of string a N a[0] A a[1] F a[2] E a[3] E a[4] S a[5] 45 N.Rajkumar
  • 46.
    Entire Data typesin c: Data type Size(bytes) Range Format string Char 1 128 to 127 %c Unsigned char 1 0 to 255 %c Short or int 2 -32,768 to 32,767 %i or %d Unsigned int 2 0 to 65535 %u Long 4 -2147483648 to 2147483647 %ld Unsigned long 4 0 to 4294967295 %lu Float 4 3.4 e-38 to 3.4 e+38 %f or %g Double 8 1.7 e-308 to 1.7 e+308 %lf Long Double 10 3.4 e-4932 to 1.1 e+4932 %lf 46 N.Rajkumar
  • 47.
    Basic C programs #include<stdio.h>//programto display given data #include<conio.h> void main() { int a=10; float b=10.88; char c=‘$’; char d[6]=“Nafees”; clrscr(); printf(“Integer value : %dn”,a); printf(“Float value : %fn”,b); printf(“Character : %cn”,c); printf(“String : %s”,d); getch(); } OUTPUT Integer value : 10 Float value : 10.88 Character : $ String : Nafees 47 N.Rajkumar
  • 48.
    Basic C programs #include<stdio.h>//programto display given data from user #include<conio.h> void main() { int a; float b; char c; char d[6]; clrscr(); printf(“Enter a integer value : “); scanf(“%d”,&a); printf(“Enter a float value : “); scanf(“%f”,&b); printf(“Enter a character : “); scanf(“%c”,&c); printf(“Enter a string : “); scanf(“%s”,d); OUTPUT Enter a integer value :12 Enter a float value : 5.34 Enter a character : h Enter a string : Nafees Integer value : 12 Float value : 5.34 Character : h String : Nafees printf(“Integer value : %dn”,a); printf(“Float value : %fn”,b); printf(“Character : %cn”,c); printf(“String : %s”,d); getch(); } 48 N.Rajkumar
  • 49.
    Types of Operator 1.Arithmetic operator 2. Relational operator 3. Logical operator 4. Assignment operator 5. Short hand assignment operator 6. Increment or decrement operator(unary) 7. Comma operator 8. Conditional operator 49 N.Rajkumar
  • 50.
    Arithmetic operator • Itis used to carry out arithmetic operations NAME OPERATOR ADDITION + SUBTRACTION - MULTIPLICATION * DIVISION / MODULUS (REMAINDER) % 50 N.Rajkumar
  • 51.
    SAMPLE PROGRAM #include<stdio.h> //Header File #include <conio.h> void main ( ) /* main is the starting of every c program */ { int a=5,b=2; //Local Declaration clrscr( ); printf(“Addition : %dn”,(a+b)); printf(“Subtraction : %dn”,(a-b)); printf(“Multiplication : %dn”,(a*b)); printf(“Division : %dn”,(a/b)); printf(“Modulus(remainder): %d”,(a%b)); getch( ); } OUTPUT Addition : 7 Subtraction : 3 Multiplication : 10 Division :2 Modulus(remainder):1 51 N.Rajkumar
  • 52.
    Division operator onDifferent Data Type Operation Result Example int/int int 5/2 = 2 int/real real 5/2.0 = 2.5 real/int real 5.0/2 = 2.5 real/real real 5.0/2.0 = 2.5 52 N.Rajkumar
  • 53.
    Relational operator • Itis used to compare two or more operands and returns 1 if true else return 0 NAME OPERATOR Less than < Less than or equal to <= Greater than > Greater than or equal to >= Equal to == Not equal to != 53 N.Rajkumar
  • 54.
    Relational Operator… #include<stdio.h> #include <conio.h> voidmain() { clrscr(); printf(“n5!=3:%d”,(5!=3)); printf(“n5<=3:%d”,(5<=3)); printf(“n5>=3:%d”,(5>=3)); printf(“n5<7 :%d”,(5<7)); printf(“n5>4 :%d”,(5>4)); getch(); } OUTPUT 5!=3 : 1 5<=3 : 0 5>=3 : 1 5<7 : 1 5>4 : 1 54 N.Rajkumar
  • 55.
    Logical operator • Itis used to perform logical operations NAME OPERATOR AND && OR || NOT ! 55 N.Rajkumar
  • 56.
    Sample program #include<stdio.h> #include <conio.h> voidmain ( ) { clrscr( ); printf(“n(5<=8)&&(4>=2):%d”,((5<=8)&&(4>=2))); printf(“n(5>=3)||(6<4):%d”,((5>=3)||(6<4))); printf(“n!(7==7):%d”,!(7==7)); getch( ); } OUTPUT (5<=8)&&(4>=2) : 1 (5>=3)||(6<4) : 1 !(7==7) : 0 56 N.Rajkumar
  • 57.
    Assignment operator • Itis used to assign a value or expression etc to a variable. • Eg: a =10.(assignment operator ‘=‘ assigns value 10 to variable a. a = b a = b + c etc,. 57 N.Rajkumar
  • 58.
  • 59.
    Increment & DecrementOperator NAME OPERATOR Increment operator ++ Decrement operator -- Pre Increment Post Increment int a=10; ++a Now value of a is 11 int a=10; a++ Now value of a is 11 Pre decrement Post decrement int a=10; --a Now value of a is 9 int a=10; a-- Now value of a is 9 59 N.Rajkumar
  • 60.
    Increment & DecrementOperator #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf(“Enter a & b value : n”); scanf(“%d%d”,&a,&b); printf(“Value of a & b before increment : %d %dn”,a,b); printf(“Value of a & b after increment : %d %dn”,a++,b++); printf(“Value of a & b after decrement : %d %dn”,a--,b—); getch(); } 60 N.Rajkumar
  • 61.
    Increment & DecrementOperator OUTPUT Enter a & b value : 6 9 Value of a & b before increment : 6 9 Value of a & b after increment : 7 10 Value of a & b after decrement : 5 8 61 N.Rajkumar
  • 62.
    Conditional Operator (or) TernaryOperator • It is used to checks the condition and execute the statement depending on the condition. • Eg: C = a > b ? a:b 62 N.Rajkumar
  • 63.
    Sample Program #include<stdio.h> #include <conio.h> voidmain ( ) { int a=5,b=8,c; clrscr( ); c = a>b?a:b; //Conditional operator printf(" n The Larger Value is %d",c); getch( ); } OUTPUT The Larger Value is 8 63 N.Rajkumar
  • 64.
    Output The Larger Valueis 8 OUTPUT The Larger Value is 8 64 N.Rajkumar
  • 65.
    Special Operator • commaoperator ( , ) EX: int a,b; 65 N.Rajkumar
  • 66.
    Expression • An expressionrepresent data item such as variable, constant are interconnected using operators. • Eg: Expression C Expression a + b + c a + b + c a2+b2 a*a + b*b 66 N.Rajkumar
  • 67.
    Precedence Operator High *, / , % Low + , - 67 N.Rajkumar
  • 68.
    Type Conversion • Convertingthe type of an expression from one type to another type. Eg: x = (int)10.45 68 N.Rajkumar
  • 69.
    Sample Program #include<stdio.h> #include <conio.h> voidmain ( ) { int c; clrscr( ); c=(int)10.45; printf("nOutput is:%d",c); getch( ); } OUTPUT Output is:10 69 N.Rajkumar
  • 70.
    PROGRAM CONTROL STRUCTURES •Depending upon the sequence of the execution of statements, the control structures are categorized as follows: • Categories: • Sequential structure – In which instructions are executed in sequence. • Selection structure – In which instruction are executed based on the result of some condition. • Iteration structure – In which instruction are executed repeatedly. 70 N.Rajkumar
  • 71.
    Sequential STRUCTURE • Itallows the program to flow in sequence START Statement 1 Statement 2 Statement N STOP 71 N.Rajkumar
  • 72.
    SELECTION STRUCTURE • Itallows the program to make a choice from alternative paths. • C provide the following selection structures – if statement – if … else statement – Nested if … else statement 72 N.Rajkumar
  • 73.
    if Statement Syntax if (conditionis true) { Statements; } If condition False True Statements 73 N.Rajkumar
  • 74.
    if…else Statement Syntax if (condition) { Truestatements; } else { False statements; } If Condition True False True statements False statements 74 N.Rajkumar
  • 75.
  • 76.
    Nested if…else Syntax IF (condition1) { statements; } elseif (condition2) { statements; } else if (condition3) { statements; } else { statements; } 76 N.Rajkumar
  • 77.
    Iteration structure • Itis used to execute some instructions several time based on some condition. – while – do…while – for 77 N.Rajkumar
  • 78.
    while Loop Syntax . while (condition) { . Bodyof the loop; . } Body of The loop condition False True 78 N.Rajkumar
  • 79.
    do…while Loop Syntax do { Body ofthe loop }while (condition); Body of The loop condition False True 79 N.Rajkumar
  • 80.
    for loop Syntax for (initialization;test condition; Increment/Decrement) { Body of the loop } 80 N.Rajkumar
  • 81.
    for loop Initialization condition False Bodyof the loop Inc / Decrement 81 N.Rajkumar
  • 82.
    Nested for loop Syntax for(initi; cond; Inc/Dec) { for (initi; cond; Inc/Dec) { Body of the loop } } 82 N.Rajkumar
  • 83.
    DECISION MAKING &BRANCHING • if statement • if else statement • Nested if else statement • switch statement • Conditional statement • goto statement • break statement • continue statement Selection statements Unconditional statements 83 N.Rajkumar
  • 84.
    if statement (Simpleif) Syntax if (condition is true) { Statements; } 84 N.Rajkumar
  • 85.
    if statement (Simpleif) #include<stdio.h> #include <conio.h> void main ( ) { int a; clrscr( ); printf("nEnter a number:"); scanf("%d",&a); if(a>10) { printf(" n a is greater than 10"); } getch( ); } OUTPUT Enter the number: 12 a is greater than 10 85 N.Rajkumar
  • 86.
    if else statement Syntax if(condition is true) { Statements; } else { Statements; } 86 N.Rajkumar
  • 87.
    if else statement #include<stdio.h> #include<conio.h> void main ( ) { int a; clrscr( ); printf("nEnter a number:"); scanf("%d",&a); if(a>10) { printf(" n a is greater than 10"); } else { printf(" n a is not greater than 10"); } getch( ); } OUTPUT Enter the number: 12 a is greater than 10 Enter the number: 1 a is not greater than10 87 N.Rajkumar
  • 88.
    Nested if elsestatement Syntax if (condition is true) { Statements; } else if (condition is true) { Statements; } else if (condition is true) { Statements; } else { Statements; } 88 N.Rajkumar
  • 89.
    Nested if elsestatement #include<stdio.h> #include<conio.h> void main() { int m1,m2,m3; float avg; printf("nEnter the marks:"); scanf("%d%d%d",&m1,&m2,&m3); avg=(m1+m2+m3)/3; printf("n The average is:%f",avg); printf("n The Grade is:"); if(avg>=60) { printf("First class"); } 89 N.Rajkumar
  • 90.
    else if(avg>=50) { printf("Second class"); } elseif(avg>=35) { printf("Third class"); } else { printf("Fail"); } getch(); } OUTPUT Enter the marks:65 75 70 The average is:70.00 The Grade is: First class 90 N.Rajkumar
  • 91.
    Nested if else #include<stdio.h> //Program to find leap year #include<conio.h> void main() { int year; printf("Enter a year to check if it is a leap year : n"); scanf("%d", &year); if ( year%400 == 0) printf("%d is a leap year.n", year); else if ( year%100 == 0) printf("%d is not a leap year.n", year); else if ( year%4 == 0 ) printf("%d is a leap year.n", year); else printf("%d is not a leap year.n", year); getch(); } OUTPUT Enter a year to check if it is a leap year :2012 2012 is a leap year 91 N.Rajkumar
  • 92.
    switch statement Syntax switch (expression) { caseconstant 1: block1; break; case constant 2: block2; break; . . default : default block; break; } Case 1 Case 2 Default case Switch 92 N.Rajkumar
  • 93.
    switch statement #include<stdio.h> #include<conio.h> void main() { inta,b,c,n; clrscr(); printf("nEnter the value of a,b:n"); scanf("%d%d",&a,&b); printf("nMENU"); printf("n1.ADDn2.SUBn3.MULTIPLYn4.EXIT"); printf("nEnter the choice:"); scanf("%d",&n); 93 N.Rajkumar
  • 94.
    switch(n) { case 1: c=a+b; printf("nThe resultof Addition is:%d",c); break; case 2: c=a-b; printf("nThe result of Subtraction is:%d",c); break; 94 N.Rajkumar
  • 95.
    case 3: c=a*b; printf("nThe resultof Multiplication is:%d",c); break; case 4: exit(0); break; default: printf(“Wrong Choice”); } getch(); } OUTPUT Enter the value of a,b: 5 6 MENU 1.ADD 2.SUB 3.MULTIPLY 4.EXIT Enter the choice:1 The result of Addition is:11 95 N.Rajkumar
  • 96.
    Conditional Statement • Ituses conditional operator (?) #include<stdio.h> #include<conio.h> void main() { int n; printf(“Enter a number : “); scanf(“%d”,&n); (n%2==0)?printf(“even”):printf(“odd”); getch(); } OUTPUT Enter a number : 3 odd 96 N.Rajkumar
  • 97.
    goto statement • Whena goto statement is encountered, the control is transferred to the label. goto label; ………… ………… ………… label: ………… 97 N.Rajkumar
  • 98.
    goto statement #include<stdio.h> #include<conio.h> void main() { intx; clrscr(); printf(“Enter a number to find odd or even no : “); scanf(“%d”,&x); if(x%2==0) goto even; else goto odd; 98 N.Rajkumar
  • 99.
    goto statement even: printf(“%d isan even no”,x); return; odd: printf(“%d is an odd no”,x); return; OUTPUT Enter a number to find odd or even no :5 5 is an odd no 99 N.Rajkumar
  • 100.
    #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf(“Entertwo value to subtract : n”); scanf(“n%d%d”,&a,&b); if(a>b) { printf(“Subtracted value is : %dn”,(a-b)); } else { break; } } OUTPUT Enter two value to subtract : 5 3 Subtracted value is : 2 When break statement is encountered inside any block or loop, control automatically passes to the first statement after the block or loop break statement 100 N.Rajkumar
  • 101.
    #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); for(a=1;a<=10;a++) { if((a==5)||(a==7)) continue; else printf(“%dn”,a); } } OUTPUT 1 2 3 4 6 8 9 10 Continuestatement terminates the current loop iteration & persist the loop with next iteration continue statement 101 N.Rajkumar
  • 102.
    • Loop: –A loopis defined as a block of statements which are repeatedly executed for certain no of times. • Types of Loops in C –while loop –do while loop –for loop(simple & nested) Looping Statements 102 N.Rajkumar
  • 103.
    • It isan entry controlled loop works only when the condition is true • Syntax: while (condition) { . Body of the loop; . } while loop 103 N.Rajkumar
  • 104.
    #include<stdio.h> //program toprint even no #include<conio.h> void main() { int n,i; printf(“Enter the limit : “); scanf(“%d”,&n); i=1; while (i<=n) { if(i%2==0) { printf(“%dt”,i); } i++; } } while loop OUTPUT Enter the limit : 10 2 4 6 8 10 104 N.Rajkumar
  • 105.
    #include<stdio.h> //program tocalculate factorial of given no #include<conio.h> void main() { int i=1,fact=1,n; printf("nEnter the Number:"); scanf("%d",&n); while(i<=n) { fact =fact *i; i++; } printf("n The value of %d! is:%d",n,fact); getch(); } while loop OUTPUT Enter the Number:3 The value of 3! is: 6 105 N.Rajkumar
  • 106.
    #include<stdio.h> //program toreverse a number and to check #include<conio.h>// whether it is palindrome or not void main() { int n,rev=0;` clrscr(); printf(“Enter a number to reverse : “); scanf(“%d”,&n); while(n!=0) { rev=rev*10; rev=rev+(n%10); n=n/10; } printf(“Reverse of given no is : %dn”,rev); while loop OUTPUT Enter a number to reverse : 452 Reverse of given number is : 254 Not Palindrome if(n==rev) { printf(“Palindrome”); } else { printf(“Not Palindrome”); } getch(): } 106 N.Rajkumar
  • 107.
    #include<stdio.h> //program tofind sum of digits #include<conio.h> void main() { int r=0,sum=0,n; printf("nEnter the no:"); scanf("%d",&n); while(n>0) { r=n%10; sum=sum+r; n=n/10; } printf("sum of the digits is:%d",sum); getch(): } while loop OUTPUT Enter the no:156 sum of the digits is:12 107 N.Rajkumar
  • 108.
    #include<stdio.h> //program tofind armstrong number #include<conio.h> void main() { int r=0,sum=0,n,a; printf("nEnter the number:"); scanf("%d",&n); a=n; while(n>0) { r=n%10; sum=sum+r*r*r; n=n/10; } if(a==sum) printf("nIt is an armstrong number"); else printf("nIt is not an armstrong number"); getch(); } OUTPUT Enter the number:153 It is an armstrong number 108 N.Rajkumar
  • 109.
    • It isan exit controlled loop. It proceeds its first iteration without checking the loop condition and proceeds next iteration by checking the condition. • Syntax: do { . Body of the loop; . }while (condition); do while loop 109 N.Rajkumar
  • 110.
    #include<stdio.h> //program toprint even no #include<conio.h> void main() { int n,i; printf(“Enter the limit : “); scanf(“%d”,&n); i=1; do { if(i%2==0) { printf(“%dt”,i); } i++; }while (i<=n); } do while loop OUTPUT Enter the limit : 10 2 4 6 8 10 110 N.Rajkumar
  • 111.
    #include<stdio.h> //program tocalculate factorial of given no #include<conio.h> void main() { int i=1,fact=1,n; printf("nEnter the Number:"); scanf("%d",&n); do { fact =fact *i; i++; }while (i<=n); printf("n The value of %d! is:%d",n,fact); getch(); } do while loop OUTPUT Enter the Number:3 The value of 3! is: 6 111 N.Rajkumar
  • 112.
    do while loop//program to generate fibonacci series OUTPUT Enter the range : 10 0 1 1 2 3 5 8 #include<stdio.h> #include<conio.h> void main() { int a,b,c,r; clrscr(); printf(“Enter the range : “); scanf(“%d”,&r); a=0,b=1; printf(“%d”,a); printf(“n%d”,b); printf(“n”); c=0; do { c=a+b; if(c<=r) printf(“%dn”,c); a=b; b=c; }while(c<=r); getch(); } 112 N.Rajkumar
  • 113.
    for loop Syntax: for (initialization;test condition; Increment/Decrement) { Body of the loop } 113 N.Rajkumar
  • 114.
    #include<stdio.h> //program togenerate number from 1 to n #include<conio.h> void main() { int i,n; printf("nEnter the limit : "); scanf("%d",&n); for(i=1;i<=n;i++) { printf(“%dn”,i); } getch(); } for loop OUTPUT Enter the limit : 5 1 2 3 4 5 114 N.Rajkumar
  • 115.
    #include<stdio.h> //program tocalculate factorial of given no #include<conio.h> void main() { int i,fact=1,n; printf("nEnter the Number:"); scanf("%d",&n); for(i=1;i<=n;i++) { fact =fact *i; } printf("n The value of %d! is:%d",n,fact); getch(); } for loop OUTPUT Enter the Number:3 The value of 3! is: 6 115 N.Rajkumar
  • 116.
    #include<stdio.h> //program togenerate given multiplication table #include<conio.h> void main() { int i,tab,n,res; printf("nEnter the multiplication table:"); scanf("%d",&tab); printf("nEnter the limit of multiplication table:"); scanf("%d",&n); for(i=1;i<=n;i++) { res=i*tab; printf("%d * %d : %dn”,i,tab,res); } getch(); } for loop OUTPUT Enter the multiplication table : 2 Enter the limit of multiplication table : 4 1 * 2 : 2 2 * 2 : 4 3 * 2 : 6 4 * 2 : 8 116 N.Rajkumar
  • 117.
    #include<stdio.h>//program to calculatestudent marks & average using for loop #include<conio.h> void main() { int n,s1,s2,s3,s4,s5,tot; float avg; clrscr(); printf(“Enter no of students : ”); scanf(“%d”,&n); printf(“Enter marks of five subjects : n”) for(i=1;i<=n;i++) { printf(“Enter student %d marksn”,i); scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5); tot=s1+s2+s3+s4+s5; avg=tot/5; printf(“Total : %dnAverage : %f”,tot,avg); } getch(); } for loop 117 N.Rajkumar
  • 118.
    • Definition ofprime number: – A natural number greater than one has not any other divisors except 1 and itself. In other word we can say which has only two divisors 1 and number itself. – For example: 5. Their divisors are 1 and 5. for loop 118 N.Rajkumar
  • 119.
    #include<stdio.h>//program to checkwhether the given no is prime or not #include<conio.h> void main() { int n, i, c = 0; printf("Enter a number :"); scanf("%d", &n); for (i = 1; i <= n; i++) { if (n % i == 0) { c++; } } if (c == 2) printf(“Given no is a Prime number"); else printf(“Given no is not a Prime number"); getch(); } for loop OUTPUT Enter a number : 7 Given no is a Prime number 119 N.Rajkumar
  • 120.
    Nested for loop Syntax for(initi; cond; Inc/Dec) { for (initi; cond; Inc/Dec) { Body of the loop } } 120 N.Rajkumar
  • 121.
    #include<stdio.h> //program togenerate numbers in triangle #include<conio.h> void main() { int i`,j,n; printf("nEnter the limit : "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf(“%d”,i); } printf(“n”); } getch(); } Nested for loop OUTPUT Enter the limit : 5 1 22 333 4444 55555 121 N.Rajkumar
  • 122.
    #include<stdio.h> //program togenerate stars(*) in triangle #include<conio.h> void main() { int I,j,n; printf("nEnter the limit : "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf(“*”); } printf(“n”); } getch(); } Nested for loop OUTPUT Enter the limit : 5 * ** *** **** ***** 122 N.Rajkumar
  • 123.
  • 124.
    Swapping without usingthird variable #include<stdio.h> #include <conio.h> void main ( ) { int a,b; clrscr( ); printf(" nEnter the value of a:"); scanf("%d",&a); printf(" nEnter the value of b:"); scanf("%d",&b); 124 N.Rajkumar
  • 125.
    a=a+b; b=a-b; a=a-b; printf(" nThe valueof a is:%d",a); printf(" nThe value of b is:%d",b); getch( ); } Output: Enter the value of a:5 Enter the value of b:6 The value of a is:6 The value of b is:5 125 N.Rajkumar
  • 126.
    Quadratic Equation #include<stdio.h> #include <conio.h> #include<math.h> voidmain ( ) { int a,b,c,d,r1,r2; clrscr( ); printf(" nEnter the value of a:"); scanf("%d",&a); printf(" nEnter the value of b:"); scanf("%d",&b); printf(" nEnter the value of c:"); scanf("%d",&c); d=b*b-4*a*c; 126 N.Rajkumar
  • 127.
    if(d>=0) { r1=(-b+sqrt(d))/(2*a); r2=(-b-sqrt(d))/(2*a); printf(" nThe rootsare %d,%d",r1,r2); } else { printf(" nThe roots are imaginary"); } getch( ); } 127 N.Rajkumar
  • 128.
    Output Enter the valueof a:4 Enter the value of b:5 Enter the value of c:6 The roots are imaginary 128 N.Rajkumar