Saturday, 11 December 2021

If else statement

 // if-else statement is a conditional control statement.

// it is used to perform opraitons based on some conditions.
// there are four types of if-else statement:_ if statement,
if-else statement, if else if ladder,
// there are besic flow charts of all those in the notes.
// syntex for if statement like this
/* if ("condition")
   {
    "if code"
   }
   */
// syntex for if else statement like this :_
/* if ("condition")
 {
     "if code" (if condition true )
 }
 else{
     "else code" (if statement false)
 }*/
//  syntex for if _else if ladder like this
/* if (condition_1)
{
    "condition_1 code" (condition_1 true)
}
else if (condition_2)
{
    "condition_2 code" (condition_2 true)
}
..
..
..
..
{at last}
else (condition_n)
{
    "condition_n code" ( all previous conditions are flase)
}
*/
project_1 (if only)

#include <stdio.h>
int main()
{
    int age;
    printf("enter your age:\n");
    scanf ("%d",&age);

    printf ("your age is %d as you entered, ", age);

    if (age>18)  
    {
        printf ("so you can vote!");

    }

    return 0;
}

project_2 (if-else)
#include <stdio.h>
int main()
{
    int age;
    printf("enter your age:\n");
    scanf ("%d",&age);

    printf ("your age is %d as you entered, ", age);

    if (age>=18)  
    {
        printf ("so you can vote!");
    }
    else
    {
        printf ("so you can't vote!");
       
    }
    return 0;
}

project_3( if-else if ladder)
#include <stdio.h>
int main()
{
    int age;
    printf("enter your age:\n");
    scanf ("%d",&age);

    printf ("your age is %d as you entered, ", age);

    if (age>=18)  
    {
        printf ("so you can vote for adults!");
    }

    else if (age>10)
    {
        printf("so you can vote for teenagers!");
    }

    else {
        printf ("so you can't vote.");

    }
    return 0;

}
// project:_9 (password match or not)
#include <stdio.h>
int main()
{
    int a;
    printf ("enter the last digits of your password: ");
    scanf ("%d",&a);

    if (a==4321)
    {
        int num1, num2;
        printf ("enter a new numerical password: ");
        scanf ("%d", &num1);

        printf ("re-enter your new numerical password: ");
        scanf ("%d", &num2);

        if (num1==num2)
        {
            printf ("congratulations! your password has changed sucessfully.");
        }
        else{
            printf ("sorry! entered & reentered passwords doesn't matched.");
        }
    }
    else{
        printf("password is incorrect.");
   
    }
    return 0;
}
output:_
PS E:\c programming> cd "e:\c programming\" ; if ($?) { gcc new.c -o new }
; if ($?) { .\new } enter the last digits of your password: 321 password is incorrect. PS E:\c programming> cd "e:\c programming\" ; if ($?) { gcc new.c -o new }
; if ($?) { .\new } enter the last digits of your password: 4321 enter a new numerical password: 54321 re-enter your new numerical password: 4321 sorry! entered & reentered passwords doesn't matched. PS E:\c programming> cd "e:\c programming\" ; if ($?) { gcc new.c -o new }
; if ($?) { .\new } enter the last digits of your password: 4321 enter a new numerical password: 54321 re-enter your new numerical password: 54321 congratulations! your password has changed successfully.


// project_15 (election pole student council)
#include <stdio.h>
int main ()
{
    int num,a,b,c;
    printf ("enter your roll number: ");
    scanf ("%d", &num);

    printf ("enter your age: ");
    scanf ("%d", &a);

    printf ("your age is %d as you entered,",a);

    if (a>=18)
    {
        printf (" so you vote for youth club.\n");

        printf ("whom you want to give your vote?\n",b);
        printf ("press 1 to vote for santa:\n",b);
        printf ("press 2 to vote for banta:\n",b);
        printf ("press 3 to vote for nota:\n",b);
        scanf ("%d", &b);

        printf ("you successfully voted for %d.", b);
    }

    else if (a>13)
    {
        printf (" so you vote for teenage club.\n");

        printf ("whom you want to give your vote?\n",c);
        printf ("press 01 to vote for ravi:\n",c);
        printf ("press 02 to vote for vijay:\n",c);
        printf ("press 03 to vote for nota:\n",c);
        scanf ("%d", &c);

        printf ("you successfully voted for %d.", c);
    }
    else {
        printf (" sorry! you aren't eligible to vote for the
student council.");
    }
    return 0;
}
output:_
enter your roll number: 3 enter your age: 21 your age is 21 as you entered, so you vote for youth club. whom you want to give your vote? press 1 to vote for santa: press 2 to vote for banta: press 3 to vote for nota: 1 you successfully voted for 1.
 

No comments:

Post a Comment

here we are to help you.you can watch videos and get information about :- (1) scientific facts (2) education (3) gaming (4) music (5) lyrics (6) cartoons and many more.So keep watching keep learning, thank you.

Recurtions

  //Why are recursions? //We can solve difficult long iteratively can be solved simply recursively. // there are some problems which suite...