Tuesday 28 October 2014

Collatz function

Difficulty: Easy

The Collatz function is defined for a positive integer n as follows.
f(n) = 3n+1 if n is odd
        n/2     if n is even

We consider the repeated application of the Collatz function starting with a given integer n, as follows:
f(n), f(f(n)), f(f(f(n))), …

It is conjectured that no matter which positive integer n you start from, this sequence eventually will have 1 in it. It has been verified to hold for numbers up to 5 × 260  [Wikipedia: Collatz Conjecture].

Sunday 19 October 2014

Count occurences of pattern string

Level: Medium

Given a source string S and a pattern string P, count the number of times the pattern string P occurs in the source string S. 
Note: Overlapping sequences are counted as separate occurrences. 

Input Format:
First line is the source string S s.t. 1 <= |S| <= 8192 characters
Second line is the pattern string P s.t. 1 <= |P| <= 8192 characters

Output Format:
Output a single integer containing the number of occurrences of pattern string P in source string S.

Print Subarray

Level: Easy

Given an input character array of A and start index S and end index E, write a function that prints the sub array starting from S (including S) and ending at index E (including E). The character array may contain spaces and tabs.
Note: You are given the main function. Just write the subroutine 'void printSubarray(char *a, int start, int end)'. 

Input Format:
First line is the input array A s.t. 1 <= |A| <= 8192
Second line is the start index S s.t. 0 <= S <= |A|-1
Third line is the ending index E s.t. 0 <= E <= |A|-1

Center Align Text

Level: Easy

Given an input string S, center justify the string by using the character '_' to align the string. There will be neither preceding spaces before the string S nor suffix spaces after the string S. The output should be center-justified in a line of width 64 characters, followed by a newline. 
Note:
1. If S has an odd number of characters then the number of preceding '_' should be one more than the number of trailing '_'
2. If S has an even number of characters then the number of preceding '_' should be equal to the number of trailing '_'

Input Format:
First line is the input string S s.t. 1 <= |S| <= 64

Wednesday 15 October 2014

Quiz on Array And Pointer


1 What is the output of the following program?

#include <stdio.h>

void foo(int a[])
{
a[0]=10;
printf("%d",a[0]);
return ;
}

int main()
{
int a[]={1,2,3};
foo(a);
printf("%d",a[0]);
return 0;
}
 
 
 
 
1 point

ans 1010

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);