Friday 26 September 2014

Finding co-prime numbers

Write a C program that given an integer ‘n’, prints the number of integers that are less than or equal to ‘n’ and co-prime to ‘n’

Two integers a and b are said to be relatively prime or co-prime if the only positive integer that evenly divides both of them is 1. That is, the only common positive factor of the two numbers is 1. This is equivalent to their greatest common divisor being 1.
Input Format:
One line containing the value of 'n' , where 1<=n<=10,000

LCM of n numbers

Write a C program that calculates the least common multiple (LCM) of 'n' numbers.

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

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


Missing Integer Problem

You are given a sequence of n-1 distinct positive integers, all of which are less than or equal to a integer ‘n’. You have to find the integer that is missing from the range [1,2,...,n]. Solve the question without using arrays.

Input Format:
One line containing the integer ‘n’ where 2<=n<=10,000
First line is followed by a sequence of ‘n-1’ distinct positive integers. Note that the sequence may not be in any particular order.

Output Format: 
One line containing the missing number.


#include<stdio.h>

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

int a[10000]={0};

for(i=1;i<=n;i++)
{
scanf("%d",&b);
a[b]=a[b]+1;}

for(i=1;i<=n;i++)
{
if(a[i]==0)
{
printf("%d",i);
}}return 0;
}



Choose k objects from n distinct objects

Write a C program that calculates the number of ways to choose k objects from n distinct objects. 'k' and 'n' both are integers.

Input Format:
First line contains the value of n, where 0<=n<=10
Second line contains the value of k, where k>=0

Output Format:
One line containing the number of ways to chose the objects

Note: In this question you are not given main() so you have to write the complete program.


Function to return the quotient

Write a function called divide that takes two non-negative integers : a and b and returns the quotient of a divided by b, if b is a factor of a, else it returns -1.

Note: In this assignment the main() function is given to you. The given code for main() cannot be changed by you and divide() is being called from inside that. You only need to write the divide function.

Also note that the #include line is also provided so you do not need to add it while writing the divide() function.



/* 
   The code given in the black background _above_ this editor 
   box will be automatically added _before_ your code. 
   So assume this code is present when you write your code.
   For coding on your local compiler, copy paste the code 
   into your editor.
*/


// Insert your code for divide() below this line

int divide(int a,int b)
{int d;if(a%b==0)
{d=a/b;

return d;


}
else
return -1;


}

/* 
   The code given in the black background _below_ this editor 
   box will be automatically added _after_ your code. 
   So assume this code is present when you write your code.
   For coding on your local compiler, copy paste the code 
   into your editor.
*/

Sunday 7 September 2014

Finding the Second Largest Element

Level: Medium

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. 

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

program---


#include<stdio.h>

int main()
{
int n,a,b,tmp;
scanf("%d",&n);

a=n;
scanf("%d",&n);
if(n!=-1)
b=n;

Triangular Matrix

Level:Medium

In this assignment, you will be given an NxN matrix. You have to determine whether the matrix is a triangular matrix.

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

You may not use arrays for this program.

Printing Right Triangles

Level: Easy

You are given a positive integer N. You have to print N rows as follows. The first row consists of one 0, the second row 2 zeroes, and so on, until the Nth row, which consists of N zeroes.




program---



#include<stdio.h>

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

Sums of Powers of Numbers

Level: Easy

In this program, you are given an input N, which is a positive integer less than or equal to 40. Write a program to find the sums of fourth powers of the first N numbers.

Sample Input2

Sample Output
17


program---


#include<stdio.h>
#include<math.h>
int main()
{long int sum=0;int i,n;
scanf("%d",&n);

Pythagorean Triples

Level: Easy

Three numbers form a Pythagorean triple if the sum of squares of two numbers is equal to the square of the third. 

 For example, 3, 5 and 4 form a Pythagorean triple, since 3*3 + 4*4 = 25 = 5*5 

You are given three integers, a, b, and c. They need not be given in increasing order. If they form a Pythagorean triple, then print "yes", otherwise, print "no". Please note that the output message is in small letters. 

Sample Input
3
5
4

Sample Output
yes



Program----


#include<stdio.h>

int main()
{int a,b,c,d,e,f;
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);