Translate

Thursday, April 30, 2020

C Program to swap two numbers without using third(temporary) variable.



    /*C Program to swap two numbers without third variable*/
    In C we have two techniques to swap the number
    1. Without using third variable
    2. Using third variable

    1. Without using third variable swap the number:


    #include<stdio.h>

    #include<conio.h>
    void main()
    {
        int a, b;
        printf("Enter First number in a = ");
        scanf("%d",&a);
        printf("Enter Second number in b = ");
        scanf("%d",&b);
        a= a+b;
        b=a-b;
        a=a-b;
        printf("\nafter swaping the value of a = %d", a);
        printf("\nafter swaping the value of b = %d", b);
        getch();
    }

    Output:


    Wednesday, April 29, 2020

    Check eligibility

    /*Program to check whether a person is eligible to vote or not */

    Control Statement - if else Statement

    #include<stdio.h>

    #include<conio.h>
    void main()
    {
        int age;
        printf("Enter Your Age? = ");
        scanf("%d",&age);
        if(age>=18)
          {
              printf("You are eligible to vote");
          }
        else
         {
              printf("You are not eligible to vote");
         }
         getch(); 
    }
    Output:




    Tuesday, April 28, 2020

    C Program to calculate gross salary of an employee

    /* Enter Basic Salary and Calculate Gross Salary of employee */

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        float bs, gs, da, hra;
        printf("Enter Basic Salary = ");
        scanf("%f", &bs);
        if(bs<1500)
          {
              hra=bs*10/100;
              da=bs*90/100;
          }
          else
          {
              hra=500;
              da=bs*98/100;
          }
          gs=bs+hra+da;
          printf("Gross salary = Rs %f", gs);
          getch();
    }
    Output:





    Sunday, April 26, 2020

    C Program to find the Largest Number among three numbers

    /* Print the Largest Number*/

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
       int a, b, c;
     printf("enter value in a = ");
     scanf("%d",& a);
     printf("enter value in b = ");
     scanf("%d",& b);
     printf("enter value in c = ");
     scanf("%d",& c);

      if(a>b)
         {
             if(a>c)
                  printf("\na is Largest %d", a);
               else
                  printf("\nc is Largest %d", c);
         }
         else
         {
             if(b>c)
                printf("\nb is Largest %d",b);
               else
                printf("\nc is Largest %d",c);
         }
        getch();

    }
    Output:




    C Program to find the smallest number among three number

    #include<stdio.h>

    #include<conio.h>
    void main()
    {
         int a, b, c;
         
        printf("enter value in a = ");
        scanf("%d",& a);
        printf("enter value in b = ");
        scanf("%d",& b);
        printf("enter value in c = ");
        scanf("%d",& c);
        
    if(a<b)
         {
             if(a<c)
                  printf("\na is smallest %d", a);
               else
                  printf("\nc is smallest %d", c);
         }
         else
         {
             if(b<c)
                  printf("\nb is smallest %d",b);
               else
                  printf("\nc is smallest %d",c);
         }
        getch();

    }

    Output:

    Saturday, April 25, 2020

    The else if ladder

    The else if ladder

    /*Control Statement: else if ladder*/


    • The else if Ladder Statement:
    This is a type of nesting in which there is an if...else statement in every else part except the last else part. This type nesting is called the else if Ladder Statement.

    The Syntax of else if Ladder:

    if(Condition 1)
           Statement A;
    else if(Condition 2)
             Statement B;
     else if(Condition 3)
                  Statement C;
      else if(Condition 4)
                    Statement C;
           else
                     Statement D;

    The Example of else if Ladder :

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        float per;
      
        printf("Enter your percentage = ");
        scanf("%f",&per);
        if(per>=85)
        printf( "grade B");
        else if(per>=70)
        printf( "grade B");
        if(per>=55)
        printf( "grade ='C'");
        else if(per>=40)
        printf( "grade D");
        else
        printf("Fail");
        getch();
           
    }

    Output:





    The Nested if-else Statements :

    The Nested if-else Statement

    /*Control statement Nested if-else statement*/

    • The Nested if-else Statements :
    In C language, we can have another if..else statement in the if block or the else block. This is called nested if-else.

    The Syntax of Nested if-else:

    if(condition 1)
    {
        if(condition 2)
              statement A1;
           else
              statement A2;
    }
    else
    {
       if(condition 3)
              statement B1;
            else
              statement B2;
    }

    The Example of Nested if-else:


    #include<stdio.h>

    #include<conio.h>
    void main()
    {
         int a, b, c;
         
        printf("enter value in a = ");
        scanf("%d",& a);
        printf("enter value in b = ");
        scanf("%d",& b);
        printf("enter value in c = ");
        scanf("%d",& c);
        
    if(a<b)
         {
             if(a<c)
               printf("\na is smallest %d", a);
               else
                printf("\nc is smallest %d", c);
         }
         else
         {
             if(b<c)
                printf("\nb is smallest %d",b);
               else
                printf("\nc is smallest %d",c);
         }
        getch();
    }

    Output:







         



    Thursday, April 23, 2020

    C program to check even or odd

    C program to Check Whether the Given Number is Even or Odd.

    Even numbers are integers that are completely divisible by 2 and has no remainder. For example- 2, 4, 6, 8, 0
    Odd numbers are not completely divisible by 2. either leaves a remainder or the result is a fraction. For example- 1, 3, 5, 7, 9.

    Here is, Program to check Even or Odd.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int num;
        printf("enter number = ");
        scanf("%d",& num);
        if(num%2==0)
            printf("The number is Even");
        else
            printf("The number is Odd");
        getch();

    }
    Output:
    When user enter the even number the output is..

    When user enter the Odd number the output is.

    Wednesday, April 22, 2020

    Check if the number is divisible by two..if-else program

    * The if-else statement


     Q. Request the user to inputs an integer and, if the number is divisible by two, divides it by two, otherwise multiplies it by three and output the result.
    Ans.
    #include<stdio.h>
    #include<conio.h>
      void main()
    {
        int num;
        
        printf("enter the integer number in num = ");
        scanf("%d",& num);
        //check is the number is divisible by two
        if(num%2==0)
            { 
               num = num/2;
               printf("The number is divisible by 2 so divide it by 2 \n and after divided by 2 the result is = %d ", num );
            }
            else
            {
               num=num*3;
               printf("the number is not divisible by 2 so multiply it by 3 \n and after mulplied by 3 the result is = %d", num);
            }
            getch();
            
    }

    Output:
    • when user enter the number that is divisible by 2 then the output is....


    • when user enter the number that is not divisible by 2 then the
    output is.....






    The if-else statement

    /*Control Statement:if else statement*/

    *  Click Here, for Control Statement:-simple if statement

    •  The if-else Statement:

    The if-else statement is a bi-conditional statement. in this statement, if the condition is true then the one part or program is executed otherwise other part of the program is executed.

    The syntax of the if-else statement: 

    if(condition is true)

       execute condition is true statement;
    else
       execute condition is false statement;



    The Flowchart of if-else statement: 


    The Example of if-else statement: 

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int num;
        printf("enter a number less than 5 =");
        scanf("%d",&num);
        if(num<5)
         printf("Good Your input is correct");
       else
         printf("Oops!! Your input is Incorrect");
         getch();
    }

    Output:

    Tuesday, April 21, 2020

    The Decision Control Structure or Control Statement

    /*Control Statement: Simple if statement*/

    By Default the instructions in a program are executed sequentially.
    and many a times, we want a set of instructions to be executed in one situation, and an entirely different set of instruction to be executed in another situation. In that time, Control statements allow us to change the sequence of instructions for execution. In other word, control statement define how the control is transferred to other parts of the program. In C language there are four types of control statements which are as-

    • if statement
    • goto statement
    • switch statement
    • loop
    The if statement in C can be used in various forms.
    i.) simple if statement
    ii.) if else statement
    iii.) Nested if-else Statement
    iv.) else-if Ladder


    •  The simple if Statement:


    In the if statement the if keyword is used. if keyword is always enclosed within a pair of parentheses. If the condition, whatever is it, is true, then the statement is executed otherwise skipped it.

    The syntax of the simple if statement: 

    if(this condition is true)
    execute this statement;

    The Flowchart of if statement: 




    The Example of if statement: 

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int num;
        printf("enter a number less than 5 =");
        scanf("%d",&num);
        if(num<5)
         printf("This is called simple if statement");
         getch();
    }

    Output:






    In general we express the condition using relational operators. the relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than other or less than. So this table define how can use relational operators in different conditions.

    This expressionis true if
    x==yx is equal to y
    x!=yx is not equal to y
    x<yx is less than y
    x<yx is greater than y
    x<=yx is less than or equal to y
    x>=yx is greater than or equal 




    Monday, April 20, 2020

    Request the user for two integers and print their remainder after division

    /*Print remainder after division*/



    #include<stdio.h>
    #include<conio.h>
     void main()
    {
      int x, y, remain,div;
    printf("Enter your first integer in x =");
    scanf("%d",&x);
    printf("Enter your second integer in y =");
    scanf("%d",&y);
    //division
    div=x/y;
    printf("division = %d",div);     
    //modulus
    remain=x%y;
    printf("\nremainder after division = %d",remain);
    getch();
    }
    Output:









    Useful Notes:

    • Division: The ‘/’ divide operator divides the first operand by the second. For example, div=x/y. Here, dividend is divided by the divisor.
    • Modulus: The ‘%’  modulus operator returns the remainder when first operand is
    • divided by the second. The modulus "%" operator give us remainder  For example, remain=x%y.

    Request the user for two integers and output them and their sum.

    Saturday, April 18, 2020

    Operators in C

    /*Operators And Expressions*/

    An operators specifies an operation to be performed that yields a value. operators are essential to form expression and to perform arithmetical and logical operations on  values or variables. some operators require two operands, while other act upon only one operand. An operand is a data item on which an operator acts.
    C includes types of operators. which are as-






  1. Arithmetic operators
  2. Assignment operators
  3. Increment and Decrements operators
  4. Relational operators
  5. Logical operators
  6. Conditional operators
  7. Comma operators
  8. Sizeof operator
  9. Bitwise operators
  10. Other operators

  11. 1.) Arithmetic Operators 

    Arithmetic operators are used for arithmetic and numeric calculations which perform addition, subtractions, multiplication etc. they are of two types...

    i. Unary arithmetic operators
    ii. Binary arithmetic operators
    • Unary Arithmetic Operators: unary operators require only one operand. 
    example: +x -y
    • Binary Arithmetic Operators
    Binary operators require two operands. 
    example: 5+4-9
    The "+" and "-" symbols are called operators and 5,4,9 are the operands.

        Operators
       Purpose
       +   
       Addition
     -
       Subtraction
     *
       Multiplication
     /  
       Division
    %
        Remainder

    Example of arithmetic Operators:

    #include<stdio.h>
    #include<conio.h>
     void main()
    {
    int x, y, result;
    printf("Enter your first integer in x =");
    scanf("%d",&x);
    printf("Enter your second integer in y =");
    scanf("%d",&y);
    //Addition
    result=x+y;
    printf("\n addition = %d",result); 
    //Subtraction
    result =x-y;
    printf("\nSubtraction = %d",result);     
    //division
    result=x/y;
    printf("\ndivision = %d",result); 
    //Remainder
    result=x%y;
    printf("\nremainder after division = %d",result);
    getch();
    }
    Ouput:









    2.) Assignment Operators

    The assignment operator is represented by equal sign(=). it is assign a value.
    example: a=10
    Here, 10 is assigned to a.

    3.) Increment and decrements Operators

    The increment and decrements operators in C are represented by ++ and -- sign. The operator "++" means add 1 and the operators "--" means subtract 1.
    for example: 
    int a=5; 
    a=a+1; / ++a;
    Result a=6
    int a=5; 
    a=a-1; / --a;
    Result a=4
    These operators are two type-
    i.) Prefix increments/decrements:  Here, operator is written before the operand. e.g. ++a or --a.
    ii.) Postfix increment/decrements: Here, operators is written after the operand. e.g. a++ or a--.

    For example:
    #include<stdio.h>
    #include<conio.h>
     void main()
    {
    int x ;
    printf("Enter your first integer in x =");
    scanf("%d",&x);
    printf("\n value of x = %d",x);  
    //prefix increment ++x
    printf("\n prefix increment = %d",++x);
    printf("\n after prefix increment the value of x = %d",x); 
    //prefix decrements --x
    printf("\n prefix decrements= %d",--x); 
    printf("\n after prefix decrements the value of x = %d",x); 
    //postfix increment x++
    printf("\n postfix increment = %d",x++);
    printf("\n after postfix increment the value of x = %d",x);
    //postfix decrement x--
    printf("\n postfix decrements = %d",x--); 
    printf("\n after postfix decrements the value of x %d",x); 
    getch();

    }
    Output:












    4.)Relational Operators


    relational operators such as greater than or less than, are used to compare value between two variables.an expression contains relational operators is called relational expression. the relational operators are.

    OperatorsPurpose
    <less than
    <=less than or equal to
    ==equal to
    !=not equal to
    >greater than
    >=greater than or equal to

    5.) Logical Operator

    An expression that combines two or more expression is termed as a logical expression. for combining these expression we used logical operators.
    C allow three types of logical operators, namely 
    OperatorsPurpose
     &&Logical And
    ||Logical Or
    !Logical Not

    6.) Conditional Operator

    The conditional operator(? and :) require three expression as operands. it is also called ternary operator. the syntax or the conditional operator is 
    conditional expression ? expression1 : expression2;
    void main()
    {
     int x,y;
    if(x>y)? a:b;
    }
    a is true and b is false.

    7.) Comma Operators

    The expression is separated by the comma operator.
    for example: x=5, b=9;  

    8.) sizeof Operator

    sizeof Operator is used to return the size of its operand in terms of bytes. it is an unary operator.

    9.) Bitwise Operator

    bitwise operators operate on integers only.

    OperatorsPurpose
    &bitwise AND
    |bitwise OR
    ~one's complement
    <<left shift
    >>right shift
    ^bitwise XOR






    C program to make a calculator using the switch statement.

    /*perform arithmetic calculation on integers in C language*/ #include<stdio.h> #include<conio.h> void main() {     char op;     ...