TCS Programming
TCS Programming
TCS Programming
1,1,2,3,4,9,8,27,16,81,32,243,64,729,128,2187…
This series is a mixture of 2 series – all the odd terms in this series
form a geometric series and all the even terms form yet another geometric
series.
The value N in a positive integer that should be read from STDIN. The Nth
term that is calculated by the program should be written to STDOUT. Other
than value of n th term,no other character / string or message should be
written to STDOUT.
For example , if N=16, the 16th term in the series is 2187, so only value
2187 should be printed to STDOUT.
#include <stdio.h>
#include <math.h>
int main()
{
int n, res, a, r, term;
scanf("%d", &n);
if(n % 2 == 1)
{
a = 1;
r = 2;
term = (n + 1) / 2;
res = pow(2, term - 1);
printf("%d", res);
}
else
{
a = 1;
r = 3;
term = n / 2;
res = pow(3, term - 1);
printf("%d", res);
}
return 0;
}
Question - 02
0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8
This series is a mixture of 2 series all the odd terms in this series
form even numbers in ascending order and every even terms is derived from
the previous term using the formula (x/2)
The value n in a positive integer that should be read from STDIN the nth
term that is calculated by the program should be written to STDOUT. Other
than the value of the nth term no other characters /strings or message
should be written to STDOUT.
For example if n=10, the 10th term in the series is to be derived from
the 9th term in the series. The 9th term is 8 so the 10th term is (8/2)=4.
Only the value 4 should be printed to STDOUT.
#include <stdio.h>
#include <math.h>
int main()
{
int n, term, res;
scanf("%d", &n);
if(n % 2 == 1)
{
term = (n + 1) / 2;
res = 2 * (term - 1);
printf("%d", res);
}
else
{
term = n / 2;
res = term - 1;
printf("%d", res);
}
return 0;
}
2
Question - 03
These three words will be read one at a time, in three separate line.
The first word should be changed like all vowels should be replaced by $.
The second word should be changed like all consonants should be replaced
by #.
The third word should be changed like all char should be converted to
upper case.
For example if you input how are you then output should be h$wa#eYOU.
You can assume that input of each word will not exceed more than 5 chars.
3
#include <stdio.h>
#include <malloc.h>
#include <string.h>
int main()
{
char *str1 = malloc(sizeof(char) * 256);
char *str2 = malloc(sizeof(char) * 256);
char *str3 = malloc(sizeof(char) * 256);
scanf("%s %s %s", str1, str2, str3);
int s1 = strlen(str1);
int s2 = strlen(str2);
int s3 =strlen(str3);
for(int i = 0; i < s1; i++)
{
if(str1[i]=='a' || str1[i]=='e' || str1[i]=='i' ||
str1[i]=='o' || str1[i]=='u' ||
str1[i]=='A' || str1[i]=='E' || str1[i]=='I' ||
str1[i]=='O' || str1[i]=='U')
str1[i] = '$';
}
for(int i = 0; i < s2; i++)
{
if(str2[i]!='a' && str2[i]!='e' && str2[i]!='i' &&
str2[i]!='o' && str2[i]!='u' &&
str2[i]!='A' && str2[i]!='E' && str2[i]!='I' &&
str2[i]!='O' && str2[i]!='U')
str2[i] ='#';
}
for(int i = 0; i < s3; i++)
{
if(str3[i] >= 'a' && str3[i] <= 'z')
str3[i] = str3[i] - 32;
}
printf("%s%s%s", str1, str2, str3);
return 0;
}