Question 1
Your task is to complete a function “count_heads()” that takes two inputs N and R.
The function should return the probability of getting exactly R heads on N successive
tosses of a fair coin. A fair coin has an equal probability of landing a head or a tail
(i.e. 0.5) on each toss.
Example 1
Input: 1 1
Output: 0.500000
Example 2
Input: 4 3
Output: 0.250000
import java.util.*;
class Main
{
public static int fact(int n)
{
if(n==0)
return 1;
return n*fact(n-1);
}
public static double count_heads(int n, int r)
{
double res;
res = fact(n) / (fact(r) * fact(n - r));
res = res / (Math.pow(2, n));
return res;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int r=sc.nextInt();
System.out.println(count_heads(n,r));
}
}
Question 2
Write a program that will print the sum of diagonal elements of a 10X10
matrix. The program will take a total of 100 numbers as input (10
numbers will be input per line and each number will be separated by a
space).
Example 1
Input: 1 234567890
0123456780
3456789640
2345678932
3456743213
3456244246
2346246235
2356246235
2462143352
3352462146
Output: 42
Example 2
Input: 1 22 33 44 55 66 77 88 99 100
100 1 88 77 66 55 44 33 22 11
88 88 1 66 55 44 33 22 11 100
88 77 66 1 44 33 22 11 100 99
77 66 55 44 1 22 11 88 99 100
66 55 44 33 22 1 77 88 99 100
44 33 22 11 100 99 1 77 66 55
33 22 11 100 99 88 77 1 55 44
22 11 100 99 88 77 66 55 1 33
100 11 22 33 44 55 99 88 77 1
Output: 10
Import java.util.*;
Public class Main
Public static void main(String[] args)
Scanner sc=new Scanner(System.in);
Int arr[][]=new int[10][10];
For(int i=0;i<10;i++)
For(int j=0;j<10;j++)
Arr[i][j]=sc.nextInt();
Int sum=0;
For(int i=0;i<10;i++)
Sum=sum+arr[i][i];
System.out.println(sum);
Question 3
Write a program that will take one string as input. The program will then
remove vowels a, e, i, o, and u (in lower or upper case ) from the string. If
there are two or more vowels that occur together then the program shall
ignore all of those vowels.
Example 1
Input: Cat
Output: Ct
Example 2
Input: Compuuter
Output: Cmpuutr
PROGRAM:
Import java.util.Scanner;
Public class RemoveVowels {
Public static boolean isVowel(char c) {
Char lowercase = Character.toLowerCase(c);
Return lowercase == ‘a’ || lowercase == ‘e’ || lowercase == ‘i’ ||
lowercase == ‘o’ || lowercase == ‘u’;
Public static String removeVowels(String input) {
StringBuilder result = new StringBuilder();
For (char c : input.toCharArray()) {
If (!isVowel(c)) {
Result.append(c);
Return result.toString();
Public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
String result = removeVowels(input);
System.out.println(result);
Question 4
Write a program that will take a string as input. The program will then determine
whether each left parenthesis ‘(’ has a matching right parenthesis ‘)’ and also all the
‘)’ has a consecutive ‘(‘. If so, the program will print 0 else the program will print 1.
Example 1
Input: HELLO AND (WELCOME (TO THE) TCEA (CONTEST)TODAY)IS
(SATURDAY())
Output: 0
Example 2
Input: (9*(7-2)*(1*5)
Output: 0
#include<bits/stdc++.h>
Using namespace std;int main()
String s;
Int c=0;
Getline(cin,s);
For (auto i:s)
If(i==’(‘) c++;
If(i==’)’) c--;
}
Cout<<(c==0);
Question 5
Write a program to find out and display prime numbers from the given list
of integers. The program will accept input in two lines. First-line contains a
number indicating the total number of integers in the list and the second
line contains integers separated by spaces.
Example 1
Input: 5
46937
Output: 3 7
Example 2
Input: 10
8 10 3 12 7 15 11 2 17 26
Output: 3 7 11 2 17
Import java.util.*;
Public class Main
Public static boolean isPrime(int n)
Boolean var;
For(int i=2;i<n/2;i++){
If(num%i!=0) var = true
Else {
Var = false; break;
}
Return var;
Public static void main(String[] args)
Scanner sc=new Scanner(System.in);
Int n=sc.nextInt();
Int arr[]=new int[n];
For(int i=0;i<n;i++)
Arr[i]=sc.nextInt();
ArrayList prime=new ArrayList<>();
For(int i=0;i<n;i++)
If(isPrime(arr[i]))
Prime.add(arr[i]);
Iterator itr=prime.iterator();
While(itr.hasNext())
System.out.print(itr.next()+” “);
Question 6
Write a program that will take a number as input. The program will
convert the inputted number to a format, which will contain an integer
part and a fraction part. The fraction part needs to be reduced to its
lowest representation. The input will not contain any repeating decimals
e.g. 1.3333…33. The output will be :
The integer part of the number ++fraction using a ’/’
Example 1
Input: 2.95
Output: 2 19/20
Example 2
Input: 3.08
Output: 3 2/25
C++ Java Python
Run
#include<bits/stdc++.h>
Using namespace std;
Int main()
String s;
Cin>>s;
String s1;
Int idx=-1;
For(int i=0;i<s.length();i++)
If(s[i]==’.’) idx=i;
Else s1+=s[i];
Int a=stoi(s1);
Int p=1;
If(idx!=-1)
For(int i=0;i<s.length()-idx-1;i++)
P*=10;
Int c=__gcd(a,p);
a/=c;p/=c;
cout<<a/p<<” “<<a-p*(a/p)<<”/”<<p;
Question 7
Given a sentence with numbers representing a word’s location in the
sentence, embedded within each word, and return the sorted sentence.
Note: We are using a maximum of 0-9 numbers only for 1 sentence
Example 1
Input: is1 Thi0s T3est 2a
Output: This is a Test
Example 2
Input: t2o j3oin 4WonderBiz I0 Technolog5ies wan1t
Output: I want to join WonderBiz Technologies
C++ Java Python
Run
#include<bits/stdc++.h>
Using namespace std;
Map<int,string> m;
Void fun(string s)
{
String s1=””,s2=””;
For(auto i:s)
If(i<=’9’&&i>=’0’) s1+=i;
Else s2+=i;
M[stoi(s1)]=s2;
Int main()
String s;
Int c=0;
Getline(cin,s);
Istringstream ss(s);
While(ss)
String word;ss>>word;
If(word==””) break;
Fun(word);
C++;
For(int i=0;i<c;i++)
Cout<<m[i]<<” “;
Question 8
Write a program that takes an integer M and M integer array elements as
input. The program needs to find the minimum difference between two
elements in the integer array. The program then needs to print all those
pairs of elements that have the minimum difference. If more than one pair
has the minimum difference, then the program should print the output in
the ascending order, if an element exists in two or more pairs, then it
should be printed two times or more.
Example 1
Input: 4
55 44 33 22
Output: 22 33 33 44 44 55
Explanation: The minimum difference between two elements is 11. Hence
the pairs are printed in the ascending order. Here 33 and 44 appear in two
different pairs; hence both are printed twice.
Example 2
Input: 5
1 99 22 44 1001
Output: 1 22
C++ Java Python
Run
#include<bits/stdc++.h>
Using namespace std;
Int main()
Int n;cin>>n;vector a(n);
Int mm=INT_MAX;
For(int i=0;i<n;i++) cin>>a[i];
Sort(a.begin(),a.end());
Map<int,vector<pair<int,int>>> m;
For(int i=1;i<n;i++)
{
Mm=min(mm,a[i]-a[i-1]);
M[a[i]-a[i-1]].push_back({a[i],a[i-1]});
For(auto i:m[mm])
Cout<<i.second<<” “<<i.first<<” “;
Question 9
Write a program to convert a number to binary format.
Example 1
Input: 0
Output: 0
Example 2
Input: 1
Output: 1
C++ Java Python
Run
#include<bits/stdc++.h>
Using namespace std;
Int main()
Int n;cin>>n;
String s=””;
While(n)
{
If((n&1)) s+=’1’;
Else s+=’0’;
n>>=1;
Reverse(s.begin(),s.end());
Cout<<s;
Question 10
Write a program that receives a word A and some texts as input. You need
to output the texts (without modifying them) in the ascending order of the
number of occurrences of the word A in the texts. The input is as follows:
an integer M(between 1 and 100, inclusive), followed by the word A in the
next line, and some text in each of the M next lines.
Note: The texts and the word A contain only lowercase Latin letters
(a,b,c…,z) and blank spaces (“ ”). The maximum size of the texts and the
word A is 100 Characters. Every text has a different number of
occurrences of the word A.
Note 2:you must print one text per line without modifying the texts.
Example 1
Input: 2
Java
I hate java
Python is a good programming language
Output: Python is a good programming language
I hate java
Example 2
Input: 3
Python
I like to code in python
Python is named after a show name monty python and not after
the snake python
I think python is good i think python is important than php
Output: i like to code in python
I think python is good i think python is important than php
Python is named after a show name monty python and not after
the snake python
C++ Java Python
Run
#include<bits/stdc++.h>
Using namespace std;
String s;
Map<string,int> m2;
Bool cmp(pair<string, int>& a,
Pair<string, int>& b)
Return a.second < b.second;
Void sort(map<string, int>& M)
Vector<pair<string, int> > A;
For (auto& it : M) {
A.push_back(it);
Sort(A.begin(), A.end(), cmp);
For (auto& it : A) {
For(int i=0;i<m2[it.first];i++)
Cout << it.first <<endl; } } int count(string s1) { istringstream
ss(s1); int c=0; while(ss) { string w; ss>>w;
If(w==s) c++;
Return c;
Int main()
Int n;getline(cin,s);n=stoi(s);
Getline(cin,s);
Transform(s.begin(),s.end(),s.begin(),::tolower);
Vector<string> v(n);
Vector<int> a(n);
Map<string,int> m;
For(int i=0;i<n;i++)
getline(cin,v[i]);
transform(v[i].begin(),v[i].end(),v[i].begin(),::tolower);
m2[v[i]]++;
m[v[i]]=count(v[i]);
sort(m);