Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Happy number using c #833

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions C/HappyNumberInC.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

#include <stdio.h>

//Function to check if number happy number or not
int CheckHappyNo(int number){
int Remender = 0, total = 0;

//Compute the total of squares of digits
while(number > 0){
Remender = number%10;
total = total + (Remender*Remender);
number = number/10;
}
return total;
}

int main()
{
int number ;
printf("Enter the number \n");
scanf("%d",&number);

int outcome = number;

while(outcome != 1 && outcome != 4){
outcome = CheckHappyNo(outcome);
}

//The HapyNum alwys ends with 1
if(outcome == 1)
printf("This number is a Happy Number");
//The number which is not HappyNum ends in a cycle of numbers repeating contains 4
else if(outcome == 4)
printf("This number isn't a Happy Number");

return 0;
}

/*

OUTPUT:

Enter the number
85
This number isn't a Happy Number

*****************************************************************
Enter the number
1
This number is a Happy Number

Input : A number
Output: Whether is Happy number or not
Algorithm: C program to check whether a number is Happy Number or not


Time Complexity: O(n)
Space Complexity: O(n)

*/
2 changes: 2 additions & 0 deletions C/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Format:

-[FCFS ](fcfs.c)

-[Find if given number is a Happy Number or Not](HappyNumberInC.c)

-[Find nth term in an A.P.](AP.c)

-[Find the middle element of linked list](middle.c)
Expand Down
101 changes: 101 additions & 0 deletions Java/Algorithms/Merge2Arrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

//JAVA Code for Merging 2 Sorted Arrays

import java.util.Scanner;

public class Merge2Arrays
{

public static void mergeArrays(int[] ary1, int[] ary2, int capacity1,
int capacity2, int[] ary3)
{

int a = 0, b = 0, c = 0;

// Traverse both array
while (a<capacity1 && b <capacity2)
{
//if first element of first array is smaaller than 2nd element,
//then keep as it and increment first array else store in C
if (ary1[a] < ary2[b])
ary3[c++] = ary1[a++];
else
ary3[c++] = ary2[b++];
}

// Assign remaining elements1 of second array if remain at last
while (a < capacity1)
ary3[c++] = ary1[a++];

// Assign remaining elements1 of second array if remain at last
while (b < capacity2)
ary3[c++] = ary2[b++];
}

public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);

int elements1;

System.out.print("Enter the number of elements1 you want to store: ");
elements1=sc.nextInt();
int[] ary1 = new int[elements1];

System.out.println("Enter your 1st Sorted Array ");

for(int a=0; a<elements1; a++)
{
//taking 1st arrayuser input
ary1[a]=sc.nextInt();
}

int elements2;
System.out.print("Enter the number of elements1 you want to store in 2 nd array ");
elements2=sc.nextInt();

int[] ary2 = new int[elements2];
System.out.println("Enter your 2nd Sorted Array ");

for(int a=0; a<elements2; a++)
{
//taking 2 nd array from user
ary2[a]=sc.nextInt();
}



int[] ary3 = new int[elements1+elements2];

//function call
mergeArrays(ary1, ary2, elements1, elements2, ary3);

System.out.println("After merging Array1 and Array2 both");
for (int a=0; a < elements1+elements2; a++)
System.out.print(ary3[a] + " ");
}
}

/*
Sample INPUT and OUTPUT:

Enter the number of elements1 you want to store: 4
Enter your 1st Sorted Array
7 9 11 13

Enter the number of elements1 you want to store in 2 nd array 4
Enter your 2nd Sorted Array
10 12 14 16

After merging Array1 and Array2 both
7 9 10 11 12 13 14 16

Input : Two Sorted Arrays
Output: Merging Both the arrays
Algorithm: JAVA code to merge 2 sorted arrays


Time Complexity: O(n)
Space Complexity: O(n)

*/
2 changes: 2 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ Format: -[Program name](package/name of the file)

-[Merge Sort Java](Sorting/MergeSort.java)

-[Merge Two Sorted Arrays using JAVA](Algorithms/Merge2Arrays.java)

-[Minimum number of denominations using Greedy algo approach](Algorithms/MinDenominations_GreedyAlgo.java)

-[Nth term of ap](Algorithms/nth_term_of_ap.java)
Expand Down