Sunday, February 25, 2018

C program : A function to find the kth occurrence of an integer n in a sequence of non-negative integers

You are given the input in two lines:

The first line contains a non-negative integer, say n, and a positive
integer k, in that order. You have to find the kth occurrence of n in
the sequence below. (When

The second line consists of a sequence of non-negative integers,
terminated with a -1.  The -1 is not part of the sequence.


#include<stdio.h>


int find(int n, int k)
{
     int num, count= -1, ocurr=0;
  while(num != -1)
{
    scanf("%d", &num);
    count++;
    if(num==n)
    {
        ocurr++;

    }
    if(ocurr==k)
    {
        return count;
    }

  }
if(ocurr<k){return -1;}

}

int main(){
 int n,k,i;
  scanf("%d %d" , &n,&k);

  i=find(n,k);
   printf("%d",i);

}
Thanks
Happy Computing !

C program : To output the sequence of differences of adjacent pairs of numbers

You are given a sequence of numbers, ending with a -1. You can assume
that are at least two numbers before the ending -1. 

Let us call the sequence x0 x1 ... xn -1.

You have to output the sequence of differences of adjacent pairs of
numbers, as follows: 

x1-x0 x2-x1 ... x_{n}-x_{n-1}


#include<stdio.h>

int main()
{
      int a ,b,diff;
  scanf("%d", &a);
  if(a !=(-1))
  {   
    while(b!= (-1))
  {
       scanf("%d", &b);
      if(b== -1){break;}
    diff=(b-a);
    printf("%d ",diff);
           a=b;
  }
  }
  return 0;
}

Thanks
Happy Computing !

C program: Find the second largest number in the input. Without using arrays.

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

#include<stdio.h>

int main(){
int largest=0,firstInput=0,secondInput=0,secondLargest=0;
scanf("%d",&firstInput);
if(firstInput != (-1))
 {
   
    scanf("%d",&secondInput);
    while(secondInput !=(-1))
    { 
        if(secondInput>firstInput)
        {
        largest=secondInput;
        secondLargest=firstInput;
        firstInput=largest;
        }
        else
        {  
          if(secondInput>secondLargest)
          secondLargest=secondInput;
        }

        scanf("%d", &secondInput);
    }


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

Thanks
Happy computing !

C Program : Given an NxN matrix. You have to determine whether the matrix is a triangular matrix.

A matrix is upper triangular if every entry below the diagonal is
0. For example,  
1 1 1
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, 
2 0 0
3 1 0
4 2 2
is a lower triangular matrix. 

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

#include<stdio.h>

int main()
{

  int i, a,n,s=0,countl=0, countu=0;
  scanf("%d",&n);
  if(n>1)
{
  for(int r=0;r<n;r++)
  {
     for(int c=0;c<n;c++)
     {
          scanf("%d",&a);
       if((c>r)&& (a==0))
         countl++;
       if((c<r)&& (a==0))
         countu++;
     
     }
  }

  for(int j=1;j<=n;j++)
  { i=n-j;
    s=s+i;
  }
  if( (countl>=s) || (countu>=s))
  {printf("yes\n");
  
  }
  else
  {
   printf("no\n");
  
  }
}
  else
    printf("no\n");
  return 0;
}

Thanks
Happy Computing !

C program ; To check if given numbers are a Pythagorean triple

#include<stdio.h>

int main(){
     int a,b,c;
 
  scanf("%i", &a);
   scanf("%i", &b);
   scanf("%i", &c);
  if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+a*a==c*c)||(b*b+c*c==a*a)||(c*c+a*a==b*b)||(c*c+b*b==a*a))
   
  {printf("1");}
  else {printf("0");}
  return 0;
}

Thanks
Happy Computing !

C program : To check whether M is an exact multiple of N

#include<stdio.h>

int main()
{
     int M,N,q;
  scanf("%i %i",&M, &N);
  if((M%N)!=0)
  {printf("0");}
  else{
    q=M/N;
    printf("%i",q);
  }
  return 0;
}

Thanks
Happy Computing !

C program to print alternate letters of a word entered from keyboard



#include<stdio.h>

int main()
{
    char str[100];
    char c='a';
    int i=0;
    printf("type a word \n");

    while(c != '\n')
    {
        c = getchar();
        str[i]=c;
        i++;
    }
    for (int j=0;j<=i;j++)
    {
        printf("%c ",str[j]);
        j++;
    }
return 0;
}

Thanks
Happy computing !