---------------
Number Series with a Twist 2
Consider the following series: 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. Write a program to find the Nth term in the 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.
You can assume that N will not exceed 30.
#include<stdio.h>
int main()
{
int i, n, a=1, b=1;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
a = a * 2;
}
else
{
b = b * 3;
}
}
if(n%2!=0)
{
printf("\n%d term of series is %d\t",n,a/2);
}
else
{
printf("\n%d term of series is %d\t",n,b/3);
}
return 0;
}
Consider the below series :
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)
Write a program to find the nth term in this 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 the value of the nth term no other characters /strings or message should be written to STDOUT.
For example if n=10,the 10 th 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.
You can assume that the n will not exceed 20,000.
#include<stdio.h>
int main()
{
int i, n, a=0, b=0;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
if(i>1)
a = a + 2;
}
else
{
b = a/2;
}
}
if(n%2!=0)
{
printf("%d",a);
}
else
{
printf("%d",b);
}
return 0;
}
String with a Twist
1. The program will receive 3 English words inputs from STDIN
- 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
- Then concatenate the three words and print them
Other than these concatenated word, no other characters/string should or message should be written to STDOUT
For example if you print 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
#include <stdio.h>
#include <string.h>
int main()
{
char a[10], b[10], c[10];
int i,j;
int x, y, z;
scanf("%s",a);
scanf("%s",b);
scanf("%s",c);
x = strlen(a);
y = strlen(b);
for(i=0;i<x;i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
{
a[i] = '%';
}
}
for(j=0;j<y;j++)
{
if(b[j]=='b'||b[j]=='c'||b[j]=='d'||b[j]=='f'||b[j]=='g'||b[j]=='h'||b[j]=='j'||b[j]=='k'||b[j]=='l'||
b[j]=='m'||b[j]=='n'||b[j]=='p'||b[j]=='q'||b[j]=='r'||b[j]=='s'||b[j]=='t'||b[j]=='v'||b[j]=='w'||
b[j]=='x'||b[j]=='y'||b[j]=='z')
{
b[j] = '#';
}
if(b[j]=='B'||b[j]=='C'||b[j]=='D'||b[j]=='F'||b[j]=='G'||b[j]=='H'||b[j]=='J'||b[j]=='K'||b[j]=='L'||
b[j]=='M'||b[j]=='N'||b[j]=='P'||b[j]=='Q'||b[j]=='R'||b[j]=='S'||b[j]=='T'||b[j]=='V'||b[j]=='W'||
b[j]=='X'||b[j]=='Y'||b[j]=='Z')
{
b[j] = '#';
}
}
z=0;
while (c[z] != '\0') {
if (c[z] >= 'a' && c[z] <= 'z')
{
c[z] = c[z] - 32;
}
z++;
}
printf("%s%s%s",a,b,c);
}
Addition of two numbers a Twist
1. Using a method, pass two variables and find the sum of two numbers.
Test case:
Number 1 – 20
Number 2 – 20.38
Sum = 40.38
There were a total of 4 test cases. Once you compile 3 of them will be shown to you and 1 will be a hidden one. You have to display an error message if numbers are not numeric.
#include<stdio.h>
addition(int x, float y)
{
float ans;
ans = (float)x + y;
printf("Answer : %.2f",ans);
}
int main()
{
int a;
float b;
printf("enter first number : ");
scanf("%d",&a);
printf("enter second number : ");
scanf("%f",&b);
addition(a, b);
}