LBP6 FebMarApr22 Runnin Notes
LBP6 FebMarApr22 Runnin Notes
com/lbp6222
https://www.jdoodle.com/
https://www.jdoodle.com/c-online-compiler/
https://www.jdoodle.com/online-compiler-c++/
https://www.jdoodle.com/online-java-compiler/
https://www.jdoodle.com/python3-programming-online/
LBP6 @7.30PM
Benifits
1) Running Notes
2) Video recordings 7 months (view)
3) Free workshops on java and python
4) hackerrank link (life time validity)
output statements:
------------------
C
printf()
%d int
%f float
%lf double
%c char
%s char[]
C++
cout<<
Java
System.out.print()
System.out.println()
System.out.printf()
python
10 forms of print() statement
Ex:
#include<stdio.h>
int main()
{
printf("welcome to c lang.");
return 1;
}
Ex:
#include<iostream>
using namespace std;
int main()
{
cout<<"Welcome to C++ Programming";
return 1;
}
Ex:
public class Prakash
{
public static void main(String[] args)
{
System.out.println("welcome to Java");
}
}
Ex:
print("Hello Python")
input statements:
-----------------
C:
scanf()
%d
%c
%f
%s
C++:
cin>>
python:
input() ---------> string
int(input()) ----> int
float(input()) --> float
Java:
1. BufferedReader ----> java.io -----> char and String
2. Console -----------> java.io -----> String and Password
3. Scanner -----------> java.util ---> all types of data types
Note: There is no direct method to read a character value, indirectly we can use
String concept.
Ex:
#include<stdio.h>
int main()
{
int a,b,c;
scanf("%d %d",&a,&b);
c=a+b;
printf("%d",c);
return 1;
}
Ex:
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b;
c=a+b;
cout<<c;
return 1;
}
Ex:
import java.util.*;
public class Prakash
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
int a,b,c;
a = obj.nextInt();
b = obj.nextInt();
c = a+b;
System.out.println(c);
}
}
Ex:
a=int(input())
b=int(input())
c=a+b
print(c)
COUT<<
5<<1
LBP1
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
if(n>=0)
{
if(n%2==0)
{
printf("even");
}
else
{
printf("odd");
}
}
else
{
printf("invalid");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
if n>=0:
if n%2==0:
print("even")
else:
print("odd")
else:
print("invalid")
LBP2
Given an integer n, perform the following conditional actions,
if n is odd, print weird,
if n is even and in the inclusive range of 2 to 5, print Not Weird.
if n is even and in the inclusive range of 6 to 20, print Weird.
if n is even and greater than 20, print Not Weird.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
if(n%2!=0)
{
printf("Weird");
}
else
{
if(n>=2 && n<=5)
printf("Not Weird");
else if(n>=6 && n<=20)
printf("Weird");
else
printf("Not Weird");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
if n%2!=0:
print("Weird")
else:
if n>=2 and n<=5:
print("Not Weird")
elif n>=6 and n<=20:
print("Weird")
else:
print("Not Weird")
LBP3
To check whether the given year is leap year or not.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int y;
scanf("%d",&y);
if((y%4==0 && y%100!=0)||(y%400==0))
printf("True");
else
printf("False");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
1st version:
------------
y=int(input())
if (y%4==0 and y%100!=0) or (y%400==0):
print("True")
else:
print("False")
2nd version:
------------
import calendar
print(calendar.isleap(int(input())))
LBP4
The e-commerce company Bookshelf wishes to analyse its monthly sales data between
minimum range 30 to max range 100. The company has categorized these book sales
into four groups depending on the number of sales with the help of these groups the
company will know which stock they should increase or decrease in their inventory
for the next month. the groups are as follows
write an alg to find the group for the given book sale count.
logic:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
if(n>=30 && n<=100)
{
if(n>=30 && n<=50)
printf("D");
else if(n>=51 && n<=60)
printf("C");
else if(n>=61 && n<=80)
printf("B");
else
printf("A");
}
else
printf("invalid");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
if n>=30 and n<=100:
if n>=30 and n<=50:
print("D")
elif n>=51 and n<=60:
print("C")
elif n>=61 and n<=80:
print("B")
else:
print("A")
else:
print("invalid")
LBP5
read n
print n+1
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
printf("%d",++n);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
There is no increment and decrement operators in python
n=int(input())
n=n+1 #n+=1
print(n)
LBP6
For each of the 6 coffee cups I buy, I get a 7th cup free. In total, I get 7 cups.
Implement a program that takes n cups bought and print as an integer the total
number of cups I would get.
1 ----> 1
2 ----> 2+1
3 ----> 3+1
4 ----> 4+2
n ----> n+n/2
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
printf("%d",n+n/6);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
print(n+n//6)
LBP7
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
printf("%d ",d);
n=n/10;
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
while n!=0:
d=n%10
print(d,end=' ')
n=n//10
LBP8
Sum of Digits
while(n!=0)
{
d=n%10;
print d
n=n/10;
}
s=0;
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
print s
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,s=0,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
s=0
while n!=0:
d=n%10
s=s+d
n=n//10
print(s)
LBP9
Implement a program to calculate sum of even digits present in the given number
digit d
logic:
d%2==0
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,s=0,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
if(d%2==0)
s=s+d;
n=n/10;
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
s=0
while n!=0:
d=n%10
if d%2==0:
s=s+d
n=n//10
print(s)
LBP10
Implement a program to calculate sum of odd digits present in the given number
logic:
d%2!=0
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,s=0,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
if(d%2!=0)
s=s+d;
n=n/10;
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
s=0
while n!=0:
d=n%10
if d%2!=0:
s=s+d
n=n//10
print(s)
LBP11
Implement a program to calculate sum of prime digits present in the given number
logic
s=0;
while(n!=0)
{
d=n%10;
if(d==2 or d==3 or d==5 or d==7){
s=s+d;
}
n=n/10;
}
print(s)
0-9---> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
---> 2, 3, 5, 7
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,d,s;
scanf("%d",&n);
s=0;
while(n!=0)
{
d=n%10;
if(d==2 || d==3 || d==5 || d==7)
s=s+d;
n=n/10;
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
public class Solution {
python implementation:
----------------------
n=int(input())
s=0
while n!=0:
d=n%10
if d==2 or d==3 or d==5 or d==7:
s=s+d
n=n//10
print(s)
LBP12
Implement a program to calculate sum of digits that are divisible by 3 in the given
number
0-9 ----> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
----> 3, 6, 9
logic:
s=0
while(n!=0)
{
d=n%10;
if(d%3==0)
s=s+d;
n=n/10;
}
print s
s=0
while(n!=0)
{
d=n%10;
if(d==3 or d==6 or d==9)
s=s+d;
n=n/10;
}
print s
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,s=0,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
if(d%3==0)
s=s+d;
n=n/10;
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
print(sum([int(i) for i in input() if i in '369']))
3 or 6 or 9 ---> in '369'
'x' in '369'
LBP13
Number of digits
Implement a program to calculate number of digits in the given number
logic:
count=0;
while(n!=0)
{
count++;
n=n/10;
}
print(count)
123 ----> 3
45678---> 5
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,c=0;
scanf("%d",&n);
while(n!=0)
{
c++;
n=n/10;
}
printf("%d",c);
return 0;
}
2nd version:
------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
printf("%d",strlen(s));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
python implementation:
----------------------
print(len(input()))
LBP14
Reverse Integer
logic:
r=0
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
print r
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,r=0,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
printf("%d",r);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
print(s[::-1])
LBP15
Duck Number
logic
flag=0
while(n!=0)
{
d=n%10;
if(d==0)
{
flag=1;
break;
}
n=n/10
}
if flag==1 print Yes else No
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,flag=0;
scanf("%d",&n);
while(n!=0)
{
if(n%10==0)
{
flag=1;
break;
}
n=n/10;
}
printf((flag==1)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
print("Yes" if "0" in input() else "No")
LBP16
Number of Occurrences
logic
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,key,c;
scanf("%d",&n);
scanf("%d",&key);
c=0;
while(n!=0)
{
if(n%10==key)
c++;
n=n/10;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
print(input().count(input()))
LBP17
Paliandrome Number
logic
temp=n
r=0
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
if temp==r print Yes else No
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,r=0,temp,d;
scanf("%d",&n);
temp=n;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
printf((temp==r)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
print("Yes" if s==s[::-1] else "No")
LBP18
Check Birth Day
logic:
read day
read month
if month=="july" and day==5 then print 1 else 0
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char month[100];
int day;
scanf("%s",month);
scanf("%d",&day);
if(strcmp(month,"july")==0 && day==5)
printf("1");
else
printf("0");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
month=input()
day=int(input())
if month=="july" and day==5:
print(1)
else:
print(0)
LBP19
Decimal to Binary
A network protocol specifies how data is exchanged via transmission media.
The protocol converts each message into a stream of 1's and 0's.
Given a decimal number, write an algorithm to convert the number into a binary
form.
logic:
array a
read n
i=0
while n!=0
{
a[i++]=n%2;
n=n/2;
}
for(i=i-1;i>=0;i--) print a[i]
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i=0;
scanf("%d",&n);
while(n!=0)
{
a[i++]=n%2;
n=n/2;
}
for(i=i-1;i>=0;i--)
printf("%d",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
print(str(bin(n))[2:])
LBP20
Lucky Customer
An e-commerce website wishes to find the lucky customer who will be eligible for
full value cash back.
For this purpose,a number N is fed to the system.
It will return another number that is calculated by an algorithm.
In the algorithm, a seuence is generated, in which each number n the sum of the
preceding numbers.
initially the sequence will have two 1's in it.
The System will return the Nth number from the generated sequence which is treated
as the order ID.
The lucky customer will be one who has placed that order.
Write an alorithm to help the website find the lucky customer.
logic:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int fib(int n)
{
if(n==0||n==1)
return n;
else
return fib(n-1)+fib(n-2);
}
int main() {
int n;
scanf("%d",&n);
printf("%d",fib(n));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def fib(n):
if n==0 or n==1:
return n
else:
return fib(n-1)+fib(n-2)
n=int(input())
print(fib(n))
LBP21
Christmas offer
An e-commerce company plans to give their customers a special discount for the
Christmas,
they are planning to offer a flat discount.
The discount value is calculated as the sum of all prime digits in the total bill
amount.
Write an algorithm to find the discount value for the given total bill amount.
input ----> the input consists of an integer order value representing the total
bill amount
condition-> no conditions
output ---> print an integer representing discount value for the given total bill
amount.
logic:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,d,s;
scanf("%d",&n);
s=0;
while(n!=0)
{
d=n%10;
if(d==2 || d==3 || d==5 || d==7)
s=s+d;
n=n/10;
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
s=0
while n!=0:
d=n%10
if d==2 or d==3 or d==5 or d==7:
s=s+d
n=n//10
print(s)
LBP22
Niven Number
Write a program to accept a number and check and display whether it is a Niven
Number or not.
Niven Number is that a number which is divisible by its sum of digits.
logic:
sum=0;
temp=n;
while(n!=0)
{
sum=sum+(n%10);
n=n/10;
}
if(temp%sum==0) then print Yes else No
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,sum=0,temp;
scanf("%d",&n);
temp=n;
while(n!=0)
{
sum=sum+(n%10);
n=n/10;
}
printf((temp%sum==0)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
s=0
temp=n
while n!=0:
d=n%10
s=s+d
n=n//10
print("Yes" if temp%s==0 else "No")
LBP23
A Special two digit number
A special two digit number is a number such that when the sum of its digits is
added to the product of its digits, the result should be equal to the original two-
digit number.
Implement a program to accept a two digit number and check whether it is a special
two digit number or not.
logic:
read n
formula = sum of digits + product of digits
a=n%10;
b=(n/10)%10;
c=(a+b)+(a*b)
if c==n then Yes else No
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a,b,c;
scanf("%d",&n);
a=n%10;
b=(n/10)%10;
c=(a+b)+(a*b);
printf((c==n)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
a=n%10
b=(n//10)%10
c=(a+b)+(a*b)
print("Yes" if n==c else "No")
LBP24
Sum of even numbers
Implement a program to find sum of even number between x and y both are inclusive.
logic
sum=0
for(i=n1;i<=n2;i++){
if(i%2==0){
sum=sum+i;
}
}
print sum
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n1,n2,i,sum;
scanf("%d %d",&n1,&n2);
sum=0;
for(i=n1;i<=n2;i++){
if(i%2==0){
sum=sum+i;
}
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n1=int(input())
n2=int(input())
sum=0
for i in range(n1,n2+1):
if i%2==0:
sum=sum+i
print(sum)
LBP25
Celsius to Fahrenheit
formula:
F = (C*9/5) + 32
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int c,f;
scanf("%d",&c);
f=(c*9/5)+32;
printf("%d",f);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
c=int(input())
f=(c*9//5)+32
print(f)
LBP26
Fahrenheit to Celsius
formula:
C = (F-32)*5/9
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int c,f;
scanf("%d",&f);
c=(f-32)*5/9;
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
f=int(input())
c=(f-32)*5//9
print(c)
LBP27
Find The Sequence Sum
(increment from i until it equals to j, then decrement from j until equals k).
Given values i,j,k.
caluclate the sequence sum as described.
int getSequenceSum(int,int,int);
logic
sum=0
while(i<=j){sum=sum+(i++);}
while(j!=k){sum=sum+(--j);}
print sum
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i,j,k,sum;
scanf("%d %d %d",&i,&j,&k);
sum=0;
while(i<=j){
sum=sum+(i++);
}
while(j!=k){
sum=sum+(--j);
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
i=int(input())
j=int(input())
k=int(input())
sum=0
while i<=j:
sum=sum+i
i=i+1
while j!=k:
j=j-1
sum=sum+j
print(sum)
LBP28
You are climbing a stair case.
It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you
climb to the top?
logic:
fib(1) =====> 1
fib(2) =====> 2
fib(3) =====> 3
.
.
.
sir, by observing the result we can only conclude that it's following Fibonacci
sequence
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int fib(int n){
if(n==0||n==1)
return 1;
else
return fib(n-1)+fib(n-2);
}
int main() {
int n;
scanf("%d",&n);
printf("%d",fib(n));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def fib(n):
if n==0 or n==1:
return 1
else:
return fib(n-1)+fib(n-2)
n=int(input())
print(fib(n))
LBP29
11 ----> 1 and 11
13 ----> 1 and 13
17 ----> 1 and 17
logic
factors=0;
for(i=1;i<=n;i++){
if(n%i==0)
factors++;
}
if factors==2 then print "true" else "false"
c implementation:
----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,factors=0;
scanf("%d",&n);
factors=0;
for(i=1;i<=n;i++){
if(n%i==0)
factors++;
}
printf((factors==2)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
LBP30
Valid Palindrome
logic
low=0
high=len-1
while(low<=high){
if(s[low]!=s[high])
return 0;
low++;
high--;
}
return 1;
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int ispali(char s[]){
int low,high;
low=0;
high=strlen(s)-1;
while(low<=high){
if(s[low]!=s[high])
return 0;
low++;
high--;
}
return 1;
}
int main() {
char s[100];
scanf("%s",s);
printf((ispali(s)==1)?"valid":"invalid");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
print("valid" if s==s[::-1] else "invalid")
Sir Can you please provide solution without inbuilt function in JAVA?
LBP31
"Secure Assets Private Ltd", a small company that deals with lockers has recently
started manufacturing digital locks which can be locked and unlocked using PINs
(passwords).
You have been asked to work on the module that is expected to generate PINs using
three input numbers.
The three given input numbers will always consist of three digits each
i.e. each of them will be in the range >=100 and <=999.
Bellow are the rules for generating the PIN.
LOGIC:
read n1,n2,n3
d1 = min(n1%10,n2%10,n3%10)
d2 = min((n1/10)%10,(n2/10)%10,(n3/10)%10)
d3 = min((n1/100)%10,(n2/100)%10,(n3/100)%10)
d4 = max(maxD(n1),maxD(n2),maxD(n3))
pin = d4*1000+d3*100+d2*10+d1
n1=123
n2=456
n3=789
d1=min(3,6,9)=3
d2=min(2,5,8)=2
d3=min(1,4,7)=1
d4=max(3,6,9)=9
pin=9*1000+1*100+2*10+3
=9000+100+20+3
=9123
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int max(int a,int b,int c){
return (a>b && a>c)?a:(b>c?b:c);
}
int min(int a,int b,int c){
return (a<b && a<c)?a:(b<c?b:c);
}
int maxD(int n){
int m=0;
while(n!=0){
if(n%10>m)
m=n%10;
n=n/10;
}
return m;
}
int main() {
int n1,n2,n3,d1,d2,d3,d4,pin;
scanf("%d %d %d",&n1,&n2,&n3);
d1=min(n1%10,n2%10,n3%10);
d2=min((n1/10)%10,(n2/10)%10,(n3/10)%10);
d3=min((n1/100)%10,(n2/100)%10,(n3/100)%10);
d4=max(maxD(n1),maxD(n2),maxD(n3));
pin=d4*1000+d3*100+d2*10+d1;
printf("%d",pin);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n1=[int(i) for i in input()]
n2=[int(i) for i in input()]
n3=[int(i) for i in input()]
d1=min(n1[2],n2[2],n3[2])
d2=min(n1[1],n2[1],n3[1])
d3=min(n1[0],n2[0],n3[0])
d4=max(max(n1),max(n2),max(n3))
print(d4*1000+d3*100+d2*10+d1)
LBP32
Program to count number of special characters and white spaces in a given string.
input --------> A string from the user
constraint ---> non-empty string
output -------> number of special characters
logic:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int counter=0;
scanf("%[^\n]s",s);
for(int i=0;s[i];i++){
if((s[i]>=65 && s[i]<=90)||(s[i]>=97 && s[i]<=122)||(s[i]>=48 && s[i]<=57))
continue;
else
counter++;
}
printf("%d",counter);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
counter=0
for i in s:
if not i.isalnum():
counter=counter+1
print(counter)
LBP33
An e-commerce company plans to give their customers a discount for the newyears
holiday.
The discount will be calcualted on the basis of the bill amount of the order
placed.
The discount amount is the sum of all the odd digits in the customers total bill
amount.
if no odd digits is present in the bill amount, then discount will be zero.
input ---------> the input consists of an integer bill amount, representing the
customers total bill amount.
output -------> print an integer representing the dicount for the given total bill
amount.
constraint ---> n>0
logic
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,s=0,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
if(d%2!=0)
s=s+d;
n=n/10;
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
s=0
while n!=0:
d=n%10
if d%2!=0:
s=s+d
n=n//10
print(s)
LPB34
Email name should be starts with alphabet and should follw by number or underscore.
It should contains either numbers or underscore finally ends with @gmail.com only,
Then given email id is true otherwise false.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define isalpha(ch) (ch>='a' && ch<='z')
#define isdigit(ch) (ch>='0' && ch<='9')
int email(char s[]){
int i;
for(i=0;s[i]&&isalpha(s[i]);i++);
if((i==0 && isdigit(s[i]))||(i==0 && s[i]=='_'))
return 0;
else if(isdigit(s[i])){
i++;
if(s[i]!='_' && strcmp(s+i,"@gmail.com")==0)
return 1;
else
return 0;
}
else if(s[i]=='_' && strcmp(s+i+1,"@gmail.com")==0){
return 1;
}
else{
return 0;
}
}
int main() {
char s[100];
scanf("%s",s);
if(email(s))
printf("true");
else
printf("false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
python implementation:
----------------------
import re
m = re.fullmatch("[a-z]+[_|0-9]@gmail[.]com",input())
print("true" if m!=None else "false")
LBP35
The IT company "Soft ComInfo" has decided to transfer its messages through
the N/W using new encryption technique.
The company has decided to encrypt the data using the non-prime number concept.
The message is in the form of a number and
the sum of non-prime digits present in the message is used as the encryption key.
input ------> The input consists of an integer numMsg representing the numeric form
of the message.
output ------> print an integer representing the encryption key.
note: Digit 1 and 0 are considered as a prime number.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,s=0,d;
scanf("%d",&n);
while(n!=0){
d=n%10;
if(d==4||d==6||d==8||d==9)
s=s+d;
n=n/10;
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
print(sum([int(i) for i in input() if i in "4689"]))
LBP36
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
for(int i=0;s[i];i++){
if(s[i]>='A' && s[i]<='Z'){
printf("%c",s[i]);
break;
}
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
for i in s:
if i.isupper():
print(i)
break
LBP37
A=>a
a=>A
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
for(int i=0;s[i];i++){
if(s[i]>='A' && s[i]<='Z')
printf("%c",s[i]+32);
else if(s[i]>='a' && s[i]<='z')
printf("%c",s[i]-32);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
print(s.swapcase())
LBP38
A company launched a new text editor that allows users to enter english letters,
numbers and white spaces only.
If a user attempts to enter any other type of characters, it is counted as miss.
Given a String text,
write an algorithm to help the developer detect the number of misses by a given
user in the given input.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%[^\n]s",s);
int i,c=0;
for(i=0;s[i];i++){
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||(s[i]>='0'&&s[i]<='9')||
(s[i]==' '))
continue;
else
c++;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
c=0
for i in s:
if i.isalnum() or i.isspace():
continue
else:
c=c+1
print(c)
LBP39
return whichever value is nearest to 21 without going over. Return if they go both
go over.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n1,n2;
scanf("%d %d",&n1,&n2);
printf("%d",bj(n1,n2));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def bj(n1,n2):
if n1>21 and n2>21:
return 0
if n1>21:
return n2
elif n2>21:
return n1
else:
return max((n1,n2))
n1=int(input())
n2=int(input())
print(bj(n1,n2))
LBP40
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,r=0,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
printf("%d",r);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
print(s[::-1])
LBP41
logic:
p=1;
while(n!=0){
d=n%10;
p=p*d;
n=n/10;
}
print p
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,p=1,d;
scanf("%d",&n);
p=1;
while(n!=0){
d=n%10;
p=p*d;
n=n/10;
}
printf("%d",p);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import math
print(math.prod([int(i) for i in input()]))
LBP42
Write an algorithm to find the sum of all the prime numbers of the given range.
logic
s=0;
for(i=n1;i<=n2;i++){
if(isprime(i))
s=s+i;
}
c implemenation:
----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isprime(int n){
int factors=0,i;
for(i=1;i<=n;i++){
if(n%i==0)
factors++;
}
return factors==2;
}
int main() {
int n1,n2,s,i;
scanf("%d %d",&n1,&n2);
s=0;
for(i=n1;i<=n2;i++){
if(isprime(i))
s=s+i;
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def isprime(n):
f=0
for i in range(1,n+1):
if n%i==0:
f=f+1
return f==2
n1=int(input())
n2=int(input())
s=0
for i in range(n1,n2+1):
if isprime(i):
s=s+i
print(s)
LBP43
An e-Commerce company plans to give thier customers a discount for the newyears
holiday.
The discount will be calcualted on the basis of the bill amount of the order place.
The discount amount is the product of the sum of all odd digits and the sum of all
even digits of
the customers total bill amount.
input ----> an integer bill amount, representing the total bill amount of the
customer.
output ---> print an inteer representing the discount amount for the given total
bill.
logic:
se=0
so=0
while(n!=0){
d=n%10;
if(d%2==0)
se=se+d;
else
so=so+d;
n=n/10;
}
print(se*so);
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,se=0,so=0,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
if(d%2==0)
se=se+d;
else
so=so+d;
n=n/10;
}
printf("%d",se*so);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
se=0
so=0
while n!=0:
d=n%10
if d%2==0:
se=se+d
else:
so=so+d
n=n//10
print(se*so)
LBP44
War of Numbers
Create a function that takes an array of integers , sums the even and odd numbers
seperately,
then return the difference between sum of even and odd numbers.
logic:
se=0
so=0
for(i=0;i<n;i++)
{
if(a[i]%2==0)
se=se+a[i];
else
so=so+a[i];
}
diff=abs(se-so)
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,se=0,so=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
if(a[i]%2==0)
se=se+a[i];
else
so=so+a[i];
}
printf("%d",abs(se-so));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
se=0
so=0
for i in L:
if i%2==0:
se=se+i
else:
so=so+i
print(abs(se-so))
LBP45
Perfect Number
s=0;
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
if s==n then yes else no
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,s=0;
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
printf((s==n)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
s=0
for i in range(1,n):
if n%i==0:
s=s+i
print('true' if s==n else 'false')
LBP46
Magic Date
Program to read date, month and year from the user and check whether it is magic
date or not.
Here are the rules for magic date.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int d,m,y;
scanf("%d-%d-%d",&d,&m,&y);
if(d*m==y%10 || d*m==y%100 || d*m==y%1000)
printf("true");
else
printf("false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
System.out.println(s[2].endsWith(Integer.toString(Integer.parseInt(s[0])*Integer.pa
rseInt(s[1]))));
}
}
python implementation:
----------------------
L=[i for i in input().split("-")]
print(str(L[2].endswith(str(int(L[0])*int(L[1])))).lower())
LBP47
Oddish or Evenish
logic:
------
s=0;
while(n!=0){
d=n%10;
s=s+d;
n=n/10;
}
if s%2==0 then print "Evenish" else "Oddish"
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,d,s=0;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
printf((s%2==0)?"Evenish":"Oddish");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
---------------------
print("Evenish" if sum([int(i) for i in input()])%2==0 else "Oddish")
LBP48
01:00 ====> 60
02:05 ====> 120+5=125
22:01=====> 1320+1=1321
a=97
A=65
0=48
1=49 and so on
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int convert(char ch){
return ch-48;
}
int main() {
char s[100];
int n1,n2;
scanf("%s",s);//s[0]s[1]s[2][s3]s[4]
if(s[0]=='0')
n1=convert(s[1]);
else
n1=convert(s[0])*10+convert(s[1]);
if(s[3]=='0')
n2=convert(s[4]);
else
n2=convert(s[3])*10+convert(s[4]);
printf("%d",n1*60+n2);
return 0;
}
Java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l=input().split(":")
print(int(l[0])*60+int(l[1]))
LBP49
Next Prime
logic:
-----
f=0
for(i=1;i<=n;i++)
{
if(n%i==0)
f++;
}
if f==2 then it is prime else not
c implementation:
----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isprime(int n)
{
int f=0,i;
for(i=1;i<=n;i++)
{
if(n%i==0)
f++;
}
return f==2;
}
int main() {
int n;
scanf("%d",&n);
while(1){
if(isprime(n)){
printf("%d",n);
break;
}
n++;
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def isprime(n):
f=0
for i in range(1,n+1):
if n%i==0:
f=f+1
return f==2
n=int(input())
while True:
if isprime(n):
print(n)
break
n=n+1
LBP50
Create a function that sums the total number of digits between two numbers
inclusive.
for example, if the numbers are 19 and 22, then (1+9)+(2+0)+(2+1)+(2+2)=19.
for(i=n1;i<=n2;i++)
{
s=s+sumofdigits(i);
}
print s value
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int sumofdigits(int n)
{
int s=0,d;
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
return s;
}
int main() {
int n1,n2,i,s=0;
scanf("%d %d",&n1,&n2);
for(i=n1;i<=n2;i++)
s=s+sumofdigits(i);
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n1=int(input())
n2=int(input())
s=0
for i in range(n1,n2+1):
s=s+sum([int(j) for j in str(i)])
print(s)
LBP51
Defanging an IP address
. ====> [.]
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i;
scanf("%s",s);
for(i=0;s[i];i++){
if(s[i]=='.')
printf("[.]");
else
printf("%c",s[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
print(s.replace('.','[.]'))
LBP52
You are given Strings jewels representing the types of stones that are jewels,
and stones representing the stones you have.
Each character in stones is a type of stone you have
you want to know how many of the stones you have are also jewels.
Letter are case sensitive. so "a" is considered as a different type of stone from
"A".
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s1[100],s2[100];
int i,j,c=0;
scanf("%s %s",s1,s2);
for(i=0;s1[i];i++)
{
for(j=0;s2[j];j++){
if(s1[i]==s2[j])
c++;
}
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s1=input()
s2=input()
c=0
for i in s1:
c=c+s2.count(i)
print(c)
LBP53
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100],ts[100]="\0";
int i,a[100],n;
scanf("%s",s);
n=strlen(s);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
ts[a[i]]=s[i];
printf("%s",ts);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
a=[int(i) for i in input().split()]
b=[0]*len(s)
for i in range(0,len(s)):
b[a[i]]=s[i]
print(''.join(b))
LBP54
Get word count
Create a function/method that takes a string and return the word count. The string
will be a sentence.
Input: A string
Constraints: No
Output: Word count
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,c=1;
scanf("%[^\n]s",s);
for(i=0;s[i];i++)
{
if(s[i]==' '&&s[i+1]!=' ')
c++;
}
printf("%d",c);
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
l=s.split()
print(len(l))
LBP55
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s1[100],s2[100];
int i,j,c=0;
scanf("%s %s",s1,s2);
j=strlen(s1)-1;
for(i=strlen(s2)-1;s2[i];i--)
{
if(s2[i]==s1[j--])
c++;
}
printf((c==strlen(s2))?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s1=input()
s2=input()
print('true' if s1.endswith(s2) else 'false')
LBP56
Create a function/method that accepts a string (of person�s first and last name)
and
returns a string with in first and last name swapped).
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s1[100],s2[100];
scanf("%s %s",s1,s2);
printf("%s %s",s2,s1);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s1=input()
s2=input()
print(s2,s1)
LBP57
create a method/function that takes a string as its argument and returns the string
in reversed order.
Stack
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%[^\n]s",s);
for(int i=strlen(s)-1;i>=0;i--)
printf("%c",s[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
print(input()[::-1])
LBP58
A word has been split into a left part and right part.
Re-form the word by adding both halves together changing the first to an uppercase
letter.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s1[100],s2[100];
scanf("%s %s",s1,s2);
if(s1[0]>='a' && s1[0]<='z')
printf("%c",s1[0]-32);
else
printf("%c",s1[0]);
for(int i=1;s1[i];i++)
printf("%c",s1[i]);
printf("%s",s2);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s1=input()
s2=input()
print((s1+s2).title())
LBP59
Anagrams
Two strings a and b are called anagrams, if they contain all the same characters in
the same frequencies.
logic:
read s1
read s2
sort s1 in asc
sort s2 in asc
compare two string for equality ---> true or false
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s1[100],s2[100],ch;
int i,j;
scanf("%s %s",s1,s2);
for(i=0;s1[i];i++)
{
for(j=i+1;s1[j];j++)
{
if(s1[i]>s1[j]){
ch = s1[i];
s1[i]=s1[j];
s1[j]=ch;
}
}
}
for(i=0;s2[i];i++)
{
for(j=i+1;s2[j];j++)
{
if(s2[i]>s2[j]){
ch = s2[i];
s2[i]=s2[j];
s2[j]=ch;
}
}
}
printf((strcmp(s1,s2)==0)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s1=input()
s2=input()
print('true' if sorted(s1)==sorted(s2) else 'false')
LBP60
Given a string, implement a program to find max occurring character in the given
string
logic:
welcome ====> e
java =======> a
a[256]={0}
for(i=0;s[i];i++){
a[s[i]]++;
}
a[97]=2
a[j]=1
a[v]=1
max element is 2
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,a[256]={0},max;
scanf("%s",s);
for(i=0;s[i];i++)
a[(int)s[i]]++;
max=0;
for(i=0;i<256;i++)
{
if(a[i]>a[max])
max=i;
}
printf("%c",max);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation
---------------------
import collections
s=input()
r=collections.Counter(s)
#print(r)
print(max(r,key=r.get))
LBP61
You are given coordinates, a string that represents the coordinates of a square of
the chess board.
bellow is the chess board for your reference.
Return True if the saquare is in white, and false if the square is in Black.
8x8=64 cells
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[10];
int x,y;
scanf("%s",s);
x=s[0]-96;
y=s[1];
if(x%2!=y%2)
printf("true");
else
printf("false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
x=ord(s[0])-96
y=ord(s[1])
print("true" if x%2!=y%2 else "false")
1, a=97
2, b=98
LBP62
Write a function that finds the word "bomb" in the given string (not case
sensitive)
return "DUCK!" if found, else return "Relax there's no bomb."
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s1[100],s2[]="bomb";
scanf("%[^\n]s",s1);//lower case or upper case ===> lowercase
int i,j,k,len,found=0;
for(i=0;s1[i];i++)
{
if(s1[i]>='A' && s1[i]<='Z')
s1[i]=s1[i]+32;
}
len=strlen(s2);
for(i=0;s1[i];i++)
{
k=i;
for(j=0;j<len;j++)
{
if(s1[k]!=s2[j])
break;
k++;
}
if(j==len)
found=1;
}
printf((found==1)?"DUCK!":"Relax, there's no bomb.");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input().lower()
print("DUCK!" if "bomb" in s else "Relax, there's no bomb.")
LBP63
Create a function that takes a string and returns the number of vowels contained
within it.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
int c=0,i;
for(i=0;s[i];i++)
{
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u')
c++;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
c=0
for i in s:
if i in "aeiouAEIOU":
c=c+1
print(c)
LBP64
Rules:
logic:
-----
xc = count number of x's in s
oc = count number of o's in s
if xc==oc then print true else false
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
int i,xc=0,oc=0;
for(i=0;s[i];i++)
{
if(s[i]=='x') xc++;
if(s[i]=='o') oc++;
}
printf((xc==oc)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
xc=s.count('x')
oc=s.count('o')
print('true' if xc==oc else 'false')
LBP65
Stuttering Function
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
printf("%c%c...%c%c...%s?",s[0],s[1],s[0],s[1],s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
print(f"{s[0]}{s[1]}...{s[0]}{s[1]}...{s}?")
LBP66
Repeating Letters
Create a method that takes a string and returns a string in which each character is
repeated once.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i;
scanf("%s",s);
for(i=0;s[i];i++)
{
printf("%c%c",s[i],s[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
for i in s:
print(i*2,end='')
LBP67
Double Letters
Create a function that takes a word and returns true if the word has two
consecutive identical letters.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,found;
scanf("%s",s);
found=0;
for(i=0;s[i];i++)
{
if(s[i]==s[i+1])
{
found=1;
break;
}
}
printf((found==1)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
found=False
for i in range(len(s)-1):
if s[i]==s[i+1]:
found=True
break
print(str(found).lower())
doubt:
------
for(i=0;i<s.length()-1;i++)
{
if(s.charAt(i)==s.charAt(i+1))
{
found=true;
break;
}
}
abc
0=>a
1=>b
2=>c
LBP68
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,ac=0,bc=0,cc=0;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]=='A') ac++;
if(s[i]=='B') bc++;
if(s[i]=='C') cc++;
}
printf("%d %d %d",ac,bc,cc);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
print(s.count('A'),s.count('B'),s.count('C'))
LBP69
Create a function that takes a string and returns a new string with all vowels
removed.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
continue;
else
printf("%c",s[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import re
print(re.sub("[aeiou]","",input()))
LBP70
Create a function that takes a string and returns a string with spaces in between
all of the characters.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i;
scanf("%s",s);
for(i=0;s[i];i++)
{
printf("%c ",s[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
for i in s:
print(i,end=' ')
LBP71
VOWEL REPLACER
Create a function that replaces all the vowels in a string with a specified
character,
LOGIC:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100],ch;
int i;
scanf("%s",s);
scanf("\n%c",&ch);
for(i=0;s[i];i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
printf("%c",ch);
else
printf("%c",s[i]);
}
return 0;
}
welcome
a
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import re
s1=input()
s2=input()
print(re.sub("[aeiou]",s2,s1))
LBP72
Write a function that takes a string name and number num (either 1 or 0) and
return "Hello"+name if number is 1, otherwise "Bye"+name.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int n;
scanf("%s",s);
scanf("%d",&n);
if(n==1)
printf("Hello %s",s);
else
printf("Bye %s",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
n=int(input())
if n==1:
print("Hello",s)
else:
print("Bye",s)
LBP73
logic:
count=0
for i=0;s[i];i++
{
if s[i]>='0' and s[i]<='9'
count++;
}
if c==5 then print true else false
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,c=0;
scanf("%[^\n]s",s);
for(i=0;s[i];i++)
{
if(s[i]>='0' && s[i]<='9')
c++;
}
printf((c==5)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
c=0
for i in s:
if i.isdigit():
c=c+1
print("true" if c==5 else "false")
LBP74
create a function that takes a string and returns, the middle character(s).
if the word's length is odd return the midlle character.
if the word's length is even, return the middle two characters.
logic:
abc ----> b
abcd ---> bc
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int n;
scanf("%s",s);
n=strlen(s);
if(n%2==0)
printf("%c%c",s[n/2-1],s[n/2]);
else
printf("%c",s[n/2]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
n=len(s)
if n%2==0:
print(s[n//2-1],s[n//2],sep='')
else:
print(s[n//2])
LBP75
logic:
for(i=0;s[i];i++)
{
if s[i] is aeiou
then print i
break
}
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
for(int i=0;s[i];i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
{
printf("%d",i);
break;
}
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
for i in range(0,len(s)):
if s[i] in "aeiou":
print(i)
break
LBP76
Longest Word
int main() {
char s[100],*p,res[100]="\0";
int m;
scanf("%[^\n]s",s);
p=strtok(s," ");
m=0;
while(p!=NULL)
{
if(strlen(p)>m)
{
m=strlen(p);
strcpy(res,p);
}
p=strtok(NULL," ");
}
printf("%s",res);
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
---------------------
L=input().split()
m=0
s=""
for i in L:
if len(i)>m:
m=len(i)
s=i
print(s)
LBP77
Given a string str, the task is to print all the permutations of str. A permutation
is an arrangement of all or part of a set of objects, with regard to the order of
the arrangement. For instance, the words �bat� and �tab� represents two distinct
permutation (or arrangements) of a similar three letter word.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void swap(char *x,char *y){
char t;
t=*x;
*x=*y;
*y=t;
}
void permute(char *p,int left,int right){
int i;
if(left==right)
printf("%s ",p);
else{
for(i=left;i<=right;i++){
swap((p+left),(p+i));
permute(p,left+1,right);
swap((p+left),(p+i));
}
}
}
int main() {
char s[100];
scanf("%s",s);
permute(s,0,strlen(s)-1);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
public class Solution {
public static void permute(String s,int left,int right){
if(left==right)
System.out.print(s+" ");
else{
for(int i=left;i<=right;i++){
s = swap(s,left,i);
permute(s,left+1,right);
s = swap(s,left,i);
}
}
}
static String swap(String a,int i,int j){
char temp;
char[] charArray=a.toCharArray();
temp = charArray[i];
charArray[i]=charArray[j];
charArray[j]=temp;
return String.valueOf(charArray);
}
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
String s = obj.nextLine();
permute(s,0,s.length()-1);
}
}
python implementation:
----------------------
from itertools import permutations
l=list(permutations(input()))
for i in l:
print(''.join(i),end=' ')
LBP78
Given a string S, the task is to remove all the duplicates in the given string.
Ex:
abc------> abc
aba -----> ab
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,j,k;
scanf("%s",s);
for(i=0;s[i];i++)
{
for(j=i+1;s[j];j++)
{
if(s[i]==s[j])
{
for(k=j;s[k];k++)
s[k]=s[k+1];
}
}
}
printf("%s",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
l=[]
for i in s:
if i not in l:
l.append(i)
print(''.join(l))
LBP79
Write a Java program to take an input string and exchange the first and last word
and reverse the middle word.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100],fw[100]="\0",lw[100]="\0";
int i,j,k;
scanf("%[^\n]s",s);
for(i=0;s[i];i++)
{
k=0;
while(s[i]!=' ')
{
fw[k++]=s[i++];
}
break;
}
for(j=strlen(s)-1;s[j];j--)
{
k=0;
while(s[j]!=' ')
{
lw[k++]=s[j--];
}
break;
}
for(k=strlen(lw)-1;k>=0;k--)
printf("%c",lw[k]);
printf(" ");
for(k=j-1;k>=i;k--)
printf("%c",s[k]);
printf("%s",fw);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
LBP80
#XXXXXX ----> 7
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
int i=0,c=0;
for(i=0;s[i];i++)
{
if(s[i]>='a' && s[i]<='z')
s[i]=s[i]-32;
}
if(s[0]='#' && strlen(s)==7)
{
for(i=1;s[i];i++)
{
if((s[i]>='A' && s[i]<='F')||(s[i]>='0' && s[i]<='9'))
c++;
}
}
printf((c==6)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
public class Solution {
python implementation:
----------------------
import re
print('true' if re.fullmatch("#[A-Fa-f0-9]{6}",input())!=None else 'false')
LBP81
logic:
hello ---> 5
world ---> 5
java ----> 4
output: java
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100],*p;
scanf("%[^\n]s",s);
p=strtok(s," ");
while(p!=NULL)
{
if(strlen(p)%2==0)
printf("%s ",p);
p=strtok(NULL," ");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
l=s.split()
for i in l:
if len(i)%2==0:
print(i,end=' ')
LBP82
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i;
scanf("%s",s);
for(i=0;s[i];i++)
{
printf("%c",s[i]+1);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
for i in s:
print(chr(ord(i)+1),end='')
LBP83
First N Vowels
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100],ns[100]="\0";
int i,j,n;
scanf("%[^\n]s",s);
scanf("%d",&n);
j=0;
for(i=0;s[i];i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
ns[j++]=s[i];
}
if(n<strlen(ns))
{
for(i=0;i<n;i++)
printf("%c",ns[i]);
}
else
printf("invalid");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import re
s=input()
n=int(input())
ns=re.sub("[^aeiou]","",s)
print("invalid" if n>len(ns) else ns[0:n])
LBP84
Create a function that takes a string and returns true or false, depending on
whether the characters are in order or not.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s1[100],s2[100]="\0";
int i,j;
scanf("%s",s1);
strcpy(s2,s1);
for(i=0;s1[i];i++)
{
for(j=i+1;s1[j];j++)
{
if(s1[i]>s1[j])
{
char ch;
ch = s1[i];
s1[i]=s1[j];
s1[j]=ch;
}
}
}
printf(strcmp(s1,s2)==0?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s1=input()
l=list(s1)
l.sort()
s2=''.join(l)
print('true' if s1==s2 else 'false')
LBP85
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char
*belowten[]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}
;
char
*tensplace[]={"","Ten","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty
","Ninety"};
char
*belowtwenty[]={"","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen
","Seventeen","Eighteen","Nineteen"};
char s[10];
scanf("%s",s);
int num,len;
len=strlen(s);
if(len==1){
num=s[0]-48;
printf("%s ",belowten[num]);
}
else if(len==2 && s[1]==48){ //10,20,30,40,50,60,70,80,90
num=s[0]-48;
printf("%s ",tensplace[num]);
}
else if(len==2 && s[0]==49){ //10,11,12,13,14,15,16,17,18,19
num=s[1]+1-48;
printf("%s ",belowtwenty[num]);
}
else if(len==2){ //21-99
num = s[0]-48;
printf("%s ",tensplace[num]);
num=s[1]-48;
printf("%s ",belowten[num]);
}
else if(len==3){
num=s[0]-48;
printf("%s ",belowten[num]);
printf("Hundred ");
if(s[1]==48){//101,102,203
num=s[2]-48;
printf("%s ",belowten[num]);
}
else if(s[1]==49){//112,219,..
num=s[2]+1-48;
printf("%s ",belowtwenty[num]);
}
else if(s[2]==48){//x10, x50, x90
num=s[1]-48;
printf("%s ",tensplace[num]);
}
else{ //856
num=s[1]-48;
printf("%s ",tensplace[num]);
num=s[2]-48;
printf("%s ",belowten[num]);
}
}
else if(len==4){
num=s[0]-48;
printf("%s ",belowten[num]);
printf("Thousand ");
if(s[1]-48!=0){
num=s[1]-48;
printf("%s ",belowten[num]);
printf("Hundred ");
}
if(s[2]==48){
num=s[3]-48;
if(num!=0)
printf("%s ",belowten[num]);
}
else
{
if(s[3]==48){
num=s[2]-48;
printf("%s ",tensplace[num]);
}
else if(s[2]==49){
num=s[3]-48+1;
printf("%s ",belowtwenty[num]);
}
else{
num=s[2]-48;
printf("%s ",tensplace[num]);
num=s[3]-48;
printf("%s ",belowten[num]);
}
}
}
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
---------------------
d1=["","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","","Eleven","
Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"
]
d2=["","","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"]
def check(n,string):
if n==0:
return ''
elif n>=19:
s=d2[n//10]+' '+d1[n%10]+string
return s
else:
return d1[n]+' '+string
n=int(input())
if n==0:
print('zero')
else:
result=check((n//1000)%100,'Thousand ')
result+=check((n//100)%10,'Hundred ')
result+=check(n%100,'')
print(result)
LBP86
C*ns*r*d Str*ngs
Someone has attempted to censor my strings by replacing every vowel with a *, l*k*
th*s.
Luckily, I've been able to find the vowels that were removed.
Given a censored string and a string of the censored vowels, return the original
uncensored string.
logic:
read s1 and s2
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s1[100],s2[100];
int i,j;
scanf("%[^\n]s",s1);
scanf("%s",s2);
j=0;
for(i=0;s1[i];i++)
{
if(s1[i]=='*')
printf("%c",s2[j++]);
else
printf("%c",s1[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s1=input()
s2=input()
j=0
for i in s1:
if i=='*':
print(s2[j],end='')
j=j+1
else:
print(i,end='')
LBP87
parentheses balance
Given a string S of '(' and ')' parentheses, we add the minimum number of
parentheses ( '(' or ')', and in any positions ) so that the resulting parentheses
string is valid.
Formally, a parentheses string is valid if and only if:
It is the empty string, or It can be written as AB (A concatenated with B), where A
and B are
valid strings, or It can be written as (A), where A is a valid string.
Given a parentheses
string, return the minimum number of parentheses we must add to make the resulting
string valid.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,c=0;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]=='(')
c++;
if(s[i]==')')
c--;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
c=0
for i in s:
if i=='(':
c=c+1
if i==')':
c=c-1
print(c)
LBP88
American keyboard
Given a string, return the true if that can be typed using letters of alphabet on
only
one row's of American keyboard like the image below.
In the American keyboard:
Note:
1. You may use one character in the keyboard more than once.
2. You may assume the input string will only contain letters of alphabet.
logic:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100],r1[]="qwertyuiop",r2[]="asdfghjkl",r3[]="zxcvbnm";
int c1=0,c2=0,c3=0,i,j;
scanf("%s",s);
for(i=0;s[i];i++)
{
for(j=0;r1[j];j++)
{
if(s[i]==r1[j]) c1++;
}
for(j=0;r2[j];j++)
{
if(s[i]==r2[j]) c2++;
}
for(j=0;r3[j];j++)
{
if(s[i]==r3[j]) c3++;
}
}
if(c1==strlen(s) || c2==strlen(s) || c3==strlen(s))
printf("true");
else
printf("false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implmenetation:
---------------------
s=input()
c1,c2,c3=0,0,0
for i in s:
if i in "qwertyuiop":
c1=c1+1
if i in "asdfghjkl":
c2=c2+1
if i in "zxcvbnm":
c3=c3+1
print(str(c1==len(s) or c2==len(s) or c3==len(s)).lower())
LBP89
Rotate String
Given two strings s and goal, return true if and only if s can become goal after
some number of shifts on s.
abcde
bcdea
cdeab
deabc
eabcd
abcdeabcde
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s1[100],s2[100],s3[100]="\0";
int i,j,c;
scanf("%s",s1);//xyz
scanf("%s",s2);//yzx
//s3=xyzxyz
strcat(s3,s1);
strcat(s3,s1);
j=0;
c=0;
for(i=0;s3[i];i++)//xyzxyz
{
if(s3[i]==s2[j])//y,y--z,z--x,x ---> c=3
{
c++;
j++;
}
}
printf((c==strlen(s2))?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s1=input()
s2=input()
print('true' if s2 in s1+s1 else 'false')
LBP90
Missing Letters
logic:
abde ---> c
a[97]=1
a[98]=1
a[99]=0
a[100]=1
a[101]=1 and so on
for(i=97;i<=122;i++)
{
if(a[i]==0)
printf("%c",i);
}
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,a[256]={0};
scanf("%s",s);
for(i=0;s[i];i++)
{
a[(int)s[i]]++;
}
for(i=97;i<=122;i++)
{
if(a[i]==0)
printf("%c",i);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
for i in range(97,123):
n=chr(i)
if n not in s:
print(n,end='')
LBP91
Create a function that takes a string and replaces each letter with its appropriate
position in the alphabet.
"a" is 1, "b" is 2, "c" is 3, etc, etc.
a-96=97-96=1
b-96=98-96=2
c-96=99-96=3 and so on
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i;
scanf("%[^\n]s",s);
for(i=0;s[i];i++)
{
if(s[i]>='A' && s[i]<='Z')
s[i]=s[i]+32;
}
for(i=0;s[i];i++)
{
if(s[i]>='a' && s[i]<='z')
printf("%d ",s[i]-96);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input().lower()
for i in s:
if i.isalpha():
print(ord(i)-96,end=' ')
LBP92
"abcabab",'a'
output: 1bc2b3b
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100],ch;
int i,c=0;
scanf("%s",s);
scanf("\n%c",&ch);
for(i=0;s[i];i++)
{
if(s[i]==ch)
printf("%d",++c);
else
printf("%c",s[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
ch=input()
c=1
for i in s:
if i==ch:
print(c,end='')
c=c+1
else:
print(i,end='')
LBP93
int main() {
char s[100];
int i,j,u;
scanf("%s",s);
for(i=0;s[i];i++)
{
u=1;
for(j=0;s[j];j++)
{
if(i!=j && s[i]==s[j]){
u=0;
break;
}
}
if(u==1)
{
printf("%c",s[i]);
break;
}
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
for i in s:
if s.count(i)==1:
print(i)
break
a[0]='n'
a[1]='d'
a[2]='a'
c=3
a[c-1] --->
LBP94
Pangrams
logic:
a[26]={0}
a[s[i]-97]++;
abc
a[97-97]=a[0]=1
a[98-97]=a[1]=1
a[99-97]=a[2]=1
a[100-97]=a[3]=0
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int flag=1,i,a[26]={0};
scanf("%[^\n]s",s);
for(i=0;s[i];i++)
{
a[s[i]-97]++;
}
for(i=0;i<26;i++)
{
if(a[i]==0)
{
flag=0;
break;
}
}
printf((flag==1)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
ss="abcdefghijklmnopqrstuvwxyz"
flag=True
for i in ss:
if i not in s:
flag=False
break
print("Yes" if flag else "No")
LBP95
Implement a function/Method to return first character in each word from the given
input string.
input-----> a string
con-------> no
output ---> first character in each string
logic: tokenization
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100],*p;
scanf("%[^\n]s",s);
p=strtok(s," ");
while(p!=NULL){
printf("%c",p[0]);
p=strtok(NULL," ");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
print(''.join([i[0] for i in input().split()]))
LBP96
Number of vowels
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,c=0;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
c++;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
c=0
for i in s:
if i in "aeiou":
c=c+1
print(c)
LBP97
Number of consonants
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,c=0;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
continue;
else
c++;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
c=0
for i in s:
if i not in "aeiou":
c=c+1
print(c)
LBP98
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,c=0;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]>='0' && s[i]<='9')
c++;
}
printf((c==strlen(s))?"Yes":"No");
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
print("Yes" if input().isdigit() else "No")
LBP99
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100],*p;
scanf("%[^\n]s",s);
p=strtok(s," ");
while(p!=NULL){
printf("%c%s ",p[0]-32,p+1);
p=strtok(NULL," ");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
}
}
python implementation:
----------------------
print(input().title())
LBP100
Student Rewarded
You need to return whether the student could be rewarded according to his
attendance record.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,ac=0,lc=0;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]=='A') ac++;
if(s[i]=='L' && s[i+1]=='L' && s[i+2]=='L') lc++;
}
printf((lc==1||ac>1)?"No":"Yes");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
print("No" if s.count('A')>1 or s.count("LLL")!=0 else "Yes")
arrays:
-------
=> 1 or 2 or 3 values ----> variables
=> huge values -----------> arrays
=> collection of similar type of data elements.
=> index concept.
=> 0 to max_size-1
int a[3];
logic:
------
read n value (size)
declare an array
C---------> int a[100];
C++ ------> int a[100];
Java -----> int a[] = new int[n];
Py -------> we dn't have array concept ---> list (growable)
for(i=0;i<n;i++)
read a[i]
for(i=0;i<n;i++)
print a[i]
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
for i in L:
print(i,end=' ')
LBP102
Implement a program to read an array elements and print sum of all its elements.
logic:
sum=0;
for(i=0;i<n;i++){
sum=sum+a[i];
}
print sum;
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sum=0;
for(i=0;i<n;i++){
sum=sum+a[i];
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
print(sum(L))
LBP103
Implement a program to read an array elements and print sum of all even elements.
logic:
sum=0;
for(i=0;i<n;i++){
if(a[i]%2==0)
sum=sum+a[i];
}
print sum;
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sum=0;
for(i=0;i<n;i++){
if(a[i]%2==0)
sum=sum+a[i];
}
printf("%d",sum);
return 0;
}
java implementation:
---------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split() if int(i)%2==0]
print(sum(L))
LBP104
Implement a program to read an array elements and print sum of all odd elements.
logic:
sum=0;
for(i=0;i<n;i++){
if(a[i]%2!=0)
sum=sum+a[i];
}
print sum;
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sum=0;
for(i=0;i<n;i++){
if(a[i]%2!=0)
sum=sum+a[i];
}
printf("%d",sum);
return 0;
}
java implementation:
---------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split() if int(i)%2!=0]
print(sum(L))
LBP105
Implement a program to read an array elements and print sum of all prime elements.
logic:
sum=0;
for(i=0;i<n;i++){
if(isprime(a[i]))
sum=sum+a[i];
}
print sum;
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isprime(int n){
int factors=0,i;
for(i=1;i<=n;i++){
if(n%i==0)
factors++;
}
return factors==2;
}
int main() {
int n,a[100],i,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sum=0;
for(i=0;i<n;i++){
if(isprime(a[i]))
sum=sum+a[i];
}
printf("%d",sum);
return 0;
}
java implementation:
---------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def isprime(n):
factors=0
for i in range(1,n+1):
if n%i==0:
factors=factors+1
return factors==2
n=int(input())
L=[int(i) for i in input().split() if isprime(int(i))]
print(sum(L))
LBP106
Implement a program to read an array elements and print sum of all palindrome
numbers in array.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int rev(int n){
int d,r=0;
while(n!=0){
d=n%10;
r=r*10+d;
n=n/10;
}
return r;
}
int main() {
int n,a[100],i,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sum=0;
for(i=0;i<n;i++){
if(a[i]==rev(a[i]))
sum=sum+a[i];
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split() if i==i[::-1]]
print(sum(L))
LBP107
Implement a program to read an array elements and print sum of all strong numbers
in array.
123 = 1! + 2! + 3! = 1+2+6 = 9
145 = 1! + 4! + 5! = 1+24+120 = 145 Yes
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int strong(int n){
int i,d,s=0,f;
while(n!=0){
d=n%10;
for(i=1,f=1;i<=d;i++)
f=f*i;
s=s+f;
n=n/10;
}
return s;
}
int main() {
int n,a[100],i,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sum=0;
for(i=0;i<n;i++){
if(a[i]==strong(a[i]))
sum=sum+a[i];
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import math
def strong(n):
s=0
while n!=0:
d=n%10
s=s+math.factorial(d)
n=n//10
return s
n=int(input())
L=[int(i) for i in input().split() if int(i)==strong(int(i))]
print(sum(L))
LBP108
Implement a program to read an array elements and print sum of elements ending with
3 in array.
a[i]%10==3
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sum=0;
for(i=0;i<n;i++){
if(a[i]%10==3)
sum=sum+a[i];
}
printf("%d",sum);
return 0;
}
java implementation:
---------------------
import java.io.*;
import java.util.*;
LBP109
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,a[100],key,index=-1;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&key);
for(i=0;i<n;i++){
if(key==a[i]){
index=i;
break;
}
}
printf("%d",index);
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
key=int(input())
if key in L:
print(L.index(key))
else:
print(-1)
LBP110
C Implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
for i in L:
print(i,end=' ')
LBP111
C Implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]<a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort(reverse=True)
for i in L:
print(i,end=' ')
LBP112
binary search
4,5
index=-1
low = 0;
high = n-1;
while(low<=high){
mid=(low+high)/2;
if(key==a[mid]){
index=mid;
break;
}
else if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
printf("%d",index);
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,j,t,key,low,high,mid,index=-1;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++)
{
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high){
mid=(low+high)/2;
if(key==a[mid]){
index=mid;
break;
}
else if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
printf("%d",index);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
key=int(input())
L.sort()
if key in L:
print(L.index(key))
else:
print(-1)
LBP113
Implement a program to read array elements and find the max element in an array.
C Implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("%d",a[n-1]);
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
print(L[n-1])
LBP114
Implement a program to read array elements and find the min element in an array.
C Implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("%d",a[1-1]);
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
print(L[1-1])
LBP115
Implement a program to read array elements and find the difference between max and
min element in an array.
C Implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("%d",a[n-1]-a[1-1]);
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
print(L[n-1]-L[1-1])
LBP116
Implement a program to read array elements and find the second max element in an
array.
sorted the array===> a[0], a[1], a[2], ..... a[n-3], a[n-2], a[n-1]
C Implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("%d",a[n-2]);
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
print(L[n-2])
LBP117
Implement a program to read array elements and find the second min element in an
array.
C Implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("%d",a[2-1]);
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
print(L[2-1])
LBP118
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,key,count=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(key==a[i])
{
count++;
}
}
printf("%d",count);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
-----------------------
n=int(input())
L=[int(i) for i in input().split()]
key=int(input())
print(L.count(key))
C ===> flexiable
Java=> fixed in size
py ==> flexiable
Only the problem is with java implementation ====> Collections Frame Works
LBP119
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,e,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&e);
for(i=n;i>=0;i--)
{
a[i]=a[i-1];
}
a[0]=e;
n++;
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Core Java
MySQL
HTML
CSS
JS
JQuery
Jdbc
Servlets
Jsp
2 or 3 Mini Projects
Rs. 12000/-
java implementation:
--------------------
import java.io.*;
import java.util.*;
Ex:
import java.util.*;
class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
LinkedList<Integer> ll=new LinkedList<Integer>();
System.out.println(ll);//[]
ll.add(111);
ll.add(222);
ll.add(333);
ll.addFirst(999);
ll.addLast(888);
ll.add(3,new Integer(777));
//ll.add("Prakash");
System.out.println(ll);//[999,111,222,777,333,888,"Prakash"]
}
}
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.insert(0,int(input()))
for i in L:
print(i,end=' ')
LBP120
inserting element at last position in an array
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,e,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&e);
a[n]=e;
n++;
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.append(int(input()))
for i in L:
print(i,end=' ')
LBP121
delete an element from first location in an array
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
a[i]=a[i+1];
n--;
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.pop(0)
for i in L:
print(i,end=' ')
LBP122
delete an element from last location in an array
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
n--;
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.pop()
for i in L:
print(i,end=' ')
LBP123
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,loc;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&loc);
for(i=loc;i<n;i++)
a[i]=a[i+1];
n--;
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.pop(int(input()))
for i in L:
print(i,end=' ')
LBP124
delete an element from an array
c implementation:
------------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,e,status=-1;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&e);
for(i=0;i<n;i++){
if(e==a[i]){
status=i;
break;
}
}
for(i=status;i<n;i++)
a[i]=a[i+1];
n--;
if(status!=-1){
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
else
printf("-1");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
e=int(input())
if e in L:
L.remove(e)
for i in L:
print(i,end=' ')
else:
print(-1)
LBP125
input ------> size,array elements and element to be updated (old element & new
element)
con---------> size<100
output -----> return array after updating
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],i,oe,ne,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&oe);
scanf("%d",&ne);
for(i=0;i<n;i++)
{
if(a[i]==oe)
a[i]=ne;
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
oe=int(input())
ne=int(input())
for i in range(0,n):
if L[i]==oe:
L[i]=ne
for i in L:
print(i,end=' ')
LBP126
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],i,loc,ne,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&loc);
scanf("%d",&ne);
a[loc]=ne;
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
loc=int(input())
ne=int(input())
L[loc]=ne
for i in L:
print(i,end=' ')
obj.add(object);
obj.add(int,object);
obj.remove(int);
LBP127
array reverse
for(i=0;i<n;i++)
print a[i]
for(i=n-1;i>=0;i--)
print a[i]
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-1;i>=0;i--)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
for i in L[::-1]:
print(i,end=' ')
n=5 ===> 0, 1, 2, 3, 4
LBP128
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d ",a[i]+1);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
for i in L:
print(i+1,end=' ')
LBP129
Implement a program to find the number of duplicate elements present in the given
array.
int b[999]={0};
for(i=0;i<n;i++){
b[a[i]]++;
}
if b[i]>=2 then count++
5
1 1 2 2 3
a[0]=1
a[1]=1
a[2]=2
a[3]=2
a[4]=3
b[1]=2
b[2]=2
b[3]=1
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,b[999]={0},i,c=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
b[a[i]]++;
}
for(i=0;i<999;i++)
{
if(b[i]>=2)
c++;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
1st version:
n=int(input())
L=[int(i) for i in input().split()]
d={}
c=0
for i in L:
d[i]=d.get(i,0)+1
#print(d)
for i in d.values():
if i>=2:
c=c+1
print(c)
LBP130
Implement a program to find the unique elements present in the given array.
5
1 1 2 3 4 ===> 1 2 3 4
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
break;
}
if(j==n)
printf("%d ",a[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
LL=[]
for i in L:
if i not in LL:
LL.append(i)
for i in LL:
print(i,end=' ')
LBP131
Implement a program to read an array and sort array elements with 0s, 1s and 2s.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],i,n,j,t;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
for i in L:
print(i,end=' ')
LBP132
replace every element with the greatest element on its right side
Implement a program to read an array and replace every element with the greatest
element on its right side.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100];
int i,j;
int n;
int max;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
max=a[i];
for(j=i+1;j<n;j++)
{
if(max<a[j])
max=a[j];
}
a[i]=max;
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
for i in range(n):
L[i]=max(L[i:])
for i in L:
print(i,end=' ')
LBP133
Implement a program to find the sum of two arrays and display the result array
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
int a[100],b[100];
int i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
printf("%d ",a[i]+b[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L1=[int(i) for i in input().split()]
L2=[int(i) for i in input().split()]
for i in range(n):
print(L1[i]+L2[i],end=' ')
LBP134
Implement a program to find the sum of elements avaiable at even index locations in
an array.
sum=0;
for(i=0;i<n;i++)
{
if(i%2==0)
sum=sum+a[i];
}
print sum
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
int a[100];
int i;
int sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(i%2==0)
sum=sum+a[i];
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
sum=0
L=[int(i) for i in input().split()]
for i in range(n):
if i%2==0:
sum=sum+L[i]
print(sum)
LBP135
Implement a program to find the sum of elements avaiable at odd index locations in
an array.
sum=0;
for(i=0;i<n;i++)
{
if(i%2!=0)
sum=sum+a[i];
}
print sum
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
int a[100];
int i;
int sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(i%2!=0)
sum=sum+a[i];
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
sum=0
L=[int(i) for i in input().split()]
for i in range(n):
if i%2!=0:
sum=sum+L[i]
print(sum)
LBP136
Implement a program to find the sum of first and last, second and second last and
so on in an array.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100];
int n,i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
i=0;
j=n-1;
while(i<=j){
printf("%d ",a[i]+a[j]);
i++;
j--;
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
i=0
j=n-1
while i<=j:
print(L[i]+L[j],end=' ')
i=i+1
j=j-1
1 2 10 12 11
12 14 20
LBP137
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int rev(int n)
{
int r=0;
int d;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return r;
}
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d ",rev(a[i]));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[i for i in input().split()]
for i in L:
print(i[::-1],end=' ')
LBP138
Implement a program to find number of even and odd elements in the given array
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int ec=0;
int oc=0;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
ec=ec+1;
else
oc=oc+1;
}
printf("%d\n%d",ec,oc);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
ec=0
oc=0
for i in L:
if i%2==0:
ec=ec+1
else:
oc=oc+1
print(ec)
print(oc)
1. Core Java
2. UI Technologies: HTML
3. UI Technologies: CSS
4. UI Technologies: JS
5. UI Technologies: JQuery
6. Database Tool: MySQL
7. Advanced Java: JDBC
8. Advanced Java: Servlets
9. Advanced Java: JSP
10. Mini Projects
https://us02web.zoom.us/meeting/register/tZ0qf-GgqTsjEtWO_shmAUP3aez8iDgENA3j
LBP139
Implement a program to sort only first half of the array and keep remaining
elements as original.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n/2;i++){
for(j=i+1;j<n/2;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
public class Solution {
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
for i in range(0,n//2):
for j in range(i+1,n//2):
if L[i]>L[j]:
L[i],L[j]=L[j],L[i]
for i in L:
print(i,end=' ')
LBP140
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
int a[100],b[100];
int i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
printf("%d ",a[i]-b[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L1=[int(i) for i in input().split()]
L2=[int(i) for i in input().split()]
for i in range(n):
print(L1[i]-L2[i],end=' ')
LBP141
rearrange an array in such an order that� smallest, largest, 2nd smallest, 2nd
largest and on
5
1 5 4 3 2 ---> 1 2 3 4 5 ---> 1 5 2 4 3 3
6
1 5 6 4 2 3 --> 1 2 3 4 5 6 --> 1 6 2 5 3 4
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
i=0;
j=n-1;
while(i<=j)
{
printf("%d %d ",a[i],a[j]);
i++;
j--;
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
i=0
j=n-1
while i<=j:
print(L[i],L[j],end=' ')
i=i+1
j=j-1
LBP142
Array of multiples
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int m,n,i;
scanf("%d",&m);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d ",m*i);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
m=int(input())
n=int(input())
for i in range(1,n+1):
print(m*i,end=' ')
LBP143
Write a function that, given the start startNum and end endNum values, return an
array containing all the numbers inclusive to that range.
Note:
The numbers in the array are sorted in ascending order.
If startNum is greater than endNum, return an array with the higher value.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,m,i;
scanf("%d %d",&n,&m);
if(n<=m){
for(i=n;i<=m;i++)
printf("%d ",i);
}
else
printf("%d",n);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
m=int(input())
if n<=m:
for i in range(n,m+1):
print(i,end=' ')
else:
print(n)
LBP144
A = 1
B = 2
C = 3
D = 4
E = 5
average = total sum of all numbers / number of item in the set
Return the result rounded to two decimal points.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,n,sum=0;
scanf("%s",s);
n=strlen(s);
for(i=0;s[i];i++)
sum=sum+(s[i]-96);
printf("%.2f",sum/(float)n);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
sum=0
for i in s:
sum=sum+ord(i)-96
print("%.2f"%(sum/len(s)))
LBP145
Create a function that takes an array of numbers and returns only the even values.
Note:
c implementation:
----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]%2==0)
printf("%d ",a[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
for i in L:
if i%2==0:
print(i,end=' ')
LBP146
print count
print sum
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,sum=0,count=0,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]>0)
count=count+1;
else
sum=sum+a[i];
}
if(n!=0)
printf("%d %d",count,sum);
else
printf(" ");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
sum=0
count=0
for i in L:
if i>0:
count=count+1
else:
sum=sum+i
if n!=0:
print(count,sum,sep=' ')
else:
print(' ')
LBP147
Create a function that takes an array of numbers and returns the sum of the two
lowest positive numbers.
input -------> size and an array
con ---------> Dn't count negative numbers
output ------> sum of two smallest positive numbers
logic:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
{
if(a[i]>=0)
{
printf("%d",a[i]+a[i+1]);
break;
}
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
for i in range(0,n):
if L[i]>=0:
print(L[i]+L[i+1])
break
LBP148
5
1 2 3 4 5
0 1 2 3 4
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],i,n,m;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
if(m<n)
{
for(i=n-m;i<n;i++)
printf("%d ",a[i]);
}
else
printf("0");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
m=int(input())
if m<n:
for i in range(n-m,n):
print(L[i],end=' ')
else:
print(0)
LBP149
Mini Peaks
Write a function that returns all the elements in an array that are strictly
greater than their adjacent left and right neighbors.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n-1;i++)
{
if(a[i]>a[i-1] && a[i]>a[i+1])
printf("%d ",a[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
for i in range(1,n-1):
if L[i]>L[i-1] and L[i]>L[i+1]:
print(L[i],end=' ')
LBP150
logic:
if all elements are prime count==n
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isprime(int n)
{
int f=0;
int i;
for(i=1;i<=n;i++)
{
if(n%i==0)
f++;
}
return f==2;
}
int main() {
int n,a[100],i,count=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(isprime(a[i]))
count++;
}
printf((count==n)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def isprime(n):
f=0
for i in range(1,n+1):
if n%i==0:
f=f+1
return f==2
n=int(input())
L=[int(i) for i in input().split()]
c=0
for i in L:
if isprime(i):
c=c+1
print('true' if c==n else 'false')
LBP151
logic:
5
10 11 7 12 14
10-11=-1
11-7=4
7-12=5
12-14=2
1+4+5+2=12
sum=0;
for(i=0;i<n-1;i++)
{
sum=sum+abs(a[i]-a[i+1]);
}
print sum
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sum=0;
for(i=0;i<n-1;i++)
{
sum=sum+abs(a[i]-a[i+1]);
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
sum=0
for i in range(0,n-1):
sum=sum+abs(L[i]-L[i+1])
print(sum)
LBP152
Odd Even Online Game
You are playing an online game. In the game, a list of N numbers is given.
The player has to arrange the numbers so that all the odd numbers of the list come
after even numbers. Write an algorithm to arrange
the given list such that all the odd numbers of the list come after the even
numbers.
logic:
array elements
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]%2==0)
printf("%d ",a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]%2!=0)
printf("%d ",a[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
for i in L:
if i%2==0:
print(i,end=' ')
for i in L:
if i%2!=0:
print(i,end=' ')
LBP153
input -----> the first line of i/p consists of an integer N, and A1,A2,...AN
representing areas of outlets.
output ----> print an integer representing the number of plots that will be
selected for outlets.
logic:
5
12 13 14 15 16 ====> 1
4
1 2 3 4 ====> 2
6
25 26 35 36 49 50 ===> 3
c implementation:
-----------------
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int a[100],i,n,c,j;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
c=0;
for(i=0;i<n;i++)
{
for(j=1;j<=a[i];j++)
{
if(j*j==a[i])
c++;
}
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
c=0
for i in L:
for j in range(1,i+1):
if j*j==i:
c=c+1
print(c)
LBP154
POOLED CAB SERVICE
A compnay wishes to provide can service for their N employees. The employees have
distance ranging from 0 to N-1. The company has calculated the total distance from
an employee's residence to the company, considering the path to be followed by the
cab is a straight path. The distance of the company from it self is 0. The distance
for the employees who live to the left side of the company is represented with a
negative sign. The distance for the employees who live to the right side of the
company is represented with a positive sign. the cab will be allotted a range of
distance. The company wishes to find the distance for the employees who live within
the particular distance range.
write a alogrithm to find the distance for the employees who live within the
distance range.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,d1,d2,flag=0;
scanf("%d",&n);
scanf("%d %d",&d1,&d2);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(abs(a[i])>=d1 && abs(a[i])<=d2)
printf("%d ",a[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
d1,d2=(int(i) for i in input().split())
L=[int(i) for i in input().split()]
for i in L:
if abs(i)>=d1 and abs(i)<=d2:
print(i,end=' ')
LBP155
A company wishes to modify the technique by which tasks in the processing queue are
executed. There are N processes with unique ID's from 0 to N-1. Each of these tasks
has its own execution time. The company wishes to implement a new algorithm for
processing tasks. for this purpose they have identified a value K by the new
algorithm, the processor will first process the task that has the Kth shortest
execution time.
logic:
C Implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t,k;
scanf("%d",&n);
scanf("%d",&k);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("%d",a[k-1]);
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
k=int(input())
L=[int(i) for i in input().split()]
L.sort()
print(L[k-1])
LBP156
INDEX FILTERNING
Create a function that takes two inputs: idx (an array of integers) and str (a
string).
The function should return another string with the letters of str at each index in
idx in order.
logic:
She is the love of my love
3
7 11 14
tle
for(i=0;i<n;i++)
{
printf("%c",s[a[i]]);//s[7] s[11] s[14]
}
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,n,a[100];
scanf("%[^\n]s",s);
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
printf("%c",s[a[i]]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
n=int(input())
L=[int(i) for i in input().split()]
for i in L:
print(s[i],end='')
LBP157
SEVEN BOOM!
Create a function that takes an array of numbers and return "Boom!" if the digit 7
appears in the array. Otherwise, return "there is no 7 in the array".
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int contains(int n)
{
int d;
while(n!=0)
{
d=n%10;
if(d==7)
{
return 1;
}
n=n/10;
}
return 0;
}
int main() {
int a[100],n,i,flag=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(contains(a[i]))
{
flag=1;
break;
}
}
printf((flag==1)?"Boom!":"there is no 7 in the array");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[i for i in input().split()]
flag=False
for i in L:
if '7' in i:
flag=True
break
print("Boom!" if flag else "there is no 7 in the array")
LBP158
logic:
------
flag=1 (true)
for(i=0;i<n-1;i++)
{
if(a[i]>0 && a[i+1]>0)
{
flag=0;
break;
}
if(a[i]<0 && a[i+1]<0)
{
flag=0;
break;
}
}
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i,a[100],flag=1,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
if(a[i]>0 && a[i+1]>0)
{
flag=0;
break;
}
if(a[i]<0 && a[i+1]<0)
{
flag=0;
break;
}
}
printf((flag==1)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
LBP159
Write a function that returns true if all parameters are truthy, and false
otherwise
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,flag=1;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]==0)
{
flag=0;
break;
}
}
printf((flag==1)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
flag=True
for i in L:
if i==0:
flag=False
break
print('true' if flag else 'false')
10 0 11 12 0
LBP160
Shared Digits
Create a function that returns true if each pair of adjacent numbers in an array
shares at least one digit and false otherwise.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int contains(int key,int n)
{
while(n!=0)
{
if(n%10==key)
{
return 1;
}
n=n/10;
}
return 0;
}
int main() {
int n,a[100],i,x,c;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
c=0;
for(i=0;i<n-1;i++)
{
x=a[i];
while(x!=0)
{
if(contains(x%10,a[i+1])==1){
c++;
break;
}
x=x/10;
}
}
printf((c==n-1)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
---------------------
n=int(input())
L=[int(i) for i in input().split()]
c=0
for i in range(n-1):
x=L[i]
while x!=0:
if str(x%10) in str(L[i+1]):
c=c+1
break
x=x//10
print('true' if c==n-1 else 'false')
LBP161
Write a function that returns true if two arrays, when combined, form a consecutive
sequence.
A consecutive sequence is a sequence without any gaps in the integers,
e.g. 1, 2, 3, 4, 5 is a consecutive sequence, but 1, 2, 4, 5 is not.
c implementation:
-----------------
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int n1,n2,i,j,t,count,a[100],b[100],c[100];
scanf("%d",&n1);
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
scanf("%d",&n2);
for(i=0;i<n2;i++)
scanf("%d",&b[i]);
j=0;
for(i=0;i<n1;i++)
c[j++]=a[i];
for(i=0;i<n2;i++)
c[j++]=b[i];
for(i=0;i<(n1+n2);i++)
{
for(j=i+1;j<(n1+n2);j++)
{
if(c[i]>c[j])
{
t=c[i];
c[i]=c[j];
c[j]=t;
}
}
}
count=0;
for(i=0;i<(n1+n2);i++)
{
if(c[i]+1==c[i+1])
count++;
}
printf((count==(n1+n2)-1)?"true":"false");
return 0;
}
Java implementation:
--------------------
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
python implementation:
----------------------
n1=int(input())
L1=[int(i) for i in input().split()]
n2=int(input())
L2=[int(i) for i in input().split()]
L3=L1+L2
L3.sort()
count=0
for i in range((n1+n2)-1):
if L3[i]+1==L3[i+1]:
count=count+1
print('true' if count==(n1+n2)-1 else 'false')
LBP162
c implementation:
-----------------
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int contains(int n)
{
int c=0;
while(n!=0)
{
if(n%10==5)
c++;
n=n/10;
}
return c;
}
int main() {
int n,a[100],i,c,cc,x,element;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
c=0;
cc=0;
for(i=0;i<n;i++)
{
x=contains(a[i]);
if(c<=x)
{
c=x;
element=a[i];
}
if(x==0)
cc++;
}
if(cc==n)
printf("%d",a[0]);
else
printf("%d",element);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int contains(int n)
{
int c=0;
while(n!=0)
{
if(n%10==5)
c++;
n=n/10;
}
return c;
}
public static void main(String args[] ) throws Exception {
Scanner obj = new Scanner(System.in);
int n,c,cc,x,element=0,i;
n=obj.nextInt();
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=obj.nextInt();
c=0;
cc=0;
for(i=0;i<n;i++)
{
x=contains(a[i]);
if(c<=x){
c=x;
element=a[i];
}
if(x==0)
cc++;
}
System.out.println((cc==n)?a[0]:element);
}
}
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
c=0
cc=0
for i in L:
x=str(i).count('5')
if c<=x:
c=x
element=i
if x==0:
cc=cc+1
if cc==n:
print(L[0])
else:
print(element)
LBP163
Write a function that accepts an array of numbers (where each number appears three
times except for one which appears only once) and finds that unique number in the
array and returns it.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
t=0;
for(j=0;j<n;j++)
{
if(i!=j && a[i]==a[j])
t++;
}
if(t==0)
printf("%d",a[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
public class Solution {
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
t=0
for i in L:
t=L.count(i)
if t==1:
print(i)
LBP164
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("%d ",a[1]);
for(i=1;i<n-1;i++)
printf("%d ",a[i-1]*a[i+1]);
printf("%d",a[n-2]);
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
print(L[1],end=' ')
for i in range(1,n-1):
print(L[i-1]*L[i+1],end=' ')
print(L[-2])
LBP165
Given an integer array and an integer N denoting the array length as input. your
task is to return the sum of third largest and second minimum elements of the
array.
logic:
a[n-3]+a[2-1]
C Implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("%d",a[n-3]+a[2-1]);
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
print(L[n-3]+L[2-1])
LBP166
Sales Report
N products each day. Write an algorithm to find the higest revenue received each
day.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,m,i,j,a[10][10],max;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
max=a[i][0];
for(j=0;j<m;j++)
{
if(max<a[i][j])
max=a[i][j];
}
printf("%d ",max);
}
return 0;
}
Java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n,m=(int(i) for i in input().split())
for i in range(n):
L=[int(i) for i in input().split()]
print(max(L),end=' ')
LBP167
Online Game
You are playing an online game. In the game, a numbers is displayed on the screen.
In order to win the game, you have to count the trailing zeros in the factorial
value
of the given number. W
rite an algorithm to count the trailing zeros in the factorial value of the given
number.
input ------> an integer num, representing the number displayed on the screen.
con---------> no
output -----> the count of trailing zeros in the factorial of the given number.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int count(int n)
{
int c=0;
while(n!=0)
{
if(n%10==0)
c++;
else
break;
n=n/10;
}
return c;
}
int main() {
int n,f,i;
scanf("%d",&n);
f=1;
for(i=1;i<=n;i++)
f=f*i;
printf("%d",count(f));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
public class Solution {
static int count(int n)
{
int c=0;
while(n!=0)
{
if(n%10==0)
c++;
else
break;
n=n/10;
}
return c;
}
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int n=obj.nextInt();
int f=1,i;
for(i=1;i<=n;i++)
f=f*i;
System.out.println(count(f));
}
}
python implementation
---------------------
import math
def count(n):
c=0
while n!=0:
if n%10==0:
c=c+1
else:
break
n=n//10
return c
n=int(input())
print(count(math.factorial(n)))
LBP168
Array pliandrome
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int flag=1,low=0,high=n-1;
while(low<=high)
{
if(a[low]!=a[high])
{
flag=0;
break;
}
low++;
high--;
}
printf((flag==1)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
print('true' if L==L[::-1] else 'false')
LBP169
Array to Matrix
1 2 3 4
1 2 3 4 5 6 7 8 9
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i,a[100],n,m,j,k=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
m=sqrt(n);
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",a[k++]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import math
n=int(input())
L=[int(i) for i in input().split()]
m=math.isqrt(n)
k=0
for i in range(m):
for j in range(m):
print(L[k],end=' ')
k=k+1
print()
LBP170
Matrix to Array
c implementaion:
----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,m,a[10][10],i,j;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",a[i][j]);
}
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n,m=(int(i) for i in input().split())
L=[]
for i in range(n):
L.append([int(i) for i in input().split()])
for i in range(n):
for j in range(m):
print(L[i][j],end=' ')
LBP171
Word Key
One programming language has the following keywords that cannot be used as
identifiers.
break,case,continue,default,defer,else,for,func,goto,if,map,range,return,struct,typ
e,var
C Implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char
s[100],*p,ss[]="break,case,continue,default,defer,else,for,func,goto,if,map,range,r
eturn,struct,type,var";
scanf("%s",s);
int flag=0;
p=strtok(ss,",");
while(p!=NULL)
{
if(strcmp(s,p)==0)
{
flag=1;
break;
}
p=strtok(NULL,",");
}
printf((flag==1)?"true":"false");
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
L=["break","case","continue","default","defer","else","for","func","goto","if","map
","range","return","struct","type","var"]
s=input()
print('true' if s in L else 'false')
LBP172
Oddly Even
Given a maximum of 100 digit numbers as input,
find the difference between the sum of odd and even position digits.
Ex:
45712
01234
4+7+2=13
5+1=6
13-6=7
45712==>21754
43210
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int rev(int n)
{
int d,r=0;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return r;
}
int main() {
int n,i,d,a[100],se,so;
scanf("%d",&n);
n=rev(n);
i=0;
while(n!=0)
{
d=n%10;
a[i++]=d;
n=n/10;
}
int len=i;
se=0;
so=0;
for(i=0;i<len;i++)
{
if(i%2==0)
se=se+a[i];
else
so=so+a[i];
}
printf("%d",so-se);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=input()
n=int(n[::-1])
L=[]
while n!=0:
L.append(n%10)
n=n//10
index=0
se=0
so=0
while index<len(L):
if index%2==0:
se=se+L[index]
else:
so=so+L[index]
index=index+1
print(so-se)
LBP173
Sweet Seventeen
bin---->dec
oct---->dec
hexa--->dec ----> 16 to 10
xxx --->dec ----> 17 to 10
ABC=3089
3089
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int dec=0,i,t=0,len;
scanf("%s",s);
len=strlen(s);
len--;
for(i=0;s[i];i++)
{
if(s[i]>='0' && s[i]<='9')
t=s[i]-48; //'0'=48
else if(s[i]>='A' && s[i]<='G')
t=s[i]-65+10;
else if(s[i]>='a' && s[i]<='g')
t=s[i]-97+10;
dec=dec+t*pow(17,len);
len--;
}
printf("%d",dec);
return 0;
}
Java Implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input(),17)
print(n)
LBP174
BeautifyMe
The cosmetic company "BeauityMe" wishes to know the alphabetic product code from
the product barcode. The barcode of the product is a numeric value and the
alphabeitc product is a string value tagged 'a-j'. The alphabetic range 'a-j'
represents the numeric range '0-9'. To produce the alphabetic product code. each
digit in the numeric barcode is replace by the corresponding matching letters.
Write an algorithm to display the alphabetic product code from the numeric
barcodes.
s[i]-97
97-97=0
98-97=1
99-97=2
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i;
scanf("%s",s);
for(i=0;s[i];i++)
{
printf("%d",s[i]-97);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
for i in s:
print(ord(i)-97,end='')
LBP175
Implement a program to read a number and print prime numbers upto n seperated by
commas.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isprime(int n)
{
int f=0,i;
for(i=1;i<=n;i++)
{
if(n%i==0)
f++;
}
return f==2;
}
int main() {
int i,n;
scanf("%d",&n);
for(i=2;i<=n;i++)
{
if(isprime(i))
printf("%d ",i);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def isprime(n):
f=0
for i in range(1,n+1):
if n%i==0:
f=f+1
return f==2
n=int(input())
for i in range(2,n+1):
if isprime(i):
print(i,end=' ')
LBP176
Implement a program to read two integers values and return GCD of two numbers.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n1,n2,n3,i;
scanf("%d %d",&n1,&n2);
for(i=1;i<=n1 && i<=n2;i++)
{
if(n1%i==0 && n2%i==0)
n3=i;
}
printf("%d",n3);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import math
n1,n2=(int(i) for i in input().split())
print(math.gcd(n1,n2))
LBP177
secret information
A spy wants to send some secret information to the government. As the data is very
important, he encrypts the message by encoding by adding some extra characters. the
original message has only upper case letters and numbers, while the extra
characters are madeup of lowercase letters and special characters. Can you help the
receiver in designing a program that accepts the message and returns the secret
information.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i;
scanf("%s",s);
for(i=0;s[i];i++)
{
if((s[i]>='A' && s[i]<='Z')||(s[i]>='0' && s[i]<='9'))
printf("%c",s[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
for i in s:
if (i>='A' and i<'Z') or i.isdigit():
print(i,end='')
LBP178
flight
amir is travelling to mumbai, but this time he is taking flight. His brother has
already told him about luggage weight limits but forgot it. Now he is taking with
him 3 trolly bags.
As per the current airlines which amir will fly. has below weight limits.
There can be at max 2 check-in and 1 cabin luggage. Check-in has total limit of L1
and Cabin has limit of L2.
Now, amir has 3 luggage has weights as W1 and W2 and W3 respectively. Now he should
be smart enough to make sure that he can travel with all the 3 luggage without
paying extra charge.
Find out whether amir can take all of his luggage without any extra charges or not.
If all good and no extra changes were paid, output "Yes" otherwise "No".
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int w1,w2,w3,l1,l2;
scanf("%d %d %d %d %d",&w1,&w2,&w3,&l1,&l2);
if(w1+w2+w3<=l1+l2)
printf("Yes");
else
printf("No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
w1,w2,w3,l1,l2=(int(i) for i in input().split())
print("Yes" if w1+w2+w3<=l1+l2 else "No")
LBP179
arrangement
given an array of size n, write a function to rearrange the numbers of the array in
such that even and odd numbers are arranged alternatively in increasing order.
n=5
a=4 1 3 5 2 ---> 1 2 3 4 5 --> 2,4 and 1,3,5 ---> 2,1,4,3,x,5
logic:
read array
sort the array
copy even elements into one array
copy odd elements into another array
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,j,t,b[100],c[100],k;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
j=0;
k=0;
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
b[j++]=a[i];//even
else
c[k++]=a[i]; //odd
}
i=0;
while(i<n/2)
{
printf("%d %d ",b[i],c[i]);
i++;
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
L1=[]
L2=[]
for i in L:
if i%2==0:
L1.append(i)
else:
L2.append(i)
i=0
while i<n/2:
print(L1[i],L2[i],end=' ')
i=i+1
LBP180
parity bits
Michael wants to check the parity of the given number. To find the parity, follow
below steps.
if it contains odd number of 1-bits, then it is "odd parity" and if contains even
number of 1-bits then "even parity"
Write a program to validate the given number belongs to odd parity or even parity.
logic:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,c=0;
scanf("%d",&n);
while(n!=0)
{
if(n%2==1)
c++;
n=n/2;
}
printf((c%2==0)?"even":"odd");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
s=str(bin(n))
print('even' if s.count('1')%2==0 else 'odd')
LBP181
sofia loved to play with the programs unfortunately. she got stuck in one of the
questions. she tought to take help of james. james asked her the problem statement.
The problem statement was.
logic:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,j,u,c=0;
scanf("%s",s);//welcome
for(i=0;s[i];i++)
{
u=1;//w
for(j=0;s[j];j++)
{
if(i!=j && s[i]==s[j])//e,l,c,o,m,e
{
u=0;
break;
}
}
if(u==1)
{
c++;
if(c==2)
{
printf("%c",s[i]);//w
break;
}
}
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
L=[]
for i in s:
if s.count(i)==1:
L.append(i)
print(L[1])
LBP182
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isprime(int n)
{
int f=0,i;
for(i=1;i<=n;i++)
{
if(n%i==0)
f++;
}
return f==2;
}
int main() {
int a[100],n,i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
int min=999,max=-1;
for(i=0;i<n;i++)
{
if(isprime(a[i]))
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
}
if(max!=-1 && min!=999)
printf("%d",max-min);
else
printf("0");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def isprime(n):
f=0
for i in range(1,n+1):
if n%i==0:
f=f+1
return f==2
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
min=999
max=-1
for i in L:
if isprime(i):
if min>i:
min=i
if max<i:
max=i
if max!=-1 and min!=999:
print(max-min)
else:
print(0)
LBP183
Given an integer N and integer array A as the input, where N denotes the length of
A
write a program to find an integer the sum of all these product with successors.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],n,i,sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
sum=sum+(a[i]*(a[i]+1));
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
sum=0
for i in L:
sum=sum+(i*(i+1))
print(sum)
LBP184
You are given an array of integers, a of size n, Your task is to find the number of
elements whose positions will remain unchanged after sorted in ascending order.
a=[1,3,2,4,5]
a=[1,2,3,4,5]
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],b[100],i,j,t,c;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
b[i]=a[i];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
c=0;
for(i=0;i<n;i++)
{
if(a[i]==b[i])
c++;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L1=[int(i) for i in input().split()]
L2=L1.copy()
L2.sort()
c=0
for i in range(n):
if L1[i]==L2[i]:
c=c+1
print(c)
LBP185
savings
There are 3 friends named Ronaldo,Messi,Rooney who worked at a compnay. Given thier
monthly salaries and monthly expendictures, returns the highest saving amoung them.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a1,a2,a,b1,b2,b,c1,c2,c;
scanf("%d %d %d %d %d %d",&a1,&a2,&b1,&b2,&c1,&c2);
a=a1-a2;
b=b1-b2;
c=c1-c2;
printf("%d",(a>b&&a>c)?a:(b>c)?b:c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a1,a2,b1,b2,c1,c2=(int(i) for i in input().split())
print(max(a1-a2,b1-b2,c1-c2))
LBP186
You are given a list of integers of size N, write an algorithm to sort the first K
elements of the list in ascending order and the remaining elements in descending
order.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],i,j,t,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n/2;i++)
printf("%d ",a[i]);
for(i=n-1;i>=n/2;i--)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
for i in range(0,n//2):
print(L[i],end=' ')
for i in range(n-1,n//2-1,-1):
print(L[i],end=' ')
LBP187
Given a string,
create a new string made up of its last two letters, reversed and seperated by
comma.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
printf("%c,%c",s[strlen(s)-1],s[strlen(s)-2]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
print(s[-1],s[-2],sep=',')
LBP188
digital root
123=1+2+3=6
159=1+5+9=15=1+5=6
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int sum(int n)
{
int s=0;
while(n!=0)
{
s=s+(n%10);
n=n/10;
}
return s;
}
int main() {
int n;
scanf("%d",&n);
while(1)
{
if(n>=0 && n<=9)
{
printf("%d",n);
break;
}
else
n=sum(n);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def sum(n):
s=0
while n!=0:
s=s+(n%10)
n=n//10
return s
n=int(input())
while True:
if n>=0 and n<=9:
print(n)
break
else:
n=sum(n)
LBP189
absolute difference
Write a program to find the absolute difference between the original number and its
reserved number.
123-321==>-102---> 102
logic: abs(n-rev(n))
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int rev(int n)
{
int r=0;
while(n!=0)
{
r=r*10+(n%10);
n=n/10;
}
return r;
}
int main() {
int n;
scanf("%d",&n);
printf("%d",abs(n-rev(n)));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=input()
print(abs(int(n)-int(n[::-1])))
LBP190
lucky draw
A person went to an exhibition. A lucky draw is going on, where one should buy a
ticket. And if they ticket number appear on the screen, that ticket will be
considered as jackpot winner.
he knows the secret of picking up the ticket number, which will be considered for
the jackpot.
If his ticket follows the above condition, then there are more chances to win the
jackpot.
Given a ticket number, find whether the ticket is eligible to be part of jackpot or
not. Print "Yes/No" based on the result.
Ex:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,len,flag,j,t;
scanf("%d",&n);
i=0;
while(n!=0)
{
a[i++]=n%10;
n=n/10;
}
len=i;
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
flag=1;
for(i=0;i<len-1;i++)
{
if(a[i+1]-a[i]>2)
{
flag=0;
break;
}
}
printf((flag==1)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
Python implementation:
----------------------
LBP191
In an online exam, the test paper set is categorized by the letters A-Z. The
students enrolled in the exam have been assigned a numeric value called application
ID. To assign the test set to the student, firstly the sum of all digits in the
application ID is calculated. If the sum is within the numeric range 1-26 the
corresponding alphanetic set code is assigned to the student, else the sum of the
digits are calcualted again and so on unitil the sum falls within the 1-26 range.
A=1
B=2
C=3
D=4
E=5
F=6
123==> 1+2+3=6 ---> F
786=7+8+6=21 ----> U
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int sum(int n)
{
int s=0;
while(n!=0)
{
s=s+(n%10);
n=n/10;
}
return s;
}
int main() {
int n;
scanf("%d",&n);
while(1)
{
if(n>=1 && n<=26)
{
printf("%c",64+n);
break;
}
else
n=sum(n);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
n=int(input())
while True:
if n>=1 and n<=26:
print(chr(64+n))
break
else:
n=sum(n)
LBP192
Cristina appeared for a corporate Hackathon. She cleated first round of this and
would like to take next challenge which is coding round. The problem statement
comes to her is
"Write a program to find numbers which are in between integer 2 and integer N and
such that the sum of its digits raised to the third power is equal to the number
with the input given.
armstrong number
123 = 1^3+2^3+3^3=1+8+27=36
153 = 1^3+5^3+3^3=1+125+27=153
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int sum(int n)
{
int s=0,d;
while(n!=0)
{
d=n%10;
s=s+d*d*d;
n=n/10;
}
return s;
}
int main() {
int n,i;
scanf("%d",&n);
for(i=2;i<=n;i++)
{
if(i==sum(i))
printf("%d ",i);
}
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def sum(n):
s=0
while n!=0:
d=n%10
s=s+d**3
n=n//10
return s
n=int(input())
for i in range(2,n+1):
if i==sum(i):
print(i,end=' ')
LBP193
Grocery Shop
There was a grocery shop. Shopkeeper would like to keep trasactions as simple as he
can. Hence, he used to take money as whole number. To optimize trasactions, he
decided if someone buys groceries from his shop, he will round money to the nearest
whole number having zeros as last digit. Write a program to help shopkeeper to make
trasactions much simple.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
while(1)
{
if(n%10==0)
{
printf("%d",n);
break;
}
else
n++;
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
while True:
if n%10==0:
print(n)
break
else:
n=n+1
LBP194
Password change
Prakash a technical person wants to update his password for every 15 days, to
create a new password, he is converting all lower case letters to upper case and
upper case letters to lower case, help prakash to update password.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
for(int i=0;s[i];i++)
{
if(s[i]>='a' && s[i]<='z')
printf("%c",s[i]-32);
else if(s[i]>='A' && s[i]<='Z')
printf("%c",s[i]+32);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
print(input().swapcase())
LBP195
Video share
Video share is an online video sharing platform. The company has decided to rate
its users channels based on the sum total of the number of views received online
and the subscribers. This sum total is referred to as user points. The rating will
be given according to the below charts.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
if(n>=30 && n<=50)
printf("Average");
else if(n>=51 && n<=60)
printf("Good");
else if(n>=61 && n<=80)
printf("Excellent");
else
printf("Outstanding");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
if n>=30 and n<=50:
print("Average")
elif n>=51 and n<=60:
print("Good")
elif n>=61 and n<=80:
print("Excellent")
else:
print("Outstanding")
LBP196
modular exponentiation
formula: (b^e)%m
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int b,e,m;
scanf("%d %d %d",&b,&e,&m);
printf("%d",(int)pow(b,e)%m);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
b,e,m=(int(i) for i in input().split())
print(b**e%m)
LBP197
Two strings are said to the same if they are of the same length and have the same
character at each index. Backspacing in a string removes the previous character in
the string.
Given two strings containing lowercase english letters and the character '#' which
represents a backspace key. determine if the two final strings are equal or not.
Return 1 if they are equal else 0.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s1[100],s2[100],s3[100]="\0",s4[100]="\0";
int i,j;
scanf("%s",s1);
scanf("%s",s2);
j=0;
for(i=0;s1[i];i++)
{
if(s1[i]!='#' && s1[i+1]!='#')
s3[j++]=s1[i];
}
j=0;
for(i=0;s2[i];i++)
{
if(s2[i]!='#' && s2[i+1]!='#')
s4[j++]=s2[i];
}
printf("%d",(strcmp(s3,s4)==0)?1:0);
return 0;
}
Java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s1=input()
s2=input()
l1=[]
l2=[]
for i in range(len(s1)-1):
if s1[i]!='#' and s1[i+1]!='#':
l1.append(s1[i])
for i in range(len(s2)-1):
if s2[i]!='#' and s2[i+1]!='#':
l2.append(s2[i])
print(1 if ''.join(l1)==''.join(l2) else 0)
L=[10,20,30]
''.join(L) ---->102030
'-'.join(L) ---> 10-20-30
LBP198
token number
Write an algorithm to generate the token number from the application ID by doing
this modifications.
45789==> 54698
98754==>
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int rev(int n)
{
int d;
int r=0;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return r;
}
int main() {
int n,d;
scanf("%d",&n);
n=rev(n);
while(n!=0)
{
d=n%10;
if(d%2==0)
printf("%d",d+1);
else
printf("%d",d-1);
n=n/10;
}
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input()[::-1])
while n!=0:
d=n%10
if d%2==0:
print(d+1,end='')
else:
print(d-1,end='')
n=n//10
LBP199
a game developing company has developed a math game for kids called "Brain Fun".
The game is for smartphone users and the player is given list of N positive numbers
and a random number K. the player need to divide all the numbers in the list with
random number k and then need to add all the quotients received in each division.
the sum of all the quotients is the score of the player.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],k,i,sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&k);
for(i=0;i<n;i++)
{
sum=sum+a[i]/k;
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
k=int(input())
s=0
for i in L:
s=s+i//k
print(s)
LBP200
Perfect Math
Perfect math is an online math program. In once of the assignments the system
displays a list of N number and a value K, and students need to calculate the sum
of remainders after dividing all the numbers from the list of N numbers by K. The
system need to develop a program to calcualte the correct answer for the
assignment.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],k,i,sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&k);
for(i=0;i<n;i++)
{
sum=sum+a[i]%k;
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
k=int(input())
s=0
for i in L:
s=s+i%k
print(s)
https://www.hackerrank.com/matrixtest
1 2 3
4 5 6
7 8 9
1 2 3 4
5 6 7 8
10
int a[10];
int a[10][10];
1 2 3
4 5 6
7 8 9
1 2 3
4
7 8
array of arrays
nested list
L=[[x,x,x],[x],[x,x]]
int a[5];
for(i=0;i<5;i++)
{
a[i]
}
int a[5][5];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
a[i][j]
}
}
Ex:
int a[3][2];
(0,0) (0,1)
(1,0) (1,1)
(2,0) (2,1)
LBP201
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[5][5],i,j,n,m;
scanf("%d",&n);
scanf("%d",&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
m=int(input())
a=[]
for i in range(n):
a.append([int(i) for i in input().split()])
for i in range(n):
for j in range(m):
print(a[i][j],end=' ')
print()
[[1,2,3],
[4,5,6],
[7,8,9]]
1
2
3
4
5
6
7
8
9
for i in range(n):
a.append([int(i) for i in input().split()])
for i in range(n):
b=[]
for j in range(m):
b.append(int(input()))
a.append(b)
"1 2 3".split() ===> 1,2,3
"hi hello how are you".split() ===> hi,hello,how,are,you
LBP202
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[5][5],i,j,n,m,s;
scanf("%d",&n);
scanf("%d",&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
m=int(input())
a=[]
for i in range(n):
a.append([int(i) for i in input().split()])
s=0
for i in range(n):
for j in range(m):
s=s+a[i][j]
print(s)
LBP203
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]%2==0)
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
s=0
for i in range(3):
for j in range(3):
if a[i][j]%2==0:
s=s+a[i][j]
print(s)
LBP204
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]%2!=0)
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
s=0
for i in range(3):
for j in range(3):
if a[i][j]%2!=0:
s=s+a[i][j]
print(s)
LBP205
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isprime(int n)
{
int i,f=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
f++;
}
return f==2;
}
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(isprime(a[i][j]))
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def isprime(n):
f=0
for i in range(1,n+1):
if n%i==0:
f=f+1
return f==2
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
s=0
for i in range(3):
for j in range(3):
if isprime(a[i][j]):
s=s+a[i][j]
print(s)
LBP206
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
s=0;
for(j=0;j<3;j++)
{
s=s+a[i][j];
}
printf("%d\n",s);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
for i in range(3):
s=0
for j in range(3):
s=s+a[i][j]
print(s)
LBP207
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
s=0;
for(j=0;j<3;j++)
{
s=s+a[j][i];
}
printf("%d\n",s);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
for i in range(3):
s=0
for j in range(3):
s=s+a[j][i]
print(s)
LBP208
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
s=0
for i in range(3):
for j in range(3):
if i==j:
s=s+a[i][j]
print(s)
LBP209
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
s=s+a[i][3-i-1];
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
public class Solution {
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
s=0
for i in range(3):
s=s+a[i][3-i-1]
print(s)
LBP210
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("%d",a[0][0]+a[2][2]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
s=0
print(a[0][0]+a[2][2])
LBP211
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
s=s*a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
s=1
for i in range(3):
for j in range(3):
if i==j:
s=s*a[i][j]
print(s)
LBP212
find the product of opposite diagonal matrix
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=1;
for(i=0;i<3;i++)
{
s=s*a[i][3-i-1];
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
s=1
for i in range(3):
s=s*a[i][3-i-1]
print(s)
LBP213
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,max;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
max=a[0][0];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(max<a[i][j])
max=a[i][j];
}
}
printf("%d",max);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
max=a[0][0]
for i in range(3):
for j in range(3):
if max<a[i][j]:
max=a[i][j]
print(max)
LBP214
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,min;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
min=a[0][0];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(min>a[i][j])
min=a[i][j];
}
}
printf("%d",min);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
min=a[0][0]
for i in range(3):
for j in range(3):
if min>a[i][j]:
min=a[i][j]
print(min)
LBP215
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,max;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
max=a[i][0];
for(j=0;j<3;j++)
{
if(max<a[i][j])
max=a[i][j];
}
printf("%d\n",max);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
for i in range(3):
max=a[i][0]
for j in range(3):
if max<a[i][j]:
max=a[i][j]
print(max)
LBP216
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,min;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
min=a[i][0];
for(j=0;j<3;j++)
{
if(min>a[i][j])
min=a[i][j];
}
printf("%d\n",min);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
for i in range(3):
min=a[i][0]
for j in range(3):
if min>a[i][j]:
min=a[i][j]
print(min)
LBP217
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[j][i]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
---------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
for i in range(3):
for j in range(3):
print(a[j][i],end=' ')
print()
LBP218
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
s=0
for i in range(3):
for j in range(3):
if i==j:
s=s+a[i][j]
print(s)
LBP219
Write a program to find frequency of odd and even elements in the matrix excluding
0.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,c1,c2;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
c1=0;//odd
c2=0;//even
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]%2==0 && a[i][j]!=0)
c2++;
if(a[i][j]%2!=0)
c1++;
}
}
printf("%d\n%d",c1,c2);
return 0;
}
java implementation:
---------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
c1=0#odd
c2=0#even
for i in range(3):
for j in range(3):
if a[i][j]%2==0 and a[i][j]!=0:
c2=c2+1
if a[i][j]%2!=0:
c1=c1+1
print(c1)
print(c2)
LBP220
identity matrix
Implement a program to check whether the given matrix is identity matrix or not
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,flag=1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j && a[i][j]!=1)
{
flag=0;
break;
}
if(i!=j && a[i][j]!=0)
{
flag=0;
break;
}
}
}
printf((flag==1)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
flag=True
a=[]
for i in range(3):
a.append([int(i) for i in input().split()])
for i in range(3):
for j in range(3):
if i==j and a[i][j]!=1:
flag=False
break
if i!=j and a[i][j]!=0:
flag=False
break
print("Yes" if flag else "No")
LBP221
two matrices are equal or not
Implement a program to check whether the given matrices are equal or not
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],b[3][3],i,j,flag=1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]!=b[i][j])
{
flag=0;
break;
}
}
}
printf((flag==1)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
flag=True
a=[]
b=[]
for i in range(3):
a.append([int(i) for i in input().split()])
for i in range(3):
b.append([int(i) for i in input().split()])
for i in range(3):
for j in range(3):
if a[i][j]!=b[i][j]:
flag=False
break
print("Yes" if flag else "No")
LBP222
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],b[3][3],c[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
b=[]
c=[]
for i in range(3):
a.append([int(i) for i in input().split()])
for i in range(3):
b.append([int(i) for i in input().split()])
for i in range(3):
cc=[]
for j in range(3):
cc.append(a[i][j]+b[i][j])
c.append(cc)
for i in range(3):
for j in range(3):
print(c[i][j],end=' ')
print()
LBP223
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],b[3][3],c[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
b=[]
c=[]
for i in range(3):
a.append([int(i) for i in input().split()])
for i in range(3):
b.append([int(i) for i in input().split()])
for i in range(3):
cc=[]
for j in range(3):
cc.append(a[i][j]-b[i][j])
c.append(cc)
for i in range(3):
for j in range(3):
print(c[i][j],end=' ')
print()
LBP224
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],b[3][3],i,j,c[3][3],k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=[]
b=[]
c=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(3):
a.append([int(i) for i in input().split()])
for i in range(3):
b.append([int(i) for i in input().split()])
for i in range(3):
for j in range(3):
for k in range(3):
c[i][j]=c[i][j]+(a[i][k]*b[k][j])
for i in range(3):
for j in range(3):
print(c[i][j],end=' ')
print()
LBP225
sort all the elements in a matrix in asc order
Implement a program to sort all the elements in asc order in the matrix
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,t,k,b[100];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
k=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[k++]=a[i][j];
}
}
for(i=0;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(b[i]>b[j])
{
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
k=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=b[k++];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
ll=[]
for i in range(3):
for j in range(3):
ll.append(l[i][j])
ll.sort()
k=0
for i in range(3):
for j in range(3):
l[i][j]=ll[k]
k=k+1
for i in range(3):
for j in range(3):
print(l[i][j],end=' ')
print()
LBP226
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,t,k,b[100];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
k=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[k++]=a[i][j];
}
}
for(i=0;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(b[i]<b[j])
{
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
k=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=b[k++];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
ll=[]
for i in range(3):
for j in range(3):
ll.append(l[i][j])
ll.sort(reverse=True)
k=0
for i in range(3):
for j in range(3):
l[i][j]=ll[k]
k=k+1
for i in range(3):
for j in range(3):
print(l[i][j],end=' ')
print()
LBP227
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,k,t;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
//logic begin
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
//a[i][j] where i is fixed
//a[0][0],a[0][1],a[0][2]
for(k=j+1;k<3;k++)
{
if(a[i][j]>a[i][k])
{
t=a[i][j];
a[i][j]=a[i][k];
a[i][k]=t;
}
}
}
}
//logic ends
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
for i in range(3):
l[i].sort()
for i in range(3):
for j in range(3):
print(l[i][j],end=' ')
print()
LBP228
Implement a program to sort all the row wise elements in desc order
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,k,t;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
//logic begin
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
//a[i][j] where i is fixed
//a[0][0],a[0][1],a[0][2]
for(k=j+1;k<3;k++)
{
if(a[i][j]<a[i][k])
{
t=a[i][j];
a[i][j]=a[i][k];
a[i][k]=t;
}
}
}
}
//logic ends
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
for i in range(3):
l[i].sort(reverse=True)
for i in range(3):
for j in range(3):
print(l[i][j],end=' ')
print()
LBP229
logic:
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],b[3][3],i,j,k,t;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=a[j][i];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=j+1;k<3;k++)
{
if(b[i][j]>b[i][k])
{
t=b[i][j];
b[i][j]=b[i][k];
b[i][k]=t;
}
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",b[j][i]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
ll=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(3):
for j in range(3):
ll[i][j]=l[j][i]
for i in range(3):
ll[i].sort()
for i in range(3):
for j in range(3):
print(ll[j][i],end=' ')
print()
LBP230
Implement a program to sort the all the column values in desc order
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],b[3][3],i,j,k,t;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=a[j][i];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=j+1;k<3;k++)
{
if(b[i][j]<b[i][k])
{
t=b[i][j];
b[i][j]=b[i][k];
b[i][k]=t;
}
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",b[j][i]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
ll=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(3):
for j in range(3):
ll[i][j]=l[j][i]
for i in range(3):
ll[i].sort(reverse=True)
for i in range(3):
for j in range(3):
print(ll[j][i],end=' ')
print()
LBP231
sparse matrix
Implement a program to check whether the given matrix is sparse matrix or not.
Note: a sparse matrix is a matrix with the majority of its elements equal to zero.
input---------> a matrix
con ----------> no
output -------> Yes or No
c implementation:
----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],counter,i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
counter=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]==0)
counter++;
}
}
printf((counter>=5)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
public class Solution {
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
counter=0
for i in l:
counter=counter+i.count(0)
print("Yes" if counter>=5 else "No")
LBP232
0 ==> 11 22 33
1 ==> 44 55 66
2 ==> 77 88 99
0 ==> 44 55 66
1 ==> 11 22 33
2 ==> 77 88 99
for i=0,i<3,i++
t=a[m-1][i]
a[m-1][i]=a[n-1][i]
a[n-1][i]=t
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,m,n,t;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
scanf("%d %d",&m,&n);
for(i=0;i<3;i++)
{
t=a[m-1][i];
a[m-1][i]=a[n-1][i];
a[n-1][i]=t;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
m=int(input())
n=int(input())
l=[l1,l2,l3]
#swaping
l[m-1],l[n-1]=l[n-1],l[m-1]
for i in range(3):
for j in range(3):
print(l[i][j],end=' ')
print()
LBP233
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,m,n,t;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
scanf("%d %d",&m,&n);
for(i=0;i<3;i++)
{
t=a[i][m-1];
a[i][m-1]=a[i][n-1];
a[i][n-1]=t;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
m=int(input())
n=int(input())
l=[l1,l2,l3]
#swaping
for i in range(3):
l[i][m-1],l[i][n-1]=l[i][n-1],l[i][m-1]
for i in range(3):
for j in range(3):
print(l[i][j],end=' ')
print()
LBP234
i=0 ==> 1 2 3
i=1 ==> 4 5 6
i=2 ==> 7 8 9
a[i][i]
a[i][3-i-1]
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,t;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
t=a[i][i];
a[i][i]=a[i][3-i-1];
a[i][3-i-1]=t;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
for i in range(3):
l[i][i],l[i][3-i-1]=l[i][3-i-1],l[i][i]
for i in range(3):
for j in range(3):
print(l[i][j],end=' ')
print()
LBP235
Program to accept a matrix and check whether it is upper triangular matrix or not
1 2 3
4 5 6
7 8 9
No
1 2 3
0 5 6
0 0 9
Yes
logic:
flag=1;
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,flag=1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(j<i && a[i][j]!=0)
{
flag=0;
}
}
}
printf((flag==1)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
flag=True
for i in range(3):
for j in range(3):
if j<i and l[i][j]!=0:
flag=False
print("Yes" if flag else "No")
LBP236
Program to accept a matrix and check whether it is lower triangular matrix or not
input -------> a 3x3 matrix
con ---------> no
output ------> Yes or No
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,flag=1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(j>i && a[i][j]!=0)
{
flag=0;
}
}
}
printf((flag==1)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
flag=True
for i in range(3):
for j in range(3):
if j>i and l[i][j]!=0:
flag=False
print("Yes" if flag else "No")
LBP237
Implement a program to read a matrix and multiplier and return scalar matrix
multiplication.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,n;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
scanf("%d",&n);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]*n);
}
printf("\n");
}
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
n=int(input())
for i in range(3):
for j in range(3):
print(l[i][j]*n,end=' ')
print()
LBP238
Symmetric matrix
Implement a program to read a matrix and check whether the given matrix is
symmertric matrix or not
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,c;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
c=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]==a[j][i])
c++;
}
}
printf((c==9)?"Yes":"No");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
c=0
for i in range(3):
for j in range(3):
if l[i][j]==l[j][i]:
c=c+1
print("Yes" if c==9 else "No")
LBP239
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
printf("%d ",a[i][j]);
}
else
printf(" ");
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
public class Solution {
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
for i in range(3):
for j in range(3):
if i==j:
print(l[i][j],end=' ')
else:
print(' ',end='')
print()
LBP240
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]*a[i][j]);
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
for i in range(3):
for j in range(3):
print(l[i][j]*l[i][j],end=' ')
print()
LBP241
Implement a program to find sum of even indexed rows in the given matrix.
0 ---> 1 2 3
1----> 4 5 6
2 ---> 7 8 9
1+2+3+7+8+9=30
logic
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i%2==0)
s=s+a[i][j];
}
}
print s
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i%2==0)
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
s=0
for i in range(3):
for j in range(3):
if i%2==0:
s=s+l[i][j]
print(s)
LBP242
Implement a program to find sum of odd indexed rows in the given matrix.
0 ---> 1 2 3
1----> 4 5 6
2 ---> 7 8 9
4+5+6 = 15
logic
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i%2!=0)
s=s+a[i][j];
}
}
print s
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i%2!=0)
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
s=0
for i in range(3):
for j in range(3):
if i%2!=0:
s=s+l[i][j]
print(s)
LBP243
Implement a program to find sum of even indexed cols in the given matrix.
0 1 2
0 ---> 1 2 3
1----> 4 5 6
2 ---> 7 8 9
1+4+7+3+6+9 =30
logic
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(j%2==0)
s=s+a[i][j];
}
}
print s
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(j%2==0)
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
LBP244
Implement a program to find sum of odd indexed cols in the given matrix.
0 1 2
0 ---> 1 2 3
1----> 4 5 6
2 ---> 7 8 9
2+5+8 =15
logic
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(j%2!=0)
s=s+a[i][j];
}
}
print s
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(j%2!=0)
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
s=0
for i in range(3):
for j in range(3):
if j%2!=0:
s=s+l[i][j]
print(s)
LBP245
Sum of elements whose sum of row index and col index is even
Implement a program to find sum of row index and col index is even in the given
matrix.
formula: (i+j)%2==0
logic
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if((i+j)%2=0)
s=s+a[i][j];
}
}
print s
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if((i+j)%2=0)
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
s=0
for i in range(3):
for j in range(3):
if (i+j)%2=0:
s=s+l[i][j]
print(s)
LBP246
Sum of elements whose sum of row index and col index is odd
Implement a program to find sum of row index and col index is odd in the given
matrix.
formula: (i+j)%2==0
logic
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if((i+j)%2=0)
s=s+a[i][j];
}
}
print s
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[3][3],i,j,s;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if((i+j)%2=0)
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
s=0
for i in range(3):
for j in range(3):
if (i+j)%2=0:
s=s+l[i][j]
print(s)
LBP247
LOGIC
s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(isprime(a[i][j])
s=s+a[i][j];
}
}
print s
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isprime(int n)
{
int f=0,i;
for(i=1;i<=n;i++)
{
if(n%i==0)
f++;
}
return f==2;
}
int main() {
int a[3][3],i,j,s=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(isprime(a[i][j]))
s=s+a[i][j];
}
}
printf("%d",s);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def isprime(n):
f=0
for i in range(1,n+1):
if n%i==0:
f=f+1
return f==2
LBP248
logic:
count=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
count=count+countprime(a[i][j]);2
123
}
}
int countprime(int n)
{
while(n!=0)
{
d=n%10;
if(d==2 or 3 or 5 or 7)
c=c+1;
n=n/10;
}
return c;
}
10 11 12
13 14 15
16 17 18
0
10 ----> 0+0 = 0
11 ----> 0+0 = 0
12 ----> 0+1 = 1
13 ----> 1+1 = 2
14 ----> 2+0 = 2
15 ----> 2+1 = 3
16 ----> 3+0 = 3
17 ----> 3+1 = 4
18 ----> 4+0 = 4
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int pc(int n)
{
int c=0,d;
while(n!=0)
{
d=n%10;
if(d==2 || d==3||d==5||d==7)
c=c+1;
n=n/10;
}
return c;
}
int main() {
int a[3][3],i,j,c=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c=c+pc(a[i][j]);
}
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
c=0
for i in range(3):
for j in range(3):
for k in str(l[i][j]):
if k in "2357":
c=c+1
print(c)
LBP249
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int rev(int n)
{
int r=0,d;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return r;
}
int main() {
int a[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",rev(a[i][j]));
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
for i in range(3):
for j in range(3):
print(str(l[i][j])[::-1],end=' ')
print()
LBP250
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int rev(int n)
{
int r=0,d;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return r;
}
int main() {
int a[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]==rev(a[i][j]))
printf("%d ",a[i][j]);
else
printf("0 ");
}
printf("\n");
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l1=[int(i) for i in input().split()]
l2=[int(i) for i in input().split()]
l3=[int(i) for i in input().split()]
l=[l1,l2,l3]
for i in range(3):
for j in range(3):
s=str(l[i][j])
if s==s[::-1]:
print(l[i][j],end=' ')
else:
print('0',end=' ')
print()
LBP251
multiples of 10
Note: The order of the numbers which are not multiples of 10 should remain
unaltered, and similarly. the order of all multiples of 10 should be unaltered.
10 11 20 15 30 45 50 ===> 11 15 45 10 20 30 50
if(a[i]%10!=0) print
if(a[i]%10==0) print
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]%10!=0)
printf("%d ",a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]%10==0)
printf("%d ",a[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
for i in L:
if i%10!=0:
print(i,end=' ')
for i in L:
if i%10==0:
print(i,end=' ')
LBP252
ERP ERG
30-50 D
51-60 C
61-80 B
81-100 A
Write an algorithm to find the ERG character for a given employee's ERP.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
if(n>=30&&n<=50)
printf("D");
else if(n>=51&&n<=60)
printf("C");
else if(n>=61&&n<=80)
printf("B");
else
printf("A");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
if n>=30 and n<=50:
print("D")
elif n>=51 and n<=60:
print("C")
elif n>=61 and n<=80:
print("B")
else:
print("A")
LBP253
encrypted digits
Write an algorithm to decrypt the code so that it can be used to access the
required information.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,a[100];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
if(n%2==0)
{
for(i=0;i<n;i=i+2)
{
printf("%d %d ",a[i+1],a[i]);
}
}
else{
for(i=0;i<n-1;i=i+2)
printf("%d %d ",a[i+1],a[i]);
printf("%d ",a[i]);
}
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
if n%2==0:
i=0
while i<n:
print(L[i+1],L[i],end=' ')
i=i+2
else:
i=0
while i<n-1:
print(L[i+1],L[i],end=' ')
i=i+2
print(L[n-1])
LBP254
Easy Shopping
The e-commerce company 'Easy Shopping' displays some specific products for its
premium customers on its user interface.
The company shortlisted a list of N products.
The list contains the price of each product.
The company will select random products for display.
The selection criteria states that only those products whose price is a perfect
cube number will be selected for display.
The selection process is automated and is done by the company's system.
The company wishes to know the total count of the products selected for display.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i,a[100],n,c;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
c=0;
while(c<=a[i])
{
if(c*c*c==a[i])
{
printf("%d ",a[i]);
break;
}
c++;
}
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
for i in L:
c=0
while c<=i:
if c*c*c==i:
print(i,end=' ')
break
c=c+1
LBP255
player's score
n-rev(n)
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int rev(int n)
{
int d;
int r=0;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return r;
}
int main() {
int n;
scanf("%d",&n);
printf("%d",n-rev(n));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=input()
print(int(n)-int(n[::-1]))
LBP256
GlobalAdd
The media compnay "GlobalAdd" has received a batch of advertisements from different
product brands.
The batch of advertisements is a numeric value where each digit represnets the
number of advertisements the media company has received from different product
brands.
Since the company banners permit only even numbers of advertisements to be
displayed,
the media company needs to know the totoal number of advertisements it will be able
to display from the given batch.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,c=0,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
if(d%2==0)
c++;
n=n/10;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
c=0
while n!=0:
d=n%10
if d%2==0:
c=c+1
n=n//10
print(c)
LBP257
FunGames
The games development company "FunGames" has developed a ballon shooter games.
The ballons are arranged in a linear sequence and each ballon has a number
associated with it.
The numbers on the ballons are fibonacci series.
In the game the player shoots 'k' ballons.
The player's score is the sum of numbers on k ballons.
0 1 1 2 3 5 8 .....
K=1 ----> 0
K=3 ----> 0+1+1=2
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i,k,a1,a2,a3,sum;
scanf("%d",&k);
a1=-1;
a2=1;
sum=0;
for(i=1;i<=k;i++)
{
a3=a1+a2;
a1=a2;
a2=a3;
sum=sum+a3;
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
k=int(input())
a1=-1
a2=1
sum=0
for i in range(1,k+1):
a3=a1+a2
sum=sum+a3
a1,a2=a2,a3
print(sum)
LBP258
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,c=0;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]>='0'&&s[i]<='9')
c++;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
c=0
for i in s:
if i.isdigit():
c=c+1
print(c)
LBP259
Morning Prayer
Student of a school are assembled in a straight line for the morning prayer.
To uplift the sprit of the students, an exercise is conducted.
THe initial letter of all the student's names is noted down(str).
The task here is to substitute the intitial letters in the list with consonants
such that if the initial letter of the student is a vowel, retain the student in
the same position.
If the initial letter of the student is a vowel, swap with the next immediate
consonants from the english alphabet sequence (a-z).
Say, initial letter of a student starts with b swap with c, next immediate
consonant.
If the initial letter is 'z' swap with 'b'.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100],ch;
int i;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
printf("%c",s[i]);
else
{
ch = s[i];
if(ch=='z')
ch = 'a';
ch++;
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
ch++;
printf("%c",ch);
}
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
for i in s:
if i in 'aeiou':
print(i,end='')
else:
ch = i
if ch=='z':
ch='a'
ch=chr(ord(ch)+1)
if ch in 'aeiou':
print(chr(ord(ch)+1),end='')
else:
print(ch,end='')
LBP260
factorial game
Let say the given number is 5, so 5! will be 120, Here 0 is the last digit.
But we dn't want 0, we want a number other than 0. Then last digit is 2.
0!=1 -----> 1
1!=1 -----> 1
2!=2 -----> 2
3!=6 -----> 6
4!=24 ----> 4
5!=120 ---> 2
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,f;
scanf("%d",&n);
f=1;
for(i=1;i<=n;i++)
f=f*i;
if(f%10==0)
printf("%d",(f/10)%10);
else
printf("%d",f%10);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import math
n=int(input())
f=math.factorial(n)
if f%10==0:
print((f//10)%10)
else:
print(f%10)
LBP261
Speed Maths
Jack was in 9th standard. He appeared for a speed maths competivie exam.
Jack is taking longer time to solve one of the problems.
Count the number of 1's in the binary representation of an integer.
Help him to solve the below problem and write a code for the same.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,d,c;
scanf("%d",&n);
c=0;
while(n!=0)
{
d=n%2;
if(d==1)
c++;
n=n/2;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
s=bin(n)
print(s.count('1'))
LBP262
puzzle
Dennis was solving a puzle.
the puzzle was to verify a number whose cube ends with the number itself.
Help Dennis to find the solution of the puzzle and write the code accordingly.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
if(n*n*n%10==n)
printf("true");
else
printf("false");
return 0;
}
java implementation:
---------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
print('true' if n**3%10==n else 'false')
LBP263
mathematics class
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i;
scanf("%s",s);
scanf("%d",&i);
if(i<strlen(s))
printf("%c",s[i-1]);
else
printf("-1");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
n=int(input())
print(s[n-1] if n<len(s) else '-1')
(i<s.length())?s.charAt(i-1):"-1"
abc, 1 ---> a
abc, 2 ---> b
abc, 3 ---> c
abc, 4 ---> -1
LBP264
power function
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a,b;
scanf("%d %d",&a,&b);
printf("%d",((int)pow(a,b))%10);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a,b=(int(i) for i in input().split())
print((a**b)%10)
LBP265
mathematical tricks
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
printf("%d",d*d);
n=n/10;
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
while n!=0:
d=n%10
print(d*d,end='')
n=n//10
LBP266
coding standards
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,flag=1;
scanf("%s",s);
for(i=0;s[i];i++)
{
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||(s[i]=='_')||
(s[i]>='0'&&s[i]<='9'))
{
if(s[0]>='0'&&s[0]<='9')
{
flag=0;
break;
}
}
else
{
flag=0;
break;
}
}
printf((flag==1)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
---------------------
import re
s=input()
print('true' if re.fullmatch("[a-zA-Z_][a-zA-Z0-9_]+",s) else 'false')
[a-zA-Z_][a-zA-Z0-9_][a-zA-Z0-9_]+
abc
a
LBP267
party quiz
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
for(i=1;;i++)
{
if(i*i>=n){
printf("%d",i*i);
break;
}
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
i=1
while True:
if i*i>=n:
print(i*i)
break
i=i+1
LBP268
Be Positive
Write a program to get two inputs from the user and find the absolute difference
between the sum of two numbers and the product of two numbers.
a and b
abs((a+b)-(a*b))
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a,b;
scanf("%d %d",&a,&b);
printf("%d",abs((a+b)-(a*b)));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
a=int(input())
b=int(input())
print(abs((a+b)-(a*b)))
LBP269
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,f;
scanf("%d",&n);
f=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
f++;
}
printf((f==2)?"yes":"no");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
f=0
for i in range(1,n+1):
if n%i==0:
f=f+1
print('yes' if f==2 else 'no')
LBP270
sentence making
The teacher in a school has started to teach the very basics rule for a sentence is
that it should start with a capital letter and ends with a full stop.
If the sentence fails any of the above mentioned criteria, it will be an incorrect
sentence.
Make a program to validate whether a given statement is a correct sentence or not.
The program should return True/False based on the validity.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%[^\n]s",s);
if((s[0]>='A'&&s[0]<='Z')&&(s[strlen(s)-1]=='.'))
printf("true");
else
printf("false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import re
s=input()
print('true' if re.fullmatch("[A-Z][a-zA-Z_0-9 ]*[.]",s) else 'false')
LBP271
Geetha Singh is trying to create a system to convert binary number to its decimal
eqivalent. Help her to automate the process.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,dec=0,d,i=0;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
dec=dec+d*(int)pow(2,i++);
n=n/10;
}
printf("%d",dec);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
public class Solution {
python implementation:
----------------------
print(eval("0b"+input()))
LBP272
Item id
A company wishes to bucketize their item id's for better search operations.
The bucket for the item ID is chosen on the basis of the maximum value of
the digit in the item ID.
Write an algorithm to find the bucket to which the item ID will be assigned.
12875 ---> 8
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,d,m;
scanf("%d",&n);
m=0;
while(n!=0)
{
d=n%10;
if(d>m)
m=d;
n=n/10;
}
printf("%d",m);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
print(max([int(i) for i in input()]))
LBP273
Next Letter
def NextLetter(ch1,ch2);
The function accepts two characters ch1 and ch2 as arguments, ch1 and ch2 are
alphabetical letters.
Implement the function to find and return the next letter so that distance between
ch2 and the next letter is the same as the distance between ch1 and ch2.
While counting distance if you exceed the letter 'z' then, count the remaining
distance starting from the letter 'a'.
ch2+(ch2-ch1)
c impelementation:
------------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char ch1,ch2;
scanf("%c %c",&ch1,&ch2);
int d = ch2-ch1;
if(ch2=='z')
printf("%c",'a'+d);
else
printf("%c",ch2+d);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
ch1,ch2=(i for i in input().split())
d=ord(ch2)-ord(ch1)
if ch2=='z':
print(chr(ord('a')+d))
else:
print(chr(ord(ch2)+d))
LBP274
Implement a program to find sum of all integers between two integer numbers taken
as input and are divisible by 7.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n1,n2,i,sum=0;
scanf("%d %d",&n1,&n2);
for(i=n1;i<=n2;i++)
{
if(i%7==0)
sum=sum+i;
}
printf("%d",sum);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n1,n2=(int(i) for i in input().split())
sum=0
for i in range(n1,n2+1):
if i%7==0:
sum=sum+i
print(sum)
LBP275
The country visa center generates the token number for its applicants from their
application ID.
the application ID is numberic value.
The token number is generated in a specific form.
the even digits in the applicant's ID are replaced by the digit one greater than
the even digit
and the odd digits in the applicant's ID are replaced by the digit one lesser than
the odd digit.
The numeric value thus generated represents the token number of applicant.
Write an algorithm togenerate the token number from the applicant ID.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int rev(int n)
{
int r=0,d;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return r;
}
int main() {
int n,d;
scanf("%d",&n);
n=rev(n);
while(n!=0)
{
d=n%10;
if(d%2==0)
printf("%d",d+1);
else
printf("%d",d-1);
n=n/10;
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=input()
n=int(n[::-1])
while n!=0:
d=n%10
if d%2==0:
print(d+1,end='')
else:
print(d-1,end='')
n=n//10
LBP276
Neon Number
Rahul's teacher gave an assignment to all the student that when they show up
tomorrow they should find a special type of number and report her.
He thought very carefully and came up with an idea to have neon numbers.
A neon number is a number where the square of the sum of each digit of the number
results in the given number.
Given an integer N,
Write a program to find whether the number N is a Neon number.
If it's not a Neon number, print the sqaure of the sum of digits of the number.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a,b,c;
scanf("%d",&n);
a=n%10;
b=(n/10)%10;
c=(a+b)*(a+b);
printf((c==n)?"true":"false");
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
a=n%10
b=(n//10)%10
print("true" if (a+b)**2==n else "false")
LBP277
A super market maintains a pricing format for all its products. A value N is
printed on each product. When the scanner reads the value N on the item, the
product of all the digits in the value N is the price of the item. the task here is
to design the software such that given the code of any item N the product
(multiplication) of all the digits of value should be computed (price).
123=1*2*3=6/-
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,prod=1,d;
scanf("%d",&n);
while(n!=0)
{
d=n%10;
prod=prod*d;
n=n/10;
}
printf("%d",prod);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import math
L=[int(i) for i in input()]
print(math.prod(L))
LBP278
Number Container
Given two positive numbers N and K. The task is to find the nunber N containers
exactly K digit or not. If contains then print True<space>digit_count otherwise
False<space>digit_count.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char n1[100];
int n2;
scanf("%s",n1);
scanf("%d",&n2);
if(strlen(n1)==n2)
printf("True %d",strlen(n1));
else
printf("False %d",strlen(n1));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n1,n2=(i for i in input().split())
n2=int(n2)
if len(n1)==n2:
print("True",len(n1))
else:
print("False",len(n1))
LBP279
pronic number
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,x;
scanf("%d",&n);
x=(int)sqrt(n);
if(n==x*(x+1))
printf("true");
else
printf("false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import math
n=int(input())
x=math.isqrt(n)
print("true" if n==x*(x+1) else "false")
LBP280
Implement a program that will test if a string is a valid PIN or not via a regular
expression.
Exactly 4 characters.
Only numeric characters (0-9).
No whitespace.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
scanf("%s",s);
int i,c=0;
for(i=0;s[i];i++)
{
if(s[i]>='0'&&s[i]<='9')
c++;
}
printf((c==4)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
import re
s=input()
print('true' if re.fullmatch("[0-9]{4}",s)!=None else 'false')
LBP281
Implement a program that takes the memory size as an argument and returns the
actual memory size.
Note: The actual storage loss on a USB device is 7% of the overall memory size!
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
printf("%.2f",n-n*(7.0/100));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
print("%.2f"%(n-n*0.07))
LBP282
Happy Number
The happy number can be defined as a number which will yield 1 when it is replaced
by the sum of the square of its digits repeatedly.
If this process results in an endless cycle of numbers containing 4, then the
number is called an unhappy number.
Write a program that accepts a number and determines whether the number is a happy
number or not. Return true if so, false otherwise.
32=9+4=13=1+9=10=1+0=1 true
16=1+36=37=9+49=58=25+64=89=........=4 false
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int sum(int n)
{
int d,s=0;
while(n!=0)
{
d=n%10;
s=s+d*d;
n=n/10;
}
return s;
}
int main() {
int n;
scanf("%d",&n);
while(1)
{
if(n==1)
{
printf("true");
break;
}
if(n==4)
{
printf("false");
break;
}
n=sum(n);
}
return 0;
}
java implementation:
---------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def sum(n):
s=0
while n!=0:
d=n%10
s=s+d*d
n=n//10
return s
n=int(input())
while True:
if n==1:
print('true')
break
if n==4:
print('false')
break
n=sum(n)
LBP283
Implement a function that takes an array of numbers and returns the mean (average)
of all those numbers.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,sum;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sum=0;
for(i=0;i<n;i++)
sum=sum+a[i];
printf("%.2f",(float)sum/n);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
sum=0
for i in L:
sum=sum+i
print("%.2f"%(sum/n))
LBP284
Factorize a Number
Implement a program to that takes a number as its argument and returns an array of
all its factors
for(i=1;i<=n;i++)
{
if(n%i==0)
print i
}
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
printf("%d ",i);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
for i in range(1,n+1):
if n%i==0:
print(i,end=' ')
LBP285
Next 5 characters
a ---> b c d e f
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char ch;
scanf("%c",&ch);
for(int i=1;i<=5;i++)
{
printf("%c ",++ch);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
LBP286
Composite Number
Implement a program to check whether the given number is composite number or not.
prime f==2
composite f>2
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,f;
scanf("%d",&n);
f=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
f++;
}
printf((f>2)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
LBP287
Immplement a program, it reads integers from the input device(until it gets -ve
number) and put them into array.
We define a hot number as any number whose last digit is 2, and
cold number as any number that is less than 50. You have to modify the program such
that
92 61 13 42 -1
-1 61 -5 -6
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
while(n!=-1)
{
if(n%10==2 && n>50)
printf("-1 ");
else if(n%10!=2 && n<50)
printf("-5 ");
else if(n%10==2 && n<50)
printf("-6 ");
else
printf("%d ",n);
scanf("%d",&n);
}
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
L=[int(i) for i in input().split()]
for n in L[0:len(L)-1]:
if n%10==2 and n>50:
print(-1,end=' ')
elif n%10!=2 and n<50:
print(-5,end=' ')
elif n%10==2 and n<50:
print(-6,end=' ')
else:
print(n,end=' ')
LBP288
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,flag=0;
scanf("%s",s);
for(i=0;s[i];i++)
{
if(s[i]=='a')
{
flag=1;
break;
}
}
printf((flag==1)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
(obj.nextLine().indexOf('a'))>=0 ? true:false
python implementation:
----------------------
print('true' if 'a' in input() else 'false')
LBP289
MathAtTip
The online math course provider 'MathAtTip' has designed a course for children
called Learning Number Recognition and Coutning.
The assessment part of the course has a question where the student is given a
number and a digit.
The student needs to find out the total count of the digits present in the number
excluding the given digit.
12345, 2 ---> 4
12345, 6 ---> 5
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,d,dd,c;
scanf("%d",&n);
scanf("%d",&d);
c=0;
while(n!=0)
{
dd=n%10;
if(dd!=d)
c++;
n=n/10;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
d=input()
print(len(s)-s.count(d))
LBP290
TodaysApparel
Write an algorithm to help the company know the number of profitable days in the
list.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,c;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
c=0;
for(i=0;i<n;i++)
{
if(a[i]>0)
c++;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
---------------------
n=int(input())
l=[int(i) for i in input().split()]
c=0
for i in l:
if i>0:
c=c+1
print(c)
LBP291
BucketId
A data company wishes to store its data files on the server. They N files.
Each file has a particular size.
the server stires the files in bucket list.
The bucket ID is calculated as the sum of the digits of its file size.
The server.. the bucket ID for every file request where the file is stored.
Write an algorithm to find the bucketIDs where the files are stored.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int sum(int n)
{
int s=0,d;
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
return s;
}
int main() {
int n,a[100],i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d ",sum(a[i]));
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def sum(n):
s=0
while n!=0:
d=n%10
s=s+d
n=n//10
return s
n=int(input())
l=[int(i) for i in input().split()]
for i in l:
print(sum(i),end=' ')
LBP292
Decimal to Octal
a[]={};
while(n!=0)
{
a[i]=n%8;
n=n/8;
}
i=i-1 to i>=0 i--
n=10
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i;
scanf("%d",&n);
i=0;
while(n!=0)
{
a[i++]=n%8;
n=n/8;
}
for(i=i-1;i>=0;i--)
printf("%d",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
print(oct(n)[2:])
LBP293
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],s,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
s=0;
for(i=0;i<n;i++)
{
s=s+a[i];
printf("%d ",s);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
s=0
for i in L:
s=s+i
print(s,end=' ')
LBP294
Implement a program to read date of birth of the person and decide he belong to
which stage.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int d,m,y,age;
scanf("%d/%d/%d",&d,&m,&y);
age = 2022-y;
if(age>60)
printf("1");
else if(age>=18 && age<=60)
printf("2");
else
printf("3");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
l=input().split("/")
age=2022-int(l[2])
if age>60:
print(1)
elif age>=18 and age<=60:
print(2)
else:
print(3)
LBP295
int main() {
int radius;
scanf("%d",&radius);
printf("%.2f",3.141592653589793*radius*radius);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
---------------------
import math
n=int(input())
print("%.2f"%(math.pi*n**2))
LBP296
Divisible by 5 or 7
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
if(i%5==0 || i%7==0)
printf("%d ",i);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
for i in range(1,n+1):
if i%5==0 or i%7==0:
print(i,end=' ')
LBP297
ending 3
Implement a program to print the list of integers which are ending with 3 in the
given range.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i,n1,n2;
scanf("%d %d",&n1,&n2);
for(i=n1;i<=n2;i++)
{
if(i%10==3)
printf("%d ",i);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n1,n2=(int(i) for i in input().split())
for i in range(n1,n2+1):
if i%10==3:
print(i,end=' ')
LBP298
m+m+m=s1
m+m+m=s2
abs(s1-s2)
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int maxD(int n)
{
int m=0,d;
while(n!=0)
{
d=n%10;
if(d>m)
m=d;
n=n/10;
}
return m;
}
int minD(int n)
{
int m=999,d;
while(n!=0)
{
d=n%10;
if(d<m)
m=d;
n=n/10;
}
return m;
}
int main() {
int n1,n2,n3;
scanf("%d %d %d",&n1,&n2,&n3);
int s1,s2;
s1=maxD(n1)+maxD(n2)+maxD(n3);
s2=minD(n1)+minD(n2)+minD(n3);
printf("%d",abs(s1-s2));
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n1=input()
n2=input()
n3=input()
s1=max([int(i) for i in n1])+max([int(i) for i in n2])+max([int(i) for i in n3])
s2=min([int(i) for i in n1])+min([int(i) for i in n2])+min([int(i) for i in n3])
print(abs(s1-s2))
LBP299
Lucky String
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,sum=0;
scanf("%s",s);
for(i=0;s[i];i++)
sum=sum+s[i];
printf((sum%2==0)?"true":"false");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
LBP300
Implement a program to find sum of last three digits in the given number.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
printf("%d",n%10+(n/10)%10+(n/100)%10);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=input()
print(int(n[-1])+int(n[-2])+int(n[-3]))
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[100];
int i,c=1;
scanf("%[^\n]s",s);
for(i=0;s[i];i++)
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
{
printf("%d",c++);
if(c==10)
c=1;
}
else
printf("%c",s[i]);
}
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s=input()
c=1
for i in s:
if i in "aeiou":
print(c,end='')
c=c+1
if(c==10):
c=1
else:
print(i,end='')
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],b[100],i,n,s1,s2;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
s1=0;
s2=0;
for(i=0;i<n;i++)
s1=s1+a[i];
for(i=0;i<n;i++)
s2=s2+b[i];
printf("%d",s1-s2);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L1=[int(i) for i in input().split()]
L2=[int(i) for i in input().split()]
print(sum(L1)-sum(L2))
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[100],i,j,t;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
L.sort()
for i in L:
print(i,end=' ')
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int a[100],i,n,c;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
c=0;
for(i=0;i<n;i++)
{
if(a[i]!=0)
printf("%d ",a[i]);
else
c++;
}
for(i=0;i<c;i++)
printf("0 ");
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[int(i) for i in input().split()]
c=0
for i in L:
if i!=0:
print(i,end=' ')
else:
c=c+1
for i in range(c):
print('0',end=' ')
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n1,n2,b[4]={0},i=0,d;
scanf("%d",&n1);
while(n1!=0)
{
d=n1%2;
b[i++]=d;
n1=n1/2;
}
for(i=0;i<4;i++)
{
if(b[i]==0)
b[i]=1;
else
b[i]=0;
}
n2=b[0]*1+b[1]*2+b[2]*4+b[3]*8;
printf("%d",n2);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
L=[0,0,0,0]
i=0
while n!=0:
d=n%2
L[i]=d
i=i+1
n=n//2
for i in range(4):
if L[i]==0:
L[i]=1
else:
L[i]=0
print(L[0]*1+L[1]*2+L[2]*4+L[3]*8)
logic:
------
a[10]={0}
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,a[10]={0},d,i,c;
scanf("%d",&n);
c=0;
while(n!=0)
{
d=n%10;
a[d]++;
n=n/10;
}
for(i=0;i<10;i++)
{
if(a[i]>=2)
c++;
}
printf("%d",(c!=0)?c:-1);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
d={}
L=[int(i) for i in input()]
for i in L:
d[i]=d.get(i,0)+1
c=0
for i in d.values():
if i>=2:
c=c+1
print(c if c!=0 else -1)
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,c=0,key,d;
scanf("%d",&n);
scanf("%d",&key);
while(n!=0)
{
d=n%10;
if(d==key)
c++;
n=n/10;
}
printf("%d",c);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
s,key=(i for i in input().split())
print(s.count(key))
n1 and n2
min prime number n1 ----->
max prime number n2 <----
1 10
2,3,5,7
min=2
max=7
otp = 9
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int isprime(int n)
{
int f=0,i;
for(i=1;i<=n;i++)
{
if(n%i==0)
f++;
}
return f==2;
}
int main() {
int n1,n2,s1,s2;
scanf("%d %d",&n1,&n2);
while(1)
{
if(isprime(n1))
{
s1=n1;
break;
}
n1++;
}
while(1)
{
if(isprime(n2))
{
s2=n2;
break;
}
n2--;
}
printf("%d",s1+s2);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
def isprime(n):
f=0
for i in range(1,n+1):
if n%i==0:
f=f+1
return f==2
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int ispali(int n)
{
int d,r=0,t;
t=n;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return t==r;
}
int main() {
int n;
scanf("%d",&n);
while(1)
{
if(ispali(n))
{
printf("%d",n);
break;
}
n++;
}
return 0;
}
java implementation:
-------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
while True:
if str(n)==str(n)[::-1]:
print(n)
break
n=n+1
LBP310: FizzBuzz
~~~~~~~~~~~~~~~~~
Given a number n, for each integer i in the range from 1 to n inclusive, print one
value per line as follows.
c implementation:
-----------------
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d",&n);
if(n%3==0 && n%5==0)
printf("FizzBuzz");
else if(n%3==0 && n%5!=0)
printf("Fizz");
else if(n%3!=0 && n%5==0)
printf("Buzz");
else
printf("%d",n);
return 0;
}
java implementation:
--------------------
import java.io.*;
import java.util.*;
python implementation:
----------------------
n=int(input())
if n%3==0 and n%5==0:
print('FizzBuzz')
elif n%3==0 and n%5!=0:
print('Fizz')
elif n%3!=0 and n%5==0:
print('Buzz')
else:
print(n)