Showing posts with label if-else. Show all posts
Showing posts with label if-else. Show all posts

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

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

Sum Of Two Matrix in C-Programming:

 We will add two matrix in C-Programming. First we have to know that how to add two matrix. So two matrix can be added if the order of this two matrix are same. Now how the addition is defined? - the matrix addition is defined as in the given picture.
Sum Of Two Matrix in C-Programming

Important Tools:

1. array
2. for-loop
3. if-else

Code:

Sum Of Two Matrix in C-Programming

#include<stdio.h>
#include<conio.h>
int main()
{
int m,n,m1,n1,a[50][50],i,j,b[50][50],c[50][50];
printf("Enter the order of the first matrix: ");
scanf("%d%d",&m,&n);
printf("Enter the order of the second matrix: ");
scanf("%d%d",&m1,&n1);
//Condition for addition
if(m!=m1||n!=n1)
printf("The sum is not possible");
//The first matrix scan and print
else
{
printf("Enter the first matrix row wise: ");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    scanf("%d",&a[i][j]);
  }
}
printf("The 1st matrix is:\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
  printf("%d\t",a[i][j]);
  }
printf("\n");
}
//the second matrix scan and print
printf("Enter the 2nd matrix row wise:\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    scanf("%d",&b[i][j]);
  }
}
printf("The 2nd matrix is:\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
  printf("%d\t",b[i][j]);
  }
printf("\n");
}
//addition of two matrix print
printf("The sum of the matrix\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    c[i][j]=a[i][j]+b[i][j];
  printf("%d\t",c[i][j]);
  }
  printf("\n");
}
}
}

Output:

The output is given below
Sum Of Two Matrix in C-Programming

Explanation:

1. We use <stdio.h> and <conio.h> two header package.
2. first of all we check the condition for the addition of two matrix. So we use if-else condition for that. If the order of two matrix is equal then it can be added if not it is impossible.
3. We use  m,n,m1,n1,a[50][50],i,j,b[50][50] and c[50][50] variable here. m and n are used for the order of the 1st matrix and n1 and  m1 are used for order of 2nd matrix. a[50][50] and b[50][50] are used for store the given two matrices and we used c[50][50] to store and show the sum of two given matrices. And i and j are used in the loop.
4. First we scan and store two matrices in the variable a[50][50] and b[50][50] respectively using for-loop for know more see my post. After that we add the two matrices and store it in the variable c[50][50]. Therefore we print the c[50][50] variable using for-loop.

12/02/2019

C-programmed code to check even or odd number:

We are going to check a number is even or odd in C-programming. Here you can use any C-programming software to do it. At first we give the code of this programming and then we explain it smoothly. We always try to make easy for the beginners.



The code for check a number whether it is even or odd at below:


 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int a,b;
 printf("Enter the number:");
 scanf("%d",&a);
 b=a%2;
 if(b==0)
 {
 printf("the number is even");
 }
 else
 {
 printf("The number is odd");
 }
 getch();
 }


Here the output of this program below:



EXPLANATION


  •  First of all we used two packages <stdio.h> and <conio.h>. to know the uses of <stdio.h> and <conio.h> visit our packages page.
  •  we used "void main()" here you can use only "main()" if you use windows operator system or you can use "int main()" if you use Linux opareting system.know more in important syntax of our page.
  •  Here we used two integer variable 'a' and 'b'. we used 'a' for store the output number. and 'b' is used to evaluate the remainder of 'a' dividing by 2.
  •  After we used "if-else" condition. That means if 'b'=0 then the remainder is 0 so 'a' is divisible by 2 and if 'b' not equal to 0 then it will be odd number.
  •  Now we use "getch()". To know more go to "important syntax"