[go: up one dir, main page]

0% found this document useful (0 votes)
21 views57 pages

Unit 4

Uploaded by

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

Unit 4

Uploaded by

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

Computer Programming with C - 102000110

Looping

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 1
Life is all about Repetition.
We do same thing everyday
What is loop?
 Loop is used to execute the block of code several times according to the condition
given in the loop. It means it executes the same code multiple times.

“Hello” 5

Output

printf("Hello\n"); Hello
printf("Hello\n"); Hello loop(condition)
{
printf("Hello\n"); Hello //statements
printf("Hello\n"); Hello }

printf("Hello\n"); Hello

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 3
if v/s while

Flowchart of if v/s Flowchart of while

False False
condition condition

True True

… …

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 4
Looping or Iterative Statements in C
Looping Statements are
Entry Controlled Loop: while, for
Exit Controlled Loop: do…while
Virtual Loop: goto

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 5

While loop

Computer Programming with C(102000110)


While Loop
 while is an entry controlled loop
 Statements inside the body of while are repeatedly executed till the condition is
true
 while is keyword
Syntax
while(condition)
{
// Body of the while
// true part
}

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 7
WAP to print 1 to n(while loop)

Program Output
1 #include <stdio.h> Enter n:10
2 void main() 1
3 { 2
4 int i,n; 3
5 i=1; 4
6 printf("Enter n:"); 5
7 scanf("%d",&n); 6
8 while(i<=n) 7
9 { 8
10 printf("%d\n",i); 9
11 i=i+1; 10
12 }
13 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 8
WAP to print multiplication table(while loop)

Program Output
1 #include<stdio.h> Enter n for multiplication table:5
2 void main() 5 * 1 = 5
3 { 5 * 2 = 10
5 * 3 = 15
4 int i=1,n;
5 * 4 = 20
5 printf("Enter n for multiplication table:"); 5 * 5 = 25
6 scanf("%d",&n); 5 * 6 = 30
7 while(i<=10) 5 * 7 = 35
8 { 5 * 8 = 40
9 printf("%d * %d = %d\n",n,i,n*i); 5 * 9 = 45
10 i=i+1; 5 * 10 = 50
11 }
12 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 10
WAP to Sum of 5 numbers entered by user(while loop)

Program Output
1 #include<stdio.h> Enter a number=10
2 void main() Enter a number=20
3 { Enter a number=30
Enter a number=40
4 int sum=0, i=1,n;
Enter a number=50
5 while(i<=5) Sum is=150
6 {
7 printf("Enter a number=");
8 scanf("%d",&n);
9 sum=sum+n;
10 i=i+1;
11 }
12 printf("Sum is=%d",sum);
13 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 11
Syntax and Logic
Swimming Rules To Swim
1. Breath control
2. Kicking legs
3. Back stroke with arms
4. Front stroke with arms
5. Crawling in water

Syntax Logic
while(condition) int i = 1;
{ while (i <= 5)
// Body of the while {
// true part printf("%d\n", i);
} i=i+1;
}

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 13
How to build logic? Step-1
Step 1: Understand the problem statement
 e.g. Write a program to find factors of a number.
 Run following questions through mind
 What is the factor of a number?
 Factor is a number that divides another number evenly with no remainder.
 For example, 1,2,3,4,6,12 are factors of 12.
 How many variables needed? What should be their data types?(Inputs/Outputs)
 To get number from user we need variable n.
 Now we need to divide n with 1,2,3,...,n. For this we will declare a loop variable i initialized as 1.
 Both variables should be of integer data type.
 What control structure you require?
 First we need a loop to divide n by 1,2,3,…,n, loop will start from 1 and ends at n.
 Inside loop we need if structure to check n%i==0 (Number n is evenly divisible by i or not).

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 14
How to build logic? Step-2
Step 2: Think for 1 or 2 examples
 Consider n=6, now take i=1
 6%1==0, TRUE; So, 1 is factor of 6
 6%2==0, TRUE; So, 2 is factor of 6
 6%3==0, TRUE; So, 3 is factor of 6
 6%4==2, FALSE; S0, 4 is not factor of 6
 6%5==1, FALSE; S0, 5 is not factor of 6
 6%6==0, TRUE; S0, 6 is factor of 6

 From this we can infer that loop variable i starts with 1 and incremented by one
for next iteration then ends at value n.
 Consider n=10, factors are 1,2,5,10
 Consider n=11, factor is 1,11
 From this we can infer that 1 and number itself are always factors of any number n.

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 15
How to build logic? Step-3
Step 3: Draw flowchart/steps on paper or in mind
Start
Steps
i=1 Step 1: Start
Step 2: Declare variables n,i
read n Step 3: Initialize variable
i ← 1
Step 4: Read value of n
i<=n? Step 5: Repeat the steps until i = n
True False 5.1: if n%i == 0
Display i
n%i==0? 5.2: i=i+1
True False Step 7: Stop
print i

i=i+1
Stop
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 16
How to build logic? Step-4
Step 4: Writing Pseudo-code
 Pseudo-code is an informal way to express the design of a computer program or
an algorithm.
 It does not require any strict programming language syntax.

Pseudo-code
Initialize i=1 integer
Declare n as integer
Input n
while i<n
if n%i
print i
end if
increment i=i+1
end while

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 17
WAP to find factors of a number(while loop)

Program Output
1 #include <stdio.h> Enter n to find factors=12
2 void main() 1,2,3,4,6,12,
3 {
4 int i=1,n;
5 printf("Enter n to find factors=");
6 scanf("%d",&n);
7 while(i<=n)
8 {
9 if(n%i==0)
10 printf("%d,",i);
11 i=i+1;
12 }
13 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 18
WAP to print reverse a number(while loop)

Program Output
1 #include <stdio.h> Enter a number=1234
2 void main() 4321
3 {
4 int n;
5 printf("Enter a number=");
6 scanf("%d",&n);
7 while(n!=0)
8 {
9 printf("%d",n%10);
10 n=n/10;
11 }
12 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 19
WAP to check given number is perfect or not(while loop)
1 void main(){
2 int i=1,n,sum=0;
3 printf("Enter a number:"); Output
4 scanf("%d",&n); Enter a number:6
5 while(i<n) 1+2+3=6
6 is a perfect number
6 {
7 if(n%i==0)
Output
8 {
9 printf("%d+",i); Enter a number:8
1+2+4+=7
10 sum=sum+i;
8 is not a perfect number
11 }
12 i=i+1;
Output
13 }
14 printf("=%d",sum); Enter a number:496
1+2+4+8+16+31+62+124+248+=496
15 if(sum==n) 496 is a perfect number
16 printf("\n%d is a perfect number",n);
17 else
18 printf("\n%d is not a perfect number",n);
19 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 20

for loop

Computer Programming with C(102000110)


for Loop
 for is an entry controlled loop
 Statements inside the body of for are repeatedly executed till the condition is true
 for is keyword
Syntax
for (initialization; condition; updateStatement)
{
// statements
}

 The initialization statement is executed only once.


 Then, the condition is evaluated. If the condition is false, the for loop is
terminated.
 If the condition is true, statements inside the body of for loop are executed, and
the update statement is updated.
 Again the condition is evaluated.
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 22
WAP to print numbers 1 to n (for loop)

Program Output
1 #include<stdio.h> Enter a number:5
2 void main() 1
3 { 2
4 int i,n; 3
5 printf("Enter a number:"); 4
6 scanf("%d",&n); 5
7 for(i=1;i<=n;i++)
8 {
9 printf("%d\n",i);
10 }
11 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 23
WAP to find factors of a number (for loop)

Program Output
1 #include <stdio.h> Enter n to find factors=12
2 void main() 1,2,3,4,6,12,
3 {
4 int i,n;
5 printf("Enter n to find factors=");
6 scanf("%d",&n);
7 for(i=1;i<=n;i++)
8 {
9 if(n%i==0)
10 printf("%d,",i);
11 }
12 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 24
WAP to check given number is perfect or not(for loop)
1 void main(){
2 int i,n,sum=0;
3 printf("Enter a number:"); Output
4 scanf("%d",&n); Enter a number:6
5 for(i=1;i<n;i++) 1+2+3=6
6 is a perfect number
6 {
7 if(n%i==0)
Output
8 {
9 printf("%d+",i); Enter a number:8
1+2+4+=7
10 sum=sum+i;
8 is not a perfect number
11 }
12 }
Output
13 printf("=%d",sum);
14 if(sum==n) Enter a number:496
1+2+4+8+16+31+62+124+248+=496
15 printf("\n%d is a perfect number",n); 496 is a perfect number
16 else
17 printf("\n%d is not a perfect number",n);
18 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 25
WAP to check given number is prime or not(for loop)
1 int main()
2 {
3 int n, i, c = 0; Output
4 printf("Enter any number n:"); Enter a number:7
5 scanf("%d", &n); 7 is a prime number
6 //logic
7 for (i = 1; i <= n; i++) {
Output
8 if (n % i == 0) {
9 c++; Enter a number:9
9 is not a prime number
10 }
11 }
12 if (c == 2) {
13 printf("n is a Prime number");
14 }
15 else {
16 printf("n is not a Prime number");
17 }
18 return 0;
19 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 26

do while loop
do while Loop
 do while is an exit controlled loop.
 Statements inside the body of do while are repeatedly executed till the condition
is true.
 Do and while are keywords. Syntax
do
{
// statement
}
while (condition);

 Loop body will be executed first, and then condition is checked.


 If the condition is true, the body of the loop is executed again and the condition is
evaluated.
 This process goes on until the condition becomes false.
 If the condition is false, the loop ends.
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 28
WAP to print Odd numbers between 1 to n(do while loop)

Program Output
1 void main() Enter a number:5
2 { 1,3,5
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 if(i%2!=0)
9 {
10 printf("%d,",i);
11 }
12 i=i+1;
13 }
14 while(i<=n);
15 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 29
WAP to find factors of a number(do while loop)

Program Output
1 void main() Enter a number:6
2 { 1,2,3,6,
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 if(n%i==0)
9 {
10 printf("%d,",i);
11 }
12 i=i+1;
13 }
14 while(i<=n);
15 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 30
WAP to print reverse a number(do while loop)

Program Output
1 void main() Enter a number=1234
2 { 4321
3 int n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 printf("%d",n%10);
9 n=n/10;
10 }
11 while(n!=0);
12 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 31

Types of jumping statement


➢In C programming language, there are control statements which do not
need any condition to control the flow of execution of a program. Such
statements are known as unconditional control statements.

The unconditional control statements provided by C are:


•break statement
•continue statement
•goto statement

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 33
break Statement
The break statement ends the loop immediately when it is encountered.
Its syntax is:
break;
❑How break statement works?

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 34
break Statement

Example:
#include <stdio.h> Output:
int main()
{ 0
int i; 1
for (i = 0; i < 10; i++) 2
{ 3
if (i == 4) {
break;
}
printf("%d\n", i);
}
return 0;
}

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 35
break Statement
// Program to calculate the sum of numbers (10 numbers max) . If the user enters a negative number, the loop
terminates

#include <stdio.h>
int main()
{
int i; Output:
double number, sum = 0.0; Enter n1: 1.0
for (i = 1; i <= 10; ++i) { Enter n2: 2.0
printf("Enter n%d: ", i); Enter n3: 3.0
scanf("%lf", &number); Enter n4: 4.0
// if the user enters a negative number, break the loop Enter n5: -3.5
if (number < 0.0) { Sum = 10.00
break;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf", sum);
return 0;
}
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 36
Continue Statement
The continue statement skips the current iteration of the loop and continues with the next
iteration. Its syntax is:
continue;
❑How continue statement works?

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 37
Continue Statement

Example: Output:
0
#include <stdio.h> 1
int main() { 2
int i; 3
5
for (i = 0; i < 10; i++) {
if (i == 4) { 6
continue; 7
} 8
printf("%d\n", i);
} 9

return 0;
}

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 38
Continue Statement
// Program to calculate the sum of numbers (10 numbers max). If the user enters a negative number, it's not added to
the result

#include <stdio.h> Output:


int main() { Enter a n1: 2.3
int i; Enter a n2: 4.5
double number, sum = 0.0; Enter a n3: 6.7
for (i = 1; i <= 10; ++i) { Enter a n4: -2.3
printf("Enter a n%d: ", i); Enter a n5: 2.2
scanf("%lf", &number); Enter a n6: -4.5
if (number < 0.0) { Enter a n7: 6.7
continue; Enter a n8: 8.9
} Enter a n9: 2.0
sum += number; // sum = sum + number; Enter a n10: -3.3
} Sum = 33.30
printf("Sum = %.2lf", sum);
return 0;
}

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 39
Difference between the break and continue statement

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 40
goto Statement
 goto is an virtual loop
 The goto statement allows us to transfer control of the program to the specified
label.
 goto is keyword
Syntax Syntax
goto label; label:
. .
. .
. .
label: goto label;

 The label is an identifier. When the goto statement is encountered, the control of
the program jumps to label: and starts executing the code.

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 41
WAP to print Odd numbers between 1 to n(goto)

Program Output
1 void main() Enter a number:5
2 { 1,3,5
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 odd:
7 if(i%2!=0)
8 {
9 printf("%d,",i);
10 }
11 i=i+1;
12 if(i<=n)
13 {
14 goto odd;
15 }
16 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 42
WAP to find factors of a number(goto)

Program Output
1 void main() Enter a number:6
2 { 1,2,3,6,
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 odd:
7 if(n%i==0)
8 {
9 printf("%d,",i);
10 }
11 i=i+1;
12 if(i<=n)
13 {
14 goto odd;
15 }
16 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 43
Types of loops
Entry Control Loop Entry Control Loop Exit Control Loop Virtual Loop
int i=1; int i; int i=1; int i=1;
while(i<=10) for(i=1;i<=10;i++) do labelprint:
{ { { printf("%d",i++);
printf("%d",i++); printf("%d",i); printf("%d",i++); if(i<=10)
} } } goto labelprint;
while(i<=10);

False
Loop Body Label Statement
condition
True True
condition condition
Loop Body
False True False
goto

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 44

Pattern
Always detect pattern in pattern
Pattern
There are important points to note in pattern
1. Determine, how many rows?
2. Determine, how many numbers/characters/columns in a row?
3. Determine, Increment/Decrement among the number of rows.
4. Determine, starting in each row

1 1 1 *
11 12 23 * *
111 123 456 * * *
1111 1234 78910 * * * *
11111 12345 * * *
* *
*

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 46
WAP to print given pattern (nested loop)
* Program
** 1 void main()
*** 2 {
**** 3 int i,j;
***** 4 for(i=1;i<=5;i++)
5 {
No. of rows: 5 6 for(j=1; j<=i; j++)
No. of characters 7 {
Row-1: * 8 printf("*");
Row-2: ** 9 }
Row-3: *** 10 printf("\n");
Row-4: **** 11 }
Row-5: ***** 12 }

Inner loop: Increment


Outer loop: Increment

Starting: *

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 47
WAP to print given pattern (nested loop)
1 Program
12 1 void main()
123 2 {
1234 3 int i,j;
12345 4 for(i=1;i<=5;i++)
5 {
No. of rows: 5 6 for(j=1; j<=i; j++)
No. of values 7 {
Row-1: 1 8 printf("%d",j);
Row-2: 12 9 }
Row-3: 123 10 printf("\n");
Row-4: 1234 11 }
Row-5: 12345 12 }

Inner loop: Increment


Outer loop: Increment

Starting: 1

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 48
WAP to print given pattern (nested loop)
5 Program
54 1 void main()
543 2 {
5432 3 int i,j;
54321 4 for(i=5;i>0;i--)
5 {
No. of rows: 5
6 for(j=5; j>=i ; j--)
No. of values 7 {
Row-1: 5 8 printf("%d",j);
Row-2: 54 9 }
Row-3: 543 10 printf("\n");
Row-4: 5432 11 }
Row-5: 54321 12 }

Inner loop: Decrement


Outer loop:
Decrement/Increment
Starting: 5
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 49
WAP to print given pattern (nested loop)
12345 Program
1234 1 #include <stdio.h>
123 2 int main() {
12 3 int i, j, rows;
1 4 printf("Enter the number of rows: ");
5 scanf("%d", &rows);
6 for (i = rows; i >= 1; --i)
7 {
8 for (j = 1; j <= i; ++j)
9 {
10 printf("%d ", j);
11 }
12 printf("\n");
}
return 0;
}

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 50
WAP to print given pattern (nested loop)
* Program
** 1 void main() First we need to print 4
*** 2 { spaces before printing *
**** 3 int i,j,k;
***** 4 for(i=1;i<=5;i++)
*
5 {
No. of rows: 5 6 for(k=5;k>i;k--) **

No. of values 7 { ***


Row-1: ----* 8 printf(" ");
****
Row-2: ---** 9 }
Row-3: --*** 10 for(j=1;j<=i;j++) *****
Row-4: -**** 11 {
Row-5: ***** 12 printf("*"); After printing spaces
13 } this inner loop prints *
Inner loop: Decrement 14 printf("\n");
Outer loop: Decrement/Increment 15 }
16 }
Starting: -(space)
Ending: *

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 51
Practice programs
1) Write a program to find sum of first N odd numbers. Ex. 1+3+5+7+………..+N
2) Write a program to find 1+1/2+1/3+1/4+....+1/n.
3) Write a program to print all Armstrong numbers in a given range. For example 153 = 1^3 + 5^3 +
3^3. So, 153 is Armstrong number.
4) Write a program to print given number in reverse order
5) Write a program to check whether a given string is palindrome or not.
6) Write a program to print Multiplication Table up to n.
1 2 3 4 5 6 7 .
2 4 6 8 10 12 14 .
3 6 9 12 15 18 21 .
4 8 12 16 20 24 28 .
5 10 15 20 25 30 35 .
. . . . . . . .

7) Construct C programs to print the following patterns using loop statement.


1 * 1 1 1 * * * * * * * * * *
22 # # 0 1 2 2 A B * * * * * *
333 * * * 1 0 1 3 3 3 2 3 4 * * * * *
4444 # # # # 0 1 0 1 4 4 4 4 C D E F * * * * * * *
55555 * * * * * *
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 52
 int main()
 {
 int x=900;
 printf("%8d", x);
 printf("\n%-8d",x);
 return 0;
 }

 In the above program, %8d specifier displays the value after 8 spaces while %-8d
specifier will make a value left-aligned.

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 53
 int main()
 {
 int x=12;
 printf("%08d", x);
 return 0;
 }

 In the above program, %08d means that the empty space is filled with zeroes and
total 8.

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 54
 int main()
 {
 float x=12.2;
 printf("%.2f", x);
 return 0;
 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 55
 #include<stdio.h>
 int main(){
 int number=50;
 printf("You\nare\nlearning\n\'c\' language\n\"Do you know C language\"");
 return 0;
 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 56
 #include <stdio.h>int main()
 {
 int number;
 float amount; number = 100;
 amount = 30.75 + 75.35;
 printf("%d \n",number); printf("%5.2f\n",amount);
 printf("%10.2f\n",amount);
 printf("%-10.2f",amount);
 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 57
 #include <stdio.h>
 int main()
 {
 int number; number = 100;
 float y=98.7654;
 printf("%d \n",number);
 printf("%10d \n",number);
 printf("%-10d \n",number);
 printf("%010d \n",number);
 printf("%10d \n",number);
 printf("%5.4f\n",y);
 printf("%f\n",y);
 printf("%7.4f\n",y);
 printf("%7.2f\n",y);
 printf("%-7.2f\n",y);
 }

Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Looping 58

Thank you

You might also like