0% found this document useful (0 votes)
29 views

Basic Code Interview

Uploaded by

KONALA SAI REDDY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Basic Code Interview

Uploaded by

KONALA SAI REDDY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Fibonacci Series

import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
for (int i = 1; i <= n; ++i) {
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
} } }
PRIME:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num=sc.nextInt();
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = true;
break;
}
}

if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}

Palstring
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String reverseStr = "";

int strLength = str.length();


for (int i = (strLength - 1); i >=0; --i) {
reverseStr = reverseStr + str.charAt(i);
}

if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
}
else {
System.out.println(str + " is not a Palindrome String.");
}
}
}
Palnum:
import java.util.*;
public class Main
{
public static void main(String[] args) {

int r,sum=0,temp;
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
temp=n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");

}
}
Armstrong
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n, count = 0, a, b, c, sum = 0;
System.out.print("Armstrong numbers from 1 to 1000:");
for(int i = 100; i <= 500; i++)
{
n = i;
while(n > 0)
{
b = n % 10;
sum = sum + (b * b * b);
n = n / 10;
}
if(sum == i)
{
System.out.print(i+" ");
}
sum = 0;
}
}
}
Duplicate letters in string
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String string1 = sc.nextLine();
int count;
char string[] = string1.toCharArray();

System.out.println("Duplicate characters in a given


string: ");
for(int i = 0; i <string.length; i++) {
count = 1;
for(int j = i+1; j <string.length; j++) {
if(string[i] == string[j] && string[i] != ' ') {
count++;
string[j] = '0';
}
}
if(count > 1 && string[i] != '0')
System.out.println(string[i]);
}
}
}
Count Number of Repeated Characters in
String
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
String str;
char ch;
int strLen, i, count, j, k, repChars=0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the String: ");
str = s.nextLine();
strLen = str.length();
char[] arr = new char[strLen];
for(i=0; i<strLen; i++)
arr[i] = str.charAt(i);
for(i=0; i<strLen; i++){
ch = arr[i];
count = 0;
for(j=(i+1); j<strLen; j++){
if(ch==arr[j]) {
count++;
for(k=j; k<(strLen-1); k++)
arr[k] = arr[k+1];
strLen--;
j--;
} }
if(count>0)
repChars++;
}
System.out.println("\nTotal Number of Repeated
Characters = " +repChars);
} }

You might also like