Tuesday, 16 September 2014

LCM of n numbers




LCM of n numbers


Input  :
First line contains the number of numbers that are input  'n', where n>1
Next line contains 'n' positive integers whose LCM is to be calculated.

Output  :
One line containing the LCM of the 'n' numbers

#include <stdio.h>
int gcd(int a,int b){
   int g ;
  while (b!=0){
   g=a%b;
   a=b;
   b=g;}

return a; // a is the gcd
}

int main(){
    int lcm , n , num1,num,i;
    scanf("%d",&n);
    scanf("%d",&num1);
     for (i=2;i<=n;i++){   
        scanf("%d",&num);
        lcm=(num1*num)/gcd(num1,num);
        num1=lcm;}


    printf("%d",num1);
    return 0;
    
    }

Friday, 5 September 2014

Triangular Matrix In C


Triangular Matrix

In this question , we will be given an NxN matrix. We have to determine whether the matrix is a triangular matrix.Write a program in C.

The diagonal of the matrix M of size NxN is the set of entries M(0,0), M(1,1), M(2,2), ..., M(N,N).

A matrix is upper triangular if every entry below the diagonal is 0. For example,
1 1 3
0 0 1
0 0 2
is an upper triangular matrix. (The diagonal itself, and the entries above and below the diagonals can be zeroes or non-zero integers.)

A matrix is lower triangular if every entry above the diagonal is 0. For example,
1 0 0
4 1 0
4 2 2
is a lower triangular matrix.

A matrix is triangular if it is either upper triangular or lower triangular.

This can be done easily using arrays .But we have to do it without using array.

Input
Enter the size of the matrix, N
Enter the NXN matrix

Output
If the input matrix is triangular, then print yes. Otherwise, print no.
C code
#include <stdio.h>
int main()
{
  int a,i,j,c1=0,c2=0,N;
  scanf("%d",&N);
  for(i=1;i<=N;i++)
    {
      for (j=1;j<=N;j++)
        { scanf("%d",&a);
          if ( (j<=i) && (a==0))
            {c1++;}
          if ( (j>=i) && (a==0))
            {c2++;} 
        }
     }
//printf("%d %d",c1,c2);
if (c1== ((N*N)-N)/2 || c2==((N*N)-N)/2) printf("yes");
else printf("no");
return 0;
}

Notes:
I have used the fact that in a matrix of NXN there are N diaganol elements and total N*N elements
so elements which are above/under the diagonal are =(N*N -N)/2

Second Largest Element In C Without Arrays

Finding the Second Largest Element without using arrays

We are given a sequence of integers as input, terminated by a -1. (That is, the input integers may be positive, negative or 0. A -1 in the input signals the end of the input.)

-1 is not considered as part of the input. 

Find the second largest number in the input. You may not use arrays.

C code as follows:

#include <stdio.h>

int main()
 {  int l,sl,a,b,c; /*a,b first two input numbers to initialise largest (l),second lar(sl) numbers;

    scanf("%d",&a);
    l=a;
    if (a!=-1) 
     {
       scanf("%d",&b);
       if (b>l){sl=l;l=b;}
       else {sl=b;}
      }
    else return 0;  
    scanf("%d",&c);
    while(c!=-1)
     { 
       if ((c<l)&& (c>sl))
           sl=c;
       if (c>l)
        {sl=l;
         l=c;}
      scanf("%d",&c);
      }
printf("%d",sl);
return 0;

}