## Saturday, December 5, 2015

### HCL interview question(Written)::Noida

Two programming questions asked by the HCL interviewing team.
Question 1::
Write a program in C/C++ which takes input as string "12345" and output will be:: 1+2+3+4+5=15.

I have written it in C. Below is the code.

/* Input(as string)::"12345" and
output should be:: 1+2+3+4+5=15 */

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[]="12345";
int i,sum=0;
for(i=0;i<sizeof(str1)-1;i++)
{
printf("%d",(int)str1[i]-48);
sum = sum + (int)str1[i]-48;
if(i!=sizeof(str1)-2)
printf("+");
}
printf(" = %d",sum);
}

Question 2:

One thread prints only odd numbers 1 3 5 7 ...
other thread prints only even numbers 2 4 6 8 ...
But the output should be mixed of both :; 1 2 3 4 5 6 7 8 ...  upto n.

I had not worked in threads, so I have implemented the program in two function.
If any one having idea of threads then please give the program, thanks in advance.
My program was as below::

#include<iostream>
using namespace std;
static int a,b;
void t_odd()
{
a+=2;
cout<<" "<<a;
}
void t_even()
{
b+=2;
cout<<" "<<b;
}

int main()
{
a = -1; b = 0;
int i,n;
cout<<"enter the value n::";
cin>>n;
for(i=1;i<=n/2;i++)
{
t_odd();
t_even();
}
if((n%2)!=0)
t_odd();
return 0;
}