Looping statements in C

Ratings:
(4)
Views:162
Banner-Img
  • Share this blog:

Looping statements and functionality in C Language

Loops :-

Set of instructions given to the compiler to execute set of statements. Until the condition becomes false, it is called Loop.

  • The basic purpose of Loop is code repetition.
  • In implementation , when we require the repetitions then always recommended to go for loops.
  • Generally iteration statements are called loops. Because way of the repetition will forms circle.
  • In “c” programming language there are three types of loops are available.

1) while

2) for

3) do-while

   

Syntax to while:-

Assignment, While (condition) { Statement 1; Statement 2; Statement 3; …………………..  

  • When we are working with iteration statement, generally it takes the repetition until the condition becomes false but if it is selection statement after execution of the body control will pass outside the body automatically.
  • When we are working with while, first if checks the condition, if the condition is true, then control will pass within the body and after execution of the body once again control will pass back to the condition
  • Until the condition becomes false, body will be repeated “n” no of times.

 

Increment Decrement
Assignment -> minCondition -> maxRelation   -></<= Add(+) Assignment -> maxCondition   -> minRelation  ->>/>= Sub(-)

    Print the no’s from 1 to 10:- From left to right    Void main() { Int I; I=1; While(i<=100) { Printf(!”%d”,i); I=i+1; } } O/P : 12345678910   Print the no’s in decrement order from top Void main() { Int i; I=15; While(i>=10) { Printf(“\n%d”,i); I=i-1; } } o/p: 15 14 13 12 11 10

  • “\n” is a special character which is used to print new line i.e vertical format data.

     

  • Void main()

{ Int I,n; Clrscr(); Printf(“Enter a value: ”); Scanf(“%d”, &n); I=2; While(i<=n) { Printf(“%d”,i); I=i+2; } Getch(); } o/p:- Enter a value=10 2 4 5 6 10  

  • Program to accept the no from user

Enter a value  : 12345 Sum of 1+5=6  

  • Program

Case1: Enter two values: 10 20 10 12 14 16 18 20 Case 2: Enter two values: 9 20 10 12 14 16 18 20 Case 3: Enter two values 20 10 20 18 16 14 12 10 Case 4:- Enter two values: 21 10 20 18 16 14 12 10  

  • Void main()

{ Int n1,n2; Clrscr(); Printf(“Ener two values:”); Scanf(“%d %d”, &n1,&n2); If (n1<=n2) { If (n1%2!=0)  ||    if(n1%2==1) N1=n1+1; While(n1<n2) { Printf(“%d”,n1); N1=n1+2; } Else { If(n1%2==1) || if (n1%2!=0) N1=n1-1; While(n1>=n2) { Printf(“%d”,n1); N1=n1-2; } } Getcch(); } Enter a value 50 0 1 2 3 5 8 13 21 34 Void main() { Int n,I,j,k; Clrscr(); Printf(“Enter a value”); Scanf(“%d”, &n); If(n>0) { I=0; J=1; Printf(“%d %d”,I,j); K=i+j; While(k<=n) { Printf(“%d”,k); I=j; J=k; K=i+j; } } Else Printf(“\  input should be >0”); Get ch(); }

 

Write a program to find first n febinosis numbers i.e if input value is 20. Then print first 20 febinoces no’s

Void main() { Int n,rn,sd,nd; Clrscr(); Printf(“Ener a value”); Scanf(“ %4d”,&n); Rn=sd=nd=0;  

While (n>0){Rn=rn*10+n%10; Sd=sd+n%10; Nd=nd+1; N=n/10; } Printf(“\n Reverse value is:%d ”,m); Printf(“\n sum of digits : %d”,sd); Printf(“\n no.of digits :%d”,nd); Getch(); } o/p: Enter a value 1234 Reverse no: 4321 Sum of digits : 10 No.of digits : 4  

 

N Rn Sd Nd
1234123   12   0 00+4=440+3 43 430+2 432 320+1 432 00+4=44+3 7 4+2 6 9+1 10 00+1=11+1 2 2+1 3 3+1 4

   

  • In scanf statement , when we are passing %4d scanf() specifier, then it will read. “four digit” data only from the user.
  • If we are providing more than “4” digits , then automatically compiler will ignore remaining digits.

  Void main() { Int n,I; Clrscr(); Printf(“enter a value”); Scanf(“%d”,&n); I=1; While(i<=10) { Printf(“\n %d * %2d=%2d”,n,I,n=i); I=i+1; } Getch();   o/p : 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50  

  • In printf statement , when we are passing %2d format specifier, then it indicates that “2” digit decimal value need to be printed, if two digits are not occurred , then go for right alignment. I.e digit will be printed towards right side and space will be printed towards leftside.
  • In printf statements, when we are passing %2d format specifier, then it indicates that “2” digit decimal value required to print. If two digits are not occurred then fo for left alignment I.e digit will be printed left side and space will be printed towards right side
  • In printf, if we are passing %5.3d format specifier then it indicates that 5 digit decimal value required to print, if 5 digits are not occurred then apply the right alignment , but mandatary to print “3” digits and of three digits are not occurred, then fill with “Zeros”.
  • In printf, when we are passing %-5.3d format specifier then it indicates , a “5” digit decimal value required to print if “5” digits are not occurred, then go for left alignment but mandatory to print “3” digits, if three digits are not occurred then fill with “Zero”

 

 

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox