|
<header> |
|
|
|
|
|
<nav class="navbar "> |
|
<a class="navbar-brand py-0" href="#"> |
|
<i class="brand material-icons pt-3">code</i> |
|
</a> |
|
<i id="admin" class="material-icons btn my-sm-0 mr-5"> |
|
account_circle |
|
</i> |
|
|
|
</nav> |
|
<div id="mySidenav" class="sidenav"> |
|
<h1 class="text-white"> <Problems></h1> |
|
<hr class="bg-light"> |
|
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a> |
|
<a href="javascript:void(0)" class="tablinks" onclick="openCity(event, 'palin')">Palindrome problem</a> |
|
<a class="tablinks" onclick="openCity(event, 'fibon')">Fibonacci series problem</a> |
|
<a class="tablinks" onclick="openCity(event, 'facto')">Factorial Number problem</a> |
|
<a class="tablinks" onclick="openCity(event, 'bubbl')">Bubble sort problem</a> |
|
<a class="tablinks" onclick="openCity(event, 'find3')">Find 3rd largest number in an array</a> |
|
</div> |
|
|
|
</header> |
|
<!--header ends here--> |
|
|
|
<!--main starts here--> |
|
<main> |
|
|
|
<div class="alert alert-warning" role="alert"> |
|
<b>oops!</b> we're not connected to backend services at present. |
|
</div> |
|
|
|
<div> |
|
<div class="jumbotron "> |
|
|
|
<div class="welcome"> |
|
<h1 style="text-align: center;">Welcome to <code-base> </h1> |
|
<h3 style="text-align: center;">Tap |
|
<i class="start_button material-icons" style="font-size:50px; border-radius: 50%; color:antiquewhite; background-color: black; margin-top: 30px;"> |
|
chevron_right</i> |
|
on left side of screen to choose the code-problems.</h3> |
|
</div> |
|
|
|
<section id="palin" class="tabcontent"> |
|
<h2>Palindrome problem</h2> |
|
<p>A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed. Like 16461, for example, it is "symmetrical". The term palindromic is derived from palindrome, which refers to a word (such as rotor or racecar) whose spelling is unchanged when its letters are reversed. |
|
</p> |
|
<div class="terminal"> |
|
<button type="button" class="btn btn-light C">C</button> |
|
<button type="button" class="btn btn-warning Java">Java</button> |
|
<pre class="one"><code> |
|
#include <stdio.h> |
|
#include <string.h> |
|
|
|
// A function to check if a string str is palindrome |
|
void isPalindrome(char str[]) |
|
{ |
|
// Start from leftmost and rightmost corners of str |
|
int l = 0; |
|
int h = strlen(str) - 1; |
|
|
|
// Keep comparing characters while they are same |
|
while (h > l) |
|
{ |
|
if (str[l++] != str[h--]) |
|
{ |
|
printf("%s is Not Palindrome", str); |
|
return; |
|
} |
|
} |
|
printf("%s is palindrome", str); |
|
} |
|
|
|
// Driver program to test above function |
|
int main() |
|
{ |
|
isPalindrome("abba"); |
|
isPalindrome("abbccbba"); |
|
isPalindrome("geeks"); |
|
return 0; |
|
} |
|
|
|
|
|
</code></pre> |
|
<pre class="two"><code> |
|
class PalindromeExample{ |
|
public static void main(String args[]){ |
|
int r,sum=0,temp; |
|
int n=454;//It is the number variable to be checked for palindrome |
|
|
|
temp=n; |
|
while(n>0){ |
|
r=n%10; //getting remainder |
|
sum=(sum*10)+r; |
|
n=n/10; |
|
} |
|
if(temp==sum) |
|
System.out.println("palindrome number "); |
|
else |
|
System.out.println("not palindrome"); |
|
} |
|
} |
|
</code></pre> |
|
</div> |
|
</section> |
|
|
|
<section id="fibon" class="tabcontent"> |
|
<h2>Fibonacci series problem</h2> |
|
<p> In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.</p> |
|
<div class="terminal"> |
|
<button type="button" class="btn btn-light C">C</button> |
|
<button type="button" class="btn btn-warning Java">Java</button> |
|
<pre class="one"> |
|
<code> |
|
#include <stdio.h> |
|
int main() |
|
{ |
|
int n1=0,n2=1,n3,i,number; |
|
printf("Enter the number of elements:"); |
|
scanf("%d",&number); |
|
printf("\n%d %d",n1,n2);//printing 0 and 1 |
|
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed |
|
{ |
|
n3=n1+n2; |
|
printf(" %d",n3); |
|
n1=n2; |
|
n2=n3; |
|
} |
|
return 0; |
|
} |
|
</code> |
|
</pre> |
|
<pre class="two"><code> |
|
class FibonacciExample1{ |
|
public static void main(String args[]) |
|
{ |
|
int n1=0,n2=1,n3,i,count=10; |
|
System.out.print(n1+" "+n2);//printing 0 and 1 |
|
|
|
for(i=2;i&count;++i)//loop starts from 2 because 0 and 1 are already printed |
|
{ |
|
n3=n1+n2; |
|
System.out.print(" "+n3); |
|
n1=n2; |
|
n2=n3; |
|
} |
|
} |
|
} |
|
</code></pre> |
|
</div> |
|
</section> |
|
|
|
<section id="facto" class="tabcontent"> |
|
<h2>Factorial Number problem</h2> |
|
<p>Factorial is represented using '!', so five factorial will be written as (5!), n factorial as (n!). Also, n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.</p> |
|
<div class="terminal"> |
|
<button type="button" class="btn btn-light C">C</button> |
|
<button type="button" class="btn btn-warning Java">Java</button> |
|
<pre class="one"><code> |
|
#include <stdio.h> |
|
|
|
int main() |
|
{ |
|
int c, n, fact = 1; |
|
|
|
printf("Enter a number to calculate its factorial\n"); |
|
scanf("%d", &n); |
|
|
|
for (c = 1; c <= n; c++) |
|
fact = fact * c; |
|
|
|
printf("Factorial of %d = %d\n", n, fact); |
|
|
|
return 0; |
|
} |
|
</code></pre> |
|
<pre class="two"><code> |
|
import java.util.Scanner; |
|
|
|
class Factorial |
|
{ |
|
public static void main(String args[]) |
|
{ |
|
int n, c, fact = 1; |
|
|
|
System.out.println("Enter an integer to calculate it's factorial"); |
|
Scanner in = new Scanner(System.in); |
|
|
|
n = in.nextInt(); |
|
|
|
if (n > 0) |
|
System.out.println("Number should be non-negative."); |
|
else |
|
{ |
|
for (c = 1; c <= n; c++) |
|
fact = fact*c; |
|
|
|
System.out.println("Factorial of "+n+" is = "+fact); |
|
} |
|
} |
|
} |
|
</code></pre> |
|
</div> |
|
</section> |
|
|
|
<section id="bubbl" class="tabcontent"> |
|
<h2>Bubble sort problem</h2> |
|
<p>Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.</br> |
|
|
|
Example: |
|
First Pass:</br> |
|
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.</br> |
|
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4</br> |
|
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2</br> |
|
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.</br> |
|
|
|
Second Pass:</br> |
|
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )</br> |
|
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2</br> |
|
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</br> |
|
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</br> |
|
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.</br> |
|
Third Pass:</br> |
|
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</br> |
|
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</br> |
|
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</br> |
|
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )</p> |
|
<div class="terminal"> |
|
<button type="button" class="btn btn-light C">C</button> |
|
<button type="button" class="btn btn-warning Java">Java</button> |
|
<pre class="one"><code> |
|
/* Bubble sort code */ |
|
|
|
#include <stdio.h> |
|
|
|
int main() |
|
{ |
|
int array[100], n, c, d, swap; |
|
|
|
printf("Enter number of elements\n"); |
|
scanf("%d", &n); |
|
|
|
printf("Enter %d integers\n", n); |
|
|
|
for (c = 0; c < n; c++) |
|
scanf("%d", &array[c]); |
|
|
|
for (c = 0 ; c < n - 1; c++) |
|
{ |
|
for (d = 0 ; d < n - c - 1; d++) |
|
{ |
|
if (array[d] > array[d+1]) /* For decreasing order use < */ |
|
{ |
|
swap = array[d]; |
|
array[d] = array[d+1]; |
|
array[d+1] = swap; |
|
} |
|
} |
|
} |
|
|
|
printf("Sorted list in ascending order:\n"); |
|
|
|
for (c = 0; c < n; c++) |
|
printf("%d\n", array[c]); |
|
|
|
return 0; |
|
} |
|
</code></pre> |
|
<pre class="two"><code> |
|
public class BubbleSortExample { |
|
static void bubbleSort(int[] arr) { |
|
int n = arr.length; |
|
int temp = 0; |
|
for(int i=0; i < n; i++){ |
|
for(int j=1; j < (n-i); j++){ |
|
if(arr[j-1] > arr[j]){ |
|
//swap elements |
|
temp = arr[j-1]; |
|
arr[j-1] = arr[j]; |
|
arr[j] = temp; |
|
} |
|
|
|
} |
|
} |
|
|
|
} |
|
public static void main(String[] args) { |
|
int arr[] ={3,60,35,2,45,320,5}; |
|
|
|
System.out.println("Array Before Bubble Sort"); |
|
for(int i=0; i < arr.length; i++){ |
|
System.out.print(arr[i] + " "); |
|
} |
|
System.out.println(); |
|
|
|
bubbleSort(arr);//sorting array elements using bubble sort |
|
|
|
System.out.println("Array After Bubble Sort"); |
|
for(int i=0; i < arr.length; i++){ |
|
System.out.print(arr[i] + " "); |
|
} |
|
|
|
} |
|
} |
|
</code></pre> |
|
</div> |
|
</section> |
|
|
|
<section id="find3" class="tabcontent"> |
|
<h2>Find 3rd largest number in an array</h2> |
|
<p>We can find the third largest number in an array in java by sorting the array and returning the 3nd largest number. Let's see the full example to find the third largest number in java array.</p> |
|
<div class="terminal"> |
|
<button type="button" class="btn btn-light C">C</button> |
|
<button type="button" class="btn btn-warning Java">Java</button> |
|
<pre class="one"><code> |
|
/*program to print 3 rd greatest number*/ |
|
|
|
#include <stdio.h> |
|
#include <conio.h> |
|
main( ) |
|
{ |
|
int a[20],i,j,n,t,b; |
|
printf("the value of n:"); |
|
scanf("%d\n",&n); |
|
printf("enter all array elements to be sort\n"); |
|
for(i=0;i<n;i++) |
|
{ |
|
scanf("%d",&a[i]); |
|
} |
|
for(i=n-1;i>=0;i--) |
|
{ |
|
for(j=0;j<i;j++) |
|
{ |
|
if(a[j]>a[j+1]) |
|
{ |
|
t=a[j]; |
|
a[j]=a[j+1]; |
|
a[j+1]=t; |
|
} |
|
} |
|
} |
|
for(i=0;i<n;i++) |
|
{ |
|
printf("\t%d\",a[i]); |
|
} |
|
b=a[n-3]; |
|
printf("the third greatest number is\n"); |
|
printf("%d",b); |
|
getch( ); |
|
} |
|
</code></pre> |
|
<pre class="two"><code> |
|
public class ThirdLargestInArrayExample{ |
|
public static int getThirdLargest(int[] a, int total){ |
|
int temp; |
|
for (int i = 0; i < total; i++) |
|
{ |
|
for (int j = i + 1; j < total; j++) |
|
{ |
|
if (a[i] > a[j]) |
|
{ |
|
temp = a[i]; |
|
a[i] = a[j]; |
|
a[j] = temp; |
|
} |
|
} |
|
} |
|
return a[total-3]; |
|
} |
|
public static void main(String args[]){ |
|
int a[]={1,2,5,6,3,2}; |
|
int b[]={44,66,99,77,33,22,55}; |
|
System.out.println("Third Largest: "+getThirdLargest(a,6)); |
|
System.out.println("Third Largest: "+getThirdLargest(b,7)); |
|
}} |
|
</code></pre> |
|
</div> |
|
</section> |
|
|
|
</div> |
|
</div> |
|
<i class="animated infinite rubberBand side-button material-icons" style="font-size:30px;cursor:pointer" onclick="openNav()"> |
|
chevron_right</i> |
|
</main> |