TCS solved ques in C++

1.Checking if a given year is leap year or not.

C++ Code:

//C++ Program
//Leap year or not
#include<iostream>
using namespace std;
//main program
int main()
{
//initialising variables
int year;
cout<<"Enter year to check: ";
//user input
cin>>year;
//checking for leap year
if( ((year % 4 == 0)&&(year % 100 != 0)) || (year % 400==0) )
{
//input is a leap year
cout<<year<<" is a leap year";
}
else
{
//input is not a leap year
cout<<year<< " is not a leap year";
}
return 0;
}

Output:

Enter year to check: 2019
2019 is not a leap year

 

2.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 again, And if yes pass no as a parameter to prime() and check whether no is prime or not?

Code:

//C++ Program
//Prime Number
#include<iostream>
using namespace std;
//function declaration
void enter();
void check(int);
void prime(int);
//main program
int main()
{
enter();
return 0;
}
//function to enter value
void enter()
{
int num;
cout<<"Enter number:";
cin>>num;
check(num);
}
//function to check whether the input is positive or negative
void check(int num)
{
if(num<0)
{
cout<<"invalid input enter value again"<<endl;
enter();
}
else
{
prime(num);
}
}
//function to check for prime number
void prime(int num)
{
int i,div=0;
for(i=1;i<=num;i++)
{
if(num%i==0)
{
div++;
}
}
//prime number only have two divisors
if(div==2)
{
cout<<num<<" is a prime number";
}
//not a prime number
else
{
cout<<num<<" is not a prime number";

}
}

 

Output:

Enter number:29

29 is a prime number.

3.Using a method, pass two variables and find the sum of two numbers.

//C++ Program
//Sum of two numbers using function
#include<iostream>
using namespace std;
//function to add two numbers
float sum(int a, float b)
{
return (float)(a+b);
}
//main program
int main()
{
//initialising variables
int a;
float b;
cout<<"Enter two numbers";
//user input
cin>>a;
cin>>b;
//call function to find sum
cout<<"Sum of "<<a<<" and "<<b<<" is "<<sum(a,b);
return 0;
}

Output

Enter two numbers 10
98.43
Sum of 10 and 98.43 is 108.43

4.Find the 15th term of the series?

0,0,7,6,14,12,21,18, 28

//C++ Program
#include<iostream>
using namespace std;
int main()
{
//initialising variables
int n,d;
cout<<"Enter the position: ";
//user input
cin>>n;
//logic to find nth element of the series
if(n==0||n==1)
{
cout<<0;
return 0;
}
else if(n%2==0)
{
n=n/2;
d=6;
}
else
{
n=n/2+1;
d=7;
}
//logic ends here
//printing output
cout<<(n-1)*d;
return 0;
}

Output:

Enter the position: 15
49

5.Find the nth term of the series.

1,1,2,3,4,9,8,27,16,81,32,243,…

//C++ Program
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
//initialising variables
int n,r,term;
cout<<"Enter the position: ";
//user input
cin>>n;
//logic to find nth element of the series
if(n==1||n==2)
{
cout<<1;
return 0;
}
else if(n%2==0)
{
n=n/2-1;
r=3;
}
else
{
n=n/2;
r=2;
}
//logic ends here
//printing output
cout<<(int)(pow(r,n));
return 0;
}

Output:

Enter the position: 17
256

6.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.

//C++ Program
#include<iostream>
using namespace std;
//main program
int main()
{
//initialising variables
int n;
cout<<"Enter position: ";
//user input
cin>>n;
//logic to find nth term
if(n==1||n==2)
cout<<0;
else if(n%2==0)
cout<<(n-1)/2;
else
cout<<n-1;
//logic ends
return 0;
}

Output:

Enter position: 198
98

7. The program will recieve 3 English words inputs from STDIN

  1. These three words will be read one at a time, in three separate line

  2. The first word should be changed like all vowels should be replaced by $

  3. The second word should be changed like all consonants should be replaced by #

  4. The third word should be changed like all char should be converted to upper case

  5. Then concatenate the three words and print them

 

//C++ program
#include<iostream>
#include<string.h>
using namespace std;
//main program;
int main()
{
//initialising Strings
char ch1[5],ch2[5],ch3[5];
cout<<"Enter 1st word: ";
//user input1;
gets(ch1);
cout<<"Enter 2nd word: ";
//user input2
gets(ch2);
cout<<"Enter 3rd word: ";
//user input3
gets(ch3);
//processing first word
for(int i=0;i<strlen(ch1);i++)
{
if(ch1[i]=='a'||ch1[i]=='e'||ch1[i]=='i'||ch1[i]=='o'||ch1[i]=='u'||ch1[i]=='A'||ch1[i]=='E'||ch1[i]=='I'||ch1[i]=='O'||ch1[i]=='U')
ch1[i]='$';
}
//processing second word
for(int i=0;i<strlen(ch2);i++)
{
if((ch2[i]>='A'&&ch2[i]<='Z'||ch2[i]>='a'&&ch2[i]<='z' )&&!( ch2[i]=='a' || ch2[i]=='e' || ch2[i]=='i' || ch2[i]=='o' || ch2[i]=='u' ||ch2[i]=='A'||ch2[i]=='E'||ch2[i]=='I'||ch2[i]=='O'||ch2[i]=='U'))
ch2[i]='#';
}
//processing third word
for(int i=0;i<strlen(ch3);i++)
{
if(ch3[i]>='a' && ch3[i]<='z')
{

ch3[i]=ch3[i]-32;
}
}
strcat(ch1,strcat(ch2,ch3));
//output
cout<<ch1;
return 0;
}