24/03/2023

Convert String to Integer in C Without Using Atoi Function
Write a Program to Convert String to Integer in C Without Using Atoi Function. C string to an integer. how to convert string to int in c without Atoi. Here we are converting a string to an integer without using an Atoi library function, first, pass the string to a function and then compare it with the first if else condition.

Convert String to Integer in C Without Using Atoi Function

If the condition is true then we can not convert a string into an integer and if the condition is false then we can convert a string to an integer number. Here we took an input number as a string, not as an integer that is why we have to convert the number to an integer or if we input the string then we cannot convert it.

Convert String to Integer in C Without Using Atoi Function


#include <stdio.h>
#include <string.h>

int strToint(char[]);

int main()
{
	/*Convert String to Integer in C Without Using Atoi Function*/

	while (1)
	{
		char str[10];
		int intNum;

		printf("\nEnter Integer Number: ");
		scanf("%s", str);

		intNum = strToint(str);

		if (intNum == 0)
		{
			printf("\nEnter The Number Not String\n\n");
		}
		else
		{
			printf("\nEquivalent Integer Value: %d", intNum);
		}
	}

	return 0;
}

int strToint(char str[])
{
	int i = 0, sum = 0;

	while (str[i] != '\0')
	{
		if (str[i] < 48 || str[i] > 57)
		{
			printf("\nCan't Convert Into Integer");
			return 0;
		}
		else
		{
			sum = sum *10 + (str[i] - 48);
			i++;
		}
	}

	return sum;

}

The Output of Convert String to Integer in C


The Output of Convert String to Integer in C

Similar to Convert String to Integer


Program to Check Prime Number in C Using Function
Write a Program to Check Prime Number in C between 1 to 100. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5. Enter the range to find all the Prime numbers within the range.

Program to Check Prime Number in C

Program to Check Prime Number in C

#include <stdio.h>

int check_prime(int num);

int main()
{
	int n1, n2, i, flag;
	printf("Enter two numbers(intervals): ");
	scanf("%d %d", &n1, &n2);
	printf("Prime numbers between %d and %d are: ", n1, n2);
	for (i = n1 + 1; i < n2; ++i)
	{
		flag = check_prime(i);
		if (flag == 0)
			printf("%d ", i);
	}

	return 0;
}

int check_prime(int num) /*User-defined function to check prime number*/
{
	int j, flag = 0;
	for (j = 2; j <= num / 2; ++j)
	{
		if (num % j == 0)
		{
			flag = 1;
			break;
		}
	}

	return flag;
}

Condition for Prime Number in C


A number that can only be divisible by Itself or 1. You can see that it's clear a number is divisible by itself or one then use a loop starting from one like condition=1 and max condition up to <=Number and divide a number by condition and increase the counter by ++ if the number divided after the end of the count is equal to 2 then the number is prime otherwise not.

The Output of Prime Numbers in C


The Output of Prime Numbers in C

Similar to Prime Number


22/03/2023

Print Pyramid Pattern in C | Number, Star, Alphabet & Invert
The patterns of the pyramid will be covered in this article. We shall learn about numbers, stars, and C language alphabets here. A polygon structure has pyramid motifs. By joining a base that will be a polygon and all the lateral sides that are triangles, a pyramid is generated. The apex, which connects all of the bases, forms a triangle with the base and the apex. Learning patterns can help you become a more logical programmer.

Pyramid Pattern in C

And when firms ask these kinds of pattern questions in examinations, competitions, and interviews, they are quite significant. Beginners can learn the basics of loops and spaces by using questions of this type. We will learn how to create pyramid patterns in this tutorial. Simple concepts are all that are required for patterns.

With the use of examples, we will go over how to design various patterns in this article.

Introduction of Number Patterns in C language


The ability to create logic while using the Flow Control Statements is increased by practising Pattern exercises, which are frequently advised by programmers as well as in publications. Additionally, it improves logical reasoning skills. This article will provide beginners and intermediate programmers with a collection of number patterns to practice.

C Program for Print Pyramid Pattern of Numbers


#include <stdio.h>

int main()
{
	int i, j;
	int k = 1;
	for (i = 0; i <= 4; i++)
	{
		printf("\n");
		for (j = 0; j < i; j++)
		{
			printf("%d\t", k);
			k++;
		}
	}

	return (0);
}

Output

1

2 3

4 5 6

7 8 9 10

Pyramid Pattern of Numbers Explanation


In this code, as we know, we have to print a triangle in the form of numbers 1 to 10. Initially, we have taken k=1, i = rows, j = column, and in this code, we will run two loops, one for each row and another one for the column, so the first loop says for(i = 0; i<=4;i++) the number of rows keeps on increasing one by one in further steps. Now in the second loop for(j = 0; j<i; j++), the number of columns will increase, and both the loops will keep on working till 10.

C Program to Print Triangle Pattern


#include <stdio.h>

int main()
{
	{
		int i, j;
		for (i = 1; i <= 4; i++)
		{
			for (j = 1; j <= i; j++)
			{
				printf("%d", i);
			}

			printf("\n");
		}

		return (0);
	}
}

Output

1

22

333

4444

Triangle Pattern Explanation


In this code, as we know, we have to print a triangle in the form of numbers 1 to 4 but this time in a row the numbers are the same. we know that i = rows, j = column, and in this code, we will run two loops, one for each row and another one for the column, so the first loop says for(i = 1; i <=4;i++) the number of rows keeps on increasing but following the same number, as we are not increasing the digit. Now in the second loop for(j = 1; j<=i; j++), the number of columns will increase.

C Program to Print This Square Pattern


#include <stdio.h>

int main()
{
	{
		int i, j;

		for (i = 1; i <= 4; i++)
		{
			for (j = 1; j <= 4; j++)

				printf("%d", i);

			printf("\n");
		}

		return (0);
	}
}

Output

1 1 1 1

2 2 2 2

3 3 3 3

4 4 4 4

Square Pattern Explanation


In this code, we want to print a square numeric pattern. Two loops will run here, as you can see in the first loop it’s written for(i=1;i<=4;i++)means each row will carry the same number but it is according to the second loop for(j=1;j<=4;j++) the number will increase.
Introduction of Star pattern in C

In the C programming language, you only need to perform two or three loops to create a star pattern. How many loops you use will depend on the pattern you need to make. One for a row and one for a column are required as the minimal number for a pattern. The first loop is referred to as an outer loop and displays the rows, while the second loop is referred to as an inner loop and displays the columns.

C Program to Print Star Triangle



#
include <stdio.h>
int main() { { int i, j, k; for (i = 1; i <= 5; i++) { for (j = 4; j >= i; j--) { printf(" "); } for (k = 1; k <= (2 i - 1); k++) { printf(”“); } printf("\n"); } return (0); } }

Output

*

* * *

* * * * *

* * * * * * *

* * * * * * * * *


Star Triangle Explanation


In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=1; i<=5; i++) Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=4; j>=i; j--).

Run a second inner loop from 1 to 2 * i - 1 to print the star. The appropriate loop form is for(k=1; k<=(2*i-1); k++).*This loop print has a star inside. Move to the next line, or print a new line, after printing each column in a row.

C Program to Print Inverted Star Pattern


#include <stdio.h>

int main()
{
	{
		int i, j, k;

		for (i = 4; i >= 1; i--)

		{

			for (j = 4; j > i; j--)

			{

				printf(" ");
			}

			for (k = 1; k < (i *2); k++)

			{

				printf(””);
			}

			printf("\n");
		}

		return (0);
	}
}

Output

* * * * * * *

* * * * *

* * *

*


Inverted Star Pattern Explanation


In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=4;i>=1;i--). Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=4; j>=i; j--).

Run a second inner loop from 1 to 2 * i - 1 to print the star. The appropriate loop form is for(k=1; k<(i2); k++).*This loop print has a star inside. Move to the next line, or print a new line, after printing each column in a row.

C Program to Print Star Triangle


#include <stdio.h>

int main()
{
	{
		int i, j;

		for (i = 1; i <= 6; i++)

		{

			for (j = 1; j < i; j++)

			{

				printf("*");
			}

			printf("\n");
		}

		return (0);
	}
}

Output

*

* *

* * *

* * * *

* * * * *


Star Triangle Explanation


In the above code, the two for loops are used as you can see. here we used i, j, and k as required. on the execution of both loops, stars will be printed. On the different lines. Move to the next line, or print a new line, after printing each column in a row.

C Program to Print Inverted Star Triangle


#include <stdio.h>

int main()
{
	{
		int i, j;

		for (i = 5; i >= 1; i--)

		{

			for (j = 1; j <= i; j++)

			{

				printf("*");
			}

			printf("\n");
		}

		return (0);
	}
}

Output

* * * * *

* * * *

* * *

* *

*

Inverted Star Triangle Explanation


In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=5; i>=1; i- - ). Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=1; j<=i; j++). This loop print has a star inside. Move to the next line, or print a new line, after printing each column in a row.
Introduction of Alphabet Pattern in C

Any alphabet pattern can be printed in C. Here, I'll show how to print the alphabet pattern in C with an explanation.

A set of upper or lowercase alphabets which together form a pattern or geometric shape, such as a square, pyramid, triangle, etc., is known as an alphabet pattern. Controlled loops that are nested are being used to produce these patterns.

C Program to Print Alphabet Pattern


#include <stdio.h>

int main()
{
	{
		int i, j;

		for (i = 1; i <= 6; i++)

		{

			for (j = 1; j <= i; j++)

			{

				printf("%c", 'A' + j - 1);
			}

			printf("\n");
		}

		return (0);
	}
}

Output

A

AB

ABC

ABCD

ABCDE

ABCDEF

Alphabet Pattern Explanation


In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=1;i<=6;i++). Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=1;j<=i;j++). And using this structure,'A' + j-1 Move to the next line, or print a new line, after printing each column in a row.

C Program to Print Alphabet Pattern


#include <stdio.h>

int main()
{
	{
		int i, j;

		for (i = 1; i <= 6; i++)

		{

			for (j = 1; j <= i; j++)
			{
				printf("%c", 'A' - 1 + i);
			}

			printf("\n");
		}

		return (0);
	}
}

Output

A

BB

CCC

DDDD

EEEEE

FFFFFF

Alphabet Pattern Explanation


In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=1;i<=6;i++). Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=1;j<=i;j++). And using this structure, 'A' -1+i; Move to the next line, or print a new line, after printing each column in a row.

C Program to Print Inverted Alphabet Program


#include <stdio.h>

int main()
{
	{
		int i, j;

		for (i = 1; i <= 6; i++)
		{
			for (j = 6; j >= i; j--)
			{
				printf("%c", 'A' - 1 + i);
			}

			printf("\n");
		}

		return (0);
	}
}

Output

AAAAAA

BBBBB

CCCC

DDD

EE

F

Inverted Alphabet Program Explanation


In this code, we will run an outer loop from 1 to i to iterate through the rows. The loop should be formatted as for(i=1; i<=6; i++). Run an inner loop from I to rows in order to iterate through the columns. The loop should be formatted as for(j=6;j<=i;j - -). And using this structure, 'A' -1+i Move to the next line, or print a new line, after printing each column in a row.
Conclusion

Pyramid patterns come in different types. The most crucial thing is to understand the structure of numbers and whitespaces. Once you understand the pattern, writing code in C or any other programming language will come naturally to you.

References:

https://www.youtube.com/watch?v=v3gcXj9k4k8
https://www.interviewbit.com/c-interview-questions/