Posts

Showing posts with the label Decision Making and Loops

Differentiate entry control and exit control loop.

  Difference between Entry control loop and Exit control loop: Basic Of Comparision Entry Controlled Loop Exit Controlled Loop Description Entry controlled loop is a loop in which the test condition is checked first, and then the loop body will be executed. Exit controlled loop is a loop in which the loop body is executed first and then the given condition is checked afterward. False Test Condition If the test condition is false, the loop body will not be executed. If the test condition is false, the loop body will be executed at least once. Example Examples of entry-controlled loops include, for loop and while loop. An example of an exit controlled loop is do…while Loop. Use Entry controlled loops are used when checking of test condition is mandatory before executing the loop body. Entry controlled loop is used when checking of test condition is mandatory after executing. Drafted on 🌏 by, Jal ☮ peace

Syntax

Image
Simple if if( Condition ) {     //code if the above condition is true. } if...else if( Condition ) {     //code if the above condition is true. } else {     //code if the above condition is false. } if...else ladder if( Condition 1 ) {     //code if the above condition 1 is true. } else {      if( Condition 2 )      {          //code if the above condition 2 is true.      }      else      {          // some code if the above condition 2 is false.      } } Nested if...else if( Condition 1 ) {      if( Condition 2 )     {         //code if the above condition 2 is true.     }     else     {         //code if the above condition 2 is false.     } } else {   ...

Program to print multipliaction table using exit control loop

Program: #include<stdio.h> #include<conio.h> void main() {     int i=1, number;      clrscr();     printf("Enter a number: ");     scanf("%d",&number);     do{         printf("%d * %d = %d\n",number,i,number*i);         i++;     }while(i<=10);     getch(); } Output: Enter a number: 5 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

How does a C program to check prime number work?

 Courtesy: Programiz.com In the program, a for loop is iterated from  i = 2  to  i < n/2 . In each iteration, whether  n  is perfectly divisible by  i  is checked using: if (n % i == 0 ) { flag = 1 ;   break ; } If  n  is perfectly divisible by  i ,  n  is not a prime number. In this case,  flag  is set to 1, and the loop is terminated using the  break  statement. Notice that we have initialized  flag  as 0 during the start of our program. So, if  n  is a prime number after the loop,  flag  will still be 0. However, if  n  is a non-prime number,  flag  will be 1.

How does a C program to count number of digits in an integer work?

 Courtesy: Programiz.com The integer entered by the user is stored in variable  n . Then the  do...while   loop  is iterated until the test expression  n! = 0  is evaluated to 0 (false). After the first iteration, the value of  n  will be 345 and the  count  is incremented to 1. After the second iteration, the value of  n  will be 34 and the  count  is incremented to 2. After the third iteration, the value of  n  will be 3 and the  count  is incremented to 3. After the fourth iteration, the value of  n  will be 0 and the  count  is incremented to 4. Then the test expression of the loop is evaluated to false and the loop terminates. Note:  We have used a  do...while  loop to ensure that we get the correct digit count when the user enters  0 . Share on:

How does a C program to find LCM of two Numbers work?

Courtesy: Programiz.com In this program, the integers entered by the user are stored in variable  n1  and  n2  respectively. The largest number among  n1  and  n2  is stored in  max . The LCM of two numbers cannot be less than  max . The test expression of  while  loop is always true. In each iteration, whether  max  is perfectly divisible by  n1  and  n2  is checked. if (min % n1 == 0 && max% n2 == 0) { ... } If this test condition is not true,  max  is incremented by  1  and the iteration continues until the test expression of the  if  statement is true. The LCM of two numbers can also be found using the formula: LCM = (num1*num2)/GCD  

How does a C program to check vowel or consonant work?

 Courtesy: Programiz.com The character entered by the user is stored in variable  c . The  lowercase_vowel  variable evaluates to 1 (true) if  c  is a lowercase vowel and 0 (false) for any other characters. Similarly, the  uppercase_vowel  variable evaluates to 1 (true) if  c  is an uppercase vowel and 0 (false) for any other character. If either  lowercase_vowel  or  uppercase_vowel  variable is 1 (true), the entered character is a vowel. However, if both  lowercase_vowel  and  uppercase_vowel  variables are 0, the entered character is a consonant.