Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Factorial of a number #529

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
37 changes: 37 additions & 0 deletions Programming/C++/Insertionsort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a few folder like InsertionSort inside the C++ directory and then place the Insertionsort.cpp file inside of it.

using namespace std;
//Insertion sort
int main(){

int n,comp=0,swap=0;
cout<<"Enter the no. of elements: ";
cin>>n;


int arr[n];
cout<<"Enter the unsorted array: ";
for(int i=0;i<n;i++){
cin>>arr[i];
}

for(int i=1;i<n;i++){
int current=arr[i];
int j=i-1;
while(arr[j]>current && j>=0){
arr[j+1]=arr[j];
j--;
comp++;
}
arr[j+1]=current;
swap++;
}

cout<<"Yor sorted array: [";
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<"]";



}
22 changes: 22 additions & 0 deletions Programming/Java/Factorial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.*;
public class Factorial {
public static void main(String[] args){
int num;
System.out.println("Enter the number to get factorial: ");
Scanner reader= new Scanner(System.in);
num=reader.nextInt();
int factorial=1;
if(num>0) {
for(int i=1;i<=num;i++){
factorial*=i;
}
System.out.println("Factorial of "+num+" is "+factorial);
}
else if(num<0){
System.out.println("Not Defined ");
}
else{
System.out.println(" factorial is 0 ");
}
}
}