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...

Square Pattern or NxN Pattern in C

Pattern Name= Square Pattern or NxN  Pattern,

Pattern N0=1

*********************************************** 

 * * * * *

 * * * * *

 * * * * *

 * * * * *

 * * * * *

#include<stdio.h>

int main()

{

        int num;

        printf("Enter the number:\n");

        scanf("%d",&num);

        for(int i=1;i<=num;i++)

 {

  for(int j=1;j<=num;j++)

 {

 printf(" *");

  }

 printf("\n");

}

}

 ************************************************

************************************************

 1 1 1 1 1

 2 2 2 2 2

 3 3 3 3 3

 4 4 4 4 4

 5 5 5 5 5

#include<stdio.h>

int main()

{

        int num;

        printf("Enter the number:\n");

        scanf("%d",&num);

        for(int i=1;i<=num;i++)

        {

                for(int j=1;j<=num;j++)

                {

  printf(" %d",i);

}

                printf("\n");

        }

}
***********************************************

 1 2 3 4 5

 1 2 3 4 5

 1 2 3 4 5

 1 2 3 4 5

 1 2 3 4 5

#include<stdio.h>

int main()

{

        int num;

        printf("Enter the number:\n");

        scanf("%d",&num);

        for(int i=1;i<=num;i++)

        {

                for(int j=1;j<=num;j++)

                {

                        printf(" %d",j);

                }

                printf("\n");

        }

}

***********************************************

 5 4 3 2 1

 5 4 3 2 1

 5 4 3 2 1

 5 4 3 2 1

 5 4 3 2 1

#include<stdio.h>

int main()

{

        int num;

        printf("Enter the number:\n");

        scanf("%d",&num);

        for(int i=num;i>=1;i--)

        {

                for(int j=num;j>=1;j--)

                {

                        printf(" %d",j);

                }

                printf("\n");

        }

}

***********************************************


 5 5 5 5 5

 4 4 4 4 4

 3 3 3 3 3

 2 2 2 2 2

 1 1 1 1 1

#include<stdio.h>

int main()

{

        int num;

        printf("Enter the number:\n");

        scanf("%d",&num);

        for(int i=num;i>=1;i--)

        {                for(int j=num;j>=1;j--)

                {

                        printf(" %d",i);

                }

                printf("\n");

        }

}

***********************************************

 A A A A A

 B B B B B

 C C C C C

 D D D D D

 E E E E E

#include<stdio.h>

int main()

{

        int num;

        printf("Enter the number:\n");

        scanf("%d",&num);

        for(int i=1;i<=num;i++)

        {

                for(int j=1;j<=num;j++)

                {

                        printf(" %c",i+64);

                }

                printf("\n");

        }

}

***********************************************

 A B C D E

 A B C D E

 A B C D E

 A B C D E

 A B C D E

#include<stdio.h>

int main()

{

        int num;

        printf("Enter the number:\n");

        scanf("%d",&num);

        for(int i=1;i<=num;i++)

        {

                for(int j=1;j<=num;j++)

                {

                        printf(" %c",j+64);

                }

                printf("\n");

        }

}
***********************************************

 E E E E E

 D D D D D

 C C C C C

 B B B B B

 A A A A A

#include<stdio.h>

int main()

{

        int num;

        printf("Enter the number:\n");

        scanf("%d",&num);

        for(int i=num;i>=1;i--)

        {

                for(int j=num;j>=1;j--)

                {

                        printf(" %c",i+64);

                }

                printf("\n");

        }

}

***********************************************

 E D C B A
 E D C B A
 E D C B A
 E D C B A
 E D C B A

#include<stdio.h>
int main()
{
        int num;
        printf("Enter the number:\n");
        scanf("%d",&num);
        for(int i=num;i>=1;i--)
        {
                for(int j=num;j>=1;j--)
                {

                        printf(" %c",j+64);


                }
                printf("\n");

        }

}
***********************************************


 

Comments

Popular Posts

🌙