tcs codevita solutions 2016

LOGIC PYRAMID:

Identify the logic behind the series

6 28 66 120 190 276....

The numbers in the series should be used to create a Pyramid. The base of the Pyramid will be the widest and will start converging towards the top where there will only be one element. Each successive layer will have one number less than that on the layer below it. The width of the Pyramid is specified by an input parameter N. In other words there will be N numbers on the bottom layer of the pyramid.

The Pyramid construction rules are as follows
1.    First number in the series should be at the top of the Pyramid
2.    Last N number of the series should be on the bottom-most layer of the Pyramid, with Nth number being the right-most number of this layer.
3.    Numbers less than 5-digits must be padded with zeroes to maintain the sanctity of a Pyramid when printed. Have a look at the examples below to get a pictorial understanding of what this rule actually means.
Example

If input is 2, output will be

 00006
00028 00066

If input is 3, output will be

  00006
 00028 00066
00120 00190 00276

Formal input and output specifications are stated below 
Input Format: 

First line of input will contain number N that corresponds to the width of the bottom-most layer of the Pyramid 
Output Format: 

The Pyramid constructed out of numbers in the series as per stated construction rules 
Constraints:
1.    0 < N <= 14


Sample Input and Output
SNo.
Input
Output
1
2

https://www.tcscodevita.com/CodevitaV5/images/pyramid2.png
2
3

https://www.tcscodevita.com/CodevitaV5/images/pyramid1.png

SOLUTION :


#include<stdio.h>
int main()
{
int x=6,y=22,n,i,j,k=0;
scanf("%d",&n);
if(n>0 && n<=14)
{
    for(i=1;i<=n;i++)
    {
        for(j=i;j<n;j++)
        {
            printf(" ");
        }
        for(k=1;k<=i;k++)
        {   
             printf("%05d ",x);
             x=x+y;
             y=y+16;
        }
        printf("\n");
     }
  }
return 0;

}



CONSECUTIVE PRIME SUM:

Some prime numbers can be expressed as Sum of other consecutive prime numbers.
For example

5 = 2 + 3
17 = 2 + 3 + 5 + 7
41 = 2 + 3 + 5 + 7 + 11 + 13

Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.

Write code to find out number of prime numbers that satisfy the above mentioned property in a given range.
Input Format:

First line contains a number N
Output Format:

Print the total number of all such prime numbers which are less than or equal to N.
Constraints:
1. 2

Sample Input and Output


SNo.InputOutputComment
1202
(Below 20, there are 2 such numbers: 5 and 17).
5=2+3
17=2+3+5+7
2151



SOLUTION:

#include<stdio.h>
int conprime(int);
int main()
{
    int n1=2 , n2, i, flag,o=0;
    scanf("%d",&n2);
    while (n1 <= n2)
    {
        flag=0;
        for(i=2; i<=n1/2; ++i)
        {
            if(n1%i == 0)
            {
                flag=1;
                break;
            }
    }
        if (flag == 0)
        {
        if(n1>2)
        {
        if(conprime(n1) == 0)
{
o++;
}
        }
        }
        ++n1;
    }
    printf("%d",o);
    return 0;
}
int conprime(int n)
{
int a[1000],s=0,flag,i,n1=2,j=0,m=0,b;
while (n1 < n)
    {
        flag=0;
        for(i=2; i<=n1/2; ++i)
        {
            if(n1%i == 0)
            {
                flag=1;
                break;
            }
        }
        if (flag == 0)
        {
        a[j] = n1;
        j++;
        }
        ++n1;
}
    for(b=0;b<j;b++)
    {
    s+=a[b];
    if(s==n)
    {
    m=1;
    break;
    }
    else
    {
    m=0;
    }
    }
    if(m==1)
    {
    return 0;
    }
    else return 1;
}

Comments

  1. my program is fully complied and run & I have checked in various online C compilers as well...

    ReplyDelete
  2. in python :
    x,y=6,22
    size = 5
    for i in range(0, size):
    for j in range(0,(size-i)*3):
    print(end=" ")
    for j in range(0, i+1):
    print(format(x, '05'), end=" ")
    x = x + y;
    y = y + 16;

    print(end='\n')

    ReplyDelete
  3. Use this code for Logic Pyramid:

    #include
    using namespace std;

    int main()
    {
    int n, t;
    cin >> t;
    while (t > 0) {
    cin >> n;
    int first = 6;
    int second = 22;

    if (n > 0 && n <= 14) {
    for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (n - i) * 3; j++) {
    cout << " ";
    }
    for (int k = 1; k <= i; k++) {
    printf("%05d", first);
    first += second;
    second += 16;
    cout << " ";
    }
    cout << endl;
    }
    }
    t--;
    }
    return 0;
    }

    ReplyDelete

Post a Comment