0% found this document useful (0 votes)
18 views

mmt-001 17 10

Uploaded by

jksvsgla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

mmt-001 17 10

Uploaded by

jksvsgla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Date 17/10/23

While.. Loop 17/10/2023


• It is better if number of iteration is not known by the user.
• Syntax:-
while(condition)
{code to be executed
}
To print even numbers between 1 to 100.
• #include <stdio.h>
• int main()
• { int i=2;
• while(i<=100)
• printf("%d ",i+=2); }
• return 0;
•}
• Output: 2 4 6 8………………100
do-while
• It is better if you have to execute the code at least once.
• Syntax:-
do
{code to be executed}
while(condition);
To print even numbers between 1 to 100.
• #include <stdio.h>
• int main()
• { int i=2;
• do
• {printf("%d ",i+=2); }
• while(i<=100);
• return 0;
•}
• Output: 2 4 6 8………………100
Draw a flowchart To print 1-100(while….loop)

i=1

False
Is i<=0
True
Print i

i++

stop
Draw a flowchart To print 1-100(do..…loop)

i=1

Print i

i++

True
Is i<=100

False

stop
H.W.
• To print multiplication table of n number.
• To find sum of 1to 100.
• To find sum of 1 to n.
• To find sum of m to n.
• Difference between while and do..while loop.
switch- case

• Syntax:
switch(expression)
{
case value1:code to be executed; break; optional
case value2:code to be executed; break;optional
default :code to be executed if all cases are not matched;
}
Array
• An array is collection of similar type of data items stored at
contiguous memory locations.
• An array is a derived data type.
• An array is used to represent a list of numbers , or a list of names.
How to declare it.
datatype arrayvariable[size];
Example:
int ar[10];
Array
Representation of an array

0 1 2 3 4 5 6 7 8 9 index
3 5 4 5 6 8 9 2 4 6
ar[0]

• How to access element of an array.


You can use array subscript (or index) to access any element stored in array.
Subscript starts with 0, which means arr[0] represents the first element in the array
arr.
Types of array
• 1. One-dimensional arrays
• 2. Multidimensional arrays –two dimensional, three dimensional…….
How to declare 1D array
datatype arrayvariable[size];
Example:
-An array of integer declared as :
int arr[];

-An array of float declared as:


float arr[];

-An array of char declared as:


char arr [];
How to initialize value in an array
• Example:
int arr[10]={1,2,3,4,5,6,7,8,9,10};
float arr[5]={1.1,2.1,3.0,4.1,5.2 };

char arr[8]={‘c’, ’o’, ‘m’, ‘p’, ‘u’, ‘t’, ’e’, ‘r’ };


Write a program in C to store ten elements in one dimensional array and display them .
#include <stdio.h>
#include<conio.h>
void main()
{
int ar[10],i;
for(i=0;i<10;i++)
{
printf(“Enter number ”);
scanf(“%d”,&ar[i]);
}
/*display elements of arrary*/
for(i=0;i<10;i++) printf(“%d “,ar[i]);
getch();
}
Output
Enter number 5
Enter number 10
Enter number 15
Enter number 20
Enter number 25
Enter number 30
Enter number 35
Enter number 40
Enter number 45
Enter number 50
5 10 15 20 25 30 35 40 45 50
Two dimensional array
How to declare it.
datatype arrayvariable[size][size];
Example:
int ar[3][3];
Write a program in C to store 9(3X3) elements in two dimensional
array and display them.
Answer
#include <stdio.h>
#include<conio.h>
void main()
{
int ar[3][3],r,c;
/* to store 9 elements in 2D Array*/
for(r=0;r<3;r++)
for(c=0;c<3;c++)
{
printf(“Enter number ”);
scanf(“%d”,&ar[r][c]);
}
/*display elements of arrary*/
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
printf(“%d “,ar[r][c]);
printf(“\n”);
}
getch();
}
Output
Enter number 5
Enter number 10
Enter number 15
Enter number 20
Enter number 25
Enter number 30
Enter number 35
Enter number 40
Enter number 45
Numbers are
5 10 15
20 25 30
35 40 45
String
• The string can be defined as the one-dimensional array of characters
terminated by a null ('\0’).
• The character array or the string is used to manipulate text such as word
or sentences.
• Each character in the array occupies one byte of memory.
• When we define a string as char s[10], the character s[10] is implicitly
initialized with the null in the memory.
• There are two ways to declare a string in c language.
-By char array char str[20];
-By string literal char str[9]=“computer”;
…..
• A single character is defined using single quote representation.
char a=‘y’;
• A string is represented using double quote marks.
Char c[]=“computer”
How to represent in memory
0 1 2 3 4 5 6 7 8
c o m p u t e r \0
To enter name and age of user and print it.
#include <stdio.h>
int main()
{ char name[10]; int age;
printf("Enter your first name and age: \n");
scanf("%s %d", name, &age);
printf("You entered: %s %d",name,age);
return 0;
}
#include <stdio.h>
#include<conio.h>
void main()
{ char name[10]; int age;
printf("Enter your first name and age: \n");
scanf("%s %d", name, &age);
printf(" entered: %s %d",name,age);
getch();
}
#include <stdio.h>
int main()
{ char name[15];
printf(“ënter name”);
gets(name); /*reads a string*/
puts(name); /*displays a string*/
return 0;}
• In order to read a string contains spaces, we use the gets() function.
Gets ignores the whitespaces. It stops reading when a newline is
reached .
• The puts function prints the string on an output device and moves the
cursor back to the first position.

You might also like