Skip to main content

FIND THE NO OF SUBSTRINGS DIVISIBLE BY 7 --> INPUT -3567 OUTPUT-4 -->(35,56,567,7)

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner input = new Scanner(System.in);
String number = input.nextLine();
String sub;
int length=number.length();
int i=0;
for(int first=0;first<length;first++)
{
for(int second=first+1;second<=length;second++)
{
sub = number.substring(first,second);
int a = Integer.parseInt(sub);
if((a%7)==0)
{
i++;
}
}
}
System.out.println(i);
}
}

Comments

Popular posts from this blog

Print word pattern - odd letters - beginner's code

1. Print the word with odd letters as P M R A O R G O R R A P M Solution: using System; public class Test { public static void Main() { string text = Console.ReadLine(); int n=text.Length; Pattern print = new Pattern (); print.XPattern(text,n); } } public class Pattern { public void XPattern(string text,int n) { char[] textarray = text.ToCharArray(); int reverseprinter = n-1; int middleLetter = n/2; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==j && j!=middleLetter) { Console.Write(textarray[j]); } else if(j==reverseprinter) { Console.Write(textarray[reverseprinter]); reverseprinter--; } else { Console.Write("*"); } } Console.WriteLine(); } } } OUTPUT Please Enter text: INPUT : program p m r a o r g o r r a p m

GRANDSON PROBLEM

https://www.geeksforgeeks.org/zoho-interview-set-1-campus/ Given a two dimensional array of string like <”luke”, “shaw”> <”wayne”, “rooney”> <”rooney”, “ronaldo”> <”shaw”, “rooney”> Where the first string is “child”, second string is “Father”. And given “ronaldo” we have to find his no of grandchildren Here “ronaldo” has 2 grandchildren. So our output should be 2. SOLUTION using System; public class Test { public static void Main() { var noOfInputs = Console.ReadLine(); int noOfInputLines = Convert.ToInt32(noOfInputs); string[,] FatherSonArray= new String[noOfInputLines,noOfInputLines]; for(int father=0;father<noOfInputLines;father++) { for(int son=0;son<2;son++) { FatherSonArray[father,son] = Console.ReadLine(); } }     Console.WriteLine("No of grandchildrens:");     string GrandFather = Console.ReadLine();     int NoOfGrandSons=0;     Console.Wr...

Quicksort in C#

using System; public class Test { public static void Main() { var unsorted = Console.ReadLine().Split(' '); int[] a = Array.ConvertAll(unsorted,x=>int.Parse(x)); int n= a.Length; Quicksort(a,0,n-1); for(int i=0;i<n;i++) { Console.WriteLine(a[i]); } } public static void Quicksort(int[] a,int low,int high) { if(low<high) { int pi= Partition(a,low,high); Quicksort(a,low,pi-1); Quicksort(a,pi+1,high); } } public static int Partition(int[]a,int low,int high) { int pivot = a[high]; int i = low-1; for(int j=low;j<=high-1;j++) { if(a[j]<pivot) { i++; int temp=a[i]; a[i]=a[j]; a[j]=temp; } } int temp2 = a[high]; a[high]=a[i+1]; a[i+1]=temp2; return i+1; } }