Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

17/07/2019

Design Different types of Pattern in C Programming, Pyramid, Pascal, Floyd's, etc: 

We draw some pattern using C-Language here using control statement.

1. Print Half-Pyramid Using Number in C Programming:

Print Half-Pyramid Using Number in C
Half-Pyramid Using Number

Important Topics:

1. For-loop

Code: You can download the image and also copy the code from below the image.

Print Half-Pyramid Using Number in C
Code

#include <stdio.h>
int main()
{
    int i, j, rows;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=1; i<=rows; ++i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}

Output: Output is given below

Print Half-Pyramid Using Number in C
Output

               ————————————————✖️———————————————

2. Print Half-Pyramid Using Alphabet in C Programming:

 Print Half-Pyramid Using Alphabet in C Programming
Half-Pyramid using Alphabet

Important Topics:

1. For-loop

Code:

 Print Half-Pyramid Using Alphabet in C Programming
Code


#include <stdio.h>
int main()
{
    int i,j;
    char input,alphabet = 'A';
    printf("Enter the uppercase character you want to print in last row: ");
    scanf("%c",&input);
    for(i=1;i<=(input-'A'+1);++i)
    {
        for(j=1;j<=i;++j)
        {
            printf("%c ",alphabet);
        }
        ++alphabet;
        printf("\n");
    }
    return 0;
}

Output:

 Print Half-Pyramid Using Alphabet in C Programming
output

                  ————————————————✖️———————————————



3. Print Inverted Half-Pyramid Using numbers in C Programming:

Print Inverted Half-Pyramid Using numbers in C Programming
Inverted half-pyramid

Important Topics:


1. For-loop

Code:

Print Inverted Half-Pyramid Using numbers in C Programming
code

#include<stdio.h>
int main()
{
  int j,i,n;
  printf("How many Rows?");
  scanf("%d",&n);
for(i=n;i>0;i--)
  {
    for(j=1;j<=i;j++)
    printf("%d ",j);
    printf("\n");
  }
  printf("\n");
}

Output: 

Print Inverted Half-Pyramid Using numbers in C Programming
Output

                  ————————————————✖️———————————————


4. Print Inverted Half-Pyramid Using '*' in C Programming:

Print Inverted Half-Pyramid Using '*' in C Programming
Inverted half-pyramid using *




Important Topics:

1. For-loop


Code:

Print Inverted Half-Pyramid Using '*' in C Programming
Code

#include<stdio.h>
int main()
{
  int j,i,n;
  printf("How many Rows?");
  scanf("%d",&n);
for(i=n;i>0;i--)
  {
    for(j=i;j>0;j--)
    printf("* ");
    printf("\n");
  }
  printf("\n");
}

Output:

Print Inverted Half-Pyramid Using '*' in C Programming
Output


                 ————————————————✖️———————————————



5. Print Full-Pyramid Using '*' in C Programming:

Print Full-Pyramid Using '*' in C Programming
Full Pyramid

Important Topics:

1. For-loop


Code:

Print Full-Pyramid Using '*' in C Programming
Code



#include<stdio.h>
int main()
{
  int j,i,n,k=0;
  printf("How many Rows?");
  scanf("%d",&n);
for(i=1;i<=n;i++,k=0)
  {
    for(j=1;j<=n-i;j++)
    printf(" ");
    while(k!=2*i-1)
    {
    printf("*");
    ++k;
    }
    printf("\n");
  }
}

Output:


Print Full-Pyramid Using '*' in C Programming
Output




                ————————————————✖️———————————————


6. Print Full Inverted Pyramid in C-Programming:


Print Full Inverted Pyramid in C-Programming
Full Inverted Pyramid

Important Topics:

1. For-loop

Code:

Print Full Inverted Pyramid in C-Programming
Code

#include<stdio.h>
int main()
{
  int j,i,n;
  printf("How many Rows?");
  scanf("%d",&n);
for(i=n;i>0;i--)
  {
    for(j=i;j>0;j--)
    printf("* ");
    printf("\n");
  }
  printf("\n");
}

Output: 

Print Full Inverted Pyramid in C-Programming
Output

————————————————✖️———————————————


7. Print Floyd's Triangle in C-Programming:

Print Floyd's Triangle in C-Programming
Floyd's Triangle

Important Topics:

1. For-loop

Code:

Print Floyd's Triangle in C-Programming
Code

#include <stdio.h>
 int main()
 {   
 int n, i, j, k= 1;   
 printf("Enter number of rows: ");   
 scanf("%d",&n);   
 for(i=1; i <= n; i++)   
 {       
 for(j=1; j <= i; ++j)       
 {           
 printf("%d ", k);           
 ++k;       
 }       
 printf("\n");   
 }   
 return 0;
 }

Output: 

Print Floyd's Triangle in C-Programming
Output

            ————————————————✖️———————————————


8. Print Pascal Triangle in C-Programming:


Print Pascal Triangle in C-Programming
Pascal Triangle

Important Topics:

1. For-loop
2. if-else

Code:

Print Pascal Triangle in C-Programming
Code
#include<stdio.h>
int main()
{
int i,j,n,space,k;
printf("Enter the rows numbers:");
scanf("%d",&n);
for(i=0; i<n; i++)
    {
        for(space=1; space <=n-i; space++)
            printf("  ");
        for(j=0; j <= i; j++)
        {
            if (j==0 || i==0)
                k = 1;
            else
                k = k*(i-j+1)/j;
            printf("%4d", k);
        }
        printf("\n");
    }
    return 0;
}

Output: 

Print Pascal Triangle in C-Programming
Output
————————————————✖️———————————————

16/03/2019

Find g.c.d of two integers in C-Programming using for-loop:

First of all, what are g.c.d means? g.c.d means Greatest common divisor, it is also known as h.c.f i.e. Highest common factor. For example, g.c.d of 9 and 6 is 3 since 3 is the greatest integer which divides both 9 and 6. Again g.c.d of 5 and 3 is 1.
Find g.c.d of two integers in C Programming using for loop

You can see also:

2. Design * triangle in C-Programming

Important Tools:

1. for-loop
2. if-else

Code:

Find g.c.d of two integers in C-Programming using for-loop

#include<stdio.h>
int main()
{
  int a,b,g,i;
  printf("Enter the two integer numbers:");
  scanf("%d%d",&a,&b);
{
  for(i=1;i<=a && i<=b;++i)
  if(a%i==0 && b%i==0)
  g=i;
}
  printf("The gcd of %d and %d is %d",a,b,g);
}

Output: 

The output is given below.
Find g.c.d of two integers in C-Programming using for-loop


Explanation:

1. We use Header Package <stdio.h> here.
2. We use 4 integers variables a,b,g and i. a and b are used to storing the value of given numbers from users. g is for getting value of g.c.d and i is for for-loops.
3. How does the for-loop works here:
If any users give two integers say a=4 and b=10 then it will entered in the loop with this value of a and b and start with initial condition i =1 and the condition is I should be less than both a and b and pre-increment the value of i by 1. After entering in the loop it will check if condition i.e. after dividing both a and b with I, the residue should be 0 then it will give the value of g.c.d.

You can see also:

2. Find the transpose of a matrix in C-Programming
3. Find the largest number in C-Programming

04/03/2019

Design  *  triangle in C-Programming:

  Here we Design a triangle with "*". You have to give number of rows and it will print that triangle including given number of rows with element "*". See the picture below, its looks like this.
Design  *  triangle in C-Programming

Important Tools:

1. for-loop


You can also see:

2. Find the Transpose of a Matrix

Code:

Design  *  triangle in C-Programming


#include<stdio.h>
int main()
{
  int i,j,n;
  printf("Enter the number of row:");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    for(j=0;j<=i;j++)
    {
      printf("*");
    }
    printf("\n");
  }
}

Output:

The output is given below:

You can also see:

1. Find the largest number in C-Programming
2. Print a MATRIX in C-Programming using array


Explanation:

1. First we use the header package <stdio.h>. To know more visit Header Package.
2. We use 3 integers variable i,j and n.
3. Then we used n for store the value of row number and we used i and j for for-loop.
4. Lets see how for-loop works here with an example. So the code is-
  for(i=0;i<n;i++)
  {
    for(j=0;j<=i;j++)
    {
      printf("*");
    }
    printf("\n");
  }
here let n=3, then first it will take i=0 and enter in the first for-loop. After that it will enter in 2nd for-loop with the initial value of j=0 and it will print a * . Again it will exist from 2nd for-loop and take a new line and again go to the 1st for-loop. Therefore it will take i=1 check the condition that is less than 3 and enter into the loop, then it will enter into the 2nd for loop and  take j=0 and print a * and again it will go into 2nd loop and increment the value i.e j=1 which is equal to 1 and again it will print a *. So it will print two * side by side. Now it again go to the first loop and take i=2 and will enter in the 2nd for-loop and it will do that same thing 3 times for j=0,j=1 and j=2, then it will exists from 2nd loop and finished here because first for-loop does not work for i=3.  

01/03/2019

Design a Latin square in C-Programming:

So first of all we have to know what is a Latin square? By definition a Latin square is an arrangement of letters or symbols that occur n times, in a 2-dimensional array of n2 compartments so that no letters or numbers appears twice in the same row or column. For more information about Latin square visit here. An example of 3x3 Latin square is given below. 
Design a Latin square in C-Programming

Important Tools:

1. for-loop.
2. Array.

Related Link:

1. How to print a matrix.
2. Sum of two matrix.

Code:

Design a Latin square in C-Programming

#include<stdio.h>
int main()
{
  int n,a[10][10],i,j;
  printf("Enter the value of n:");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  for(j=0;j<n;j++)
  a[i][j]=(i+j)%n+1;
  printf("The Latin for %d will be\n",n);
  for(i=0;i<n;i++)
  {
    for(j=0;j<n;j++)
    {
    printf("%3d",a[i][j]);
    }
    printf("\n");
  }
}

Output:

The output is given below:
Design a Latin square in C-Programming

Explanation:

1. We use header package <stdio.h>.
2. Therefore we introduced 4 int variable n, a[10][10], i, j. Where a[10][10] is array type variable.
3. First of all we used n for store the order of Latin square.
4. Therefore we used for-loop to evaluate all the value for each compartment of that Latin square.
5. Meaning of
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
  a[i][j]=(i+j)%n+1;
       If we consider for the value n=3 then we have the value in a[0][0] is {(0+0)%3+1}=(0+1)=1. Again for a[0][1] we have the value {(0+1)%3+1}=(1+1)=2, for a[0][2] we have {(0+2)%3+1}=(2+1)=3. and so on it will give the value of a[1][0], a[1][1], a[1][2], a[2][0], a[2][1] and a[2][2] are 2,3,1,3,1 and 2 respectively.
6. Then we again use for-loop to print the value of the Latin square. It works similar as we already mention on this Explanation no 6.
7. You can see that we use "%3d" to print the numbers of the Latin square. The reason for using "%3d" is, it will print the numbers giving two space between every to consecutive number.

You can also see:

1. Find the largest number in C-Programming.
2. Find the terms of Fibonacci sequence in C-Programming (Using For-loop)
3. Sum of n real numbers using array

26/02/2019

Find the transpose of a matrix in C-Programming:

 First of all we have to know what is the definition of the transpose of a matrix. For a matrix A, we define the transpose of A by AT interchanging the elements of corresponding row and column of A. More easily we have to swap the 1st row with the 1st column and 2nd row with the 2nd column and so on. So here we find the transpose of a matrix in C-Programming.
Find the transpose of a matrix in C-Programming
Transpose of Matrix

Important:

1. for-loop
2. use of array

You can also see:

Representation of a matrix 
Sum of two matrix


Code:

Find the transpose of a matrix in C-Programming
Find the transpose of a matrix in C-Programming

#include<stdio.h>
int main()
{
  int n,m,i,j,a[20][20];
  printf("Enter the order of the matrix:");
  scanf("%d%d",&n,&m);
  printf("Enter the element of the matrix row wise:");
  for(i=0;i<n;i++)
  {
  for(j=0;j<m;j++)
  scanf("%d",&a[i][j]);
  }
  printf("The given matrix is:\n");
  for(i=0;i<n;i++)
{
  for(j=0;j<m;j++)
  {
  printf("%d \t",a[i][j]);
  }
  printf("\n");
}
  printf("The transpose of the given matrix is:\n");
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    printf("%d \t",a[j][i]);
  }
  printf("\n");
}
}

Output:

The output is:
Find the transpose of a matrix in C-Programming

Explanation:


1. First we used the header package <stdio.h>. Visit Header package to know more.
2. We include the integer variables n,m,i,j and a[20][20]. First we used n for store how many row and m for how many column. i and j have been used for for-loop. a[20][20] has been used to store the element of the matrix. Here we use int a[20][20] for store only the integer element in the matrix, you can use float a[20][20] for store the real number.
3. Then we store the order of the matrix in n and m with scanf.
4. Then we store all the elements of the matrix using for-loop row wise.
5. After that we display the matrix using for-loop by printf syntax.
6. Next we display the transpose of the matrix.


Lets see with an example how the first for-loop works-
The loops is like this:
for(i=0;i<n;i++)
  {
  for(j=0;j<m;j++)
  scanf("%d",&a[i][j]);
  }
Suppose that user want to type a 3x2 matrix. So n=3 and m=2, now 1st for-loop start from i=0 which less than 3 so it will go into 2nd for loop and start with j=0 which is less than 2 so it will enter in the bracket of 2nd for-loop and scan the number and store it in 00 position. Now it will leave from the bracket and increased by 1 so j will be now 1 and which is less than 2 so it again enter into the 2nd for-loop and doing the same process and it will store the number in the 01 position and exit and increasing j with 1. Now j will be 2 which is not less that 2 therefore it will not entered in the 2nd for-loop.  It will again enter 1st for-loop for i=1 and doing the same process and exit and will increase the value of i that is, it will be 2 which is less than 3 and it will enter in the 1st for-loop and doing the same process. It will running till the condition is satisfied.

22/02/2019

Find the largest number in C-Programming:

 Here we find the largest number among 3 three numbers. We first give the required code then we will explain. This programming is valid for real numbers not for complex numbers.
Find the largest number in C-Programming

Important Tools:

1. if-else

Code:

Find the largest number in C-Programming


#include<stdio.h>
int main()
{
  float a,b,c;
  printf("Enter the three numbers:");
  scanf("%f%f%f",&a,&b,&c);
  if(a>b && a>c)
  printf("%f is the largest number among %f,%f,%f",a,a,b,c);
  if(b>a && b>c)
  printf("%f is the largest number among %f,%f,%f",b,a,b,c);
  else
  printf("%f is the largest number among %f,%f,%f",c,a,b,c);
}

Output:

The output is given below:
Find the largest number in C-Programming

Explanation:

1. First of all we used header package <stdio.h>.
2. We use 3 float variables a,b,c for store the three numbers which are given by the users.
3. We use if-else here. First if is used for if a is the largest and the second if is used for if b is largest and for c we use the else case. For know more about the use of if-else command, please visit our syntax page.
4. Point that i don't use any curly bracket after the command if and else. Since only one statement are there after if command and also after else command so it not necessary to use this curly bracket. You can use it if you want there is no problem.

                                       If you have any doubts comment below.

20/02/2019

Print a MATRIX in C-Programming using array: 

Here we print a matrix with the help of array; so what is a matrix? - Matrix is a collection of numbers arranged into a fixed number of rows and columns. The numbers can be real or complex, but we use real numbers here.
Print a MATRIX in C-Programming using array

Code:

Print a MATRIX in C-Programming using array

#include<stdio.h>
#include<conio.h>
int main()
{
  int m,n,a[100][100],i,j;
  printf("Enter the order of the matrix:\n");
  scanf("%d%d",&m,&n);
  printf("Give the %d*%d matrix",m,n);
  printf("\n");
  for(i=0;i<n;i++)
  {
    printf("Enter the %d row\n",i+1);
    for(j=0;j<m;j++)
    scanf("%d",&a[i][j]);
  }
  printf("The given matrix is:\n");
  for (i=0;i<n;i++)
  {
    for(j=0;j<m;j++)
    printf("%d\t",a[i][j]);
    printf("\n");
  }
}

Output: 

The output is given below-
Print a MATRIX in C-Programming using array

Explanation: 

1. First we use header package <stdio.h> and <conio.h>. To know more visit package page.
2. We use 5 integer variables. we use m and n for taking the order of the matrix. Then we use a[100][100] variable which called array its means that a variable has 100x100 box space, easily you can think that it would give a 10000 box space. You can change the array size 100 by changing it with any number. We use i and j variable for the use of for-loop.
3. First we take the order of the matrix by users and store it in m and n.
4. After that we store the numbers in variable a[100][100] with the help of for-loop. we scan the numbers and store all the numbers in a[100][100] variable. Now we have to print all the numbers that we stored it in the variable a[100][100], for that we use for-loop again for to print those numbers.

If you have any doubts then comment below.

17/02/2019

Find the terms of Fibonacci sequence in C-Programming (Using For-loop): 

First of all we have to know what is Fibonacci Sequence. So Fibonacci sequence starts with two initial value 0 and 1. After that the next term of the sequence will be addition of 0 and 1 that is 1.Now we have three terms of the sequence they are 0,1,1 now next term will be addition of previous two terms that is 1 and 1 so 4th term of the sequence would be 2 and so on. Generally the Fibonacci sequence is like {0,1,1,2,3,5,8,13,21,35,...}. Now we will do it on C-Program.
Find the terms of Fibonacci sequence in C-Programming

Code:

Find the terms of Fibonacci sequence in C-Programming


#include<stdio.h>
int main()
{
  int n,f=0,s=1,sum,i;
  printf("How many term you want to see:");
  scanf("%d",&n);
  printf("The first %d terms are\n",n);
  for(i=0;i<n;i++)
  {
  printf("%d\n",f);
  sum=f+s;
  f=s;
  s=sum;
  }
}

Output: 

The output is given below:
Find the terms of Fibonacci sequence in C-Programming

Explanation:

1. We use the Header package <stdio.h>.
2. Next we introduce 5 integers variables n,f,s,sum and i. Now we use 'n' for save the value that how many terms the users want to find. Next we use 'f' and 's' for the initializing. Since Fibonacci sequence is starting with 0 and 1 so we save the initial value of 'f' is 0 and 's' is 1. We use 'sum' to add the numbers and 'i' for loop.
3. We use for-loop here. First we introduce the condition i starts from 0 and increasing by 1 it will stop at 'n'(users input this number).
4. Every time in for loop first print the value of f then it will do the sum=f+s then we save 'f' in 's' and after that save 's' in the variable 'sum'. So what is happened here, first it will print 0 since 0 is the initial value of 'f', then sum=0+1=1 after that f=1 since the initial value of 's' is 1. After that s=1 since 'sum' has the value 1. Again loop is begin and print the value of 'f' i.e 1 here and continue in this way.

 If you have any doubts please comment below. 


16/02/2019

Sum of n real numbers using array: 

Here we want to sum the numbers using array. you can give many numbers and add them by array. We introducing here the array syntax. For example 1+2+3+2+5. First we will give the code and after that we will explain.
Sum of n real numbers using array

Code:

Sum of n real numbers using array input

#include<stdio.h>
int main()
{
  int i,n;
  float a[30],sum=0;
  printf("How many numbers you want to add:");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  scanf("%f",&a[i]);
  for(i=0;i<n;i++)
  sum=sum+a[i];
  printf("The sum of the given numbers is %.2f",sum);
}

Output:

The output is given below:
Sum of n real numbers using array

Explanation: 

1. We first use <stdio.h> header package.
2. we use two integers variable i and n and two float variable a[30] and sum.
3. we use n for how many numbers users want to do sum and i is for for-loop. we use a[30] array variable to store the given numbers and we use sum variable to sum all the given numbers. Here you can change the array size by replacing the number 30. To know more about use of array variable visit syntax page.
4. We use two for-loop. First is for take all the given value from the users and save it in the array variable a[30]. Second for-loop for doing the sum of all the numbers. To know more about for-loop visit syntax package.
5. After that we print the sum and we use the syntax "%.2f" for showing 2 numbers after point in sum, like 12.35 or 4.20. You can use .3 or .4 as your wish for the changing. If you use .3 then it will like 1.235 or 5.125.


                                           If you have any doubts please comment below.