Chapter 3. Control structure in ‘C’

Chapter 3. Control structure in ‘C’



Question 1.Write a C program to display prime numbers between 1 to 100.

Program :
#include<stdio.h>       // including standard input output library
#include<stdbool.h>     // including standard boolean library to get support of boolean data type
void main()             // main function
{
    bool isPrime;       // declaration of 'isPrime' variable of type boolean
    int i,j;            // declaration of 'i' and 'j' variable of type integer
    printf("The prime numbers between 1 and 100 : \n");        // simple print statements

    for(i=2;i<=100;i++)     // looping statement 'for loop'
    {
        // we 're going to check prime numbers between 1 to 100.
        // so started loop from 2 to 100.
        // 1 is not a prime number so we are excluding number 1 from the loop.
        isPrime = false;
        // lets considering the number which we' ll get from variable 'i' is not a prime number

        for(j=2;j<=i-1;j++)     //optimized loop => for(j=2;j<=i/2;j++)
        {
            // now to check the number is prime or not, the number should not give reminder as 0
            // so we are starting to divide the number from 2 to (number-1)

            if((i % j) == 0)    // condition to check if the number give reminder as 0
            {
                isPrime = true;     // if above condition is true, the number is not a prime number
                break;              // if we found that the number is not a prime
                                        //we dont need to check further
                                        // so we can break the loop.
            }
        }
    if(isPrime == false)    // now if the number is not giving reminder than
                                        //that number is prime number.
        printf("\n>> %d is a Prime Number ",i);     // simple print statement.
    }
}


Question 2. Write a program to select and print the largest of the three nos. using nested­ if­else statement.

#include <stdio.h>
void main()     // main function initialize
{
    int n1, n2, n3;     // declaring 3 integer variable named as n1,n2 and n3

    printf("Enter three numbers: \n");  // displaying simple message to get 3 inputs

    // passing address of n1, n2 and n3 to store values which we 're going to get from user
    scanf("%d %d %d", &n1, &n2, &n3);   // run time initialization of n1, n2 and n3.

    printf("\n>>Using ternary operator : \n");  // simple printf statement to print strings

    // Using ternary operator to find maximum of 3 numbers
    // syntax : condition ? statement1 : statement2 ;
    (n1>n2)?((n1>n3)?printf("max number: %d",n1):printf("max number: %d",n3)):((n2>n3)?printf("max number: %d",n2):printf("max number: %d",n3));

    printf("\n\n>>Using conditional statements : \n");  // simple print statement.

    if (n1>=n2) // conditional statement to check the condition
    {
        // Remember, if you want to execute only one statement after if condition then no need
        // to write that statement into braces or brackets.
        if(n1>=n3)  // conditional statement to check the condition
            printf("max number: %d", n1);   // print statements
        else        // else part without braces or brackets because only one statement to execute.
            printf("max number: %d", n3);
    }
    else
    {
        if(n2>=n3)
            printf("max number: %d", n2);
        else
            printf("max number: %d",n3);
    }
}



Question 3.  Write the program to print pattern for
*     
* *     
* * *    
* *  *  * 

Program :

#include<stdio.h>
void main()
{
     int i,j;
     for(i=1;i<=4;i++)
     {
          for(j=1;j<=i;j++)
          {
               printf(“*”);
          }
      printf(“\n”);
      }
}


Question 4. Write a C Program to check whether the given number is prime or not.

Program:

#include <stdio.h>
int main()
{
    int n, i, flag = 0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=2; i<=n/2; ++i)
    {
        // condition for nonprime number
        if(n%i==0)
        {
            flag=1;
            break;
        }
    }

    if (flag==0)
        printf("%d is a prime number.",n);
    else
        printf("%d is not a prime number.",n);
   
    return 0;
}


Question 5.  Write a program to perform addition, multiplication, subtraction and division with switch statement.

Program :

#include<stdio.h>

void main()
{
    int choice, a, b;           // Declaration of variable 'choice', 'a' and 'b' of type integer
    int sum, div, mul, sub;     // Declaration of variable 'sum', 'div', 'mul', 'sub' of type integer
    char c= 'y';    // Declaring and Initializing character variable 'c' with value 'y'

    while(c=='y')   // looping statement > while
    {
        // Giving options to perform certain operation !!
        printf("\nAddition  -> 1\nSubtraction  -> 2 \nMultiplication  -> 3 \nDivision  -> 4\n");
        printf("\aEnter your choice of operation to perform -: \n");
        scanf("%d",&choice);    // run time initialization to choice variable
        printf("\a\nEnter two number :- \n");   // asking for parameter
        scanf("%d%d",&a,&b);    // run time initialization of variable 'a' and 'b'
        switch(choice)  // switch statement to perform selected operation.
        {
            case 1: sum = a + b;    // if given choice is 1 than this case will execute
            printf("\a\n Sum = %d", sum);   // simple print statement
            break;  // break statement will exit from the switch block.

            case 2: sub = a - b;    // if given choice is 2 than this case will execute
            printf("\a\n Difference = %d", sub);
            break;

            case 3: mul = a * b;    // if given choice is 3 than this case will execute
            printf("\a\n Multiplication = %d", mul);
            break;

            case 4: div = a - b;    // if given choice is 4 than this case will execute
            printf("\a\n Division = %d", div);
            break;

            default : printf("\aWRONG CHOICE");     // if non of the choice met than this default statement will execute
        }
        printf("\nDo you want to continue [y / n]");    // if you want to perform any operation again than press y
        // if you give y than it will be stored into 'c' variable
        c=getch();
        // now again check the condition of while loop,
        // if it is true than again perform all the task
        // if it is false than exit from while loop
    }
}


Question 6.  Write a program to print the following pattern.          
1          
0  1          
1  0   1          
0   1   0   1

Program:

#include<stdio.h>
void main()
 {
  int i,j;
  for(i=1;i<=4;i++)
  {
     for(j=1;j<=i;j++)
     {
         if((i+j)%2==0)
         {  
           printf(“1”);
          }
         else
         {
           printf(“0”);
         }
      }
      printf(“\n”);
   }
}


Comments

Popular posts from this blog

Write a C Program to Find Transpose of a Matrix.