SlideShare uma empresa Scribd logo
1 de 40
Fundamental structure
of programming in 'c'
Presented by:
Mr. Yogesh Kumar
M.Sc. I Sem.(Bioinformatics)
‘C’ language is simple as compare to the English language
Here is steps comparison…
Steps in learning English language
Steps in learning ‘c’
alphabets words sentences paragraph
Alphabets , digits
Special symbols
Constants,
variables
keywords
instructi
ons
program
Before planning a program we need to define its logic
(the correct sequence of instructions need to solve the
problem at hand).the term algorithm is often used to
refer to the logic of a program.
It is a step by step description of how to arrive at the
solution of the given problem.(exp..)
*Write a program to find the larger of two given numbers.
Algorithm.
1.Input two numbers a and b
2.Assign big=a
3. If (b>big)the big=b
4.Output big
5.stop
 Flowchart is the diagrammatic representation of
programs and algorithm . it is generally used to
understand the program and to solve the program
 It uses boxes of different shapes to denote different
types of instructions.
 The process of drawing a flowchart for an algorithm is
often referred to flowcharting
Only a few symbols are needed to indicate the necessary
operations in a flowchart. These basic flowchart symbols
have been standardized by the American National
standards institute (ANSI).
As shown……
flow lines
connector
start processing Input/output
decision
example.1
no
yes
start
Read
num
i=1
Is
i<=10
Write
(i*num)
i=i+1
stop
Tokens in ‘c’
 The alphabets ,numbers and special symbols when
properly combined form constants , variables &
keywords
 Alphabets A,B,C………..Z
a , b, c ……....z
 Digits 0,1,2,3……….9
 Special symbols ~, ’, !, @, #, %, ^, &, =,|,?, /,
[ ] : ; ””,’ <> ., { }
Constant, variables & keywords
3
5
PRIMARY CONSTANT
 Integer constant
 Real constant
 Character constant
 Array
 Pointer
 Structure
 Union
 Enum.etc
SECONDARY CONSTANT
As we saw earlier , an entity that may vary during program
execution is called a variable.
 Type of variable used in program depend on the type of
constant stored in it.
 Float/real, integer ,or character constant
 Variable name are name given to locations in memory
Exp….
float=6.0 (4 byte)
Int =6 (2 byte)
char=‘a’ (1 byte)
 Keywords are the words whose meaning has already been
explained to the c compiler
 The keywords cannot be used as variable name if we do so,
we are typing to assign new value to keywords which is not
allowed by compiler
 There are 32 keywords available in c
 Some are …….
short return double switch register signed
long void float union static unsigned
int break if goto continue volatile
char default for do sizeof enum
else case while auto extern typedef ..etc
• structure
•class
• integer
• float
•Character (char)
•int %d
•float %f
•char %c
•Array
•Pointer
•Function
•String
DATA TYPES
User define data type Fundamental data type Derived data type
Arithmetic operators
+
-
*
/
%
purpose
Addition
Subtraction
Multiplication
Division
Remainder after
division
example
7+5=12
7-5=2
7*5=35
8/2=4
7%5=2
ARITHMETIC OPERATORS
&& Means logical AND
|| Means logical OR
! Means logical NOT
LOGICAL OPERATOR
RELATIONAL OPERATORS
> Means greater than
< Means less than
== Means equal to
>= Means greater than equal to
<= Means less than equal to
!= Means not equals to
 The general form of if statement is
if ( condition)
{
Statement-block
}
If condition is true the statement block will executed
otherwise the statement block will skipped.
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,big;
printf(“enter two numbersn”);
scanf(“%f%f”,&a,&b);
big=a;
if(b>big) big=b;
printf(“larger number is:%f”,big);
}
RUN
Enter two numbers
12.5 45.0
Larger number is 45.0
ENTER
false
true
next statement
condition
Statement-block
if (condition)
{
statement-1;
}
else
{
statement-2;
}
enter
false true
next statement
Condition
?
Statement-1
Statement-2
The switch statement test the value of given expression
against a list of case values
General form
switch(expression)
{
case val-1:
statement-1
break;
case val-2:
statement-2;
break;
default:
default-statement;
break;
#include<stdio.h>
#include<conio.h>
void main()
{
char grade;
switch(grade)
{
case ‘A’:
printf(“passed with first division”);
break;
case ‘B’:
printf(“passed with second division”);
break;
case ‘C’:
printf(“conditional pass”;
break;
default:
printf(“fail”);
break;
}
getch();
}
 There are two types of repetitive structures
1. Conditional controlled (in this body is repetitively
executed until the given condition become true)
a) while statement
b) do while statement
2. Counter controlled (in this the number of time the set of
statement is executed ex.. )For loop
a) while statement
while(condition)
{
Statement(s);
}
1. evaluate the condition.
2. If the condition true then execute the statement(s)and repeat step 1.
3. If the condition is false then the control is transferred out of loop.
Exp…..
#include<stdio.h>
main()
{
int num=1,s=0;
While(num<=10)
{
sum +=num;
num+1=1;
}
printf(“sum of first 10 natural numbers : %d ”, s);
getch();
}
RUN
Sum of first 10 natural num is : 55
The sequence of operation in while loop
no
yes
start
Read
num
i=1
Is
i<=10
Write
(i*num)
i=i+1
stop
In while statement, condition is evaluated first. Therefore
the body of loop may not be executed at all if condition
is not satisfied at the very first attempt. But in do loop,
condition is evaluated at the end. Therefore the body of
the loop is executed at lest once in this statement
The general form of this statement is..
do
{
Statement(s);
}
while (condition);
printf(“…………….”);
}
Do while statement
#include<stdio.>
main( )
{
int num=1,s=0;
do
{
s +=num;
num +=1;
}
while(num<=10);
printf(“sum of first natural no is %d”,s) ;
getch();
}
Run:
Sum of 10 natural number is 55.
(do while )..exp..sum of first 10 natural number
• The difference b/w. while & do while loop
I. In while the condition is tested before executing
body of loop. /In do while the condition is tested
after executing the body
II. Body of do loop is executed at lest once but body of
while loop may not be executed at all. /In while , If
initial the condition is not satisfied the loop will
to get executed even once
 There are situation where you want to have a
statement or group of statements to be executed
numbers of time and the number of repetition does
not depend on the condition but it is simply a
repetition up to a certain numbers. The best way of
repetition is a for loop. The general form of the loop
for single statement is:
for ( initialization ; condition ; increment)
{
statement(s);
}
#include<stdio.h>
main( )
{
int I,s=0;
for (i=1;1<=10;i++)
s=s+1;
printf(“sum of first natural number is:%d”,s);
getch( );
}
Run:
Sum of first 10 natural number is : 55
Loop construct can be nested or embedded within one
another. The inner loop must be completely embedded
with the outer loop . There should no overlapping of loops.
**Demonstration of nested loops**
#include<stdio.h>
void main()
{
int r,c,sum;
for (r=1;r<=3;r++) /*outer loop*/
{
for(c=1;c<=2;c++) /*inner loop*/
{
sum=r+c;
printf(“r=%d c=%d sum=%dn”,r,c,sum);
}}
getch();
}
Output
r=1 c=1 sum=2
r=1 c=2 sum=3
r=2 c=1 sum=3
r=2 c=2 sum=4
The loop that we have used so far executed the statements within them a finite number of
times. However, in real life programming, one comes across a situation when it is not
known beforehand how many times the statements in the loop are to be executed. This
situation can be programmed as shown below:
/* execution of a loop an unknown number of times*/
#include<stdio.h>
void main()
{
Char another;
Int num;
do
{
printf(“enter a number”);
scanf(“%d”,&num);
printf(“square of%d is %d”, num, num*num);
Printf(“n want to enter another number y/n”);
Scanf(“%c”,&another);
}while(another==‘y’);
}
getch();
}
Output
Enter a number 5
Square of 5 is 25
Want to enter another number y/n y
Enter a number 7
Square of 7 is 49
Want to enter anotehr number y/n n
THANK YOU ……. .
Claguage 110226222227-phpapp02

Mais conteúdo relacionado

Mais procurados

Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 
C Prog. - Decision & Loop Controls
C Prog. - Decision & Loop ControlsC Prog. - Decision & Loop Controls
C Prog. - Decision & Loop Controls
vinay arora
 
Programming C Language
Programming C LanguageProgramming C Language
Programming C Language
natarafonseca
 

Mais procurados (19)

Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
C Prog. - Decision & Loop Controls
C Prog. - Decision & Loop ControlsC Prog. - Decision & Loop Controls
C Prog. - Decision & Loop Controls
 
Programming C Language
Programming C LanguageProgramming C Language
Programming C Language
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 

Semelhante a Claguage 110226222227-phpapp02

[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
yasir_cesc
 

Semelhante a Claguage 110226222227-phpapp02 (20)

C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
What is c
What is cWhat is c
What is c
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
C tutorial
C tutorialC tutorial
C tutorial
 
C operators
C operatorsC operators
C operators
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Programming in C
Programming in CProgramming in C
Programming in C
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
What is c
What is cWhat is c
What is c
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C tutorial
C tutorialC tutorial
C tutorial
 

Último

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Último (20)

Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 

Claguage 110226222227-phpapp02

  • 1. Fundamental structure of programming in 'c' Presented by: Mr. Yogesh Kumar M.Sc. I Sem.(Bioinformatics)
  • 2. ‘C’ language is simple as compare to the English language Here is steps comparison… Steps in learning English language Steps in learning ‘c’ alphabets words sentences paragraph Alphabets , digits Special symbols Constants, variables keywords instructi ons program
  • 3. Before planning a program we need to define its logic (the correct sequence of instructions need to solve the problem at hand).the term algorithm is often used to refer to the logic of a program. It is a step by step description of how to arrive at the solution of the given problem.(exp..) *Write a program to find the larger of two given numbers. Algorithm. 1.Input two numbers a and b 2.Assign big=a 3. If (b>big)the big=b 4.Output big 5.stop
  • 4.  Flowchart is the diagrammatic representation of programs and algorithm . it is generally used to understand the program and to solve the program  It uses boxes of different shapes to denote different types of instructions.  The process of drawing a flowchart for an algorithm is often referred to flowcharting
  • 5. Only a few symbols are needed to indicate the necessary operations in a flowchart. These basic flowchart symbols have been standardized by the American National standards institute (ANSI). As shown…… flow lines connector start processing Input/output decision
  • 8.  The alphabets ,numbers and special symbols when properly combined form constants , variables & keywords  Alphabets A,B,C………..Z a , b, c ……....z  Digits 0,1,2,3……….9  Special symbols ~, ’, !, @, #, %, ^, &, =,|,?, /, [ ] : ; ””,’ <> ., { } Constant, variables & keywords
  • 9. 3 5
  • 10. PRIMARY CONSTANT  Integer constant  Real constant  Character constant  Array  Pointer  Structure  Union  Enum.etc SECONDARY CONSTANT
  • 11. As we saw earlier , an entity that may vary during program execution is called a variable.  Type of variable used in program depend on the type of constant stored in it.  Float/real, integer ,or character constant  Variable name are name given to locations in memory Exp…. float=6.0 (4 byte) Int =6 (2 byte) char=‘a’ (1 byte)
  • 12.  Keywords are the words whose meaning has already been explained to the c compiler  The keywords cannot be used as variable name if we do so, we are typing to assign new value to keywords which is not allowed by compiler  There are 32 keywords available in c  Some are ……. short return double switch register signed long void float union static unsigned int break if goto continue volatile char default for do sizeof enum else case while auto extern typedef ..etc
  • 13. • structure •class • integer • float •Character (char) •int %d •float %f •char %c •Array •Pointer •Function •String DATA TYPES User define data type Fundamental data type Derived data type
  • 14.
  • 16. && Means logical AND || Means logical OR ! Means logical NOT LOGICAL OPERATOR
  • 17. RELATIONAL OPERATORS > Means greater than < Means less than == Means equal to >= Means greater than equal to <= Means less than equal to != Means not equals to
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.  The general form of if statement is if ( condition) { Statement-block } If condition is true the statement block will executed otherwise the statement block will skipped.
  • 23. #include<stdio.h> #include<conio.h> void main() { float a,b,big; printf(“enter two numbersn”); scanf(“%f%f”,&a,&b); big=a; if(b>big) big=b; printf(“larger number is:%f”,big); } RUN Enter two numbers 12.5 45.0 Larger number is 45.0
  • 27. The switch statement test the value of given expression against a list of case values General form switch(expression) { case val-1: statement-1 break; case val-2: statement-2; break; default: default-statement; break;
  • 28. #include<stdio.h> #include<conio.h> void main() { char grade; switch(grade) { case ‘A’: printf(“passed with first division”); break; case ‘B’: printf(“passed with second division”); break; case ‘C’: printf(“conditional pass”; break; default: printf(“fail”); break; } getch(); }
  • 29.  There are two types of repetitive structures 1. Conditional controlled (in this body is repetitively executed until the given condition become true) a) while statement b) do while statement 2. Counter controlled (in this the number of time the set of statement is executed ex.. )For loop a) while statement while(condition) { Statement(s); }
  • 30. 1. evaluate the condition. 2. If the condition true then execute the statement(s)and repeat step 1. 3. If the condition is false then the control is transferred out of loop. Exp….. #include<stdio.h> main() { int num=1,s=0; While(num<=10) { sum +=num; num+1=1; } printf(“sum of first 10 natural numbers : %d ”, s); getch(); } RUN Sum of first 10 natural num is : 55 The sequence of operation in while loop
  • 32. In while statement, condition is evaluated first. Therefore the body of loop may not be executed at all if condition is not satisfied at the very first attempt. But in do loop, condition is evaluated at the end. Therefore the body of the loop is executed at lest once in this statement The general form of this statement is.. do { Statement(s); } while (condition); printf(“…………….”); } Do while statement
  • 33. #include<stdio.> main( ) { int num=1,s=0; do { s +=num; num +=1; } while(num<=10); printf(“sum of first natural no is %d”,s) ; getch(); } Run: Sum of 10 natural number is 55. (do while )..exp..sum of first 10 natural number
  • 34. • The difference b/w. while & do while loop I. In while the condition is tested before executing body of loop. /In do while the condition is tested after executing the body II. Body of do loop is executed at lest once but body of while loop may not be executed at all. /In while , If initial the condition is not satisfied the loop will to get executed even once
  • 35.  There are situation where you want to have a statement or group of statements to be executed numbers of time and the number of repetition does not depend on the condition but it is simply a repetition up to a certain numbers. The best way of repetition is a for loop. The general form of the loop for single statement is: for ( initialization ; condition ; increment) { statement(s); }
  • 36. #include<stdio.h> main( ) { int I,s=0; for (i=1;1<=10;i++) s=s+1; printf(“sum of first natural number is:%d”,s); getch( ); } Run: Sum of first 10 natural number is : 55
  • 37. Loop construct can be nested or embedded within one another. The inner loop must be completely embedded with the outer loop . There should no overlapping of loops. **Demonstration of nested loops** #include<stdio.h> void main() { int r,c,sum; for (r=1;r<=3;r++) /*outer loop*/ { for(c=1;c<=2;c++) /*inner loop*/ { sum=r+c; printf(“r=%d c=%d sum=%dn”,r,c,sum); }} getch(); } Output r=1 c=1 sum=2 r=1 c=2 sum=3 r=2 c=1 sum=3 r=2 c=2 sum=4
  • 38. The loop that we have used so far executed the statements within them a finite number of times. However, in real life programming, one comes across a situation when it is not known beforehand how many times the statements in the loop are to be executed. This situation can be programmed as shown below: /* execution of a loop an unknown number of times*/ #include<stdio.h> void main() { Char another; Int num; do { printf(“enter a number”); scanf(“%d”,&num); printf(“square of%d is %d”, num, num*num); Printf(“n want to enter another number y/n”); Scanf(“%c”,&another); }while(another==‘y’); } getch(); } Output Enter a number 5 Square of 5 is 25 Want to enter another number y/n y Enter a number 7 Square of 7 is 49 Want to enter anotehr number y/n n