Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

if ,if else ,if else if ,nested if statement in c language |decision making statement in c |conditional statements in c

what is if and else statement 

The if-else statement in C is a flow control statement used for decision-making in the C program. It is one of the core concepts of C programming. It is an extension of the if in C that includes an else block along with the already existing if block. 

if statement 

The if statement is the simplest form of a conditional statement. It allows you to execute a block of code if a specified condition is true. 
(In simple words we will get out put only for true condition).


 else statement 


The if-else statement allows you to specify two code blocks: one to execute if the condition is true, and another to execute if the condition is false.
(In simple words we will get out put both true and false conditions ). 

If else if statement 

The if else if statements are used when the user has to decide among multiple options. As soon as one of the conditions controlling the if is true, if-else-if ladder is similar to the switch statement.

Nested If-else Statements

nested if-else statements are implemented using an if statement inside another if statement. The syntax of nested if-else statements is straightforward, 

(Examples for if and if else statements)

Example :1 


#include<stdio.h>
#include<conio.h>
void main ()

{
    double a=2,b=5,c=5;

    if(a+b<=c||a+c<=b||b+c<=a)
        printf("these sides don't form a valid triangle.\n");

        if(a==b&&b==c)

            printf("the triangle is equilateral.\n");

        if((a==b&&a!=c)||(a==c&&a!=b)||(b==c&&b!=a))

        printf("the triangle is isosceles.\n");

            if(a!=b&&a!=c&&b!=c)
            printf("the triangle is scalene.\n");

            return 0;

}

OUTPUT

the triangle is isosceles.

Example 2


#include <stdio.h>
#include<conio.h>
void main()
{
    if(5<10)
     {

        printf("5 is less then 10.");
}
else
{
   printf ("5 is grater then 10.");
}
}

OUTPUT

5 is less then 10

Example 3


#include <stdio.h>
#include<conio.h>
void main()
{
   int year;
   printf("enter an year :");
   scanf("%d\n",&year);
   if(year%4==0)
   {
       printf("%d year is leap year",year);
   }
   else{
    printf("%d\n year is not leap",year);
   }
}

OUTPUT

enter an year:2008 
year is leap year,


Example :4


#include<stdio.h>
#include<conio.h>
void main()

{
    int num1;
    printf("enter a number:");
     scanf("%d\n",num1);

    if(num1&2==0)
    {printf("%d\n is an even number",num1);}
        else{
            printf("%d\n is an odd number",num1);
        } 

}
OUTPUT 

it depends on your given  integer 

IF ELSE IF EXAMPLES

Example 1

#include <stdio.h>
#include<conio.h>
void main()
{
    int x=5, y=10;

    if (x>y)
    {
        printf("x is greater then y");
    }else if (x==y)
    {
        printf("x is equal to y");
    }

    else if (y<x)
    {
       printf ("y is less then x");
    }
    else
    {
        printf("y is grater then x");
    }
}


OUTPUT


y is grater then x


Example 2 


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


int main()
{

    int marks = 91;

    if (marks <= 100 && marks >= 90)

        printf("A+ Grade");

    else if (marks < 90 && marks >= 80)

        printf("A Grade");

    else if (marks < 80 && marks >= 70)

        printf("B Grade");

    else if (marks < 70 && marks >= 60)

        printf("C Grade");

    else if (marks < 60 && marks >= 50)

        printf("D Grade");

    else

        printf("F Failed");

    return 0;
}

OUTPUT


A+ grade

Nested If-else Statements 

Example 1

#include<stdio.h>
#include<conio.h>
void main()
{
    int x=5;
    if(x>1)
    if(x==5){
        printf("x value is 5");
    }else{
    printf("x value is not 5");
    }
    else{
        printf("enter any value that should be greater then 1");
    }
}

OUtPUT

x value is 5

Example 2


#include <stdio.h>  
  #include<conio.h>
int main() {  
   int num;  
  
printf("Enter a number: ");  
scanf("%d", &num);  
  
   if (num > 0) {  
printf("%d is positive.\n", num);  
   } else {  
      if (num < 0) {  
printf("%d is negative.\n", num);  
      } else {  
printf("%d is zero.\n", num);  
      }  
   }  
  
   return 0;  
}  

OUTPUT 

Enter a number:5 

5 is positive. 

Enter a number:-2 

-2 is negative.


Example 3

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

int main () {


   int a = 50;
   int b = 100;


   if( a == 50 ) {


      if( b == 100 ) {

         printf("Value of a is 50 and b is 100\n" );
      }
   }

   printf("Exact value of a is : %d\n", a );
   printf("Exact value of b is : %d\n", b );

   return 0;
}

OUTPUT

Value of a is 50 and b is 100
Exact value of a is : 50
Exact value of b is : 100

Comments

Popular Posts

🌙