898 views

Question 1: (8 marks)

Write a program to print the pattern below. Taking n as input, print n lines that print the pattern.

Sample input 1:
3

Sample output 1:
*    *
**  **
******

Sample input 2:
5

Sample output 2:
*        *
**      **
***    ***
****  ****
**********

Question 2: (12 marks)

The int datatype has a limitation of maximum value. It can only store values upto a certain limit. So what we want to do is take a very big integer and add 1 to it.

INPUT FORMAT:

First line contains number of digits in the integer, n. This is at maximum 50.

The next line consists of all the digits of the integer, separated by spaces.

You are required to add 1 to this integer and output the digits of the resulting integer (without spaces)

Sample input:
12
8 7 5 1 2 5 4 8 7 5 4 5

Sample output:
875125487546

Question 3: (8 marks)

Leap years are an interesting way of putting together lost time over the previous 4 years. These are those years when February has 29 days. One of your friends Raj was born on 29th of February. He wishes to know how many times till date he lived through his actual birthday (29th February). Take his birth year and present year as inputs and print the number of times he has experienced 29th of February. Note if present year is a leap year, do include its 29th of February as well. You may also display an error as “Birth Year is incorrect” if the birth year entered is not a leap year.

Note: Leap years are years which are multiples of 4 with the exception of years which are divisble by 100 but not by 400.

Sample input 1:
1996 2019

Sample output 1:
6

Sample input 2:
1997 2019

Sample output 2:
Birth year is incorrect

Question 4: (12 marks)

Sort an array in ascending order by flipping (exchanging) 2 adjacent integers not in the correct order until there is no such pair.

The leftmost swappable pair must be swapped first, i.e. the first pair encountered while going from left to right, which is in the opposite (descending order) should be swapped. Then the whole process should be repeated.

INPUT FORMAT:

First line contains 1 integer N denoting the number of integers in input. Next line contains N integers separated by spaces.

OUTPUT FORMAT:

For each flip performed, display the array in a line separated by spaces.

Sample input:
5
10 7 6 2 5

Sample output
10 7 6 2 5
7 10 6 2 5
7 6 10 2 5
6 7 10 2 5
6 7 2 10 5
6 2 7 10 5
2 6 7 10 5
2 6 7 5 10
2 6 5 7 10
2 5 6 7 10

Question 5: (12 marks)

In this question we’ll deal with Fibonacci series. The series is defined on whole numbers like fib(0), fib(1),… as follows:

fib(0) = 0

fib(1) = 1

fib(n) = fib(n-1) + fib(n-2), for n >= 2

INPUT FORMAT:

An integer N, 0 <= N <= 1000

OUTPUT FORMAT:

Add the unit digits of all the Fibonacci numbers from fib(0) to fib(N) and print only this sum

Sample Input:
8

Sample Output:
24

Question 6: (8 marks)

You are to write a program which takes 2 complex numbers as input and prints the sum and multiplication of the 2 complex numbers.

INPUT FORMAT:

The first and second lines contain 2 integers (the real and imaginary parts) for each complex number involved.

OUTPUT FORMAT:

The complex numbers sum and product on separate lines in the form a+bi or a-bi (depending on whether the complex part is negative or not).

Sample input:
1 2
1 3

Sample output:
2+5i
-5+5i
posted May 18 | 898 views
7
Like
0
Love
0
Haha
1
Wow
0
Angry
0

3rd Code

#include<iostream>
#include<stdlib.h>
using namespace std;

void cleap(int n)
{
if(n%4==0)
{
if(n%100==0)
{
if(n%400==0)
{
}
else
{
cout<<"Birth Year is incorrect";

exit(0);
}
}
}
else
{
cout<<"Birth Year is incorrect";

exit(0);
}
}

int countleap(int n,int z)
{

if(n%4==0)
{
if(n%100==0)
{
if(n%400==0)
{
z++;
return z;
}
else
return z;

}

else
{
z++;
return z;
}
}
else
return z;
}

int main()
{
int n;
int t;

cin>>n>>t;

cleap(n);
int k=0;

for(int i=n;i<=t;i++)
{
k=countleap(i,k);
}

cout<<k;
return 0;

}



5th CODE

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
int n;

cin>>n;

int a[n];
a[0]=0;
a[1]=1;

for(int i=2;i<=n;i++)
{
a[i]=a[i-1]+a[i-2];
}

int s=0;

for(int i=0;i<=n;i++)
{
if(a[i]!=0)
{

int c=ceil(log10(a[i]));
if(c==1)
{

int b=(int)(pow(10,(c)));
int p= a[i] % b;
s=s+p;
}

if(c>=2)
{
int b=(int)(pow(10,(c-1)));
int p= a[i] % b;
s=s+p;
}
}

if(a[i]==1)
{
s=s+1;
}

}
cout<<s;
return 0;

}

How much time was given for doing these 6 codes?

4th Code

#include<iostream>
using namespace std;

int main()
{
int n;
cin>>n;

int a[n];

for(int i=0;i<n;i++)
{
cin>>a[i];
}

for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";

int j=0;

while(j!=n)
{
int p=0;
if(a[j]>a[j+1] && j!=(n-1))
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
p=1;
}
if(p==0)
j++;
else
j=0;

if(p==1)
{

for(int k=0;k<n;k++)
{
cout<<a[k]<<" ";
}
cout<<"\n";
}

}

}

6th CODE

#include<iostream>
using namespace std;

int main()
{
int a,b,c,d;

cin>>a>>b>>c>>d;

int x=a+c;
int y=b+d;

if(y<0)
{
cout<<x<<y<<"i";
}

if(y>=0)
{
cout<<x<<"+"<<y<<"i";
}

cout<<"\n";

//MULTIPLICATION LOGIC

int q= c*a;
int w= c*b;
int e=d*a;
int r=-1*(d*b);

int m= q+r;
int n= w+e;

if(n>=0)
cout<<m<<"+"<<n<<"i";
else
cout<<m<<n<<"i";

return 0;

}