Showing posts with label Loops in C. Show all posts
Showing posts with label Loops in C. Show all posts

Multiple Initialization of a for loop

Multiple Initialization of a for loop 

Program to print this pattern of numbers
1     2
3     4
5     6
7     8
9     10



             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i,j;   
               clrscr();   
              printf("The two columns of natural numbers");
              for(i=1;j=2 ;i<=9 && j<=10; i+=2, j+=2)
              {
              
                      printf"%d    %d\n", i,j);  
              } 
                   getch(); 
                   return 0;
             }


Output :
The two columns of natural numbers
1     2
3     4
5     6
7     8
9     10

उपरोक्त प्रोग्राम में हमने दो variables i और j को एक ही loop में initialize किया है ।  


NEXT   : continue statement
PREVIOUS : do - while loop

do-while loop

do-while loop
C Programming मे तीसरा Loop, do-while loop है । do-while loop को exit controlled loop भी कहते है। exit controlled का मतलब इस Loop में condition, statement execute होने के बाद test होती है। इस loop की खास बात यह है कि condition, true हो या false हो do-while loop एक बार तो execute होता ही है।   
Syntax :
do-while loop
Example 
   /* Program to print natural numbers from 1 to 10 */      
              #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i;  
               clrscr();
               i=1;   
              printf("Natural Numbers from 1 to 10");
              do
              {
                printf("%d ", i );
                i++; 
              }while(i<=10); 
               getch(); 
               return 0;
             }

Output :

Natural Numbers from 1 to 10
1 2 3 4 5 6 7 8 9 10


    /* Program to find the H.C.F of two numbers by continued division method. */      
              #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int dividend, divisor, remainder;  
               clrscr();   
              printf("Enter two numbers");
              scanf("%d%d", &dividend, &divisor);
              do
              {
                 remainder = dividend % divisor;
                 if(remainder  == 0)
                 {
                      printf("The HCF ïs =%d", divisor);
                 }
                 else
                 {
                     dividend = divisor;
                     divisor = remainder;
                  }
   
              }while(reminder !=0); 
                   getch(); 
                   return 0;
             }


Output :
Enter two numbers 100 56
The HCF is = 4

Armstrong Numbers 
Armstrong numbers
 /* Program to check that a number is Armstrong  or not */      
              #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int number, sum=0; 
               int duplicate, digit; 
               clrscr();   
              printf("Enter the number");
              scanf("%d", &number);
              duplicate = number;
              do
              {
                 digit = number%10; 
                 sum =sum+digit*digit*digit;
                 number = number /10;
               }while(number > 0);
               
              if(duplicate == sum)
              {
                 printf("%d is Armstrong", duplicate);
              }
               else
              { 
                printf("%d is not Armstrong", duplicate);
              }

                   getch(); 
                   return 0;
             }


Output :
Enter the number 153
153 is Armstrong 












while loop

while Loop 
C Language में दूसरा loop, while loop है ।   while loop को Entry Controlled कहते है ।  Entry Controlled का मतलब है कि इनमें पहले condition check होती  है और बाद  statement(s) execute होते है। 
Syntax : 
Program to print numbers from 1 to 10 using while loop
1 2 3 4 5 6 7 8 9 10
    #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i; 
               clrscr();   
               printf("Numbers from 1 to 10\n");
               i=1;
               while(i<=10)
               {
                        
                  printf("%d ", i);   
                  i++;
                }
                      
                   getch(); 
                   return 0;
             }

Output :
1 2 3 4 5 6 7 8 9 10 

Program to print numbers from 1 to 10 using while loop
10 9 8 7 6 5 4 3 2 1 
              #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i; 
               clrscr();   
               printf("Numbers from 10 to 1\n");
               i=10;
               while(i>=1)
               {
                        
                  printf("%d ", i);   
                  i--;
                }
                      
                   getch(); 
                   return 0;
             }

Output :
10 9 8 7 6 5 4 3 2 1 





Nesting of for loop

Nesting of for loop
Nesting of for loop का मतलब for loop के अन्दर for loop । यहाँ बाहरी loop को outer loop और अदंर वाले loop को inner loop कहेगे।  

Syntax 

     
nesting of for loop



जब हमे Column और Row के रूप में  काम करना हो तो Nesting of loops का उपयोग करते है। Inner loop, Outer loop की प्रत्येक  Value के  लिए Execute होगा ।

एक Example के द्वारा समझते है । 
Program to print this pattern of numbers
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5
1 2 3 4 5 
1 2 3 4 5

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i,j;   
               clrscr();   
               for(i=1; i<=5; i++)
               {
                   for(j=1;j<=5;j++)
                  {
               
                       printf("%d ", j);   
                  }
                      printf"\n");  
              } 
                   getch(); 
                   return 0;
             }


Output :
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5
1 2 3 4 5 
1 2 3 4 5


उपरोक्त प्रोग्राम में Outer loop, row के लिए और Inner loop, Column के लिए । सबसे पहले program का control outer loop में i =1 पर आयेगा उसके बाद inner loop पर j=1 पर आ जायेगा और printf("%d", j), 1 से 5 तक values को print करायेगा जैसे ही j की value 6 होगी तो inner loop की condition false हो जायेगी और program का control, inner loop से बाहर  printf("\n) पर आयेगा और new line कर देगा उसके बाद वापस  Outer loop पर जायेगा  और i =2 हो जायेगी  इसके बाद inner loop पर  j=1 पर आयेगा और printf("%d", j) फिर से , 1 से 5 तक values को print करायेगा जैसे ही j की value 6 होगी तो inner loop की condition false हो जायेगी और program का control, inner loop से बाहर  printf("\n)  पर आयेगा और new line कर देगा उसके बाद वापस  Outer loop पर जायेगा  और i =3 हो जायेगी सके बाद inner loop पर  j=1 पर आयेगा और printf("%d", j) फिर से 1 से 5 तक values को print करायेगा जैसे ही j की value 6 होगी तो inner loop की condition false हो जायेगी और program का control, inner loop से बाहर  printf("\n)  पर आयेगा और new line कर देगा उसके बाद वापस  Outer loop पर जायेगा  और i =4 हो जायेगी इसके बाद inner loop पर  j=1 पर आयेगा और printf("%d", j) फिर से 1 से 5 तक values को print करायेगा जैसे ही j की value 6 होगी तो inner loop की condition false हो जायेगी और program का control, inner loop से बाहर  printf("\n)  पर आयेगा और new line कर देगा उसके बाद वापस  Outer loop पर जायेगा  और i =5 हो जायेगी सके बाद inner loop पर  j=1 पर आयेगा और printf("%d", j) फिर से 1 से 5 तक values को print करायेगा जैसे ही j की value 6 होगी तो inner loop की condition false हो जायेगी और program का control, inner loop से बाहर  printf("\n)  पर आयेगा और new line कर देगा उसके बाद वापस  Outer loop पर जायेगा  और i =6 हो जायेगी और outer loop की condition false हो जायेगी  और program का control,  outer loop  से  भी बाहर आ जायेगा, इसके बाद हमे उपरोक्त Output मिलेगा। ।    

उपरोक्त प्रोग्राम में हमने j की value को प्रिन्ट कराया तो हमे output यह मिला । 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5
1 2 3 4 5 
1 2 3 4 5
यदि उपरोक्त प्रोग्राम में हम i की value को प्रिन्ट करायेगे तो हमे output यह मिलेगा ।
1 1 1 1 1 
2 2 2 2 2 
3 3 3 3 3
4 4 4 4 4 
5 5 5 5 5 


Program to print this pattern of numbers
1 2  
1 2 3 
1 2 3 4  
1 2 3 4 5

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i,j;   
               clrscr();   
               for(i=1; i<=5; i++)
               {
                   for(j=1;j<=i;j++)
                  {
               
                       printf("%d ", j);   
                  }
                      printf"\n");  
              } 
                   getch(); 
                   return 0;
             }
Output :
1 2  
1 2 3 
1 2 3 4  
1 2 3 4 5


Program to print this pattern of stars. 
* * * * * 
* * * * 
* * * 
* * 



             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i,j;   
               clrscr();   
               for(i=1; i<=5; i++)
               {
                   for(j=i;j<=5;j++)
                  {
               
                       printf("* ");   
                  }
                      printf"\n");  
              } 
                   getch(); 
                   return 0;
             }
Output :

* * * * * 
* * * *
* * * 
* * 
*

NEXT : while loop
PREVIOUS : for loop-II


for loop

/* Program to print numbers from 10 to 1*/

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i;   
               clrscr();   
               printf("Numbers from 10 to 1:\n");  
               for(i=10;i>=1;i--)
               {
                       printf("%d ",i);   
               }   
                   getch(); 
                   return 0;
             }
Output :
Number from 10 to 1 :
10 9 8 7 6 5 4 3 2 1



अगर आपको 1 से 10 के नम्बर्स का जोड़ ज्ञात करना हो । उसके लिए प्रोग्राम इस तरह बनायगे। मगर प्रोग्राम बनाने से पूर्व क्रियाविधि को समझ लेते है। यहाँ अगर हम कोई variable जैसे x लेते है और उनमें कोई Value डाल देते है जैसे x=1 तो x की value 1 हो जाती है और अगर हम बाद में x=2 करदे तो x की value 1 से हट जायेगी और उसकी जगह value 2 आ जायेगी और अगर x=3 करदे तो x की value 2 हट जायेगी और उसकी जगह value 3 आ जायेगी इसका मतलब यह है कि अगर variable में कोई  value  है और हम कोई नई value  डालते है  तो पुरानी value हट जाती है। 

/* Program to find sum of numbers from 1 to 10*/

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i,sum=0;   
               clrscr();   
               printf("Sum of numbers from 1 to 10:\n");  
               for(i=1;i<=10;i++)
               {
                       sum =sum+i; 
               }   
               printf("Sum =%d",sum);  
                   getch(); 
                   return 0;
             }


Output :
Sum of Numbers from 1 to 10 :
Sum =55



उपरोक्त प्रोग्राम में हमने दो variables, i और sum=0 लिए है । sumकी value 0 इसलिए की है। क्योकि जब हम कोई variable declare करते है यदि उसमे कोई value store नही कराते है  तो उनमे कोई garbage value आ जाती है और प्रोग्राम का आउटपुट गलत आता है ।  इसलिए  उपरोक्त प्रोग्राम में sum=0 लिया है। और उक्त प्रोग्राम इस तरह से Execute होगा । सबसे पहले sum की value, 0 और i की value 1 आयेगी और स्टेटमेंट sum =sum+i, execute होगा जो निम्नप्रकार से है। 


sum


Program to find Factorial of a number (5!= 5x4x3x2x1) 


/* Program to find Factorial of a number*/

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i, num, fact=1;   
               clrscr();   
               printf("Enter a number\n");  
               scanf("%d",&num);

               for(i=num;i>=1;i--)
               {
                       fact =fact*i;   
               }   
               printf("Factorial =%d",fact);
               getch(); 
               return 0;
             }
Output :
Enter a number 5 :
Factorial = 120

उपरोक्त प्रोग्राम सिर्फ 1 से 7 तक की value का ही Factorial calculate करेगा अगर 7 से ज्यादा value input करायेगे तो Output गलत आयेगी क्योकि integer की range सिर्फ -32767 से +32678 तक ही होती है । अगर हमे 7 से ज्यादा value का factorial निकालना है तो हमे variables को long में declare करना होगा । 

Program to find   sum of even series (2+4+6.....................+20)


/* Program to find   sum of even series (2+4+6.....................+20)*/

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i, num, sum=0;   
               clrscr();   
               for(i=1; i<=20; i=i+2)
               {
                       sum =sum+i;   
               }   
               printf("Sum of series =%d",sum);
               getch(); 
               return 0;
             }
Output :
Sum of series = 110

Fibonacci Series 


/* Program to print the numbers of Fibonacci series*/

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i, n1=0,n2=1, next_term, choice, i;   
               clrscr();   
               printf("How many numbers");
               scanf("%d",&choice);
               printf("Fibonacci series: %d %d", n1, n2);
               for(i=1; i<=choice-2; i++)
               {
                      next_term = n1+n2;   
                      printf("%d", next_term);
                      n1 = n2;
                      n2 = next_term;
                }
               getch(); 
               return 0;
             }
Output :
How many terms : 10 
Fibonacci series : 0 1 1 2 3 5 8 13 21 34 


Fibonacci series में अगला नम्बर Previous two number का जोड़ होता है 
जैसे 
Fibonacci series



Base Exponent program 
             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int base, exponent, result=1;
               clrscr();   
               printf("Enter Base and Exponent :");
               scanf("%d %d",&base,&exponent);
               for(i=1; i<=exponent; i++)
               {
                    result = result*base;
                }
              printf("The Result is %d", result);
               getch(); 
               return 0;
             }
Output :
Enter Base and Exponent 3 2 
The Result is 9 





   /* Program for increasing the digits of a number */      
              #include<stdio.h>
             #include<conio.h>
             #include<math.h>

             int main(void)

             {   
               int i, number, sum=0, last_digit;  
               clrscr();   
              printf("Enter the number");
              scanf("%d", &number);
              
              for(i=0; number>0; i++)
              {
                 last_digit = number %10;
                 sum =sum(last_digit+1)*pow(10,i);
                 number = number /10;
              }
             printf("The number obtained by increasing the each digit is %d", sum);
             getch(); 
             return 0;
             }


Output :
Enter the numbers 3567
The number obtained by increasing the each digit is 4678
  
उपरोक्त प्रोग्राम में pow function के लिए math.h header file का use किया है। 



NEXT :  Nesting of for loop
PREVIOUS :for loop