Saturday, July 27, 2019

TCS ninja special number program

//A number is special number its digitsw only consist 0,1,2,3,4,5

//Given a number n and we have to find n-th special number


Saturday, July 20, 2019

Prime Numbers code Question



1.      Write code to find out the number of prime numbers that satisfy the below mentioned property in a given range.

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.

Follow the given instructions while generating input and outputs.

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
The result should be as follows:
20
2
Explanation for the output:
Below 20, there are 2 such numbers: 5 and 17.
5=2+3
17=2+3+5+7



#include <stdio.h>

int main()
{
    int i,j,no,sum;
    scanf("%d",&no);
    for(i=2;i<no;i++)
    {
        for(j=2;j<i;j++)
        {
            if(i%j==0)
            break;
        }
        if(j==i)
        {
        sum+=i;
        printf("%d+",j);
           
        }
        if(sum==no)
        {
         printf("\b=%d",sum);   
        break;
        }
    }
  

    return 0;
}

Thursday, July 18, 2019

Generate series 1 1 2 3 4 9 27 16 81 243 and find nth term

#include <stdio.h>
#include<math.h>
int main()
{
int i,j,t,u,s,n,c=0;

scanf("%d",&n);

u=n;
for(i=0;i<n;i++)
{
   
        s=pow(2,i);
        if(c==u)
        {
      printf(" %d",s);
      break;
        }
    c++;
    t=pow(3,i);
     if(c==u)
    {
      printf(" %d",t);
      break;
    }

   
    c++;
  
      
   
}

    return 0;
}

Array Check tcs ninja problem

i/p
 array a holds 1 2 3 4
o/p of array b   is  24 12 8 6


#include<stdio.h>
int main() 
{
int n,i,j,a[100],b[100];
scanf("%d",&n);
  for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
    }
  
    for(i=0;i<n;i++)
   {
       b[i]=1;
      for(j=0;j<n;j++)
       {
           if(i==j)
  
          {
  
          }
       
          else
  
          {
  
             b[i]=b[i]*a[j];

          }
 
        }
  
   }
   

   for(i=0;i<n;i++)
 
      printf("%d ",b[i]);
   
 
  return 0;

}

Tuesday, July 16, 2019

(TCSNINJA)Write a program to print the following the series(1 odd,2 even,3 odd,4 even....

/* write a program to print the following the series(1 odd,2 even,3 odd,4 even....)
i/p:4
o/p:1 2 4 5 7 9 10 12 14 16 */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,lst=1,j,k,ecnt,ocnt;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
   {
   if(i%2!=0)
      {
      ocnt=1;
for(ocnt=1;ocnt<=i;)
{
if(lst%2!=0)
printf(" %d",lst,ocnt++);
lst++;
}
printf("\n");

       }
    else
       {
      ecnt=1;
for(ecnt=1;ecnt<=i;)
{
if(lst%2==0)
printf(" %d",lst,ecnt++);
lst++;
}

printf("\n");

       }
    }
    getch();
  }

read n numbers and print count of distinct elements of array






int main() {
   int n[15];
   int sz,k;
   int i,j,b=1,d=0;
   scanf("%d",&sz);
  for(k=0;k<sz;k++)
  scanf("%d",&n[k]);
 
   for(i=0;i<sz;i++)
   {
       d++;
       for(j=i+1;j<sz;j++)
       {
           if(n[i]==n[j] && b==1)
           {
           d--;
           b=0;
           }
       }
    b=1;
   }
   printf("\n%d", d);
}


Friday, July 12, 2019

Read n integers of sorted array and find the start and last ocuurance of give X integer value

i/p 
10
1 2 2 2 2 3 4 8 8 8
8

o/p
7    9
#include<stdio.h>
#include<conio.h>
void main()
{
    int n,X,a[100]={1,2,2,2,2,3,4,8,8,8},findex=-1,lindex=-1;
    int i;
    clrscr();

    scanf("%d",&n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);

    scanf("%d",&X);

    for(i=0;i<n;i++)
    {
if(X==a[i])
{
findex=i;
break;
}
    }
    for(i=n;i>=0;i--)
    {
if(X==a[i])
{
lindex=i;
break;
}
    }
    if(findex!=-1 && lindex!=-1)
    printf("%d   %d",findex,lindex);
    else
    printf("\nNO OCCURANCE");

    getch();
}

Thursday, July 11, 2019

Lucky Number 1..7..9

lucky no is the number which must contains 1 or 7 or 9
the following program returns count of lucky numbers
int range 1 to n

i/p    175
o/p   16

i/p  20
o/p 6

#include<stdio.h>

int main() {
   int ctr=0,n,digit,rem,no,num,lcount=0;
   scanf("%d",&n);
   no=n;
   while(no>=1)
   {
       num=no;
   while(num)
   {
       digit=num%10;
       if(digit==1||digit==7||digit==9)
       {
           num=num/10;
           continue;
       }
       else
          break;
       num=num/10;
   }
   if(num==0)
   lcount++;
   no--;
   }
   printf("\n%d",lcount);
}




Wednesday, July 10, 2019

Hallow Program ( input rows and cols)

#include <stdio.h>

int main()
{
    int i, j, rows, columns;

    /* Input number of rows and columns from user */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &columns);

    /* Iterate over each row */
    for(i=1; i<=rows; i++)
    {
        /* Iterate over each column */
        for(j=1; j<=columns; j++)
        {
            if(i==1 || i==rows || j==1 || j==columns)
            {
                /* Print star for 1st and last row, column */
                printf("1");
            }
            else
            {
                printf("0");
            }
        }

        /* Move to the next line */
        printf("\n");
    }

    return 0;
}

Enter number of rows: 5
Enter number of columns: 5
11111
10001
10001
10001
11111



Tuesday, July 9, 2019

TCS Ninja Coding Questions 2019


For More Problems Click On The bellow link
What is the output of this C code?
#include<stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **p); } 
5 5 5
2) 5 5 junk
3) 5 junk junk
4) Compile time error2) 
Given the below statements about C programming language.
1) main() function should always be the first function present in a C program file
2) all the elements of a union share their memory location
3) A void pointer can hold the address of any type and can be typecasted to any type
4) A static variable holds random junk value if it is not initialized
Which of the above are correct statements?
1) 2,3
2) 1,2
3) 1,2,3
4) 1,2,3,4
Solution: Option 3
TCS Ninja placement papers – Coding section
This is the most important section of all. If you fail to implement the code, then very likely that you would not make to the next round. Hence focus on this round is very important. TCS coding questions in TCS previous year placement papers will give you an idea about the kind of questions asked. 
1) Write a C program to calculate the factorial of a non-negative integer N. The factorial of a number N is defined as the product of all integers from 1 up to N. Factorial of 0 is defined to be 1. The number N is a non-negative integer that will be passed to the program as the first command line parameter. Write the output to stdout formatted as an integer WITHOUT any other additional text. You may assume that the input integer will be such that the output will not exceed the largest possible integer that can be stored in an int type variable.
Kindly note that certain keywords like “scanf, getc, getch, getchar” cannot be used while solving this problem.
#include<stdio.h>

int main(int a, char *b[])  //command line arguments

{

Int x,y,f=1;

x=atoi(b[1]);   //atoi function is to convert a character to integer

for(i=1;i<=x;i++)

{

f=f*i;

}

printf("%d",f);

return 0; } 

Nth Fibonacci Number using Command Line Arguments

Using Command Line Arguments
[code language=”cpp”]
#include<stdio.h>
#include<stdlib.h>
int fib(int n)
{
int a=0,b=1,c,i;
if(n==0) return a;
for(i=2;i<=n;i++)
{
c=a+b;
a=b;
b=c;
}
return b;
}
int main(int argc, char * argv[])
{
if(argc==1)
{
printf("No arguments");
return 0;
}
else
{
int n;
n=atoi(argv[1]);
printf("%d",fib(n));
return 0;
}
}

Square Root of Prime Number using Command Line Argument

The square root of a Prime number by checking first if it is a prime number?
Write a C program which will check whether a given number N is a Prime or Not. If the Number N is a Prime, then find it’s square root and print that value to the STDOUT as floating point number with exactly 2 decimal precision.
If the number is not Prime, then print the value 0.00 to STDOUT.
The given number will be positive non zero integer and it will be passed to the program as first command line argument.
Other than floating point No other information should be printed to STDOUT.
Also, you can study other Command Line Programming Questions here on our TCS Dashboard.
It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later.
[code language=”cpp”]
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<math.h>
bool isPrime(int n)
{
if(n<2)
return false;
int i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
return false;
}
return true;
}
int main(int argc, char *argv[])
{
if(argc==1)
{
printf("No arguments");
return 0;
}
else
{
int n;
n=atoi(argv[1]);
float sq=0;
if(isPrime(n))
{
sq=sqrt(n);
printf("%.2f",sq);
}
else
printf("%.2f",sq);
return 0;
}
}

Command Line program to find Armstrong number using Command line arguments

A number is called Armstrong number if the Sum of the cubes of its digits is equal to the number itself.
C program to find Armstrong number using Command line arguments
The following is a C program to check whether the given number is Armstrong number or not using command line arguments.
It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later
[code language=”cpp”]
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
 int Given_number= atoi(argv[1]);
 int num;
 for(num=1; num<=Given_number; num++)
 {
 int a=num;
 int s=0;
 int r=0;
while(a>0)
 {
 s=a%10;
 r=r+(s*s*s);
 a=a/10;
 }
 if(r==num)
 printf(" %d isarmstrong no \n", num);
 }
}
[/code]
Code – 2
[code language=”cpp”]
#include<stdio.h>
void main(int argc, char * argv[])
{
int num,num1,arms=0,rem;
if ( argc != 2 )
{
printf("Enter the number:\n");
scanf("%d",&num);
}
else
{
num = atoi(argv[1]);
}
num1=num;
while(num>0)
{
rem=num%10;
arms=arms+rem*rem*rem;
num=num/10;
}
if(num1==arms)
{
printf(" \n%d is an Armstrong number",num1);
}
else
{
printf("\n%d is NOT an Armstrong number",num1);
}
}

Command Line Program to Convert Binary to Octal

This is a very smart program very short and quick method –
#include<stdio.h>
void main(int argc,char *argv[])
{ 
   long int n,r,c,b=1,s=0;
    n=atoi(argv[1]);
    c=n;
    while(c!=0)
    {
    r=c%10;
    s=s+r*b;
    c=c/10;
    b=b*2;
         }
         printf("%lo",s);
         getch();
}


Ques. Write a code to check whether no is prime or not. Condition use function check() to find whether entered no is positive or negative ,if negative then enter the no, And if yes pas no as a parameter to prime() and check whether no is prime or not?
(TCS Ninja – June 2019 Slot 2)

#include<bits/stdc++.h>

using namespace std;



void prime(int num){

  int count=0;

  for(int i=2;i<num;i++){

    if(num%i==0){

      count++;

  break;

      }

    }

  if(count==0){

    cout<<"prime"<<endl;

  }

  else{

    cout<<"Not Prime"<<endl;

  }

}



int main(){

  int n;

  cout<<"Enter the number: ";

  cin>>n;

  if(n>0){

    prime(n);

  }

  else{

    cout<<"negative number.Please enter a postive number"<<endl;

}



return 0;

}

Ques. Find the 15th term of the series?
0,0,7,6,14,12,21,18, 28

#include 



int a1(int x);

int a2(int y);



void main()

{

  int n;

  scanf("%d",&n);

  if(n%2==0)

  a1(n/2);

  else

    a2(n/2+1);

}



int a1(int x)

{

int s=0;

for(int i=0;i<x-1;i++)

{

s=s+6;

}

printf("%d",s);

}



int a2(int x)

{

int s=0;

for(int i=0;i<x-1;i++)

{

s=s+7;

}

printf("%d",s);

}