Q1) Express a number as sum of consecutive numbers
import java.util.*;
class ConsecutiveSum {
static void printRange(int last, int first){
System.out.print(first);
for (int x = first + 1; x <= last; x++) System.out.print(" + " + x);
System.out.println();
}
static int process(int n){
int printed = 0;
for (int len = 2; len < n; len++){
for (int first = 1; first < len; first++){
if (2*n == len*(len+2*first-1)){
System.out.print(n + " = ");
printRange(first+len-1, first);
printed++;
}
}
}
return printed==0 ? -1 : printed;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (process(n)==-1) System.out.println(-1);
}
}
Q2) Add two binary numbers (as strings)
import java.util.*;
class BinaryAdd {
static String add(String a, String b){
StringBuilder sb = new StringBuilder();
int i=a.length()-1, j=b.length()-1, carry=0;
while(i>=0 || j>=0 || carry>0){
int d = carry;
if (i>=0) d += a.charAt(i--)-'0';
if (j>=0) d += b.charAt(j--)-'0';
sb.append(d%2);
carry = d/2;
}
return sb.reverse().toString();
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String a=sc.next(), b=sc.next();
System.out.println("Sum: " + add(a,b));
}
}
Q3) Check if a number is Armstrong (3-digit) and list all in a range
import java.util.*;
class ArmstrongRange {
static boolean isArm(int n){
int s=0, t=n;
while(t>0){ int d=t%10; s+=d*d*d; t/=10; }
return s==n;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int L=sc.nextInt(), R=sc.nextInt();
boolean found=false;
for(int x=L; x<=R; x++){
if(x>=100 && x<=999 && isArm(x)){
System.out.print(x + " ");
found=true;
}
}
if(!found) System.out.println(-1);
else System.out.println();
}
}
Q4) Palindrome test for a word (ignore case)
import java.util.*;
class PalWord {
static boolean isPal(String s){
s=s.toLowerCase();
int i=0,j=s.length()-1;
while(i<j) if(s.charAt(i++)!=s.charAt(j--)) return false;
return true;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.next();
System.out.println(isPal(s) ? "PALINDROME" : "NOT PALINDROME");
}
}
Q5) Prime factors of a number (ascending)
import java.util.*;
class PrimeFactors {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<=1){ System.out.println(-1); return; }
for(int p=2; p*p<=n; p++){
while(n%p==0){ System.out.print(p + " "); n/=p; }
}
if(n>1) System.out.print(n);
System.out.println();
}
}
Q6) Generate first N Fibonacci numbers
import java.util.*;
class FibonacciN {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<=0){ System.out.println(-1); return; }
long a=0,b=1;
for(int i=1;i<=n;i++){
System.out.print(a + (i==n?"":" "));
long c=a+b; a=b; b=c;
}
System.out.println();
}
}
Q7) Sort an array using Bubble Sort and print swaps count
import java.util.*;
class BubbleSortCount {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<=0){ System.out.println(-1); return; }
int[] a=new int[n];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
int swaps=0;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(a[j]>a[j+1]){
int t=a[j]; a[j]=a[j+1]; a[j+1]=t; swaps++;
}
}
}
for(int x:a) System.out.print(x+" ");
System.out.println("\nSwaps: "+swaps);
}
}
Q8) Count vowels, consonants, digits, and spaces in a sentence
import java.util.*;
class CharTally {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
int v=0,c=0,d=0,sp=0,oth=0;
for(char ch: s.toCharArray()){
if("aeiouAEIOU".indexOf(ch)>=0) v++;
else if(Character.isLetter(ch)) c++;
else if(Character.isDigit(ch)) d++;
else if(Character.isWhitespace(ch)) sp++;
else oth++;
}
System.out.println("Vowels: "+v);
System.out.println("Consonants: "+c);
System.out.println("Digits: "+d);
System.out.println("Spaces: "+sp);
System.out.println("Others: "+oth);
}
}