diff --git a/01knapsack(A.V).cpp b/01knapsack(A.V).cpp new file mode 100644 index 00000000..ada47d6a --- /dev/null +++ b/01knapsack(A.V).cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +int max(int a, int b) +{ + return (a > b) ? a : b; +} +int knapsack(int wt[], int val[], int W, int N, int** tb) + { + if(N<=0 || W==0) + { + return 0; + } + if(tb[N][W]!=-1) + { //Memoization + return tb[N][W]; + } + + if(wt[N-1]<=W) + { + return tb[N][W]=max(val[N-1] + knapsack(wt, val, W-wt[N-1],N-1, tb), knapsack(wt, val, W, N-1, tb)); + } + else if(wt[N-1]>W) + { + return tb[N][W]=knapsack(wt, val, W, N-1, tb); + } + } + int knapSackR(int W, int wt[], int val[], int n) + { + // double pointer to declare the + // table dynamically + int** dp; + dp = new int*[n]; + + // loop to create the table dynamically + for (int i = 0; i < n; i++) + dp[i] = new int[W + 1]; + + // loop to initially filled the + // table with -1 + for (int i = 0; i < n; i++) + for (int j = 0; j < W + 1; j++) + dp[i][j] = -1; + return knapsack(wt, val, W, n, dp); +} +int main(){ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + int t,N,W,x; + cin>>t; //t=[1,100] + while(t--) + { + cin>>N; //N=[1,1000] + cin>>W; //W=[1,1000] + int wt[W]; //wt[i]=[1,1000] + int val[N]; //val[i]=[1,1000] + + for (int i = 0; i >x; + val[i]=x; + } + + for (int i = 0; i >x; + wt[i]=x; + } + int n = sizeof(val) / sizeof(val[0]); + cout << knapSackR(W, wt, val, n); + } + } + + //top down approach + #include +using namespace std; + + +void knap(int n, int w, int val[],int weight[]) +{ + int t[n+1][w+1]; + + + //fill 0th rows and coloumns with zero. + for(int i=0;i<=n;i++) + t[i][0]=0; + + for(int j=0;j<=w;j++) + t[0][j]=0; + + + for(int i=1;i<=n;i++) + { + for(int j=1;j<=w;j++) + { + if(j>y; +while(y--) +{ int n; cin>>n; + int w; cin>>w; + int val[n];int weight[n]; + + for(int i=1;i<=n;i++) + cin>>val[i]; + + for(int i=1;i<=n;i++) + cin>>weight[i]; + + knap(n,w,val,weight); + +} +return 0; +} \ No newline at end of file diff --git a/2D Matrices using C b/2D Matrices using C new file mode 100644 index 00000000..136681df --- /dev/null +++ b/2D Matrices using C @@ -0,0 +1,40 @@ +#include +int main() { + int r, c, a[100][100], b[100][100], sum[100][100], i, j; + printf("Enter the number of rows (between 1 and 100): "); + scanf("%d", &r); + printf("Enter the number of columns (between 1 and 100): "); + scanf("%d", &c); + + printf("\nEnter elements of 1st matrix:\n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("Enter element a%d%d: ", i + 1, j + 1); + scanf("%d", &a[i][j]); + } + + printf("Enter elements of 2nd matrix:\n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("Enter element a%d%d: ", i + 1, j + 1); + scanf("%d", &b[i][j]); + } + + // adding two matrices + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + sum[i][j] = a[i][j] + b[i][j]; + } + + // printing the result + printf("\nSum of two matrices: \n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("%d ", sum[i][j]); + if (j == c - 1) { + printf("\n\n"); + } + } + + return 0; +} diff --git a/3D.java b/3D.java new file mode 100644 index 00000000..7344fbb4 --- /dev/null +++ b/3D.java @@ -0,0 +1,49 @@ +public static void main(String[] args) { + Scanner getIt = new Scanner(System.in); + System.out.println("Input 1 integer n: "); + int n = getIt.nextInt(); + if (n > 0){ + final double M[][][] = new double[n][n][n]; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + for (int k = 0; k < n; k++) + M[i][j][k] = 3.0; + System.out.println("Input 1 integer p: "); + int p = getIt.nextInt(); + if(p > 0){ + for(int q = 1; q <= p; q++){ + for (int i = 0; i < n; i++){ + for (int j = 0; j < n; j++){ + for (int k = 0; k < n; k++){ + System.out.printf("%f ", Math.pow(matrixMult(M[i], M[i])[j][k], q)); + } + System.out.println("\n"); + } + + } + System.out.println("xxxxxxxxxxxxxxxxxxxxx"); + } + } + else{ + System.out.println("Woops, you entered a negative p."); + } + } + else{ + System.out.println("Woops, you entered a negative n."); + } +} + +public static double[][] matrixMult(double a[][], double b[][]) { + int aRows = a.length; + int aCols = a[0].length; + int bCols = b[0].length; + double[][] result = new double[aRows][bCols]; + for(int i = 0; i < aRows; i++){ + for(int j = 0; j < bCols; j++){ + for(int k = 0; k < aCols; k++){ + result[i][j] += a[i][k] * b[k][j]; + } + } + } + return result; +} diff --git a/3_matrix_multiplication_using_numpy.py b/3_matrix_multiplication_using_numpy.py new file mode 100644 index 00000000..03129fbb --- /dev/null +++ b/3_matrix_multiplication_using_numpy.py @@ -0,0 +1,23 @@ +import numpy as np + +np.random.seed(42) + +A = np.random.randint(0, 10, size=(2,2)) + +B = np.random.randint(0, 10, size=(2,3)) + +C = np.random.randint(0, 10, size=(3,3)) + +print("Matrix A is:\n{}, shape={}\n".format(A, A.shape)) + +print("Matrix B is:\n{}, shape={}\n".format(B, B.shape)) + +print("Matrix C is:\n{}, shape={}\n".format(C, C.shape)) + +D = np.matmul(np.matmul(A,B), C) + +print("Multiplication in the order (AB)C:\n\n{},shape={}\n".format(D, D.shape)) + +D = np.matmul(A, np.matmul(B,C)) + +print("Multiplication in the order A(BC):\n\n{},shape={}".format(D, D.shape)) diff --git a/A C++ Program to print ASCII value of given character b/A C++ Program to print ASCII value of given character new file mode 100644 index 00000000..3dc975bb --- /dev/null +++ b/A C++ Program to print ASCII value of given character @@ -0,0 +1,8 @@ +#include +using namespace std; +int main() +{ + char c = 'A'; + cout << "The ASCII value of " << c << " is " << int(c); + return 0; +} diff --git a/A Program for 2D Matrices b/A Program for 2D Matrices new file mode 100644 index 00000000..136681df --- /dev/null +++ b/A Program for 2D Matrices @@ -0,0 +1,40 @@ +#include +int main() { + int r, c, a[100][100], b[100][100], sum[100][100], i, j; + printf("Enter the number of rows (between 1 and 100): "); + scanf("%d", &r); + printf("Enter the number of columns (between 1 and 100): "); + scanf("%d", &c); + + printf("\nEnter elements of 1st matrix:\n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("Enter element a%d%d: ", i + 1, j + 1); + scanf("%d", &a[i][j]); + } + + printf("Enter elements of 2nd matrix:\n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("Enter element a%d%d: ", i + 1, j + 1); + scanf("%d", &b[i][j]); + } + + // adding two matrices + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + sum[i][j] = a[i][j] + b[i][j]; + } + + // printing the result + printf("\nSum of two matrices: \n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("%d ", sum[i][j]); + if (j == c - 1) { + printf("\n\n"); + } + } + + return 0; +} diff --git a/A star.py b/A star.py new file mode 100644 index 00000000..a332af0d --- /dev/null +++ b/A star.py @@ -0,0 +1,116 @@ +class Vertex: + def __init__(self, name, heuristic): + self.name = name + self.heuristic = heuristic + self.neighbors = [] + self.visited = 0 + + def add_neighbor(self, vertex): + if vertex not in self.neighbors: + self.neighbors.append(vertex) + self.neighbors.sort() + +class Graph: + vertices = {} + + def add_vertex(self, vertex): + if isinstance(vertex, Vertex) and vertex.name not in self.vertices: + self.vertices[vertex.name] = vertex + return True + return False + + def add_edge(self, u, v, d): + if u in self.vertices and v in self.vertices: + for key, value in self.vertices.items(): + if key == u: + value.add_neighbor([v, d]) + if key == v: + value.add_neighbor([u, d]) + return True + return False + + def priority(self, fx): + val = [] + for node in fx: + val.append(node[1] + self.vertices[node[0]].heuristic) + return self.vertices[fx[val.index(min(val))][0]] + + def cost(self, node, fx): + for i in fx: + if node.name == i[0]: + return i[1] + + def astar(self, start, end): + ol = [] + cl = [] + gx = 0 + fstr = '' + + ol.append(start) + while ol != []: + fx = [] + temp = ol.pop(0) + temp.visited = 1 + if temp.name == end.name: + cl.append(end.name) + break + cl.append(temp.name) + + for neighbor in temp.neighbors: + if self.vertices[neighbor[0]].visited == 0: + fx.append(neighbor) + ol.append(self.priority(fx)) + gx += self.cost(ol[0], fx) + for node in range(len(cl)): + if node == len(cl) - 1: + fstr += cl[node] + break + fstr += cl[node] + ' --> ' + print('Path to goal state is', fstr, 'with cost of', gx, '.') + +if __name__=='__main__': + graph = Graph() + + #Example 1: + a = Vertex('A', 14) + b = Vertex('B', 12) + c = Vertex('C', 11) + d = Vertex('D', 6) + e = Vertex('E', 4) + f = Vertex('F', 11) + g = Vertex('G', 0) + + graph.add_vertex(a) + graph.add_vertex(b) + graph.add_vertex(c) + graph.add_vertex(d) + graph.add_vertex(e) + graph.add_vertex(f) + graph.add_vertex(g) + + edges = [['AB', 4], ['AC', 3], ['CD', 7], ['CE', 10], ['DE', 2], ['BE', 12], ['BF', 6], ['FG', 16], ['EG', 5]] + for edge in edges: + graph.add_edge(edge[0][:1], edge[0][1:], edge[1]) + + graph.astar(a, g) + print('\n') + + + #Example 2: + p = Vertex('P', 7) + q = Vertex('Q', 6) + r = Vertex('R', 2) + s = Vertex('S', 1) + t = Vertex('T', 0) + + graph.add_vertex(p) + graph.add_vertex(q) + graph.add_vertex(r) + graph.add_vertex(s) + graph.add_vertex(t) + + edges = [['PQ', 1], ['PR', 4], ['QR', 2], ['QS', 5], ['RS', 2], ['QT', 12], ['ST', 3]] + for edge in edges: + graph.add_edge(edge[0][:1], edge[0][1:], edge[1]) + + graph.astar(p, t) \ No newline at end of file diff --git a/A string is palindrome or not b/A string is palindrome or not new file mode 100644 index 00000000..5dac8be4 --- /dev/null +++ b/A string is palindrome or not @@ -0,0 +1,28 @@ +#include +#include + +int main(){ + char string1[20]; + int i, length; + int flag = 0; + + printf("Enter a string:"); + scanf("%s", string1); + + length = strlen(string1); + + for(i=0;i < length ;i++){ + if(string1[i] != string1[length-i-1]){ + flag = 1; + break; + } +} + + if (flag) { + printf("%s is not a palindrome", string1); + } + else { + printf("%s is a palindrome", string1); + } + return 0; +} diff --git a/ASCCI Value of Character b/ASCCI Value of Character new file mode 100644 index 00000000..7b99efbb --- /dev/null +++ b/ASCCI Value of Character @@ -0,0 +1,10 @@ +Using Kotlin Language + +fun main(args: Array) { + val c = 'a' + val ascii = c.toInt() + println("The ASCII value of $c is: $ascii") +} + +Output +The ASCII Value of a is : 65 diff --git a/ASCII b/ASCII new file mode 100644 index 00000000..e84fc638 --- /dev/null +++ b/ASCII @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main() +{ + char x; + cin>>x; + + cout<<"the ASCII value of "< +int main() { + char c; + printf("Enter a character: "); + scanf("%c", &c); + + // %d displays the integer value of a character + // %c displays the actual character + printf("ASCII value of %c = %d", c, c); + + return 0; +} diff --git a/ASCII Values b/ASCII Values new file mode 100644 index 00000000..b1a25d84 --- /dev/null +++ b/ASCII Values @@ -0,0 +1,7 @@ +int main() +{ + char a; + prinf("Enter the character = "); + scanf("%c",&a); + printf("The Ascii value of Character %c = %d",a,a); +} diff --git a/ASCII value b/ASCII value new file mode 100644 index 00000000..087d70d5 --- /dev/null +++ b/ASCII value @@ -0,0 +1,3 @@ +inp = str(input("Enter the character ")) + +print(f"ASCII value of {inp} is "+str(ord(inp))) diff --git a/ASCII value of a character b/ASCII value of a character new file mode 100644 index 00000000..e84fc638 --- /dev/null +++ b/ASCII value of a character @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main() +{ + char x; + cin>>x; + + cout<<"the ASCII value of "< +int main() { + char c; + printf("Enter a character: "); + scanf("%c", &c); + printf("ASCII value of %c = %d", c, c); + return 0; +} diff --git a/ASCII_Java b/ASCII_Java new file mode 100644 index 00000000..89910c95 --- /dev/null +++ b/ASCII_Java @@ -0,0 +1,12 @@ +// Java program to print +// ASCII Value of Character +public class AsciiValue { + + public static void main(String[] args) + { + + char c = 'e'; + int ascii = c; + System.out.println("The ASCII value of " + c + " is: " + ascii); + } +} diff --git a/Add two 2D matrix b/Add two 2D matrix new file mode 100644 index 00000000..18ba14f8 --- /dev/null +++ b/Add two 2D matrix @@ -0,0 +1,33 @@ +#include + +int main() { +int a[3][3],b[3][3],c[3][3],i,j; +char n; +printf ("\nenter 9 element of matrix1"); +for(i=0;i<3;i++) +{ + for(j=0;j<3;j++) + { + scanf ("%d",&a[i][j]); + } +} +printf ("\nenter 9 element of matrix2"); +for(i=0;i<3;i++) +{ + for(j=0;j<3;j++) + { + scanf ("%d",&b[i][j]); + } +} +printf ("\nadditon is:"); +for (i=0;i<3;i++) +{ + for(j=0;j<3;j++) + { + c[i][j]=a[i][j]+b[i][j]; + printf ("%d",c[i][j]); + } + printf ("\n"); +} +return 0; +} diff --git a/Adding two matrices using python b/Adding two matrices using python new file mode 100644 index 00000000..24bbb99e --- /dev/null +++ b/Adding two matrices using python @@ -0,0 +1,18 @@ +# taking input for size of matrix1 row,column +n1,m1 = map(int,input().split()) +# taking matrix1 input +l1 = [list(map(int,input().split())) for i in range(n1)] +# taking input for size of matrix2 row,column +n2,m2 = map(int,input().split()) +# taking matrix2 input +l2 = [list(map(int,input().split())) for i in range(n2)] +if n1!=n2 or m1!=m2: + print("Matrices can't be added") +else: + # adding elments of matrices + for i in range(n1): + for j in range(m1): + l1[i][j] += l2[i][j] + # print updated matrix + for i in l1: + print(*i) diff --git a/Addition of two matrix b/Addition of two matrix index 0986763b..89a2fc40 100644 --- a/Addition of two matrix +++ b/Addition of two matrix @@ -1,32 +1,39 @@ -#include - -int main() -{ - int m, n, c, d, first[10][10], second[10][10], sum[10][10]; - - printf("Enter the number of rows and columns of matrix\n"); - scanf("%d%d", &m, &n); - printf("Enter the elements of first matrix\n"); - - for (c = 0; c < m; c++) - for (d = 0; d < n; d++) - scanf("%d", &first[c][d]); - - printf("Enter the elements of second matrix\n"); - - for (c = 0; c < m; c++) - for (d = 0 ; d < n; d++) - scanf("%d", &second[c][d]); - - printf("Sum of entered matrices:-\n"); - - for (c = 0; c < m; c++) { - for (d = 0 ; d < n; d++) { - sum[c][d] = first[c][d] + second[c][d]; - printf("%d\t", sum[c][d]); - } - printf("\n"); - } - - return 0; -} +#include +#define N 4 + +// This function adds A[][] and B[][], and stores +// the result in C[][] +void add(int A[][N], int B[][N], int C[][N]) +{ + int i, j; + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + C[i][j] = A[i][j] + B[i][j]; +} + +int main() +{ + int A[N][N] = { {1, 1, 1, 1}, + {2, 2, 2, 2}, + {3, 3, 3, 3}, + {4, 4, 4, 4}}; + + int B[N][N] = { {1, 1, 1, 1}, + {2, 2, 2, 2}, + {3, 3, 3, 3}, + {4, 4, 4, 4}}; + + int C[N][N]; // To store result + int i, j; + add(A, B, C); + + printf("Result matrix is \n"); + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + printf("%d ", C[i][j]); + printf("\n"); + } + + return 0; +} diff --git a/All Stack Operations b/All Stack Operations new file mode 100644 index 00000000..dcb66264 --- /dev/null +++ b/All Stack Operations @@ -0,0 +1,169 @@ +#include +#include +using namespace std; + +class stack{ + private: + int top; + int arr[5]; + public: + stack(){ + top=-1; + for(int i=0;i<5;i++){ + arr[i]=0; + } + } + + bool isEmpty(){ + if(top==-1) + return true; + else + return false; + } + + bool isFull(){ + if (top==4) + return true; + else + return false; + } + + + void push(int val){ + + if(isFull()){ + cout<<"stack full"; + } + else{ + + top++; + arr[top] =val; + } + } + + int pop(){ + + if(isEmpty()){ + cout<<"stack is empty"; + return 0; + } + else{ + int popvalue=arr[top]; + arr[top]=0; + top--; + return popvalue; + } + } + + int coun(){ + return (top+1); + } + + int peek(int pos){ + + if(isEmpty()){ + cout<<"stack empty<=0;i--){ + cout<>option; + switch(option){ + case 0: + break; + case 1: + cout<<"enter value to push"<>value; + s1.push(value); + break; + case 2: + cout<<"pop function called..."<>position; + cout<<"peek function called...value at position"<>position; + cout<>value; + s1.change(position,value); + break; + case 8: + cout<<"display function called"< +#define ll long long +using namespace std; +void solve(int arr[],int n,int k,int index) +{ + + int t=n/k; + while(t--) + { + + + arr[index]=0; + index=(index + (k))%n; + + + } +} +int main() +{ + int t=0; + cin>>t; + while(t--) + { + int n=0; + int k=0,x=0,y=0; + cin>>n>>k>>x>>y; + int arr[n]; + + + if(arr[y-1]==0) + cout<<"YES"; + else + cout<<"NO"; +cout<<"\n"; + + } + return 0; +} diff --git a/Arithmetic calc b/Arithmetic calc new file mode 100644 index 00000000..5962d09b --- /dev/null +++ b/Arithmetic calc @@ -0,0 +1,16 @@ +num1 = int(input('Enter First number: ')) +num2 = int(input('Enter Second number ')) +add = num1 + num2 +dif = num1 - num2 +mul = num1 * num2 +div = num1 / num2 +floor_div = num1 // num2 +power = num1 ** num2 +modulus = num1 % num2 +print('Sum of ',num1 ,'and' ,num2 ,'is :',add) +print('Difference of ',num1 ,'and' ,num2 ,'is :',dif) +print('Product of' ,num1 ,'and' ,num2 ,'is :',mul) +print('Division of ',num1 ,'and' ,num2 ,'is :',div) +print('Floor Division of ',num1 ,'and' ,num2 ,'is :',floor_div) +print('Exponent of ',num1 ,'and' ,num2 ,'is :',power) +print('Modulus of ',num1 ,'and' ,num2 ,'is :',modulus) diff --git a/Armstrong b/Armstrong new file mode 100644 index 00000000..3f130bf0 --- /dev/null +++ b/Armstrong @@ -0,0 +1,25 @@ +#include +#include +void main() +{ +int p,t,n,r,c=0,s=0; +printf("enter the range"); +scanf("%d",&p); +p=n; +t=n; +while(p!=0) +{ +p=p/10; +c++; +} +while(t!=0) +{ +r=t%10; +t=t/10; +s=s+pow(r,c); +} +if(s==n) +printf("armstrong"); +else +printf("not armstrong"); +} diff --git a/Armstrong Number in C b/Armstrong Number in C new file mode 100644 index 00000000..e2fd95a4 --- /dev/null +++ b/Armstrong Number in C @@ -0,0 +1,19 @@ +#include + int main() +{ +int n,r,sum=0,temp; +printf("enter the number="); +scanf("%d",&n); +temp=n; +while(n>0) +{ +r=n%10; +sum=sum+(r*r*r); +n=n/10; +} +if(temp==sum) +printf("armstrong number "); +else +printf("not armstrong number"); +return 0; +} diff --git a/Armstrong_number b/Armstrong_number new file mode 100644 index 00000000..a3c2d76d --- /dev/null +++ b/Armstrong_number @@ -0,0 +1,19 @@ +#include + int main() +{ +int n,r,sum=0,temp; +printf("enter the number="); +scanf("%d",&n); +temp=n; +while(n>0) +{ +r=n%10; +sum=sum+(r*r*r); +n=n/10; +} +if(temp==sum) +printf("armstrong number "); +else +printf("not armstrong number"); +return 0; +} diff --git a/Array Reversed b/Array Reversed new file mode 100644 index 00000000..f21e7916 --- /dev/null +++ b/Array Reversed @@ -0,0 +1,19 @@ +#include +int main() +{ + +int n; +scanf(“%d”,&n); +int arr[n]; +int i; +for(i = 0; i < n; i++) +{ +scanf(“%d”,&arr[i]); +} +printf(“Reversed array is:\n”); +for(i = n-1; i >= 0; i–) +{ +printf(“%d\n”,arr[i]); +} +return 0; +} diff --git a/Array program in c b/Array program in c new file mode 100644 index 00000000..4e02a823 --- /dev/null +++ b/Array program in c @@ -0,0 +1,20 @@ +#include +int main() +{ + int array[100], n, c; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d elements\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("The array elements are:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", array[c]); + + return 0; +} diff --git a/Array_Smallest_Element b/Array_Smallest_Element new file mode 100644 index 00000000..4e665c58 --- /dev/null +++ b/Array_Smallest_Element @@ -0,0 +1,17 @@ +#include + +int main() { + int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; + int loop, smallest; + + smallest = array[0]; + + for(loop = 1; loop < 10; loop++) { + if( smallest > array[loop] ) + smallest = array[loop]; + } + + printf("Smallest element of array is %d", smallest); + + return 0; +} diff --git a/Arrays: Left Rotation b/Arrays: Left Rotation new file mode 100644 index 00000000..ade267a3 --- /dev/null +++ b/Arrays: Left Rotation @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(){ +int x,d,n,i,a[1000000],j; + +scanf("%d",&n); + +scanf("%d",&d); + +for(i=0;i +using namespace std; + +// A recursive binary search function. It returns +// location of x in given array arr[l..r] is present, +// otherwise -1 +int binarySearch(int arr[], int l, int r, int x) +{ + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the middle + // itself + if (arr[mid] == x) + return mid; + + // If element is smaller than mid, then + // it can only be present in left subarray + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + // Else the element can only be present + // in right subarray + return binarySearch(arr, mid + 1, r, x); + } + + // We reach here when element is not + // present in array + return -1; +} + +int main(void) +{ + int arr[] = { 2, 3, 4, 10, 40 }; + int x = 10; + int n = sizeof(arr) / sizeof(arr[0]); + int result = binarySearch(arr, 0, n - 1, x); + (result == -1) ? cout << "Element is not present in array" + : cout << "Element is present at index " << result; + return 0; +} diff --git a/BUBBLE SORT b/BUBBLE SORT new file mode 100644 index 00000000..97a6f877 --- /dev/null +++ b/BUBBLE SORT @@ -0,0 +1,34 @@ +#include +void swap(int *k, int *l); +void main(){ + + int a[100],i,j,n,temp; + printf("enter number of elements"); + scanf("%d",&n); + for (i = 0; i < 5 ; i++) + { + scanf("%d",&a[i]); + } + for(i=0;ia[j+1]) + { + swap(&a[j],&a[j+1]); + } + + } + } + for (j = 0; j < n ; j++) + { + printf("%d ",a[j]); + } + +} +void swap(int *k,int *l){ + int temp; + temp=*k; + *k=*l; + *l=temp; +} diff --git a/Balanced Parenthesis by using Stacks b/Balanced Parenthesis by using Stacks new file mode 100644 index 00000000..2a81a5b4 --- /dev/null +++ b/Balanced Parenthesis by using Stacks @@ -0,0 +1,94 @@ +/* + * C++ Program to Check for balanced parenthesis by using Stacks + */ +#include +#include +using namespace std; +struct node +{ + char data; + node *next; +}*p = NULL, *top = NULL, *save = NULL,*ptr; +void push(char x) +{ + p = new node; + p->data = x; + p->next = NULL; + if (top == NULL) + { + top = p; + } + else + { + save = top; + top = p; + p->next = save; + } +} +char pop() +{ + if (top == NULL) + { + cout<<"underflow!!"; + } + else + { + ptr = top; + top = top->next; + return(ptr->data); + delete ptr; + } +} +int main() +{ + int i; + char c[30], a, y, z; + cout<<"enter the expression:\n"; + cin>>c; + for (i = 0; i < strlen(c); i++) + { + if ((c[i] == '(') || (c[i] == '{') || (c[i] == '[')) + { + push(c[i]); + } + else + { + switch(c[i]) + { + case ')': + a = pop(); + if ((a == '{') || (a == '[')) + { + cout<<"invalid expr!!"; + getch(); + } + break; + case '}': + y = pop(); + if ((y == '[') || (y == '(')) + { + cout<<"invalid expr!!"; + getch(); + } + break; + case ']': + z = pop(); + if ((z == '{') || (z == '(')) + { + cout<<"invalid expr!!"; + getch(); + } + break; + } + } + } + if (top == NULL) + { + cout<<"balanced expr!!"; + } + else + { + cout<<"string is not valid.!!"; + } + getch(); +} diff --git a/Best First Search.py b/Best First Search.py new file mode 100644 index 00000000..c0b16014 --- /dev/null +++ b/Best First Search.py @@ -0,0 +1,119 @@ +class Vertex: + def __init__(self, name, heuristic): + self.name = name + self.hr = heuristic + self.neighbors = [] + self.visited = 0 + + def add_neighbor(self, v): + if v not in self.neighbors: + self.neighbors.append(v) + self.neighbors.sort() + +class Graph: + vertices = {} + + def add_vertex(self, vertex): + if isinstance(vertex, Vertex) and vertex.name not in self.vertices: + self.vertices[vertex.name] = vertex + return True + return False + + def add_edge(self, u, v): + if u in self.vertices and v in self.vertices: + for key, value in self.vertices.items(): + if key == u: + value.add_neighbor(v) + if key == v: + value.add_neighbor(u) + return True + return False + + def priority(self, queue): + rank = [] + for i in queue: + for j in self.vertices: + if i.name == j and i.visited == 0: + rank.append(i.hr) + rank.sort() + queue = [] + for i in rank: + for j in self.vertices: + if i == self.vertices[j].hr: + queue.append(self.vertices[j]) + return queue + + def BestFirstSearch(self, start, end): + ol = [] + cl = [] + + ol.append(start) + while ol != []: + temp = ol.pop(0) + temp.visited = 1 + if temp.name == end.name: + cl.append(end.name) + break + cl.append(temp.name) + + queue = [] + queue += [self.vertices[i] for i in temp.neighbors] + ol += queue + ol = self.priority(ol) + return cl + +if __name__=='__main__': + graph = Graph() + """ + #Example 1: + + a = Vertex('A', 10) + b = Vertex('B', 2) + c = Vertex('C', 1) + d = Vertex('D', 5) + e = Vertex('E', 6) + f = Vertex('F', 0) + g = Vertex('G', 7) + + graph.add_vertex(a) + graph.add_vertex(b) + graph.add_vertex(c) + graph.add_vertex(d) + graph.add_vertex(e) + graph.add_vertex(f) + graph.add_vertex(g) + + edges = ['AB', 'AC', 'AD', 'BE', 'CF', 'DG', 'EF', 'GF'] + for edge in edges: + graph.add_edge(edge[:1], edge[1:]) + """ + + #Example 2: + + a = Vertex('A', 80) + b = Vertex('B', 50) + c = Vertex('C', 40) + d = Vertex('D', 20) + e = Vertex('E', 30) + f = Vertex('F', 0) + + graph.add_vertex(a) + graph.add_vertex(b) + graph.add_vertex(c) + graph.add_vertex(d) + graph.add_vertex(e) + graph.add_vertex(f) + + edges = ['AB', 'AC', 'AE', 'BF', 'CE', 'BD', 'DF', 'EF'] + for edge in edges: + graph.add_edge(edge[:1], edge[1:]) + + path = graph.BestFirstSearch(a, f) + + fstr = '' + for node in range(len(path)): + if node == len(path) - 1: + fstr += path[node] + break + fstr += path[node] + ' --> ' + print(fstr) \ No newline at end of file diff --git a/BinartToDecimal.cpp b/BinartToDecimal.cpp new file mode 100644 index 00000000..c5495ce8 --- /dev/null +++ b/BinartToDecimal.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +int convertBinaryToDecimal(long long); + +int main() +{ + long long n; + + cout << "Enter a binary number: "; + cin >> n; + + cout << n << " in binary = " << convertBinaryToDecimal(n) << "in decimal"; + return 0; +} + +int convertBinaryToDecimal(long long n) +{ + int decimalNumber = 0, i = 0, remainder; + while (n!=0) + { + remainder = n%10; + n /= 10; + decimalNumber += remainder*pow(2,i); + ++i; + } + return decimalNumber; +} diff --git a/Binary Number to Decimal and vice-versa b/Binary Number to Decimal and vice-versa new file mode 100644 index 00000000..6b73a730 --- /dev/null +++ b/Binary Number to Decimal and vice-versa @@ -0,0 +1,21 @@ +#include +#include +int convert(long long n); +int main() { + long long n; + printf("Enter a binary number: "); + scanf("%lld", &n); + printf("%lld in binary = %d in decimal", n, convert(n)); + return 0; +} + +int convert(long long n) { + int dec = 0, i = 0, rem; + while (n != 0) { + rem = n % 10; + n /= 10; + dec += rem * pow(2, i); + ++i; + } + return dec; +} diff --git a/Binary Search Tree b/Binary Search Tree new file mode 100644 index 00000000..2bfdfc5c --- /dev/null +++ b/Binary Search Tree @@ -0,0 +1,39 @@ +# Python3 Program for recursive binary search. + +# Returns index of x in arr if present, else -1 +def binarySearch (arr, l, r, x): + + # Check base case + if r >= l: + + mid = l + (r - l) // 2 + + # If element is present at the middle itself + if arr[mid] == x: + return mid + + # If element is smaller than mid, then it + # can only be present in left subarray + elif arr[mid] > x: + return binarySearch(arr, l, mid-1, x) + + # Else the element can only be present + # in right subarray + else: + return binarySearch(arr, mid + 1, r, x) + + else: + # Element is not present in the array + return -1 + +# Driver Code +arr = [ 2, 3, 4, 10, 40 ] +x = 10 + +# Function call +result = binarySearch(arr, 0, len(arr)-1, x) + +if result != -1: + print ("Element is present at index % d" % result) +else: + print ("Element is not present in array") diff --git a/Binary To Decimal_Manas b/Binary To Decimal_Manas new file mode 100644 index 00000000..02d2537f --- /dev/null +++ b/Binary To Decimal_Manas @@ -0,0 +1,27 @@ +# Python3 program to convert +# binary to decimal + +# Function to convert +# binary to decimal +def binaryToDecimal(n): + num = n; + dec_value = 0; + + # Initializing base + # value to 1, i.e 2 ^ 0 + base = 1; + + temp = num; + while(temp): + last_digit = temp % 10; + temp = int(temp / 10); + + dec_value += last_digit * base; + base = base * 2; + return dec_value; + +# Driver Code +num = 10101001; +print(binaryToDecimal(num)); + +# This code is contributed by Manas Garg diff --git a/Binary heap in c++ b/Binary heap in c++ new file mode 100644 index 00000000..0374173a --- /dev/null +++ b/Binary heap in c++ @@ -0,0 +1,210 @@ +/* + * C++ Program to Implement Binary Heap + */ +#include +#include +#include +#include +using namespace std; +/* + * Class Declaration + */ +class BinaryHeap +{ + private: + vector heap; + int left(int parent); + int right(int parent); + int parent(int child); + void heapifyup(int index); + void heapifydown(int index); + public: + BinaryHeap() + {} + void Insert(int element); + void DeleteMin(); + int ExtractMin(); + void DisplayHeap(); + int Size(); +}; +/* + * Return Heap Size + */ +int BinaryHeap::Size() +{ + return heap.size(); +} + +/* + * Insert Element into a Heap + */ +void BinaryHeap::Insert(int element) +{ + heap.push_back(element); + heapifyup(heap.size() -1); +} +/* + * Delete Minimum Element + */ +void BinaryHeap::DeleteMin() +{ + if (heap.size() == 0) + { + cout<<"Heap is Empty"<::iterator pos = heap.begin(); + cout<<"Heap --> "; + while (pos != heap.end()) + { + cout<<*pos<<" "; + pos++; + } + cout<= 0 && parent(in) >= 0 && heap[parent(in)] > heap[in]) + { + int temp = heap[in]; + heap[in] = heap[parent(in)]; + heap[parent(in)] = temp; + heapifyup(parent(in)); + } +} + +/* + * Heapify- Maintain Heap Structure top down + */ +void BinaryHeap::heapifydown(int in) +{ + + int child = left(in); + int child1 = right(in); + if (child >= 0 && child1 >= 0 && heap[child] > heap[child1]) + { + child = child1; + } + if (child > 0 && heap[in] > heap[child]) + { + int temp = heap[in]; + heap[in] = heap[child]; + heap[child] = temp; + heapifydown(child); + } +} + +/* + * Main Contains Menu + */ +int main() +{ + BinaryHeap h; + while (1) + { + cout<<"------------------"<>choice; + switch(choice) + { + case 1: + cout<<"Enter the element to be inserted: "; + cin>>element; + h.Insert(element); + break; + case 2: + h.DeleteMin(); + break; + case 3: + cout<<"Minimum Element: "; + if (h.ExtractMin() == -1) + { + cout<<"Heap is Empty"< + +//variable to store maximum number of nodes +int complete_node=15; + +//Array to store the tree +char tree[]={'\0','D','A','F','E','B','R','T','G','Q','\0','\0','V','\0','J','L'}; + +int get_right_child(int index) +{ + + //node is not null + //and the result must lie within the number of nodes for a complete binary + if(tree[index]!='\0'&& ((2*index)+1)<=complete_node) + return(2*index)+1; + //right child doesn't exist + return -1; +} + int get_left_child(int index) + { + //node is not null + //and the result must lie within the number of nodes for a complete binary + if(tree[index]!='\0' && (2*index)<=complete_node) + return 2*index; + //left child doesn't exist + return -1; + } + + void preorder(int index) + { + + //checking for valid index and null node + if(index>0 && tree[index]!='\0') + { + printf("%c",tree[index]);//visiting root + preorder(get_left_child(index));//visitingleft subtree + preorder(get_right_child(index));//visiting right subtree + + } + + } + void postorder(int index) + { + //checking for void index and null node + if(index>0 && tree[index]!='\0') + { + + postorder(get_left_child(index));//visiting left subtree + postorder(get_right_child(index));//visiting right subtree + printf("%c",tree[index]);//visiting root + + } + } + void inorder(int index) + { + //checking for valid index and null node + if(index>0 && tree[index]!='\0') + { + + inorder(get_left_child(index));//visiting left subtree + printf("%c",tree[index]);//visiting root + inorder(get_right_child(index));//visiting right subtree + } + + } + + +int main() +{ + printf("Preorder:\n"); + preorder(1); + printf("\nPostorder:\n"); + postorder(1); + printf("\nInorder:\n"); + inorder(1); + printf("\n"); + return 0; +} + + + diff --git a/Binary to Decimal (Type-Hard) b/Binary to Decimal (Type-Hard) new file mode 100644 index 00000000..511fb7c0 --- /dev/null +++ b/Binary to Decimal (Type-Hard) @@ -0,0 +1,32 @@ +// Recursive Java program to convert binary +// decimal + + +class GFG +{ + static int toDecimal(String binary,int i) + { + // If we reached last character + int n = binary.length(); + if (i == n-1) + return binary.charAt(i) - '0'; + + // Add current tern and recur for + // remaining terms + return ((binary.charAt(i) - '0') << (n-i-1)) + + toDecimal(binary, i+1); + } + + // Driver code + public static void main(String []args) + { + String binary = "1010"; + int i=0; + System.out.println(toDecimal(binary,i)); + + } + +} + +// This code is contributed +// by Yogii7664 (Yogesh Soni) diff --git a/Binary to Octal (Type-Hard) b/Binary to Octal (Type-Hard) new file mode 100644 index 00000000..ab9f4637 --- /dev/null +++ b/Binary to Octal (Type-Hard) @@ -0,0 +1,31 @@ +public class BinaryOctal { + + public static void main(String[] args) { + long binary = 101001; + int octal = convertBinarytoOctal(binary); + System.out.printf("%d in binary = %d in octal", binary, octal); + } + + public static int convertBinarytoOctal(long binaryNumber) + { + int octalNumber = 0, decimalNumber = 0, i = 0; + + while(binaryNumber != 0) + { + decimalNumber += (binaryNumber % 10) * Math.pow(2, i); + ++i; + binaryNumber /= 10; + } + + i = 1; + + while (decimalNumber != 0) + { + octalNumber += (decimalNumber % 8) * i; + decimalNumber /= 8; + i *= 10; + } + + return octalNumber; + } +} diff --git a/Binary to decimal b/Binary to decimal new file mode 100644 index 00000000..baac2403 --- /dev/null +++ b/Binary to decimal @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +int main() +{ + long bi, deci = 0, j = 1, r; + cout << " Input a binary number: "; + cin>> bi; + while (bi != 0) + { + r = bi%10; + deci = deci + r*j; + j = j*2; + bi = bi/10; + } + cout<<" The decimal number: " << deci<<"\n"; +} diff --git a/Binary to decimal converter b/Binary to decimal converter index da2c287d..62752959 100644 --- a/Binary to decimal converter +++ b/Binary to decimal converter @@ -1,9 +1,31 @@ -import java.util.Scanner; -class BinaryToDecimal { - public static void main(String args[]){ - Scanner input = new Scanner( System.in ); - System.out.print("Enter a binary number: "); - String binaryString =input.nextLine(); - System.out.println("Output: "+Integer.parseInt(binaryString,2)); +#include +using namespace std; + + +int binaryToDecimal(int n) +{ + int num = n; + int dec_value = 0; + + // Initializing base value to 1, i.e 2^0 + int base = 1; + + int temp = num; + while (temp) { + int last_digit = temp % 10; + temp = temp / 10; + + dec_value += last_digit * base; + + base = base * 2; } + + return dec_value; +} + +int main() +{ + int num = 10101001; + + cout << binaryToDecimal(num) << endl; } diff --git a/Binary to decimal using python b/Binary to decimal using python new file mode 100644 index 00000000..f725730f --- /dev/null +++ b/Binary to decimal using python @@ -0,0 +1,9 @@ +# convert binary to decimal +num = list(input('Enter a binary number: ')) +result = 0 + +for k in range(len(num)): + digit=num.pop() + if digit == '1': + result = result + pow(2,k) +print('The decimal value is',result) diff --git a/Binary to decimals b/Binary to decimals new file mode 100644 index 00000000..baac2403 --- /dev/null +++ b/Binary to decimals @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +int main() +{ + long bi, deci = 0, j = 1, r; + cout << " Input a binary number: "; + cin>> bi; + while (bi != 0) + { + r = bi%10; + deci = deci + r*j; + j = j*2; + bi = bi/10; + } + cout<<" The decimal number: " << deci<<"\n"; +} diff --git a/Binary to octal using C b/Binary to octal using C new file mode 100644 index 00000000..67f60559 --- /dev/null +++ b/Binary to octal using C @@ -0,0 +1,30 @@ +#include +#include +int convert_to_octal(long long bin); +int main() { + long long bin; + printf("Enter a binary number: "); + scanf("%lld", &bin); + printf("%d is the octal number", convert_to_octal(bin)); + return 0; +} + +int convert_to_octal(long long bin) { + int octal = 0, dec = 0, i = 0; + + + while (bin != 0) { + dec += (bin % 10) * pow(2, i); + ++i; + bin /= 10; + } + i = 1; + + + while (dec != 0) { + octal += (dec % 8) * i; + dec /= 8; + i *= 10; + } + return octal; +} diff --git a/BinarySearch.cpp b/BinarySearch.cpp new file mode 100644 index 00000000..8afb3afc --- /dev/null +++ b/BinarySearch.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +int binarysearch(int arr[], int num, int start, int end) +{ while(start<=end){ + int middle = (start+end)/2; + if(arr[middle] == num) + return middle; + if(arr[middle]>num) + end=middle-1; + else + start=middle+1; + + } + return -1; +} + +int main() +{ + int arr[]={1,2,6,7,8,10}; + int num; + cin>>num; + int n = sizeof(arr)/ sizeof(arr[0]); + int ans=binarysearch(arr,num,0,n-1); + cout<", end=' ') + + # Traverse right + inorder(root.right) + + +# Insert a node +def insert(node, key): + + # Return a new node if the tree is empty + if node is None: + return Node(key) + + # Traverse to the right place and insert the node + if key < node.key: + node.left = insert(node.left, key) + else: + node.right = insert(node.right, key) + + return node + + +# Find the inorder successor +def minValueNode(node): + current = node + + # Find the leftmost leaf + while(current.left is not None): + current = current.left + + return current + + +# Deleting a node +def deleteNode(root, key): + + # Return if the tree is empty + if root is None: + return root + + # Find the node to be deleted + if key < root.key: + root.left = deleteNode(root.left, key) + elif(key > root.key): + root.right = deleteNode(root.right, key) + else: + # If the node is with only one child or no child + if root.left is None: + temp = root.right + root = None + return temp + + elif root.right is None: + temp = root.left + root = None + return temp + + # If the node has two children, + # place the inorder successor in position of the node to be deleted + temp = minValueNode(root.right) + + root.key = temp.key + + # Delete the inorder successor + root.right = deleteNode(root.right, temp.key) + + return root + + +root = None +root = insert(root, 8) +root = insert(root, 5) +root = insert(root, 1) +root = insert(root, 6) +root = insert(root, 7) +root = insert(root, 10) +root = insert(root, 14) +root = insert(root, 4) + +print("Inorder traversal: ", end=' ') +inorder(root) + +print("\nDelete 10") +root = deleteNode(root, 10) +print("Inorder traversal: ", end=' ') +inorder(root) diff --git a/Binary_search in c b/Binary_search in c new file mode 100644 index 00000000..49eb580d --- /dev/null +++ b/Binary_search in c @@ -0,0 +1,37 @@ +#include +void main() +{ + int a[100],i,l,u,n,key,m; + printf("enter number of elements"); + scanf("%d",&n); + printf("enter the elements"); + for (i = 0; i < n ; i++) + { + scanf("%d",&a[i]); + } + printf("enter key value"); + scanf("%d",&key); + l=0; + u=n-1; + while (l<=u) + { + m=(l+u)/2; + if (a[m]==key) + { + printf("element found"); + break; + } + else if (a[m] +#include + +struct { + unsigned int age : 3; +} Age; + +int main( ) { + + Age.age = 4; + printf( "Sizeof( Age ) : %d\n", sizeof(Age) ); + printf( "Age.age : %d\n", Age.age ); + + Age.age = 7; + printf( "Age.age : %d\n", Age.age ); + + Age.age = 8; + printf( "Age.age : %d\n", Age.age ); + + return 0; +} diff --git a/Bubble sort b/Bubble sort new file mode 100644 index 00000000..c7b34f8e --- /dev/null +++ b/Bubble sort @@ -0,0 +1,34 @@ +#include + +int main(){ + + int count, temp, i, j, n[30]; + + printf("How many numbers are u going to enter?: "); + scanf("%d",&count); + + printf("Enter %d numbers: ",count); + + for(i=0;i=0;i--) + { + for(j=0;j<=i;j++) + { + if(n[j]>n[j+1]) + { + temp=n[j]; + n[j]=n[j+1]; + n[j+1]=temp; + } + } + } + + printf("Sorted elements: "); + for(i=0;i +void swap(int *k, int *l); +void main(){ + + int a[100],i,j,n,temp; + printf("enter number of elements"); + scanf("%d",&n); + for (i = 0; i < 5 ; i++) + { + scanf("%d",&a[i]); + } + for(i=0;ia[j+1]) + { + swap(&a[j],&a[j+1]); + } + + } + } + for (j = 0; j < n ; j++) + { + printf("%d ",a[j]); + } + +} +void swap(int *k,int *l){ + int temp; + temp=*k; + *k=*l; + *l=temp; +} diff --git a/C Program To Enter 2 Number and Perform All Arithmetic Operations b/C Program To Enter 2 Number and Perform All Arithmetic Operations new file mode 100644 index 00000000..52112d93 --- /dev/null +++ b/C Program To Enter 2 Number and Perform All Arithmetic Operations @@ -0,0 +1,29 @@ +#include + +int main() +{ + int p, q; + int sum, sub, mul, mod; + float div; + + /** Input two numbers from user **/ + printf("Enter any two numbers::\n"); + scanf("%d%d", &p, &q, "\n"); + + /** Perform all arithmetic operations **/ + sum = p + q; + sub = p - q; + mul = p * q; + div = (float)p / q; + mod = p % q; + + /** Print result of all arithmetic operations **/ + printf("\n"); + printf("SUM %d + %d = %d\n", p, q, sum); + printf("DIFFERENCE %d - %d = %d\n", p, q, sub); + printf("PRODUCT %d * %d = %d\n", p, q, mul); + printf("QUOTIENT %d / %d = %f\n", p, q, div); + printf("MODULUS %d %% %d = %d\n", p, q, mod); + + return 0; +} diff --git a/C Program for Insertion Sort b/C Program for Insertion Sort index 36b44bfb..174ba4fd 100644 --- a/C Program for Insertion Sort +++ b/C Program for Insertion Sort @@ -1,30 +1,54 @@ #include -#include +#include -int main() +void insert(int arr[], int i) { -int a[50],c,n,i,j,key; -printf("enter number of elements to enter"); -scanf("%d",&n); -for(i=0;i= 0 && arr[j] > key) + { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; } -for(j=1;j=1 && a[j-1]>key){ -a[j]=a[j-1]; - -j--; +void insertionSort(int arr[], int n) +{ + int i; + for (i = 1; i +int main() { + int base, exp; + long long result = 1; + printf("Enter a base number: "); + scanf("%d", &base); + printf("Enter an exponent: "); + scanf("%d", &exp); + + while (exp != 0) { + result *= base; + --exp; + } + printf("Answer = %lld", result); + return 0; +} diff --git a/C Program to Find ASCII Value of a Character b/C Program to Find ASCII Value of a Character new file mode 100644 index 00000000..dcbf2c24 --- /dev/null +++ b/C Program to Find ASCII Value of a Character @@ -0,0 +1,10 @@ +#include +int main() { + char c; + printf("Enter a character: "); + scanf("%c", &c); + + printf("ASCII value of %c = %d", c, c); + + return 0; +} diff --git a/C Program to Find Smallest Element in Array b/C Program to Find Smallest Element in Array new file mode 100644 index 00000000..441e2ce4 --- /dev/null +++ b/C Program to Find Smallest Element in Array @@ -0,0 +1,26 @@ +#include + +int main() { + int a[30], i, num, smallest; + + printf("\nEnter no of elements :"); + scanf("%d", &num); + + //Read n elements in an array + for (i = 0; i < num; i++) + scanf("%d", &a[i]); + + //Consider first element as smallest + smallest = a[0]; + + for (i = 0; i < num; i++) { + if (a[i] < smallest) { + smallest = a[i]; + } + } + + // Print out the Result + printf("\nSmallest Element : %d", smallest); + + return (0); +} diff --git a/C Program to Find the Length of a String b/C Program to Find the Length of a String new file mode 100644 index 00000000..c90dfe30 --- /dev/null +++ b/C Program to Find the Length of a String @@ -0,0 +1,17 @@ +#include +#include + +int main() +{ + char Str[100]; + int Length; + + printf("\n Please Enter any String \n"); + gets (Str); + + Length = strlen(Str); + + printf("Length of a String = %d\n", Length); + + return 0; +} diff --git a/C Program to Sort the Array in Descending Order b/C Program to Sort the Array in Descending Order new file mode 100644 index 00000000..d0c6de44 --- /dev/null +++ b/C Program to Sort the Array in Descending Order @@ -0,0 +1,83 @@ + /* + + * C program to accept a set of numbers and arrange them + + * in a descending order + + */ + + + + #include + + void main () + + { + + + + int number[30]; + + + + int i, j, a, n; + + printf("Enter the value of N\n"); + + scanf("%d", &n); + + + + printf("Enter the numbers \n"); + + for (i = 0; i < n; ++i) + + scanf("%d", &number[i]); + + + + /* sorting begins ... */ + + + + for (i = 0; i < n; ++i) + + { + + for (j = i + 1; j < n; ++j) + + { + + if (number[i] < number[j]) + + { + + a = number[i]; + + number[i] = number[j]; + + number[j] = a; + + } + + } + + } + + + + printf("The numbers arranged in descending order are given below\n"); + + + + for (i = 0; i < n; ++i) + + { + + printf("%d\n", number[i]); + + } + + + + } diff --git a/C Program to convert binary to decimal number b/C Program to convert binary to decimal number new file mode 100644 index 00000000..aa3e14fd --- /dev/null +++ b/C Program to convert binary to decimal number @@ -0,0 +1,20 @@ +#include +int convert(long long n); +int main() { + long long n; + printf("Enter a binary number: "); + scanf("%lld", &n); + printf("%lld in binary = %d in decimal", n, convert(n)); + return 0; +} + +int convert(long long n) { + int dec = 0, i = 0, rem; + while (n != 0) { + rem = n % 10; + n /= 10; + dec += rem * pow(2, i); + ++i; + } + return dec; +} diff --git a/C Program to multiply 2D or 3D Matrices b/C Program to multiply 2D or 3D Matrices new file mode 100644 index 00000000..666f21db --- /dev/null +++ b/C Program to multiply 2D or 3D Matrices @@ -0,0 +1,69 @@ +#include +void getMatrixElements(int matrix[][10], int row, int column) { + + printf("\nEnter elements: \n"); + + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("Enter a%d%d: ", i + 1, j + 1); + scanf("%d", &matrix[i][j]); + } + } +} + +void multiplyMatrices(int first[][10], + int second[][10], + int result[][10], + int r1, int c1, int r2, int c2) { + + // Initializing elements of matrix mult to 0. + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + result[i][j] = 0; + } + } + + // Multiplying first and second matrices and storing it in result + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + for (int k = 0; k < c1; ++k) { + result[i][j] += first[i][k] * second[k][j]; + } + } + } +} + +// function to display the matrix +void display(int result[][10], int row, int column) { + + printf("\nOutput Matrix:\n"); + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("%d ", result[i][j]); + if (j == column - 1) + printf("\n"); + } + } +} + +int main() { + int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2; + printf("Enter rows and column for the first matrix: "); + scanf("%d %d", &r1, &c1); + printf("Enter rows and column for the second matrix: "); + scanf("%d %d", &r2, &c2); + while (c1 != r2) { + printf("Error! Enter rows and columns again.\n"); + printf("Enter rows and columns for the first matrix: "); + scanf("%d%d", &r1, &c1); + printf("Enter rows and columns for the second matrix: "); + scanf("%d%d", &r2, &c2); + } + getMatrixElements(first, r1, c1); + getMatrixElements(second, r2, c2); + + multiplyMatrices(first, second, result, r1, c1, r2, c2); + display(result, r1, c2); + + return 0; +} diff --git a/C Program to print ASCII value of given character b/C Program to print ASCII value of given character new file mode 100644 index 00000000..78a9e039 --- /dev/null +++ b/C Program to print ASCII value of given character @@ -0,0 +1,7 @@ +#include +int main() +{ + char c = 'k'; + printf("The ASCII value of %c is %d", c, c); + return 0; +} diff --git a/C Program to print Alphabet Triangle b/C Program to print Alphabet Triangle new file mode 100644 index 00000000..6f01273b --- /dev/null +++ b/C Program to print Alphabet Triangle @@ -0,0 +1,20 @@ +#include +#include +int main(){ + int ch=65; + int i,j,k,m; + system("cls"); + for(i=1;i<=5;i++) + { + for(j=5;j>=i;j--) + printf(" "); + for(k=1;k<=i;k++) + printf("%c",ch++); + ch--; + for(m=1;m +int main() { + int n, i; + float num[100], sum = 0.0, avg; + + printf("Enter the numbers of elements: "); + scanf("%d", &n); + + while (n > 100 || n < 1) { + printf("Error! number should in range of (1 to 100).\n"); + printf("Enter the number again: "); + scanf("%d", &n); + } diff --git a/C String Initialization b/C String Initialization new file mode 100644 index 00000000..823f756b --- /dev/null +++ b/C String Initialization @@ -0,0 +1,8 @@ +// Declare C String without Size +char name[] = "Tutorial Gateway"; +// Declare String with Size +char name[50] = "Tutorial Gateway"; +// Declare String of Characters +char name[] = {'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 'G', 'a', 't', 'e', 'w', 'a', 'Y', '\0'}; + +char name[16] = {'T', 'u', 't', 'o', 'r', 'i', 'a', 'l',' G', 'a', 't', 'e', 'w', 'a', 'Y', '\0'}; diff --git a/C code for sorting a given array (ascending & descending) b/C code for sorting a given array (ascending & descending) new file mode 100644 index 00000000..53757006 --- /dev/null +++ b/C code for sorting a given array (ascending & descending) @@ -0,0 +1,54 @@ +// C code for sorting a array in ascending and descinding +#include //including stdio.h for printf and other functions +#include + + +int main() //default function for call +{ + int a[100],n,i,j; + printf("Array size: "); + scanf("%d",&n); + printf("Elements: "); + + for(i=0;i a[i]) //Comparing other array elements + { + int tmp = a[i]; //Using temporary variable for storing last value + a[i] = a[j]; //replacing value + a[j] = tmp; //storing last value + } + } + } + printf("\n\nAscending : "); //Printing message + for (int i = 0; i < n; i++) //Loop for printing array data after sorting + { + printf(" %d ", a[i]); + } + for (int i = 0; i < n; i++) //Loop for descending ordering + { + for (int j = 0; j < n; j++) //Loop for comparing other values + { + if (a[j] < a[i]) //Comparing other array elements + { + int tmp = a[i]; //Using temporary variable for storing last value + a[i] = a[j]; //replacing value + a[j] = tmp; //storing last value + } + } + } + printf("\n\nDescending : "); //Printing message + for (int i = 0; i < n; i++) //Loop for printing array data after sorting + { + printf(" %d ", a[i]); //Printing data + } + + return 0; //returning 0 status to system + +} diff --git a/C language program Fibonacci series b/C language program Fibonacci series new file mode 100644 index 00000000..612791d3 --- /dev/null +++ b/C language program Fibonacci series @@ -0,0 +1,14 @@ +#include +int fib(int m){ +if(m==0||m==1) + return m; +else + return(fib(m-1)+fib(m-2)); +} +int main(){ + int n; + scanf("%d",&n); + int res=fib(n); + printf("%d",res); + return 0; + } diff --git a/C language program to find length of a string b/C language program to find length of a string new file mode 100644 index 00000000..c90dfe30 --- /dev/null +++ b/C language program to find length of a string @@ -0,0 +1,17 @@ +#include +#include + +int main() +{ + char Str[100]; + int Length; + + printf("\n Please Enter any String \n"); + gets (Str); + + Length = strlen(Str); + + printf("Length of a String = %d\n", Length); + + return 0; +} diff --git a/C program check if an integer is prime or not b/C program check if an integer is prime or not new file mode 100644 index 00000000..7a1e602e --- /dev/null +++ b/C program check if an integer is prime or not @@ -0,0 +1,25 @@ +#include + +int main() +{ + int n, c; + + printf("Enter a number\n"); + scanf("%d", &n); + + if (n == 2) + printf("Prime number.\n"); + else + { + for (c = 2; c <= n - 1; c++) + { + if (n % c == 0) + break; + } + if (c != n) + printf("Not prime.\n"); + else + printf("Prime number.\n"); + } + return 0; +} diff --git a/C program for febonacci siries b/C program for febonacci siries new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/C program for febonacci siries @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/C program to check if an integer is prime or not b/C program to check if an integer is prime or not new file mode 100644 index 00000000..7a1e602e --- /dev/null +++ b/C program to check if an integer is prime or not @@ -0,0 +1,25 @@ +#include + +int main() +{ + int n, c; + + printf("Enter a number\n"); + scanf("%d", &n); + + if (n == 2) + printf("Prime number.\n"); + else + { + for (c = 2; c <= n - 1; c++) + { + if (n % c == 0) + break; + } + if (c != n) + printf("Not prime.\n"); + else + printf("Prime number.\n"); + } + return 0; +} diff --git a/C program to find length of a string b/C program to find length of a string new file mode 100644 index 00000000..c90dfe30 --- /dev/null +++ b/C program to find length of a string @@ -0,0 +1,17 @@ +#include +#include + +int main() +{ + char Str[100]; + int Length; + + printf("\n Please Enter any String \n"); + gets (Str); + + Length = strlen(Str); + + printf("Length of a String = %d\n", Length); + + return 0; +} diff --git a/C program to find prime no. in a given range b/C program to find prime no. in a given range new file mode 100644 index 00000000..9b64644e --- /dev/null +++ b/C program to find prime no. in a given range @@ -0,0 +1,29 @@ +#include + +int main(){ + int num,i,ctr,m,n; + + printf("Input starting number of range: "); + scanf("%d",&m); + + printf("Input ending number of range : "); + scanf("%d",&n); + printf("The prime numbers between %d and %d are : \n",m,n); + + for(num = m;num<=n;num++) + { + ctr = 0; + + for(i=2;i<=num/2;i++) + { + if(num%i==0){ + ctr++; + break; + } + } + + if(ctr==0 && num!= 1) + printf("%d ",num); + } +printf("\n"); +} diff --git a/C program to get input from a user using scanf b/C program to get input from a user using scanf new file mode 100644 index 00000000..1138fd3a --- /dev/null +++ b/C program to get input from a user using scanf @@ -0,0 +1,12 @@ +#include +int main() +{ + int x; + + printf("Input an integer\n"); + scanf("%d", &x); // %d is used for an integer + + printf("The integer is: %d\n", x); + + return 0; +} diff --git a/C++ Program for Linked List b/C++ Program for Linked List new file mode 100644 index 00000000..de5c40fc --- /dev/null +++ b/C++ Program for Linked List @@ -0,0 +1,199 @@ +#include + +using namespace std; + +class node +{ +public: + int data; + node* next; +}; + +node* newNode(int data) +{ + node* new_node = new node(); + new_node->data = data; + new_node->next = NULL; + return new_node; +} + +void append(node** head_ref, int data) +{ + node* temp = *head_ref; + if(*head_ref == NULL) + *head_ref = newNode(data); + else + { + while(temp->next != NULL) + temp = temp->next; + temp->next = newNode(data); + } +} + +void append_at_start(node** head_ref, int data) +{ + node* temp = *head_ref; + node* newnode = newNode(data); + *head_ref = newnode; + newnode->next = temp; +} + +void append_at_position(node** head_ref, int pos, int data) +{ + int i = 0; + node *temp = *head_ref, *newnode = newNode(data);; + if(*head_ref == NULL) + { + *head_ref = newnode; + return; + } + if(pos == 1) + { + newnode->next = *head_ref; + *head_ref = newnode; + return; + } + while(i < pos-2 && temp != NULL) + { + temp = temp->next; + i++; + } + if(temp == NULL) + { + cout << "Cann't Add, Invalid Position\n"; + return; + } + newnode->next = temp->next; + temp->next = newnode; +} + +void popStart(node** head_ref) +{ + if(*head_ref == NULL) + { + cout << "Empty List\n"; + return; + } + *head_ref = (*head_ref)->next; +} + +void popLast(node** head_ref) +{ + node* temp = *head_ref; + if(*head_ref == NULL) + { + cout << "Empty List\n"; + return; + } + if(temp->next == NULL) + { + *head_ref = NULL; + return; + } + while(temp->next->next != NULL) + temp = temp->next; + temp->next = NULL; +} + +void popPosition(node** head_ref, int pos) +{ + int i = 0; + node* temp = *head_ref; + if(*head_ref == NULL) + { + cout << "Empty List\n"; + return; + } + if(pos == 1) + { + *head_ref = (*head_ref)->next; + return; + } + while(i < pos-2 && temp != NULL) + { + temp = temp->next; + i++; + } + if(temp == NULL || temp->next == NULL) + { + cout << "Cann't Pop, Invalid Position\n"; + return; + } + temp->next = temp->next->next; +} + +void reverse_list(node** head_ref) +{ + node *current = *head_ref, *prev = NULL, *after = NULL; + if(*head_ref == NULL) + return; + while(current != NULL) + { + after = current->next; + current->next = prev; + prev = current; + current = after; + } + *head_ref = prev; +} + +void swap(node** head_ref, int x, int y) +{ + node *prev1 = NULL, *prev2 = NULL, *curr1 = *head_ref, *curr2 = *head_ref; + if(x == y) + return; + while(curr1 && curr1->data != x) + { + prev1 = curr1; + curr1 = curr1->next; + } + while(curr2 && curr2->data != y) + { + prev2 = curr2; + curr2 = curr2->next; + } + if(curr1 == NULL || curr2 == NULL) + return; + if(prev1 != NULL) + prev1->next = curr2; + else + *head_ref = curr2; + if(prev2 != NULL) + prev2->next = curr1; + else + *head_ref = curr1; + node* temp = curr1->next; + curr1->next = curr2->next; + curr2->next = temp; +} + +void print(node* head) +{ + while(head != NULL) + { + cout << head->data << " "; + head = head->next; + } + cout << endl; +} + +int main() +{ + node* head = NULL; + append(&head, 6); + append(&head, 7); + append(&head, 8); + append(&head, 9); + append(&head, 10); + append(&head, 11); + append_at_start(&head, 1); + append_at_position(&head, 4, 10); + popStart(&head); + popLast(&head); + popPosition(&head, 6); + print(head); + reverse_list(&head); + swap(&head, 7, 11); + print(head); + return 0; +} diff --git a/C++ Program to Find Factorial b/C++ Program to Find Factorial new file mode 100644 index 00000000..810be643 --- /dev/null +++ b/C++ Program to Find Factorial @@ -0,0 +1,13 @@ +#include +using namespace std; +int fact(int n) { + if ((n==0)||(n==1)) + return 1; + else + return n*fact(n-1); +} +int main() { + int n = 5; + cout<<"Factorial of "< +using namespace std; + +int main() +{ + int i, n; + float arr[100]; + + cout << "Enter total number of elements(1 to 100): "; + cin >> n; + cout << endl; + + // Store number entered by the user + for(i = 0; i < n; ++i) + { + cout << "Enter Number " << i + 1 << " : "; + cin >> arr[i]; + } + + // Loop to store largest number to arr[0] + for(i = 1;i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + cout << "Largest element = " << arr[0]; + + return 0; +} diff --git a/C++ Program to Find the Largest and Smallest Elements in an Array b/C++ Program to Find the Largest and Smallest Elements in an Array new file mode 100644 index 00000000..f9f1d337 --- /dev/null +++ b/C++ Program to Find the Largest and Smallest Elements in an Array @@ -0,0 +1,26 @@ +#include +using namespace std; +int main () +{ + int arr[10], n, i, max, min; + cout << "Enter the size of the array : "; + cin >> n; + cout << "Enter the elements of the array : "; + for (i = 0; i < n; i++) + cin >> arr[i]; + max = arr[0]; + for (i = 0; i < n; i++) + { + if (max < arr[i]) + max = arr[i]; + } + min = arr[0]; + for (i = 0; i < n; i++) + { + if (min > arr[i]) + min = arr[i]; + } + cout << "Largest element : " << max; + cout << "Smallest element : " << min; + return 0; +} diff --git a/C++ Program to Sort Elements in Lexicographical Order (Dictionary Order) b/C++ Program to Sort Elements in Lexicographical Order (Dictionary Order) new file mode 100644 index 00000000..e0744e09 --- /dev/null +++ b/C++ Program to Sort Elements in Lexicographical Order (Dictionary Order) @@ -0,0 +1,32 @@ +#include +using namespace std; + +int main() +{ + string str[10], temp; + + cout << "Enter 10 words: " << endl; + for(int i = 0; i < 10; ++i) + { + getline(cin, str[i]); + } + + for(int i = 0; i < 9; ++i) + for( int j = i+1; j < 10; ++j) + { + if(str[i] > str[j]) + { + temp = str[i]; + str[i] = str[j]; + str[j] = temp; + } + } + + cout << "In lexicographical order: " << endl; + + for(int i = 0; i < 10; ++i) + { + cout << str[i] << endl; + } + return 0; +} diff --git a/C++ code to count factorial of a given number b/C++ code to count factorial of a given number new file mode 100644 index 00000000..f6b1d2a1 --- /dev/null +++ b/C++ code to count factorial of a given number @@ -0,0 +1,22 @@ +// C++ program to find factorial of given number +#include +using namespace std; + +// function to find factorial of given number +unsigned int factorial(unsigned int n) +{ + if (n == 0) + return 1; + return n * factorial(n - 1); +} + +// Driver code +int main() +{ + int num; + cout << "Enter a value : "; + cin >> num; + cout << "Factorial of " + << num << " is " << factorial(num) << endl; + return 0; +} diff --git a/C++ code to find the largest element in array b/C++ code to find the largest element in array new file mode 100644 index 00000000..38961ee3 --- /dev/null +++ b/C++ code to find the largest element in array @@ -0,0 +1,31 @@ +// C++ program to find maximum +// in arr[] of size n +#include +using namespace std; + +int largest(int arr[], int n) +{ + int i; + + // Initialize maximum element + int max = arr[0]; + + // Traverse array elements + // from second and compare + // every element with current max + for (i = 1; i < n; i++) + if (arr[i] > max) + max = arr[i]; + + return max; +} + +// Driver Code +int main() +{ + int arr[] = {10, 324, 45, 90, 9808}; + int n = sizeof(arr) / sizeof(arr[0]); + cout << "Largest in given array is " + << largest(arr, n); + return 0; +} diff --git a/C++ code to find the length of a string b/C++ code to find the length of a string new file mode 100644 index 00000000..bfa79765 --- /dev/null +++ b/C++ code to find the length of a string @@ -0,0 +1,14 @@ +// C++ code to count the length of a string +#include +#include +using namespace std; +int main () +{ + char str[50]; + int len; + cout << "Enter an array or string : "; + gets(str); + len = strlen(str); + cout << "Length of the string is : " << len; + return 0; +} diff --git a/C++ code to find the smallest element in array b/C++ code to find the smallest element in array new file mode 100644 index 00000000..f9d55188 --- /dev/null +++ b/C++ code to find the smallest element in array @@ -0,0 +1,32 @@ +//C++ programe to find the smallest element of an array +#include +using namespace std; +int findSmallestElement(int arr[], int n){ + /* We are assigning the first array element to + * the temp variable and then we are comparing + * all the array elements with the temp inside + * loop and if the element is smaller than temp + * then the temp value is replaced by that. This + * way we always have the smallest value in temp. + * Finally we are returning temp. + */ + int temp = arr[0]; + for(int i=0; iarr[i]) { + temp=arr[i]; + } + } + return temp; +} +int main() { + int n; + cout<<"Enter the size of array: "; + cin>>n; int arr[n-1]; + cout<<"Enter array elements: "; + for(int i=0; i>arr[i]; + } + int smallest = findSmallestElement(arr, n); + cout<<"Smallest Element is: "< +using namespace std; + + +void rvereseArray(int arr[], int start, int end) +{ + while (start < end) + { + int temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + start++; + end--; + } +} + +void printArray(int arr[], int size) +{ + for (int i = 0; i < size; i++) + cout << arr[i] << " "; + + cout << endl; +} + + +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6}; + + int n = sizeof(arr) / sizeof(arr[0]); + + // To print original array + printArray(arr, n); + + // Function calling + rvereseArray(arr, 0, n-1); + + cout << "Reversed array is" << endl; + + // To print the Reversed array + printArray(arr, n); diff --git a/C++ to Find the Largest and Smallest Elements in an Array b/C++ to Find the Largest and Smallest Elements in an Array new file mode 100644 index 00000000..f9f1d337 --- /dev/null +++ b/C++ to Find the Largest and Smallest Elements in an Array @@ -0,0 +1,26 @@ +#include +using namespace std; +int main () +{ + int arr[10], n, i, max, min; + cout << "Enter the size of the array : "; + cin >> n; + cout << "Enter the elements of the array : "; + for (i = 0; i < n; i++) + cin >> arr[i]; + max = arr[0]; + for (i = 0; i < n; i++) + { + if (max < arr[i]) + max = arr[i]; + } + min = arr[0]; + for (i = 0; i < n; i++) + { + if (min > arr[i]) + min = arr[i]; + } + cout << "Largest element : " << max; + cout << "Smallest element : " << min; + return 0; +} diff --git a/CPP Program to convert binary to decimal number b/CPP Program to convert binary to decimal number new file mode 100644 index 00000000..aec2a98f --- /dev/null +++ b/CPP Program to convert binary to decimal number @@ -0,0 +1,23 @@ +#include +#include + +using namespace std; + +long long binaryToDecimal(string str) +{ + int sum = 0; + int b = 1; + + for (int i = str.length() - 1; i >= 0; i--) { + if (str[i] == '1') + sum += b; + b = b * 2; + } + return sum; +} + +int main() +{ + cout< + +using namespace std; + +int main() +{ + string str = "abcdef"; + int count = 0; + while(str[count] != '\0') + { + count++; + } + cout< +#include +using namespace std; + +int maxSubArraySum(int a[], int size) +{ + int max_so_far = INT_MIN, max_ending_here = 0; + + for (int i = 0; i < size; i++) + { + max_ending_here = max_ending_here + a[i]; + if (max_so_far < max_ending_here) + max_so_far = max_ending_here; + + if (max_ending_here < 0) + max_ending_here = 0; + } + return max_so_far; +} + +int main() +{ + int a[] = {-2, -3, 4, -1, -2, 1, 5, -3}; + int n = sizeof(a)/sizeof(a[0]); + int max_sum = maxSubArraySum(a, n); + cout << "Maximum contiguous sum is " << max_sum; + return 0; +} diff --git a/CPP program to find smallest element in array b/CPP program to find smallest element in array new file mode 100644 index 00000000..9d69ab4b --- /dev/null +++ b/CPP program to find smallest element in array @@ -0,0 +1,23 @@ +#include +using namespace std; +int findSmallestElement(int arr[], int n){ + int temp = arr[0]; + for(int i=0; iarr[i]) { + temp=arr[i]; + } + } + return temp; +} +int main() { + int n; + cout<<"Enter the size of array: "; + cin>>n; int arr[n-1]; + cout<<"Enter array elements: "; + for(int i=0; i>arr[i]; + } + int smallest = findSmallestElement(arr, n); + cout<<"Smallest Element is: "< +int main() { + int i, n; + float arr[100]; + printf("Enter the number of elements (1 to 100): "); + scanf("%d", &n); + + for (i = 0; i < n; ++i) { + printf("Enter number%d: ", i + 1); + scanf("%f", &arr[i]); + } + + // storing the largest number to arr[0] + for (i = 1; i < n; ++i) { + if (arr[0] < arr[i]) + arr[0] = arr[i]; + } + + printf("Largest element = %.2f", arr[0]); + + return 0; +} diff --git a/C_Program Factorial b/C_Program Factorial new file mode 100644 index 00000000..23eab865 --- /dev/null +++ b/C_Program Factorial @@ -0,0 +1,16 @@ +#include + +int main() +{ + int c, n, f = 1; + + printf("Enter number to calculate its factorial\n"); + scanf("%d", &n); + for (c = 1; c <= n; c++) + f = f * c; + + printf("Factorial of %d = %d\n", n, f); + + return 0; +} + diff --git a/C_Program to Print ASCII Value b/C_Program to Print ASCII Value new file mode 100644 index 00000000..a73b9c24 --- /dev/null +++ b/C_Program to Print ASCII Value @@ -0,0 +1,12 @@ +#include +int main() { + char c; + printf("Enter a character: "); + scanf("%c", &c); + + // %d displays the integer value of a character + // %c displays the actual character + printf("ASCII value of %c = %d", c, c); + + return 0; +} diff --git a/C_Program to find lenght of a string b/C_Program to find lenght of a string new file mode 100644 index 00000000..a20fd7c3 --- /dev/null +++ b/C_Program to find lenght of a string @@ -0,0 +1,10 @@ +#include +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/C_Program to perform arithmetic operations b/C_Program to perform arithmetic operations new file mode 100644 index 00000000..52112d93 --- /dev/null +++ b/C_Program to perform arithmetic operations @@ -0,0 +1,29 @@ +#include + +int main() +{ + int p, q; + int sum, sub, mul, mod; + float div; + + /** Input two numbers from user **/ + printf("Enter any two numbers::\n"); + scanf("%d%d", &p, &q, "\n"); + + /** Perform all arithmetic operations **/ + sum = p + q; + sub = p - q; + mul = p * q; + div = (float)p / q; + mod = p % q; + + /** Print result of all arithmetic operations **/ + printf("\n"); + printf("SUM %d + %d = %d\n", p, q, sum); + printf("DIFFERENCE %d - %d = %d\n", p, q, sub); + printf("PRODUCT %d * %d = %d\n", p, q, mul); + printf("QUOTIENT %d / %d = %f\n", p, q, div); + printf("MODULUS %d %% %d = %d\n", p, q, mod); + + return 0; +} diff --git a/C_length Of String b/C_length Of String new file mode 100644 index 00000000..5d3de3c2 --- /dev/null +++ b/C_length Of String @@ -0,0 +1,18 @@ +// C program to find the length of string +#include +#include + +int main() +{ + char Str[1000]; + int i; + + printf("Enter the String: "); + scanf("%s", Str); + + for (i = 0; Str[i] != '\0'; ++i); + + printf("Length of Str is %d", i); + + return 0; +} diff --git a/Calciii.cs b/Calciii.cs new file mode 100644 index 00000000..728563dc --- /dev/null +++ b/Calciii.cs @@ -0,0 +1,94 @@ +//Simple Calculator using c# that performs all the arithmetic operations +using System; + +namespace Calculator + +{ + + class Program + + { + + static void Main(string[] args) + + { + + // Declare variables and then initialize to zero. + + int num1 = 0; int num2 = 0; + + // Display title as the C# console calculator app. + + Console.WriteLine("Console Calculator in C#\r"); + + Console.WriteLine("------------------------\n"); + + // Ask the user to type the first number. + + Console.WriteLine("Type a number, and then press Enter"); + + num1 = Convert.ToInt32(Console.ReadLine()); + + // Ask the user to type the second number. + + Console.WriteLine("Type another number, and then press Enter"); + + num2 = Convert.ToInt32(Console.ReadLine()); + + // Ask the user to choose an option. + + Console.WriteLine("Choose an option from the following list:"); + + Console.WriteLine("\ta - Add"); + + Console.WriteLine("\ts - Subtract"); + + Console.WriteLine("\tm - Multiply"); + + Console.WriteLine("\td - Divide"); + + Console.Write("Your option? "); + + // Use a switch statement to do the math. + + switch (Console.ReadLine()) + + { + + case "a": + + Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2)); + + break; + + case "s": + + Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2)); + + break; + + case "m": + + Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2)); + + break; + + case "d": + + Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2)); + + break; + + } + + // Wait for the user to respond before closing. + + Console.Write("Press any key to close the Calculator console app..."); + + Console.ReadKey(); + + } + + } + +} diff --git a/Calculate Length of String without Using strlen() Function in C Lang b/Calculate Length of String without Using strlen() Function in C Lang new file mode 100644 index 00000000..a20fd7c3 --- /dev/null +++ b/Calculate Length of String without Using strlen() Function in C Lang @@ -0,0 +1,10 @@ +#include +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/Calculate compound interest b/Calculate compound interest new file mode 100644 index 00000000..ca6b213a --- /dev/null +++ b/Calculate compound interest @@ -0,0 +1,21 @@ +public class JavaExample { + + public void calculate(int p, int t, double r, int n) { + + double amount = p * Math.pow(1 + (r / n), n * t); + + double cinterest = amount - p; + + System.out.println("Compound Interest after " + t + " years: "+cinterest); + + System.out.println("Amount after " + t + " years: "+amount); + + } + + public static void main(String args[]) { + + JavaExample obj = new JavaExample(); obj.calculate(2000, 5, .08, 12); + + } + +} diff --git a/Calculate compound interest.java b/Calculate compound interest.java new file mode 100644 index 00000000..ca6b213a --- /dev/null +++ b/Calculate compound interest.java @@ -0,0 +1,21 @@ +public class JavaExample { + + public void calculate(int p, int t, double r, int n) { + + double amount = p * Math.pow(1 + (r / n), n * t); + + double cinterest = amount - p; + + System.out.println("Compound Interest after " + t + " years: "+cinterest); + + System.out.println("Amount after " + t + " years: "+amount); + + } + + public static void main(String args[]) { + + JavaExample obj = new JavaExample(); obj.calculate(2000, 5, .08, 12); + + } + +} diff --git a/Calculate power of a number using a while loop_Java b/Calculate power of a number using a while loop_Java new file mode 100644 index 00000000..f4b492e2 --- /dev/null +++ b/Calculate power of a number using a while loop_Java @@ -0,0 +1,17 @@ +public class Power { + + public static void main(String[] args) { + + int base = 3, exponent = 4; + + long result = 1; + + while (exponent != 0) + { + result *= base; + --exponent; + } + + System.out.println("Answer = " + result); + } +} diff --git a/Calculate power of number b/Calculate power of number new file mode 100644 index 00000000..59f8573c --- /dev/null +++ b/Calculate power of number @@ -0,0 +1,7 @@ +import math + +base_number = float(input("Enter the base number")) +exponent = float(input("Enter the exponent")) + +power = math.pow(base_number,exponent) +print("Power is =",power) diff --git a/Calculate the Power of a Number b/Calculate the Power of a Number new file mode 100644 index 00000000..e7d59525 --- /dev/null +++ b/Calculate the Power of a Number @@ -0,0 +1,16 @@ +#include +int main() { + int base, exp; + long long result = 1; + printf("Enter a base number: "); + scanf("%d", &base); + printf("Enter an exponent: "); + scanf("%d", &exp); + + while (exp != 0) { + result *= base; + --exp; + } + printf("Answer = %lld", result); + return 0; +} diff --git a/Calculate the Power of a Number Using Java b/Calculate the Power of a Number Using Java new file mode 100644 index 00000000..f4b492e2 --- /dev/null +++ b/Calculate the Power of a Number Using Java @@ -0,0 +1,17 @@ +public class Power { + + public static void main(String[] args) { + + int base = 3, exponent = 4; + + long result = 1; + + while (exponent != 0) + { + result *= base; + --exponent; + } + + System.out.println("Answer = " + result); + } +} diff --git a/Calculating power of a number in python b/Calculating power of a number in python new file mode 100644 index 00000000..f9da35a2 --- /dev/null +++ b/Calculating power of a number in python @@ -0,0 +1,3 @@ +number=input("Enter the number: ") +power=input("Enter the power: ") +print(f"{number} raised to the power {power} is {number**power}") diff --git a/Calculating the power of a number in python b/Calculating the power of a number in python new file mode 100644 index 00000000..8f3d3344 --- /dev/null +++ b/Calculating the power of a number in python @@ -0,0 +1,3 @@ +number = int(input("enter your nuumber: ")) +power=int(input("Enter the power: ")) +print(number**power) diff --git a/Calculator using Java b/Calculator using Java new file mode 100644 index 00000000..736bfb76 --- /dev/null +++ b/Calculator using Java @@ -0,0 +1,47 @@ +package practice; +import java.util.Scanner; +import java.util.ArrayList; +import java.lang.Math.*; +class Calculation{ + public int add(int a, int b) { + return a+b; + } + + public int sub(int a, int b) { + if(a>b) { + return a-b; + }else { + return b-a; + } + } + public int mul(int a, int b) { + return a*b; + } + public int div(int a, int b) { + if(a>b) { + return a/b; + }else { + return b/a; + } + } +} + + + +public class solution { + + public static void main(String[] args) { + // TODO Auto-generated method stub +Scanner sc = new Scanner(System.in); +Calculation cal = new Calculation(); +int x =sc.nextInt(); +int y=sc.nextInt(); +int add = cal.add(x,y ); +int sub = cal.sub(x, y); +int mul=cal.mul(x, y); +int div=cal.div(y, x); + +System.out.println("add is "+add+" sub is "+sub+"mul is "+mul+" div is "+div); +} + +} diff --git a/Check Triangle is Equilateral, Isosceles or Scalene (Type-Hard) b/Check Triangle is Equilateral, Isosceles or Scalene (Type-Hard) new file mode 100644 index 00000000..8c13667b --- /dev/null +++ b/Check Triangle is Equilateral, Isosceles or Scalene (Type-Hard) @@ -0,0 +1,34 @@ +// Java program for the above approach +class GFG{ + +// Function to check if the triangle +// is equilateral or isosceles or scalene +static void checkTriangle(int x, int y, int z) +{ + + // Check for equilateral triangle + if (x == y && y == z ) + System.out.println("Equilateral Triangle"); + + // Check for isoceles triangle + else if (x == y || y == z || z == x ) + System.out.println("Isoceles Triangle"); + + // Otherwise scalene triangle + else + System.out.println("Scalene Triangle"); +} + +// Driver Code +public static void main(String[] args) +{ + + // Given sides of triangle + int x = 8, y = 7, z = 9; + + // Function call + checkTriangle(x, y, z); +} +} + +// This code is contributed by jana_sayantan diff --git a/Check a triangle is equilateral, isosceles or scalene b/Check a triangle is equilateral, isosceles or scalene new file mode 100644 index 00000000..d3c22251 --- /dev/null +++ b/Check a triangle is equilateral, isosceles or scalene @@ -0,0 +1,11 @@ +print("Input lengths of the triangle sides: ") +x = int(input("x: ")) +y = int(input("y: ")) +z = int(input("z: ")) + +if x == y == z: + print("Equilateral triangle") +elif x==y or y==z or z==x: + print("isosceles triangle") +else: + print("Scalene triangle") diff --git a/Check number is even or odd b/Check number is even or odd new file mode 100644 index 00000000..a3ad4ec1 --- /dev/null +++ b/Check number is even or odd @@ -0,0 +1,8 @@ +#include +void main() +{ + int a; + printf("enter the number: "); + scanf("%d",&a); + a&1?printf("odd"):printf("even"); +} diff --git a/Checking whether a year is leap in python b/Checking whether a year is leap in python new file mode 100644 index 00000000..5e521a53 --- /dev/null +++ b/Checking whether a year is leap in python @@ -0,0 +1,12 @@ +def is_leap(year): + leap=False + if (year%400==0): + leap= False + elif(year%100==0): + leap= False + elif(year%4==0): + leap=True + return leap + +year = int(input()) +print(is_leap(year)) diff --git a/Checking whether given year is leap or not via python b/Checking whether given year is leap or not via python new file mode 100644 index 00000000..5e521a53 --- /dev/null +++ b/Checking whether given year is leap or not via python @@ -0,0 +1,12 @@ +def is_leap(year): + leap=False + if (year%400==0): + leap= False + elif(year%100==0): + leap= False + elif(year%4==0): + leap=True + return leap + +year = int(input()) +print(is_leap(year)) diff --git a/Circular Quee b/Circular Quee new file mode 100644 index 00000000..9110f596 --- /dev/null +++ b/Circular Quee @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +using namespace std; + +class cqueue{ +private: + int front,rear,cnt; + int arr[5]; +public: + cqueue(){ + cnt=0; + front =-1; + rear=-1; + for(int i=0;i<5;i++) + arr[i]=0; + } + + +bool isEmpty(){ + + if(front==-1 && rear==-1) + return true; + else + return false; +} + +bool isFull(){ + + if((rear+1)%5==front) + return true; + else + return false; +} + +void enqueue(int val){ + + if(isFull()) + cout<<"queue full"; + else if(isEmpty()){ + + front =rear=0; + arr[rear]=val; + } + else{ + rear = (rear+1)%5; + arr[rear]=val; + + } + cnt++; +} + +int dequeue(){ + + int x; + if(isEmpty()){ + cout<<"queue empty"<>option; + switch(option){ + case 0: + break; + case 1: + cout<<"enqueue operation"<>value; + q1.enqueue(value); + break; + case 2: + cout<<"dequeue operation:"<1;i--)//4,3 + { + no = (no*i); //20+6 + } + System.out.println("Factorial is : "+(no)); + } + +} diff --git a/Code for fibonacci series using java b/Code for fibonacci series using java new file mode 100644 index 00000000..3dc8557e --- /dev/null +++ b/Code for fibonacci series using java @@ -0,0 +1,20 @@ +import java.util.Scanner; +public class FibonacciSeries { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("Enter range for Fibonacci Series : "); + int no=sc.nextInt(); + int i=0,b=1; + int c=i+b; + System.out.println(i); + System.out.println(c); + for(i=0;i<=no;i++) + { + i=b; + b=c; + c=i+b; + System.out.println(c); + } + } + +} diff --git a/Code for sum of array elements b/Code for sum of array elements new file mode 100644 index 00000000..b42c2100 --- /dev/null +++ b/Code for sum of array elements @@ -0,0 +1,20 @@ +import java.util.Scanner; +public class AdditionOfElements { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter the no. of elements you want to add in array : "); + int n= sc.nextInt(); + int arr[] = new int[n]; + System.out.println("Enter the numbers : "); + for(int i=0;i +int main() { + int n, i; + unsigned long long fact = 1; + printf("Enter an integer: "); + scanf("%d", &n); + + // shows error if the user enters a negative integer + if (n < 0) + printf("Error! Factorial of a negative number doesn't exist."); + else { + for (i = 1; i <= n; ++i) { + fact *= i; + } + printf("Factorial of %d = %llu", n, fact); + } + + return 0; +} diff --git a/CoinChangeProblem.cpp b/CoinChangeProblem.cpp new file mode 100644 index 00000000..de9136dc --- /dev/null +++ b/CoinChangeProblem.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +int coinChange(int coins[],int n,int sum) +{ + // DP table to store data.. + int dp[n+1][sum+1]; + + for(int i=1;i<=n;i++) dp[i][0]=0; + + for(int i=0;i<=sum;i++) dp[0][i]=INT_MAX; + + for(int i=1;i<=n;i++) + { + for(int j=1;j<=sum;j++) + { + // is a valid operation.. + if(j-coins[i-1]>=0) + { + dp[i][j]=min(dp[i-1][j],1+dp[i][j-coins[i-1]]); + } + // is an invalid operation.. + else + { + dp[i][j]=dp[i-1][j]; + } + } + } + + // final answer.. + return dp[n][sum]; +} + +int main() +{ + int N,total; + + // Taking number of coins.. + cin>>N; + + int coins[N]; + + for(int i=0;i>coins[i]; + + // Required Sum.. + cin>>total; + + cout<y: + awins+=1 + elif x +#include +int convert(long long n); +int main() { + long long n; + printf("Enter a binary number: "); + scanf("%lld", &n); + printf("%lld in binary = %d in decimal", n, convert(n)); + return 0; +} + +int convert(long long n) { + int dec = 0, i = 0, rem; + while (n != 0) { + rem = n % 10; + n /= 10; + dec += rem * pow(2, i); + ++i; + } + return dec; +} diff --git a/Convert Decimal to Binary, Octal and Hexadecimal using python b/Convert Decimal to Binary, Octal and Hexadecimal using python new file mode 100644 index 00000000..a573dbd3 --- /dev/null +++ b/Convert Decimal to Binary, Octal and Hexadecimal using python @@ -0,0 +1,14 @@ +#convert decimal to binary,octal, hexadecimal +#taking user input + +decimal = int(input("Enter Decimal Value: \n")) +convert = int(input("Convert into: [1] Binary, [2] Octal, [3] Hexadecimal: ")) + +if convert == 1: + print("Binary valure is \n",bin(decimal)) +elif convert == 2: + print("Octal valure is \n",oct(decimal)) +elif convert == 3: + print("Hexadecimal Value is \n",hex(decimal)) +else: + print("Invalid Input") diff --git a/Convert a binary number to octal_Python 3 b/Convert a binary number to octal_Python 3 new file mode 100644 index 00000000..1a81c965 --- /dev/null +++ b/Convert a binary number to octal_Python 3 @@ -0,0 +1,71 @@ +# Python3 implementation to convert a binary number +# to octal number + +# function to create map between binary +# number and its equivalent octal +def createMap(bin_oct_map): + bin_oct_map["000"] = '0' + bin_oct_map["001"] = '1' + bin_oct_map["010"] = '2' + bin_oct_map["011"] = '3' + bin_oct_map["100"] = '4' + bin_oct_map["101"] = '5' + bin_oct_map["110"] = '6' + bin_oct_map["111"] = '7' + +# Function to find octal equivalent of binary +def convertBinToOct(bin): + l = len(bin) + + # length of string before '.' + t = -1 + if '.' in bin: + t = bin.index('.') + len_left = t + else: + len_left = l + + # add min 0's in the beginning to make + # left substring length divisible by 3 + for i in range(1, (3 - len_left % 3) % 3 + 1): + bin = '0' + bin + + # if decimal point exists + if (t != -1): + + # length of string after '.' + len_right = l - len_left - 1 + + # add min 0's in the end to make right + # substring length divisible by 3 + for i in range(1, (3 - len_right % 3) % 3 + 1): + bin = bin + '0' + + # create dictionary between binary and its + # equivalent octal code + bin_oct_map = {} + createMap(bin_oct_map) + i = 0 + octal = "" + + while (True) : + + # one by one extract from left, substring + # of size 3 and add its octal code + octal += bin_oct_map[bin[i:i + 3]] + i += 3 + if (i == len(bin)): + break + + # if '.' is encountered add it to result + if (bin[i] == '.'): + octal += '.' + i += 1 + + # required octal number + return octal + +# Driver Code +bin = "1111001010010100001.010110110011011" +print("Octal number = ", + convertBinToOct(bin)) diff --git a/Convert binary to decimal using C b/Convert binary to decimal using C new file mode 100644 index 00000000..8099518e --- /dev/null +++ b/Convert binary to decimal using C @@ -0,0 +1,21 @@ +#include +#include +int convert_to_dec(long long num); +int main() { + long long num; + printf("Enter a binary number: "); + scanf("%lld", &num); + printf("%d is the decimal value.", convert_to_dec(num)); + return 0; +} + +int convert_to_dec(long long num) { + int dec = 0, i = 0, rem; + while (num != 0) { + rem = num % 10; + num /= 10; + dec += rem * pow(2, i); + ++i; + } + return dec; +} diff --git a/Convert binary to octal, hexadecimal and decimal number [Python] b/Convert binary to octal, hexadecimal and decimal number [Python] new file mode 100644 index 00000000..2b85718f --- /dev/null +++ b/Convert binary to octal, hexadecimal and decimal number [Python] @@ -0,0 +1,6 @@ +inpt = str(input("Enter binary number\n")) + +print("The binary value of", inpt, "is:") +print(int(inpt, 2),"in decimal") +print(oct(int(inpt)), "in octal.") +print(hex(int(inpt)), "in hexadecimal.") diff --git a/Convert decimal to binary using java b/Convert decimal to binary using java new file mode 100644 index 00000000..e25b7d73 --- /dev/null +++ b/Convert decimal to binary using java @@ -0,0 +1,41 @@ +public class DecimalToBinaryExample2{ + +public static void toBinary(int decimal){ + + int binary[] = new int[40]; + + int index = 0; + + while(decimal > 0){ + + binary[index++] = decimal%2; + + decimal = decimal/2; + + } + + for(int i = index-1;i >= 0;i--){ + + System.out.print(binary[i]); + + } + +System.out.println();//new line + +} + +public static void main(String args[]){ + +System.out.println("Decimal of 10 is: "); + +toBinary(10); + +System.out.println("Decimal of 21 is: "); + +toBinary(21); + +System.out.println("Decimal of 31 is: "); + +toBinary(31); + +}} diff --git a/Converting Binary into Decimal numbers b/Converting Binary into Decimal numbers new file mode 100644 index 00000000..ec7d0a47 --- /dev/null +++ b/Converting Binary into Decimal numbers @@ -0,0 +1,21 @@ +#include + +int main() + +{ + int n,s=0,i=0; + + printf("Enter the binary numbers:"); + scanf("%d",&n); + + while(n>0) + { + s=s+(n%10)*pow(2,i); + n=n/10; + i++; + } + + printf("Entered number in decimal is %d",s); + + getch(); +} diff --git a/Count of Leap Years in a given year range b/Count of Leap Years in a given year range new file mode 100644 index 00000000..b7cebb12 --- /dev/null +++ b/Count of Leap Years in a given year range @@ -0,0 +1,33 @@ + + +#include + +using namespace std; + + +int calNum(int year) +{ + return (year / 4) - (year / 100) + + (year / 400); +} + + +void leapNum(int l, int r) +{ + l--; + int num1 = calNum(r); + int num2 = calNum(l); + cout << num1 - num2 << endl; +} + + +int main() +{ + int l1 = 1, r1 = 400; + leapNum(l1, r1); + + int l2 = 400, r2 = 2000; + leapNum(l2, r2); + + return 0; +} diff --git a/Count of vowel and Consonants in string b/Count of vowel and Consonants in string new file mode 100644 index 00000000..40e599e8 --- /dev/null +++ b/Count of vowel and Consonants in string @@ -0,0 +1,10 @@ +s1 = input("enter a string ") +vc, cc = 0, 0 + +for s in s1: + if s.isalpha(): + if s in [ 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' ] : + vc = vc + 1 + else: + cc = cc + 1 +print("vowels = ", vc, " consonants = ", cc) diff --git a/Count of vowels & consonants in string b/Count of vowels & consonants in string new file mode 100644 index 00000000..4f6588e3 --- /dev/null +++ b/Count of vowels & consonants in string @@ -0,0 +1,28 @@ +public class CountVowelConsonant { + public static void main(String[] args) { + + //Counter variable to store the count of vowels and consonant + int vCount = 0, cCount = 0; + + //Declare a string + String str = "This is a really simple sentence"; + + //Converting entire string to lower case to reduce the comparisons + str = str.toLowerCase(); + + for(int i = 0; i < str.length(); i++) { + //Checks whether a character is a vowel + if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') { + //Increments the vowel counter + vCount++; + } + //Checks whether a character is a consonant + else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') { + //Increments the consonant counter + cCount++; + } + } + System.out.println("Number of vowels: " + vCount); + System.out.println("Number of consonants: " + cCount); + } +} diff --git a/Count the maximum unit of water trapped in between the buildings b/Count the maximum unit of water trapped in between the buildings new file mode 100644 index 00000000..e0922e56 --- /dev/null +++ b/Count the maximum unit of water trapped in between the buildings @@ -0,0 +1,52 @@ +Problem Statement : +You are give a N numver of buildings with may be simmilar or different heights and an array denoting the all heights. + + +\\\\\\\\\\\\\\\\\\\\\\\ + +#include +#include +using namespace std; +int main() +{ + int n;//there are n buildings + cin>>n; + int arr[n];//array to store the size of n buldings + for(int i=0;i>arr[i]; + + } + int left[n],right[n]; //these are the two arrays which whicha are used to precompute the height of the leftmost highest and the rightmost highest buildings + int s=arr[0]; + for(int i=0;i=0;i--) + { + if(arr[i]>d) + { + d=arr[i]; + } + right[i]=d; + + } + + int water = 0; // denotes the maximum amout of trapped water + + for(int i=0;i +using namespace std; +// Counting the number of setbits using AND opreator(&) and Bitshift(>>) +int countsetbits(int n) +{ + int ans; + while(n!=0){ + ans+=(n&1); + n=n>>1;//using right shift +} + return ans; +} +// More faster approach +int countsetbitsfaster (int n) +{ + int a; + while(n>0) + { + n=n&(n-1);//removes the set bit from right to left + a++; + } + +return a; +} +int main() +{ + int n = 13; + + cout< +#include +#include +#include +using namespace std; +int main() +{ + +string s; +cin>>s; +int arr[26]; +memset(arr,0,sizeof(arr)); +int n=s.length(); +for(int i=0;i0) + { + cout< + +int main(){ +int n,count=0; + +printf("Enter an integer:"); +scanf("%d",&n); + +while(n!=0){ +n/=10; +count++; +} + +printf("Number of digits:%d",count); +} diff --git a/Counting valleys in c b/Counting valleys in c new file mode 100644 index 00000000..66326fe9 --- /dev/null +++ b/Counting valleys in c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(){ + int n,i,u=1,d=-1,sum=0,countv=0,counth=0; + + scanf("%d",&n); + char steps[n]; + + scanf("%s",steps); + + for(i=0;i +using namespace std; + +float Calculatepower(float base, int exponent) { + float power = 1; + while (exponent != 0) { + power *= base; + --exponent; + } + return power; +} + + +int main() +{ + int exponent; + float base; + cin >> base; + cin >> exponent; + float power = Calculatepower(base, exponent); + cout << power; + return 0; +} diff --git a/Cpp Program to reverse the array b/Cpp Program to reverse the array new file mode 100644 index 00000000..28b5eaec --- /dev/null +++ b/Cpp Program to reverse the array @@ -0,0 +1,22 @@ +#include +using namespace std; + +void reverseArray(int arr[], int n){ + int i = 0, j = n-1; + while(i +int fib(int m){ +if(m==0||m==1) + return m; +else + return(fib(m-1)+fib(m-2)); +} +int main(){ + int n; + scanf("%d",&n); + int res=fib(n); + printf("%d",res); + return 0; + } diff --git a/Create Looping Array Elements b/Create Looping Array Elements new file mode 100644 index 00000000..db6c736d --- /dev/null +++ b/Create Looping Array Elements @@ -0,0 +1,9 @@ +var fruits, text, fLen, i; +fruits = ["Banana", "Orange", "Apple", "Mango"]; +fLen = fruits.length; + +text = "
    "; +for (i = 0; i < fLen; i++) { + text += "
  • " + fruits[i] + "
  • "; +} +text += "
"; diff --git a/Create PHP Casting Strings and Floats to Integers b/Create PHP Casting Strings and Floats to Integers new file mode 100644 index 00000000..92950c4b --- /dev/null +++ b/Create PHP Casting Strings and Floats to Integers @@ -0,0 +1,13 @@ + "; + +// Cast string to int +$x = "23465.768"; +$int_cast = (int)$x; +echo $int_cast; +?> diff --git a/Create Program to Print Leap Years using java b/Create Program to Print Leap Years using java new file mode 100644 index 00000000..24aeeeb9 --- /dev/null +++ b/Create Program to Print Leap Years using java @@ -0,0 +1,31 @@ +import java.util.Scanner; + +public class PrintLeapYear { + + public static void main(String[] args) { + + int startYear, endYear, i; + + //create a scanner object to get the input + Scanner in = new Scanner(System.in); + + //get the start year from user + System.out.print("Enter the Start Year:"); + startYear = in.nextInt(); + + //get the end year from user + System.out.print("Enter the End Year:"); + endYear = in.nextInt(); + + //print the leap years + System.out.println("Leap years:"); + + //loop through between start and end year + for (i = startYear; i <= endYear; i++) { + + //find the year is leap year or not, if yes print it + if ( (0 == i % 4) && (0 != i % 100) || (0 == i % 400) ){ + System.out.println(i); + } + } +} diff --git a/Create Repeating Numbers b/Create Repeating Numbers new file mode 100644 index 00000000..b3d0fd94 --- /dev/null +++ b/Create Repeating Numbers @@ -0,0 +1,18 @@ +public class RepeatingNumbers { + + public static void print(int n) { + //Write your code here + int val=1; + for(int i=1;i<=n;i++){ + for(int j=1;j<=Math.pow(2,i-1);j++){ + System.out.print(val++); + if(val==10){ + val=1; + } + } + System.out.println(); + } + + } + +} diff --git a/Create a program to find lenght of a string length in javascript.js b/Create a program to find lenght of a string length in javascript.js new file mode 100644 index 00000000..3f8055cd --- /dev/null +++ b/Create a program to find lenght of a string length in javascript.js @@ -0,0 +1,22 @@ + + + + + + Program to find string length + + +

Program to find string length

+String: + + + + diff --git a/Creating a matrix multiplications b/Creating a matrix multiplications new file mode 100644 index 00000000..786b5649 --- /dev/null +++ b/Creating a matrix multiplications @@ -0,0 +1,43 @@ +// C program to multiply two square matrices. +#include +#define N 4 + +// This function multiplies mat1[][] and mat2[][], +// and stores the result in res[][] +void multiply(int mat1[][N], int mat2[][N], int res[][N]) +{ + int i, j, k; + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) { + res[i][j] = 0; + for (k = 0; k < N; k++) + res[i][j] += mat1[i][k] * mat2[k][j]; + } + } +} + +int main() +{ + int mat1[N][N] = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 }, + { 4, 4, 4, 4 } }; + + int mat2[N][N] = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 }, + { 4, 4, 4, 4 } }; + + int res[N][N]; // To store result + int i, j; + multiply(mat1, mat2, res); + + printf("Result matrix is \n"); + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) + printf("%d ", res[i][j]); + printf("\n"); + } + + return 0; +} diff --git a/Cube of a number b/Cube of a number new file mode 100644 index 00000000..c91dee9f --- /dev/null +++ b/Cube of a number @@ -0,0 +1,15 @@ +import java.util.*; +class Cube +{ +public static void main(String args[]) +{ +int i,num,sq; +for(i=1;i<10;i++) +{ +num=i*i*i; +sq=(int)Math.sqrt(num); +if(sq*sq==num) +System.out.print(num); +} +} +} diff --git a/DecimalToBinary b/DecimalToBinary new file mode 100644 index 00000000..bbef45b3 --- /dev/null +++ b/DecimalToBinary @@ -0,0 +1,12 @@ +import java.util.*; +import java.lang.*; + +class deciToBin { + + public static void main (String[] args) { + Scanner sc=new Scanner(System.in); + String binaryString=sc.next(); + int decimal=Integer.parseInt(binaryString,2); + System.out.println(decimal); + } +} diff --git a/Deletion of BST b/Deletion of BST new file mode 100644 index 00000000..32c6ca4d --- /dev/null +++ b/Deletion of BST @@ -0,0 +1,153 @@ +#include +using namespace std; + +bool c=false; + +struct node +{ + int data; + node* left; + node* right; +}; + +struct node* getNode(int data) +{ + node* newNode=new node(); + newNode->data=data; + newNode->left=NULL; + newNode->right=NULL; + return newNode; +} + +void inorder(struct node* root) +{ + if (root != NULL) + { + inorder(root->left); + cout<data<<" "; + inorder(root->right); + } +} + +node* findMin(node*root) +{ + while(root->left!=NULL) + { + root=root->left; + } + return root; +} + +struct node* Insert(struct node* root, int data) +{ + if (root == NULL) + return getNode(data); + + if (data < root->data) + root->left = Insert(root->left, data); + else if (data > root->data) + root->right = Insert(root->right, data); + + return root; +} + +bool Search(node*root,int value) +{ + if(root==NULL) + return false; + else if(root->data == value) + { + return true; + } + else if(value < root-> data) + Search(root->left,value); + else if(value > root->data) + Search(root->right,value); +} + + +node* Delete( node* root,int value) +{ + c=Search(root,value); + if(root==NULL) + return root; + else if(value< root->data) + { + root->left= Delete(root->left,value); + } + else if(value> root->data) + { + root->right= Delete(root->right,value); + } + + // Node deletion + else + { + //case 1: Leaf Node + if(root->left==NULL&&root->right==NULL) + { + delete root; + root=NULL; + return root; + } + //case 2: one child + else if(root->left==NULL) + { + struct node* temp=root; + root=root->right; + delete temp; + return root; + } + else if(root->right==NULL) + { + struct node* temp=root; + root=root->left; + delete temp; + return root; + } + //case 3: 2 child + else + { + struct node*temp=findMin(root->right); + root->data=temp->data; + root->right=Delete(root->right,temp->data); + } + } + return root; + +} + + +int main() +{ + node* root=NULL; + root=Insert(root,20); + Insert(root,15); + Insert(root,25); + Insert(root,18); + Insert(root,10); + Insert(root,16); + Insert(root,19); + Insert(root,17); + + cout<<"Before Deletion: "< +#include +using namespace std; +#define INFINITY 9999 +#define max 5 + +void dijkstra(int G[max][max],int n,int startnode) +{ + int cost[max][max],distance[max],pred[max]; + int visited[max],count,mindistance,nextnode,i,j; + for(i=0;i +#define n 5 +int compltedPhilo = 0,i; +struct fork +{ + int taken; + } +ForkAvil[n]; + +struct philosp +{ + int left; + int right; +} +Philostatus[n]; + +void goForDinner(int philID) +{ + if(Philostatus[philID].left==10 && Philostatus[philID].right==10) + printf("Philosopher %d completed his dinner\n",philID+1); + + else if(Philostatus[philID].left==1 && Philostatus[philID].right==1) +{ + + printf("Philosopher %d completed his dinner\n",philID+1); + + Philostatus[philID].left = Philostatus[philID].right = 10; + int otherFork = philID-1; + + if(otherFork== -1) + otherFork=(n-1); + + ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; + printf("Philosopher %d released fork %d and fork %d\n",philID+1,philID+1,otherFork+1); + compltedPhilo++; + } + else if(Philostatus[philID].left==1 && Philostatus[philID].right==0) + { + if(philID==(n-1)){ + if(ForkAvil[philID].taken==0){ + ForkAvil[philID].taken = Philostatus[philID].right = 1; + printf("Fork %d taken by philosopher %d\n",philID+1,philID+1); + } + else + { + printf("Philosopher %d is waiting for fork %d\n",philID+1,philID+1); + } + } + else + { + int dupphilID = philID; + philID-=1; + + if(philID== -1) + philID=(n-1); + + if(ForkAvil[philID].taken == 0){ + ForkAvil[philID].taken = Philostatus[dupphilID].right = 1; + printf("Fork %d taken by Philosopher %d\n",philID+1,dupphilID+1); + }else{ + printf("Philosopher %d is waiting for Fork %d\n",dupphilID+1,philID+1); + } + } + } + else if(Philostatus[philID].left==0){ + if(philID==(n-1)){ + if(ForkAvil[philID-1].taken==0){ + ForkAvil[philID-1].taken = Philostatus[philID].left = 1; + printf("Fork %d taken by philosopher %d\n",philID,philID+1); + }else{ + printf("Philosopher %d is waiting for fork %d\n",philID+1,philID); + } + }else{ + if(ForkAvil[philID].taken == 0){ + ForkAvil[philID].taken = Philostatus[philID].left = 1; + printf("Fork %d taken by Philosopher %d\n",philID+1,philID+1); + }else{ + printf("Philosopher %d is waiting for Fork %d\n",philID+1,philID+1); + } + } + }else{} +} + +int main() +{ + for(i=0;i + +int main() { + int low, high, i, flag; + printf("Enter two numbers(intervals): "); + scanf("%d %d", &low, &high); + printf("Prime numbers between %d and %d are: ", low, high); + + // iteration until low is not equal to high + while (low < high) { + flag = 0; + + // ignore numbers less than 2 + if (low <= 1) { + ++low; + continue; + } + + // if low is a non-prime number, flag will be 1 + for (i = 2; i <= low / 2; ++i) { + + if (low % i == 0) { + flag = 1; + break; + } + } + + if (flag == 0) + printf("%d ", low); + + // to check prime for the next number + // increase low by 1 + ++low; + } + + return 0; +} diff --git a/Euler's Method to find the highest common factor of two numbers b/Euler's Method to find the highest common factor of two numbers new file mode 100644 index 00000000..24b35734 --- /dev/null +++ b/Euler's Method to find the highest common factor of two numbers @@ -0,0 +1,14 @@ +#include +using namespace std; + +int hcf(int a,int b) +{ + +return b==0?a:hcf(b,a%b); +} +int main() +{ + int a,b; + cin>>a>>b; + cout< +#include +#include + +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) { + key = arr[i]; + j = i - 1; + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } +} + +void printArray(int arr[], int n) +{ + int i; + for (i = 0; i < n; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +int main() +{ + clock_t start,end; + double cpu_time_used; + + int arr[] = { 99,40,67,33,2 }; + printf("Old Array:- 99 40 67 33 2\n"); + int n = sizeof(arr) / sizeof(arr[0]); + start = clock(); + insertionSort(arr, n); + printf("Sorted Array:- "); + printArray(arr, n); + end = clock(); + cpu_time_used = ((double)(end - start))/CLOCKS_PER_SEC; + printf("Execution Time:- %lf",cpu_time_used); + + return 0; +} diff --git a/ExecutionTimeQuickSort.c b/ExecutionTimeQuickSort.c new file mode 100644 index 00000000..0506e58c --- /dev/null +++ b/ExecutionTimeQuickSort.c @@ -0,0 +1,60 @@ +// Quick sort with Execution Time in C + +#include +#include +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} +int i,j; +int part(int arr[], int low, int high) +{ + int pivot = arr[high]; + int i = (low - 1); + + for (j = low; j < high; j++) + { + if (arr[j] <= pivot) + { + i++; + swap(&arr[i], &arr[j]); + } + } + swap(&arr[i + 1], &arr[high]); + return (i + 1); +} +void quickSort(int arr[], int low, int high) +{ + if (low < high) + { + int pivot = part(arr, low, high); + quickSort(arr, low, pivot - 1); + quickSort(arr, pivot + 1, high); + } +} + +void printArray(int arr[], int size) +{ + for (i = 0; i < size; ++i) + printf("%d ", arr[i]); + printf("\n"); +} + +int main() +{ + clock_t start,end; + double cpu_time_used; + + int arr[] = {64, 34, 25, 12, 22, 11, 90}; + printf("Old Array:- 64 34 25 12 22 11 90\n"); + int n = sizeof(arr) / sizeof(arr[0]); + start = clock(); + quickSort(arr, 0, n - 1); + printf("Sorted array:- "); + printArray(arr, n); + end = clock(); + cpu_time_used = ((double)(end - start))/CLOCKS_PER_SEC; + printf("Execution Time:- %lf",cpu_time_used); +} diff --git a/FIBONANCI b/FIBONANCI new file mode 100644 index 00000000..9c82a47a --- /dev/null +++ b/FIBONANCI @@ -0,0 +1,33 @@ +#include +using namespace std; + +int main() +{ + int n, t1 = 0, t2 = 1, nextTerm = 0; + + cout << "Enter the number of terms: "; + cin >> n; + + cout << "Fibonacci Series: "; + + for (int i = 1; i <= n; ++i) + { + // Prints the first two terms. + if(i == 1) + { + cout << " " << t1; + continue; + } + if(i == 2) + { + cout << t2 << " "; + continue; + } + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + + cout << nextTerm << " "; + } + return 0; +} diff --git a/Factor Number b/Factor Number new file mode 100644 index 00000000..97a59149 --- /dev/null +++ b/Factor Number @@ -0,0 +1,22 @@ +/* C Program to Find factors of a number */ + +#include + +int main() +{ + int i, Number; + + printf("\n Please Enter any number to Find Factors \n"); + scanf("%d", &Number); + + printf("\n Factors of the Given Number are:\n"); + for (i = 1; i <= Number; i++) + { + if(Number%i == 0) + { + printf(" %d ", i); + } + } + + return 0; +} diff --git a/Factorial Of Number b/Factorial Of Number index b8476f7c..49cf63be 100644 --- a/Factorial Of Number +++ b/Factorial Of Number @@ -18,3 +18,13 @@ int factorial(int a) { return factorial(a)*factorial(a-1); } } + + +#using python to find factorial of a number +def factorial(n): + if(n <= 1): + return 1 + else: + return(n*factorial(n-1)) +n = int(input("Enter number:") +print(factorial(n)) diff --git a/Factorial in python b/Factorial in python new file mode 100644 index 00000000..7708570c --- /dev/null +++ b/Factorial in python @@ -0,0 +1,6 @@ +num1=int(input("Enter the number : ")) +fact=1; +for i in range(num1,0,-1): + fact=fact*num1 + num1-=1 +print("The factorial of the number is",fact) diff --git a/Factorial of a Given Number b/Factorial of a Given Number new file mode 100644 index 00000000..28ea0c46 --- /dev/null +++ b/Factorial of a Given Number @@ -0,0 +1,19 @@ +fun findFactorial(num: Int): Long { + if (num < 1) { + println("Enter the Non Negative Number") + } + var factorial: Long = 1 + for (i in num downTo 2) { + factorial = factorial * i + } + return factorial +} + +fun main(args: Array) { + val num = 3 + println("Factorial of " + num + " is : " + findFactorial(num)) +} + + +Output +Factorial of 3 is : 6 diff --git a/Factorial of a given number b/Factorial of a given number new file mode 100644 index 00000000..f1426e56 --- /dev/null +++ b/Factorial of a given number @@ -0,0 +1,19 @@ +#include +using namespace std; + +int main() +{ + unsigned int input; + unsigned long long fact = 1; + + cout << "Enter a positive integer: "; + cin >> input; + + for(int i = 1; i <=n; ++i) + { + fact *= i; + } + + cout << "Factorial of " << input << " = " << fact; + return 0; +} diff --git a/Factorial of a number by python b/Factorial of a number by python new file mode 100644 index 00000000..7708570c --- /dev/null +++ b/Factorial of a number by python @@ -0,0 +1,6 @@ +num1=int(input("Enter the number : ")) +fact=1; +for i in range(num1,0,-1): + fact=fact*num1 + num1-=1 +print("The factorial of the number is",fact) diff --git a/Factorial of a number using python b/Factorial of a number using python new file mode 100644 index 00000000..bb0fa689 --- /dev/null +++ b/Factorial of a number using python @@ -0,0 +1,9 @@ +#using python +def factorial(n): + if(n <= 1): + return 1 + else: + return(n*factorial(n-1)) +n = int(input("Enter number:")) +print("The required output:") +print(factorial(n)) diff --git a/Factorial of a number via python b/Factorial of a number via python new file mode 100644 index 00000000..7708570c --- /dev/null +++ b/Factorial of a number via python @@ -0,0 +1,6 @@ +num1=int(input("Enter the number : ")) +fact=1; +for i in range(num1,0,-1): + fact=fact*num1 + num1-=1 +print("The factorial of the number is",fact) diff --git a/Factorial of a number. b/Factorial of a number. new file mode 100644 index 00000000..70220ed2 --- /dev/null +++ b/Factorial of a number. @@ -0,0 +1,10 @@ +num = int(input("Enter a number: ")) +factorial = 1 +if num < 0: + print("Sorry, factorial does not exist for negative numbers") +elif num == 0: + print("The factorial of 0 is 1") +else: + for i in range(1,num + 1): + factorial = factorial*i + print("The factorial of",num,"is",factorial) diff --git a/Factorial of number b/Factorial of number new file mode 100644 index 00000000..8d3bafcd --- /dev/null +++ b/Factorial of number @@ -0,0 +1,14 @@ +# change the value for a different result +num = 9 + +factorial = 3 + +# check if the number is negative, positive or zero +if num < 0: + print("Sorry, factorial does not exist for negative numbers") +elif num == 0: + print("The factorial of 0 is 1") +else: + for i in range(3,num + 3): + factorial = factorial*i + print("The factorial of",num,"is",factorial) diff --git a/Factorial of number sg b/Factorial of number sg new file mode 100644 index 00000000..07b09ea2 --- /dev/null +++ b/Factorial of number sg @@ -0,0 +1,19 @@ +#include +int main() { + int n, i; + unsigned long long fact = 1; + printf("Enter an integer: "); + scanf("%d", &n); + + // shows error if the user enters a negative integer + if (n < 0) + printf("Error! Factorial of a negative number doesn't exist."); + else { + for (i = 1; i <= n; ++i) { + fact *= i; + } + printf("Factorial of %d = %llu", n, fact); + } + + return 0; +} diff --git a/Factorial of number.py b/Factorial of number.py new file mode 100644 index 00000000..c18e6746 --- /dev/null +++ b/Factorial of number.py @@ -0,0 +1,19 @@ +#Python program to find the factorial of a number provided by the user. + +# change the value for a different result +num = 7 + +# To take input from the user +#num = int(input("Enter a number: ")) + +factorial = 1 + +# check if the number is negative, positive or zero +if num < 0: + print("Sorry, factorial does not exist for negative numbers") +elif num == 0: + print("The factorial of 0 is 1") +else: + for i in range(1,num + 1): + factorial = factorial*i + print("The factorial of",num,"is",factorial) diff --git a/Factorial program b/Factorial program new file mode 100644 index 00000000..4094bc8f --- /dev/null +++ b/Factorial program @@ -0,0 +1,23 @@ +#include + +int main() + +{ + + int i,fact=1,number; + + printf("Enter a number: "); + + scanf("%d",&number); + + for(i=1;i<=number;i++){ + + fact=fact*i; + + } + + printf("Factorial of %d is: %d",number,fact); + +return 0; + +} diff --git a/Factorial program in c b/Factorial program in c new file mode 100644 index 00000000..1663258b --- /dev/null +++ b/Factorial program in c @@ -0,0 +1,12 @@ +#include +int main() +{ + int i,fact=1,number; + printf("Enter a number: "); + scanf("%d",&number); + for(i=1;i<=number;i++){ + fact=fact*i; + } + printf("Factorial of %d is: %d",number,fact); +return 0; +} diff --git a/Factorial.cpp b/Factorial.cpp new file mode 100644 index 00000000..6c91a4cd --- /dev/null +++ b/Factorial.cpp @@ -0,0 +1,31 @@ +// FACTORIAL OF NUMBER + +#include +#define ll unsigned long long int +using namespace std; +clock_t start; +ll rcrsn(ll n){ + start=clock(); + if(n>0) return n*rcrsn(n-1); + else return 1; + start=clock()-start; +} +int main(){ + ll n; cin>>n; + + // ITERATIVE + ll ans=1,t=n; + start=clock(); + while(t)ans*=t,t--; + //cout<0){ + n3 = n1 + n2; + n1 = n2; + n2 = n3; + System.out.print(" "+n3); + printFibonacci(count-1); + } + } + public static void main(String args[]){ + int count=10; + System.out.print(n1+" "+n2);//printing 0 and 1 + printFibonacci(count-2);//n-2 because 2 numbers are already printed + } +} diff --git a/Fibonacci Series In C++ b/Fibonacci Series In C++ new file mode 100644 index 00000000..3950198c --- /dev/null +++ b/Fibonacci Series In C++ @@ -0,0 +1,21 @@ +//Fibonacci Series using Recursion +#include +using namespace std; + +int fib(int n) +{ + if (n <= 1) + return n; + return fib(n-1) + fib(n-2); +} + +int main () +{ + int n = 9; + cout << fib(n); + getchar(); + return 0; +} + +// This code is contributed +// by Akanksha Rai diff --git a/Fibonacci Series Using Dynamic Programming b/Fibonacci Series Using Dynamic Programming new file mode 100644 index 00000000..8d75d235 --- /dev/null +++ b/Fibonacci Series Using Dynamic Programming @@ -0,0 +1,40 @@ +import java.util.*; +class Solution { + + // Function to find the fibonacci Series + static int fib(int n) + { + + // Declare an array to store + // Fibonacci numbers. + // 1 extra to handle case, n = 0 + int Fibonacci[] = new int[n + 2]; + + int i; + + // 0th and 1st number of + // the series are 0 and 1 + Fibonacci[0] = 0; + Fibonacci[1] = 1; + + for (i = 2; i <= n; i++) { + + // Add the previous 2 numbers + // in the series and store it + Fibonacci[i] = Fibonacci[i - 1] + Fibonacci[i - 2]; + } + + // Nth Fibonacci Number + return Fibonacci[n]; + } + + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + + + for (int i = 0; i < N; i++) + System.out.print(fib(i) + " "); + } +} diff --git a/Fibonacci Series in C b/Fibonacci Series in C new file mode 100644 index 00000000..1a7861e7 --- /dev/null +++ b/Fibonacci Series in C @@ -0,0 +1,16 @@ +#include +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 +using namespace std; + +int main() +{ + int n,arr[50]; + cout<<"Enter No. of Fabionacci term: "; + cin>>n; + arr[0]=0; + arr[1]=1; + cout< + +unsigned int factorial(unsigned int n) +{ + if (n == 0) + return 1; + return n * factorial(n - 1); +} + +int main() +{ + int num = 10; + printf("Factorial of %d is %d", num, factorial(num)); + return 0; +} diff --git a/FibonacciSeries.cpp b/FibonacciSeries.cpp new file mode 100644 index 00000000..cb212a0c --- /dev/null +++ b/FibonacciSeries.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +int fib(int x) { + if((x==1)||(x==0)) { + return(x); + }else { + return(fib(x-1)+fib(x-2)); + } +} +int main() { + int x , i=0; + cout << "Enter the number of terms of series : "; + cin >> x; + cout << "\nFibonnaci Series : "; + while(i < x) { + cout << " " << fib(i); + i++; + } + return 0; +} diff --git a/Fibonacci_C_codebybipradeep b/Fibonacci_C_codebybipradeep new file mode 100644 index 00000000..c5673dd0 --- /dev/null +++ b/Fibonacci_C_codebybipradeep @@ -0,0 +1,37 @@ +// C program to print fibonacci +// series using recursion +#include + +// Recursive function to print +// Fibonacci series +void fib(int a, int b, int sum, int N) +{ + + // Print first N term of the series + if (N != 0) { + + printf(" %d", a); + sum = a + b; + a = b; + b = sum; + + // Decrement N + N--; + + // recursive call function fib + fib(a, b, sum, N); + } +} + +// Driver Code +int main() +{ + // Given Number N + int N = 10; + + // First term as 0 + // Second term as 1 and + // Sum of first and second term + fib(0, 1, 0, N); + return 0; +} diff --git a/Fibonacci_Python_Code b/Fibonacci_Python_Code new file mode 100644 index 00000000..9f6b1b3d --- /dev/null +++ b/Fibonacci_Python_Code @@ -0,0 +1,23 @@ +# Program to display the Fibonacci sequence up to n-th term + +nterms = int(input("How many terms? ")) + +# first two terms +n1, n2 = 0, 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence:") + while count < nterms: + print(n1) + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/Fibonacci_series b/Fibonacci_series new file mode 100644 index 00000000..cd8dcb20 --- /dev/null +++ b/Fibonacci_series @@ -0,0 +1,16 @@ +include +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 +int fib(int m){ +if(m==0||m==1) + return m; +else + return (fib(m-1)+fib(m-2)); +} + +int main() +{ + int n; + printf("Enter the number"); + scanf("%d",&n); + int res=fib(n); + printf("%d",res); +} diff --git a/Fibonnaci Series/C++ b/Fibonnaci Series/C++ index 9c82a47a..f7cb6b4d 100644 --- a/Fibonnaci Series/C++ +++ b/Fibonnaci Series/C++ @@ -27,7 +27,7 @@ int main() t1 = t2; t2 = nextTerm; - cout << nextTerm << " "; + cout << nextTerm is << " "; } return 0; } diff --git a/Fibonnaci Series/C_using_arrays.c b/Fibonnaci Series/C_using_arrays.c new file mode 100644 index 00000000..f0cea498 --- /dev/null +++ b/Fibonnaci Series/C_using_arrays.c @@ -0,0 +1,25 @@ +/** + Fibonacci series in C++ using array by codebind.com + */ + +#include + +int main(void) { + int i,number; + long int arr[40]; + + std::cout << "Enter the number : "<< std::endl; + std::cin >> number; + + arr[0]=0; + arr[1]=1; + + for(i = 2; i< number ; i++){ + arr[i] = arr[i-1] + arr[i-2]; + } + + std::cout << "Fibonacci series is: "; + for(i=0; i< number; i++) + std::cout << arr[i] << std::endl; + return 0; +} diff --git a/Fibonnaci Series/Fibonacci Series-Recursion-C b/Fibonnaci Series/Fibonacci Series-Recursion-C new file mode 100644 index 00000000..bb84cf84 --- /dev/null +++ b/Fibonnaci Series/Fibonacci Series-Recursion-C @@ -0,0 +1,30 @@ +#include + +int fibonacci(int); + +int main() +{ + int n, i = 0, c; + + scanf("%d",&n); + + printf("Fibonacci series\n"); + + for ( c = 1 ; c <= n ; c++ ) + { + printf("%d\n", fibonacci(i)); + i++; + } + + return 0; +} + +int fibonacci(int n) +{ + if ( n == 0 ) + return 0; + else if ( n == 1 ) + return 1; // 0+1=1 + else + return ( Fibonacci(n-1) + Fibonacci(n-2) );//recursively calls fibonacci function +} diff --git a/Fibonnaci Series/Fibonaci_DP.cpp b/Fibonnaci Series/Fibonaci_DP.cpp new file mode 100644 index 00000000..408d1f58 --- /dev/null +++ b/Fibonnaci Series/Fibonaci_DP.cpp @@ -0,0 +1,22 @@ +#include +using namspace std; +vectorarr; +int main() +{ +long long int val; //upto which term +cin>>val; +arr.push_back(1); +arr.push_back(1); +for(long long int i=2;i<=val;i++;) +{ +arr.push_back(arr[i-1]+arr[i-2]); +} +for(long long int i=1;i<=val;i++;) +{ + cout<<" "< +int fibo(int term); +int main() +{ + int i,term=10; + for(i=0;i +int main() { + int i, n, t1 = 0, t2 = 1, nT; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nT; + } + + return 0; +} diff --git a/Find ASCII Value of Character b/Find ASCII Value of Character new file mode 100644 index 00000000..7fb487ab --- /dev/null +++ b/Find ASCII Value of Character @@ -0,0 +1,4 @@ +# Program to find the ASCII value of the given character + +c = 'p' +print("The ASCII value of '" + c + "' is", ord(c)) diff --git a/Find Factorial of a number b/Find Factorial of a number new file mode 100644 index 00000000..1d9303a3 --- /dev/null +++ b/Find Factorial of a number @@ -0,0 +1,16 @@ +#include +using namespace std; +int main() { + int n; + cout << "Enter the number: "; + cin >> n; + + long long fact = 1; + + for (int i=2; i<=n; i++) { + fact = fact*i; + } + + cout << "Factorial: " << fact; + return 0; +} diff --git a/Find Factorial of a number (PHP) b/Find Factorial of a number (PHP) new file mode 100644 index 00000000..46a77101 --- /dev/null +++ b/Find Factorial of a number (PHP) @@ -0,0 +1,16 @@ + diff --git a/Find Power of a Number b/Find Power of a Number new file mode 100644 index 00000000..57efad14 --- /dev/null +++ b/Find Power of a Number @@ -0,0 +1,24 @@ +/* C Program to find Power of a Number using For Loop */ + +#include + +int main() +{ + int i, Number, Exponent; + long Power = 1; + + printf("\n Please Enter any Number : "); + scanf(" %d", &Number); + + printf("\n Please Enter the Exponent Vlaue: "); + scanf(" %d", &Exponent); + + for(i = 1; i <= Exponent; i++) + { + Power = Power * Number; + } + + printf("\n The Final result of %d Power %d = %ld", Number, Exponent, Power); + + return 0; +} diff --git a/Find Sum Of All Array Elements b/Find Sum Of All Array Elements new file mode 100644 index 00000000..60e55679 --- /dev/null +++ b/Find Sum Of All Array Elements @@ -0,0 +1,26 @@ +#include + + +int main() +{ + int a[1000],i,n,sum=0; + + printf("Enter size of the array : "); + scanf("%d",&n); + + printf("Enter elements in array : "); + for(i=0; i +#include +int main() +{ + int i, n; + float *arr; + arr=(float*)malloc(1*sizeof(float)); + printf("Enter the number of elements "); + scanf("%d", &n); + for (i = 0; i < n; ++i) { + printf("Enter number%d: ", i + 1); + scanf("%f", &arr[i]); + } + for (i = 1; i < n; ++i) { + if (arr[0] < arr[i]) + arr[0] = arr[i]; + } + printf("Largest element = %.2f", arr[0]); + return 0; +} diff --git a/Find factorial of a number b/Find factorial of a number new file mode 100644 index 00000000..855b2cab --- /dev/null +++ b/Find factorial of a number @@ -0,0 +1,19 @@ +class Factorial { + + int factorial(int n) + { + + // single line to find factorial + return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); + } + + // Driver Code + public static void main(String args[]) + { + Factorial obj = new Factorial(); + int num = 5; + System.out.println( + "Factorial of " + num + + " is " + obj.factorial(num)); + } +} diff --git a/Find factorial of number b/Find factorial of number new file mode 100644 index 00000000..dd98440f --- /dev/null +++ b/Find factorial of number @@ -0,0 +1,14 @@ +def fact(n): + f = 1 + for i in range(1, n+1): + f = f * i + return f +num = int(input(" Enter a number: ")) +if num < 0: + print(" b+ve") +elif num == 0 or num == 1: + print("Factorial = 1") +else: + ans = fact(num) + print("Factorial = ", ans) + diff --git a/Find largest element in an array b/Find largest element in an array new file mode 100644 index 00000000..f28eec6c --- /dev/null +++ b/Find largest element in an array @@ -0,0 +1,17 @@ +#include + +int main() { + int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; + int loop, largest; + + largest = array[0]; + + for(loop = 1; loop < 10; loop++) { + if( largest < array[loop] ) + largest = array[loop]; + } + + printf("Largest element of array is %d", largest); + + return 0; +} diff --git a/Find leap year in a given range b/Find leap year in a given range new file mode 100644 index 00000000..2a718199 --- /dev/null +++ b/Find leap year in a given range @@ -0,0 +1,33 @@ +#include + +using namespace std; + +// Function to calculate the number +// of leap years in range of (1, year) +int calNum(int year) +{ + return (year / 4) - (year / 100) + + (year / 400); +} + +// Function to calculate the number +// of leap years in given range +void leapNum(int l, int r) +{ + l--; + int num1 = calNum(r); + int num2 = calNum(l); + cout<<"Number of leap years : "; + cout << num1 - num2 << endl; +} + + +int main() +{ + int l,r; + cout<<"Enter range for which we want to calculate leap years"<>l>>r; + leapNum(l, r); + + return 0; +} diff --git a/Find length of a string b/Find length of a string new file mode 100644 index 00000000..ed08fd81 --- /dev/null +++ b/Find length of a string @@ -0,0 +1,12 @@ +#include +#include +int main() { + char string1[]={"naman"}; + int i=0, length; + while(string1[i] !='\0') { + i++; + } + length=i; + printf(" string length is %d",length); + return 0; +} diff --git a/Find length of string using python b/Find length of string using python new file mode 100644 index 00000000..ce705762 --- /dev/null +++ b/Find length of string using python @@ -0,0 +1,9 @@ +def findLen(str): + counter = 0 + for i in str: + counter += 1 + return counter + + +str = "Krupali" +print(findLen(str)) diff --git a/Find number of unique ways to climb stairs in C++ b/Find number of unique ways to climb stairs in C++ new file mode 100644 index 00000000..56a7f44f --- /dev/null +++ b/Find number of unique ways to climb stairs in C++ @@ -0,0 +1,19 @@ +int climbStairs(int n) { + vector dp(n+1, 0); + //Using dynamic programming here to store the results of the subproblems in the vector dp and then using them to get to bigger problem. + //Base case + if(n == 0 || n == 1) + { + return 1; + } + dp[0] =1; + dp[1] = 1; + dp[2] = 2; + //There are two ways to reach a particular number i, we can either reach it from i-1 or i-2 so adding the possibilities of both. + for(int i=3;i<=n;i++) + { + dp[i] = dp[i-1]+dp[i-2]; + } + + return dp[n]; + } diff --git a/Find power of any number b/Find power of any number new file mode 100644 index 00000000..59f8573c --- /dev/null +++ b/Find power of any number @@ -0,0 +1,7 @@ +import math + +base_number = float(input("Enter the base number")) +exponent = float(input("Enter the exponent")) + +power = math.pow(base_number,exponent) +print("Power is =",power) diff --git a/Find power of any number in python b/Find power of any number in python new file mode 100644 index 00000000..6e55d0c0 --- /dev/null +++ b/Find power of any number in python @@ -0,0 +1,31 @@ +# Python Program to find Power of a Number using While loop + +number = int(input(" Please Enter any Positive Integer : ")) +exponent = int(input(" Please Enter Exponent Value : ")) + +power = 1 +i = 1 + +while(i <= exponent): + power = power * number + i = i + 1 + +print("The Result of {0} Power {1} = {2}".format(number, exponent, power)) + + + + + +# Python Program to find Power of a Number using pow function + +number = int(input(" Please Enter any Positive Integer : ")) +exponent = int(input(" Please Enter Exponent Value : ")) + +power = 1 +i = 1 + +while(i <= exponent): + power = power * number + i = i + 1 + +print("The Result of {0} Power {1} = {2}".format(number, exponent, power)) diff --git a/Find power of any number[Python] b/Find power of any number[Python] new file mode 100644 index 00000000..da4cac99 --- /dev/null +++ b/Find power of any number[Python] @@ -0,0 +1,8 @@ +import math + +number = int(input("Enter any Positive Integer : ")) +exponent = int(input("Enter Exponent Value : ")) + +power = math.pow(number, exponent) + +print("{0} Power {1} = {2}".format(number, exponent, power)) diff --git a/Find the Largest Element in an array b/Find the Largest Element in an array new file mode 100644 index 00000000..98a77125 --- /dev/null +++ b/Find the Largest Element in an array @@ -0,0 +1,22 @@ +#include +int main() { + int i, n; + float arr[100]; + printf("Enter the number of elements (1 to 100): "); + scanf("%d", &n); + + for (i = 0; i < n; ++i) { + printf("Enter number%d: ", i + 1); + scanf("%f", &arr[i]); + } + + // storing the largest number to arr[0] + for (i = 1; i < n; ++i) { + if (arr[0] < arr[i]) + arr[0] = arr[i]; + } + + printf("Largest element = %.2f", arr[0]); + + return 0; +} diff --git a/Find the Largest Element in an array in C Lang b/Find the Largest Element in an array in C Lang new file mode 100644 index 00000000..98a77125 --- /dev/null +++ b/Find the Largest Element in an array in C Lang @@ -0,0 +1,22 @@ +#include +int main() { + int i, n; + float arr[100]; + printf("Enter the number of elements (1 to 100): "); + scanf("%d", &n); + + for (i = 0; i < n; ++i) { + printf("Enter number%d: ", i + 1); + scanf("%f", &arr[i]); + } + + // storing the largest number to arr[0] + for (i = 1; i < n; ++i) { + if (arr[0] < arr[i]) + arr[0] = arr[i]; + } + + printf("Largest element = %.2f", arr[0]); + + return 0; +} diff --git a/Find the largest element in an array using PYTHON b/Find the largest element in an array using PYTHON new file mode 100644 index 00000000..029b98d8 --- /dev/null +++ b/Find the largest element in an array using PYTHON @@ -0,0 +1,9 @@ +def LargestNumber(arr): # function to find maximum element + max_element=arr[0] + for i in arr: + max_element=max(max_element,i) + return max_element + +arr=[4,5,1,3,6,0,8,2] #input array +ans=LargestNumber(arr) +print("Largest element is",ans) diff --git a/Find the power of any number in Python b/Find the power of any number in Python new file mode 100644 index 00000000..84d25459 --- /dev/null +++ b/Find the power of any number in Python @@ -0,0 +1,4 @@ +number=2 #given number +exponent=4 #given exponent +power=2**4 +print("The result power of",number,"raised to",exponent,"is",power) diff --git a/Find the smallest number in a given array b/Find the smallest number in a given array new file mode 100644 index 00000000..ba0dbd52 --- /dev/null +++ b/Find the smallest number in a given array @@ -0,0 +1,17 @@ +import java.io.*; +import java.util.*; +public class smallest +{ +public static void main(string args[]) +{ +int arr[]={2,4,5,7,34,67,89,45,32,14}; +int min=arr[0]; +int l=arr.length; +for(int i=0;i& nums) { + int n = nums.size(); + int ans = nums[0]; + for(int i = 1;i +#include +using namespace std; + +int main() +{ + int i, pre, root, trex; + float num, ans = 0.0, inc = 1.0; + cout << "Enter the number to get it's nth root: "; + cin >>num; + cout << "Enter the 'n' value of root: "; + cin >>root; + cout << "Enter the precision for answer: "; + cin >> pre; + + for(i = 1; i <= pre; i++) + { + while((pow(ans, root)) <= num) + { + ans = ans + inc; + } + ans = ans - inc; + inc = inc/10; + } + cout <<"The "< +using namespace std; + +int power(int base , int exp) +{ + long long int power_of_num = 1; + + while(exp!=0) + { + power_of_num *= base; + exp -= 1; + } + + return power_of_num; +} + +int main() +{ + int base, exp; + cin>>base>>exp; + cout< +int main(void) +{ + int i; + for (i = 1; i <= 100; i++) + { + + if (i % 15 == 0) + printf ("FizzBuzz\t"); + + else if ((i % 3) == 0) + printf("Fizz\t"); + + else if ((i % 5) == 0) + printf("Buzz\t"); + + else + printf("%d\t", i); + + } + + return 0; +} diff --git a/From a C-style string b/From a C-style string new file mode 100644 index 00000000..d12cc7b5 --- /dev/null +++ b/From a C-style string @@ -0,0 +1,42 @@ +#include +using namespace std; + +int main() +{ + char line[150]; + int vowels, consonants, digits, spaces; + + vowels = consonants = digits = spaces = 0; + + cout << "Enter a line of string: "; + cin.getline(line, 150); + for(int i = 0; line[i]!='\0'; ++i) + { + if(line[i]=='a' || line[i]=='e' || line[i]=='i' || + line[i]=='o' || line[i]=='u' || line[i]=='A' || + line[i]=='E' || line[i]=='I' || line[i]=='O' || + line[i]=='U') + { + ++vowels; + } + else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z')) + { + ++consonants; + } + else if(line[i]>='0' && line[i]<='9') + { + ++digits; + } + else if (line[i]==' ') + { + ++spaces; + } + } + + cout << "Vowels: " << vowels << endl; + cout << "Consonants: " << consonants << endl; + cout << "Digits: " << digits << endl; + cout << "White spaces: " << spaces << endl; + + return 0; +} diff --git a/Given a number N, the task is to print the first N prime number from 1 to N. b/Given a number N, the task is to print the first N prime number from 1 to N. new file mode 100644 index 00000000..59faf504 --- /dev/null +++ b/Given a number N, the task is to print the first N prime number from 1 to N. @@ -0,0 +1,16 @@ +# Python3 program to find sum in Nth group + +# Check if given array is Monotonic +def isMonotonic(A): + + return (all(A[i] <= A[i + 1] for i in range(len(A) - 1)) or + all(A[i] >= A[i + 1] for i in range(len(A) - 1))) + +# Driver program +A = [6, 5, 4, 4] + +# Print required result +print(isMonotonic(A)) + +# This code is written by +# Sanjit_Prasad diff --git a/Graph in Data Structure b/Graph in Data Structure new file mode 100644 index 00000000..2bf4a9c4 --- /dev/null +++ b/Graph in Data Structure @@ -0,0 +1,153 @@ +#include + +#include + +using namespace std; + +#define MAX 20 + +/* + + * Adjacency Matrix Class + + */ + +class AdjacencyMatrix + +{ + + private: + + int n; + + int **adj; + + bool *visited; + + public: + + AdjacencyMatrix(int n) + + { + + this->n = n; + + visited = new bool [n]; + + adj = new int* [n]; + + for (int i = 0; i < n; i++) + + { + + adj[i] = new int [n]; + + for(int j = 0; j < n; j++) + + { + + adj[i][j] = 0; + + } + + } + + } + + /* + + * Adding Edge to Graph + + */ + + void add_edge(int origin, int destin) + + { + + if( origin > n || destin > n || origin < 0 || destin < 0) + + { + + cout<<"Invalid edge!\n"; + + } + + else + + { + + adj[origin - 1][destin - 1] = 1; + + } + + } + + /* + + * Print the graph + + */ + + void display() + + { + + int i,j; + + for(i = 0;i < n;i++) + + { + + for(j = 0; j < n; j++) + + cout<>nodes; + + AdjacencyMatrix am(nodes); + + max_edges = nodes * (nodes - 1); + + for (int i = 0; i < max_edges; i++) + + { + + cout<<"Enter edge (-1 -1 to exit): "; + + cin>>origin>>destin; + + if((origin == -1) && (destin == -1)) + + break; + + am.add_edge(origin, destin); + + } + + am.display(); + + return 0; + +} diff --git a/Greater number b/Greater number new file mode 100644 index 00000000..44608557 --- /dev/null +++ b/Greater number @@ -0,0 +1,24 @@ +/* C Program to Find Largest of Two numbers */ + +#include + +int main() { + int a, b; + printf("Please Enter Two different values\n"); + scanf("%d %d", &a, &b); + + if(a > b) + { + printf("%d is Largest\n", a); + } + else if (b > a) + { + printf("%d is Largest\n", b); + } + else + { + printf("Both are Equal\n"); + } + + return 0; +} diff --git a/GuessingGame.cpp b/GuessingGame.cpp new file mode 100644 index 00000000..2a52a566 --- /dev/null +++ b/GuessingGame.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include + +void play_game(int n) +{ + int random; + if(n==1) + random = rand() % 500; + else if(n==2) + random = rand() % 1000; + else + random = rand() % 10000; + int try1 = 10; + std::cout<> guess; + if(guess==random) + { + if(n==1) + { + std::cout << "\nಠ︵ಠ: HOW DARE YOU BEAT ME!! MY DAD WILL SEE YOU IN THE NEXT ROUND..\n"; + play_game(2); + } + else if(n==2) + { + std::cout << "\n༎ຶ ෴ ༎ຶ: AND I'LL BE GONE, GONE TONIGHT. THE GROUND BENEATH MY FEET IS OPEN WIDE. THE WAY THAT I BEEN HOLDIN' ON TOO TIGHT, WITH NOTHING IN BETWEEN... THE FINAL BOSS WILL SEE YOU SOON\n"; + play_game(3); + } + else + { + std::cout << "\n༼;´༎ຶ ۝ ༎ຶ༽: CONGRATULATIONS! I MEAN WHAT ELSE CAN I SAY? YOU BEAT THE GAME.\n" << "\n(ʘᴗʘ✿): NOICE\n"; + } + break; + } + else if (guessrandom-5) + std::cout << "(◕ᴗ◕✿): VERY CLOSE - Your Guess Is A Little Low\n"; + else + std::cout << "(◕ᴗ◕✿): Your Guess Is Too Low\n"; + std::cout << "tries left = " << try1--; + if(try1 < 0) + { + std::cout << "\n\nಠ‿ಠ: HAHA YOU LOST THE GAME\n"; + break; + } + else + std::cout << "\n\nTry again: "; + } + else + { + if(guess>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"; + do + { + std::cout << "\n0. Quit" << std::endl << "1. start" << std::endl << "enter your choice: "; + std::cin >> choice; + switch (choice) + { + case 0: + std::cout << "(◕ᴗ◕✿): Thanks for playing\n"; + break; + case 1: + play_game(1); + break; + default: + std::cout << "\nwrong choice\n"; + break; + } + } + while(choice!=0); +} diff --git a/Hacktoberfest: Program To Check whether a Triangle is Equilateral, Isosceles or Scalene b/Hacktoberfest: Program To Check whether a Triangle is Equilateral, Isosceles or Scalene new file mode 100644 index 00000000..9ce19b97 --- /dev/null +++ b/Hacktoberfest: Program To Check whether a Triangle is Equilateral, Isosceles or Scalene @@ -0,0 +1,32 @@ +// C++ program for the above approach +#include +using namespace std; + +// Function to check if the triangle +// is equilateral or isosceles or scalene +void checkTriangle(int x, int y, int z) +{ + + // Check for equilateral triangle + if (x == y && y == z) + cout << "Equilateral Triangle"; + + // Check for isoceles triangle + else if (x == y || y == z || z == x) + cout << "Isoceles Triangle"; + + // Otherwise scalene triangle + else + cout << "Scalene Triangle"; +} + +// Driver Code +int main() +{ + + // Given sides of triangle + int x = 8, y = 7, z = 9; + + // Function call + checkTriangle(x, y, z); +} diff --git a/Hacktoberfest: Program to calculate count of vowels & consonants in string b/Hacktoberfest: Program to calculate count of vowels & consonants in string new file mode 100644 index 00000000..71831861 --- /dev/null +++ b/Hacktoberfest: Program to calculate count of vowels & consonants in string @@ -0,0 +1,27 @@ +//Program to calculate count of vowels & consonants in string + +#include +#include +#include + +int main() +{ + int i, vowels= 0, consonant = 0; + + char str[] = "A compiler is a special program that processes statements written in a particular programming language and turns them into machine language or code that a computer's processor uses."; + + for(i = 0; i < strlen(str); i++){ + str[i] = tolower(str[i]); + if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') { + vowels++; + } + else if(str[i] >= 'a' && str[i] <= 'z'){ + consonant++; + } + } + + printf("Number of vowels are : %d\n", vowels); + printf("Number of consonant are: %d", consonant); + + return 0; +} diff --git a/Hacktoberfest: Program to find factorial of number b/Hacktoberfest: Program to find factorial of number new file mode 100644 index 00000000..5c96fd19 --- /dev/null +++ b/Hacktoberfest: Program to find factorial of number @@ -0,0 +1,21 @@ +//Program to find factorial of number + +#include +using namespace std; + +int main() +{ + int n; + int factorial = 1; + + cout << "Enter a positive integer: "; + cin >> n; + + for(int i = 1; i <=n; ++i) + { + factorial *= i; + } + + cout << "Factorial of " << n << " = " << factorial; + return 0; +} diff --git a/Hacktoberfest: Program to print leap years in given range b/Hacktoberfest: Program to print leap years in given range new file mode 100644 index 00000000..5e818296 --- /dev/null +++ b/Hacktoberfest: Program to print leap years in given range @@ -0,0 +1,29 @@ +//Program to print leap years in given range + +#include + +int LeapYear(int year) +{ + if( (year % 400==0)||(year%4==0 && year%100!=0) ) + return 1; + else + return 0; +} + +int main() +{ + int i,End,Beg; + printf("Enter the year from where you want to print all leap years: "); + scanf("%d",&Beg); + printf("Enter the year till where you want to print all leap years: "); + scanf("%d",&End); + + printf("Leap years from %d to %d:\n",Beg,End); + for(i=Beg;i<=End;i++) + { + if(LeapYear(i)) + printf("%d\t",i); + } + + return 0; +} diff --git a/Hacktoberfest: c Program to reverse the array b/Hacktoberfest: c Program to reverse the array new file mode 100644 index 00000000..76a5815d --- /dev/null +++ b/Hacktoberfest: c Program to reverse the array @@ -0,0 +1,30 @@ +#include +int main() +{ + int n, c, d, a[100], b[100]; + + printf("Enter the number of elements in array\n"); + scanf("%d", &n); + + printf("Enter array elements\n"); + + for (c = 0; c < n ; c++) + scanf("%d", &a[c]); + + // Copying elements into array b starting from the end of array a + + for (c = n - 1, d = 0; c >= 0; c--, d++) + b[d] = a[c]; + + // Copying reversed array into the original, we are modifying the original array. + + for (c = 0; c < n; c++) + a[c] = b[c]; + + printf("The array after reversal:\n"); + + for (c = 0; c < n; c++) + printf("%d\n", a[c]); + + return 0; +} diff --git a/HeapSort.cpp b/HeapSort.cpp new file mode 100644 index 00000000..cd06234b --- /dev/null +++ b/HeapSort.cpp @@ -0,0 +1,27 @@ +#include +using namespace std; +void heapify(int a[],int n,int node){ + int mx=node; + int left=2*node+1,right=2*node+2; + if(lefta[mx])mx=left; + if(righta[mx])mx=right; + if(mx!=node){ + swap(a[node],a[mx]); + heapify(a,n,mx); + } +} +void heap(int a[],int n){ + int index=(n/2)-1; + for(int i=index;i>=0;i--)heapify(a,n,i); + for(int i=n-1;i>0;i--){ + swap(a[0],a[i]); + heapify(a,i,0); + } +} +int main(){ + int n; cin>>n; + int a[n]; + for(int i=0;i>a[i]; + heap(a,n); + for(int i=0;i + +using namespace std; + +void heapify(int array[], int numOfElements, int current) +{ + int leftChild = 2 * current + 1; + int rightChild = 2 * current + 2; + int largest = current; + // If left child is larger than root + if(leftChild < numOfElements && array[leftChild] > array[current]) + largest = leftChild; + // If right child is larger than root + if(rightChild < numOfElements && array[rightChild] > array[largest]) + largest = rightChild; + // If largest element is not root + if(largest != current) + { + swap(array[largest], array[current]); + // Recursively heapify the subtree + heapify(array, numOfElements, largest); + } +} + +// Function for heap sort +void heapSort(int *array, int numOfElements) +{ + // heapify the array + for(int i = numOfElements/2 - 1; i >= 0; i--) + heapify(array, numOfElements, i); + + for(int i = numOfElements-1; i > 0; i--) + { + // swap first and last + swap(array[0], array[i]); + heapify(array, i, 0); } +} + +//Function printing the array +void printArray(int array[], int numOfElements) +{ + for (int i = 0; i < numOfElements; ++i) + cout << array[i] << " "; + cout << "\n"; +} + +int main() +{ + int array[] = { 12, 11, 13, 5, 6, 1, 7 }; + int numOfElements = sizeof(array) / sizeof(array[0]); + heapSort(array, numOfElements); + printArray(array, numOfElements); + return 0; +} diff --git a/HeartPattern.c b/HeartPattern.c new file mode 100644 index 00000000..0e5890fc --- /dev/null +++ b/HeartPattern.c @@ -0,0 +1,51 @@ +#include + +int main() +{ + int i, j, n; + + printf("Enter value of n : "); + scanf("%d", &n); + + for(i=n/2; i<=n; i+=2) + { + for(j=1; j=1; i--) + { + for(j=i; j +#include +using namespace std; +class number +{ + private: + int x,y,z; + public: + number(int n,int n1, int n2) + { + x=n; + y=n1; + z=n2; + } + void operator -() + { x=-x; + y=-y; + z=-z; + } + void showdata() + { cout<<"\n x="< +using namespace std; +#define max 500 +int multiply(int x,int res[],int size) +{ + int carry=0; + for(int i=0;i=0;i--) + { + cout<>n; + factorial(n); + return 0; + +} + +Input: + +N = 100 + +Output: + +93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 diff --git a/How to print Swastic Pattern b/How to print Swastic Pattern new file mode 100644 index 00000000..d89205e3 --- /dev/null +++ b/How to print Swastic Pattern @@ -0,0 +1,82 @@ +Source Code : +///////////////// + +#include +using namespace std; +int main() +{ + int n; + cin>>n; + cout<<"*"; + for(int i=0;i<(n-3)/2;i++) + { + cout<<" "; + } + for(int i=0;i<(n+1)/2;i++) + { + cout<<"*"; +} + cout< +int main() { + int n, rev = 0, remainder; + printf("Enter an integer: "); + scanf("%d", &n); + while (n != 0) { + remainder = n % 10; + rev = rev * 10 + remainder; + n /= 10; + } + printf("Reversed number = %d", rev); + return 0; +} diff --git a/Invisibility using Python b/Invisibility using Python new file mode 100644 index 00000000..f2f3e73a --- /dev/null +++ b/Invisibility using Python @@ -0,0 +1,71 @@ +import cv2 +import time +import numpy as np + +#recording video from webcam + +camera=cv2.VideoCapture(0) +#For final outcome +frame_width = int(camera.get(3)) +frame_height = int(camera.get(4)) + +out =cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height)) +#time for webcamera to sleep(5s) before it works + +time.sleep(5) +count=0 +background=0 + +#capturing background which will work as invisible cloak + +for i in range(60): + ret,background=camera.read() + +background =np.flip(background,axis=1) + +# reading the frame from camera, till it is open + +while(camera.isOpened()): + ret,img=camera.read() + if not ret: + break + count+=1 + # laterally flipping the image + img=np.flip(img,axis=1) + # converting RGB to HSV + hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV) + # Generating mask to detect Red Color + #Range for Lower Red + lower_red=np.array([0,125,50]) + upper_red=np.array([10,255,255]) + mask1=cv2.inRange(hsv,lower_red,upper_red) + + #Range for Upper Red + lower_red=np.array([170,120,70]) + upper_red=np.array([180,255,255]) + mask2=cv2.inRange(hsv,lower_red,upper_red) + mask1 = mask1+mask2 + + mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8)) + #Dilate the mask image + mask1=cv2.morphologyEx(mask1,cv2.MORPH_DILATE,np.ones((3,3),np.uint8)) + + # Creating an inverted Mask to segment out the red color from the image + mask2=cv2.bitwise_not(mask1) + + # Segmenting the cloth out of the frame using bitwise and inverted mask + res1=cv2.bitwise_and(img,img,mask=mask2) + + # Creating image showing static background pixels by pixels only for the masked region + res2=cv2.bitwise_and(background,background,mask=mask1) + + #Generating final output + + op=cv2.addWeighted(res1,1,res2,1,0) + out.write(op) + cv2.imshow("ABRACADABRA",op) + cv2.waitKey(1) + +camera.release() +out.release() +cv2.destroyAllWindows() diff --git a/Java Program to count the total number of vowels and consonants in a string b/Java Program to count the total number of vowels and consonants in a string new file mode 100644 index 00000000..d06700c1 --- /dev/null +++ b/Java Program to count the total number of vowels and consonants in a string @@ -0,0 +1,28 @@ +public class CountVowelConsonant { + public static void main(String[] args) { + + //Counter variable to store the count of vowels and consonant + int vCount = 0, cCount = 0; + + //Declare a string + String str = "This is a really simple sentence"; + + //Converting entire string to lower case to reduce the comparisons + str = str.toLowerCase(); + + for(int i = 0; i < str.length(); i++) { + //Checks whether a character is a vowel + if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') { + //Increments the vowel counter + vCount++; + } + //Checks whether a character is a consonant + else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') { + //Increments the consonant counter + cCount++; + } + } + System.out.println("Number of vowels: " + vCount); + System.out.println("Number of consonants: " + cCount); + } +} diff --git a/Java Program to find factorial of a number b/Java Program to find factorial of a number new file mode 100644 index 00000000..cc269198 --- /dev/null +++ b/Java Program to find factorial of a number @@ -0,0 +1,20 @@ +import java.util.Scanner; + +public class Factorial { + //method to get factorial value of a number + public static int calcFactorial(int number){ + int factorial = 1; + for(int i=1;i<=number;i++){ + factorial=factorial*i; + } + return factorial; + } + + public static void main(String []args){ + int number; + Scanner sc= new Scanner(System.in); + System.out.print("Enter number : "); + number = sc.nextInt(); + System.out.println("Factorial Value of " +number+ " is : "+calcFactorial(number)); + } +} diff --git a/Java Program to find lenght of a string in java b/Java Program to find lenght of a string in java new file mode 100644 index 00000000..b736fa6b --- /dev/null +++ b/Java Program to find lenght of a string in java @@ -0,0 +1,16 @@ + public class CountVowelConsonant { + public static void main(String[] args) { + + // String Handling + + char[] myArray = {'W' , 'e' , 'l' ,'c' , 'o' , 'm' ,'e'}; + + // Sequence of characters + String firstString = "Welcome"; + + System.out.println("lenght of a string" +" : " + firstString.length()); + + + } + } + diff --git a/Java Program to find power of any number b/Java Program to find power of any number new file mode 100644 index 00000000..2fe050f3 --- /dev/null +++ b/Java Program to find power of any number @@ -0,0 +1,17 @@ +public class Power { + + public static void main(String[] args) { + + int base = 3, exponent = 4; + + long result = 1; + + while (exponent != 0) + { + result *= base; + exponent--; + } + + System.out.println("Answer = " + result); + } +} diff --git a/Java Program to find the largest element in a given array b/Java Program to find the largest element in a given array new file mode 100644 index 00000000..f593401f --- /dev/null +++ b/Java Program to find the largest element in a given array @@ -0,0 +1,31 @@ +import java.util.Scanner; + +public class LargestElement { + //method to get the largest element in a given array + public static int findLargestElement(int array[]){ + int largest = 0; + for(int i=0;i largest) + { + largest = array[i]; + } + } + return largest; + } + + public static void main(String []args){ + int elementCount,number; + Scanner sc= new Scanner(System.in); + System.out.print("Enter number of elements in the array : "); + elementCount = sc.nextInt(); + int array[] = new int[elementCount]; + for(int i = 0; i < elementCount; i++) + { + System.out.print("Enter number " +(i+1) +": "); + number = sc.nextInt(); + array[i] = number; + } + + System.out.println("Largest element in the array is : "+findLargestElement(array)); + } +} diff --git a/Java Program to find the smallest element in a given array b/Java Program to find the smallest element in a given array new file mode 100644 index 00000000..3c51d4e7 --- /dev/null +++ b/Java Program to find the smallest element in a given array @@ -0,0 +1,31 @@ +import java.util.Scanner; + +public class SmallestElement { + //method to get the smallest element in a given array + public static int findSmallestElement(int array[]){ + int smallest = array[0]; + for(int i=0;i max) + max = arr[i]; + + return max; + } + + // Driver method + public static void main(String[] args) + { + System.out.println("Largest in given array is " + largest()); + } + } diff --git a/Java_Program To Subtract Two Matrices b/Java_Program To Subtract Two Matrices new file mode 100644 index 00000000..3afe988b --- /dev/null +++ b/Java_Program To Subtract Two Matrices @@ -0,0 +1,56 @@ + +class SUBMatrix +{ + public static void main(String args[]) + { + int row, col,i,j; + Scanner in = new Scanner(System.in); + + System.out.println("Enter the number of rows"); + row = in.nextInt(); + + System.out.println("Enter the number columns"); + col = in.nextInt(); + + int mat1[][] = new int[row][col]; + int mat2[][] = new int[row][col]; + int res[][] = new int[row][col]; + + System.out.println("Enter the elements of matrix1"); + + for ( i= 0 ; i < row ; i++ ) + { + + for ( j= 0 ; j < col ;j++ ) + mat1[i][j] = in.nextInt(); + + } + System.out.println("Enter the elements of matrix2"); + + + for ( i= 0 ; i < row ; i++ ) + { + + for ( j= 0 ; j < col ;j++ ) + mat2[i][j] = in.nextInt(); + + + } + + + for ( i= 0 ; i < row ; i++ ) + for ( j= 0 ; j < col ;j++ ) + res[i][j] = mat1[i][j] - mat2[i][j] ; + + System.out.println("subtract of two matrices:-"); + + for ( i= 0 ; i < row ; i++ ) + { + for ( j= 0 ; j < col ;j++ ) + System.out.print(res[i][j]+"\t"); + + System.out.println(); + } + + } +} diff --git a/Kotlin ASCCI Value of Character b/Kotlin ASCCI Value of Character new file mode 100644 index 00000000..7b99efbb --- /dev/null +++ b/Kotlin ASCCI Value of Character @@ -0,0 +1,10 @@ +Using Kotlin Language + +fun main(args: Array) { + val c = 'a' + val ascii = c.toInt() + println("The ASCII value of $c is: $ascii") +} + +Output +The ASCII Value of a is : 65 diff --git a/Large element in array b/Large element in array new file mode 100644 index 00000000..2817be1a --- /dev/null +++ b/Large element in array @@ -0,0 +1,13 @@ +#Initialize array +arr = [25, 11, 7, 75, 56]; + +#Initialize max with first element of array. +max = arr[0]; + +#Loop through the array +for i in range(0, len(arr)): + #Compare elements of array with max + if(arr[i] > max): + max = arr[i]; + +print("Largest element present in given array: " + str(max)); diff --git a/Largest element in an array. b/Largest element in an array. new file mode 100644 index 00000000..33680616 --- /dev/null +++ b/Largest element in an array. @@ -0,0 +1,17 @@ +#Initialize array +arr = [25, 11, 7, 75, 56]; + +#Initialize max with first element of array. +max = arr[0]; + +#Loop through the array +for i in range(0, len(arr)): + #Compare elements of array with max + if(arr[i] > max): + max = arr[i]; + +print("Largest element present in given array: " + str(max)); + + + + diff --git a/Largest element in array in java b/Largest element in array in java new file mode 100644 index 00000000..88f7f509 --- /dev/null +++ b/Largest element in array in java @@ -0,0 +1,20 @@ +class Test +{ + static int arr[] = {10, 324, 45, 90, 9808}; + + // Method to find maximum in arr[] + static int largest() + { + int i; + int max = arr[0]; + for (i = 1; i < arr.length; i++) + if (arr[i] > max) + max = arr[i]; + + return max; + } + public static void main(String[] args) + { + System.out.println("Largest in given array is " + largest()); + } + } diff --git a/Largest element in array sg b/Largest element in array sg new file mode 100644 index 00000000..98a77125 --- /dev/null +++ b/Largest element in array sg @@ -0,0 +1,22 @@ +#include +int main() { + int i, n; + float arr[100]; + printf("Enter the number of elements (1 to 100): "); + scanf("%d", &n); + + for (i = 0; i < n; ++i) { + printf("Enter number%d: ", i + 1); + scanf("%f", &arr[i]); + } + + // storing the largest number to arr[0] + for (i = 1; i < n; ++i) { + if (arr[0] < arr[i]) + arr[0] = arr[i]; + } + + printf("Largest element = %.2f", arr[0]); + + return 0; +} diff --git a/Largest_element_in_array_Manas b/Largest_element_in_array_Manas new file mode 100644 index 00000000..aac415d8 --- /dev/null +++ b/Largest_element_in_array_Manas @@ -0,0 +1,25 @@ +# Python3 program to find maximum +# in arr[] of size n + +# python function to find maximum +# in arr[] of size n +def largest(arr,n): + + # Initialize maximum element + max = arr[0] + + # Traverse array elements from second + # and compare every element with + # current max + for i in range(1, n): + if arr[i] > max: + max = arr[i] + return max + +# Driver Code +arr = [10, 324, 45, 90, 9808] +n = len(arr) +Ans = largest(arr,n) +print ("Largest in given array is",Ans) + +# This code is contributed by Manas diff --git a/Leap Year Finder in python b/Leap Year Finder in python new file mode 100644 index 00000000..5e521a53 --- /dev/null +++ b/Leap Year Finder in python @@ -0,0 +1,12 @@ +def is_leap(year): + leap=False + if (year%400==0): + leap= False + elif(year%100==0): + leap= False + elif(year%4==0): + leap=True + return leap + +year = int(input()) +print(is_leap(year)) diff --git a/Leap year b/Leap year new file mode 100644 index 00000000..1af9e169 --- /dev/null +++ b/Leap year @@ -0,0 +1,11 @@ +year = int(input("Enter year: ")) +if (year % 4) == 0: + if (year % 100) == 0: + if (year % 400) == 0: + print("{0} is a leap year".format(year)) + else: + print("{0} is not a leap year".format(year)) + else: + print("{0} is a leap year".format(year)) +else: + print("{0} is not a leap year".format(year)) diff --git a/LeapYear.py b/LeapYear.py new file mode 100644 index 00000000..77f6a496 --- /dev/null +++ b/LeapYear.py @@ -0,0 +1,16 @@ +from datetime import date +today = date.today + +repeat = input("How many leap years do you want: ") +leapyear = 2020 +current_date = date.today() +if current_date.year != 2020: + leapyear = 2024 + +while (not repeat.isdigit() or (int(repeat) == 0)): + repeat = input("Error. Enter a number for how many leap years: ") + +print(leapyear) +for x in range (int(repeat)-1): + leapyear = leapyear + 4 + print(leapyear) diff --git a/Length Of the String In C_Lang b/Length Of the String In C_Lang new file mode 100644 index 00000000..a20fd7c3 --- /dev/null +++ b/Length Of the String In C_Lang @@ -0,0 +1,10 @@ +#include +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/Length of String b/Length of String new file mode 100644 index 00000000..5003ff61 --- /dev/null +++ b/Length of String @@ -0,0 +1,8 @@ +#include +using namespace std; +int main() +{ +string s="HELLO WORLD"; +int len=s.length(); +cout< +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/Length of String without Using strlen() Function b/Length of String without Using strlen() Function new file mode 100644 index 00000000..a20fd7c3 --- /dev/null +++ b/Length of String without Using strlen() Function @@ -0,0 +1,10 @@ +#include +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/Length of a String b/Length of a String new file mode 100644 index 00000000..5036b276 --- /dev/null +++ b/Length of a String @@ -0,0 +1,13 @@ + #include + #include + using namespace std; + int main () + { + char str[50]; + int len; + cout << "Enter a string : "; + gets(str); + len = strlen(str); + cout << "Length of the string is : " << len; + return 0; + } diff --git a/Length of a string b/Length of a string new file mode 100644 index 00000000..2e51a1de --- /dev/null +++ b/Length of a string @@ -0,0 +1,12 @@ +import java.util.*; +import java.lang.*; + +class deciToBin { + + public static void main (String[] args) { + Scanner sc=new Scanner(System.in); + String str=sc.nextLine(); + System.out.println(str.length()); + + } +} diff --git a/Length of a string by python b/Length of a string by python new file mode 100644 index 00000000..bc456277 --- /dev/null +++ b/Length of a string by python @@ -0,0 +1,2 @@ +String=input() +print(len(String)) diff --git a/Length of string.py b/Length of string.py new file mode 100644 index 00000000..71e761a6 --- /dev/null +++ b/Length of string.py @@ -0,0 +1,10 @@ +#Python program to demonstrate the use of +# len() method + +# Length of below string is 5 +string = "geeks" +print(len(string)) + +# Length of below string is 15 +string = "geeks for geeks" +print(len(string)) diff --git a/Linear Search Using Recursion-C++ b/Linear Search Using Recursion-C++ new file mode 100644 index 00000000..dbcb51f8 --- /dev/null +++ b/Linear Search Using Recursion-C++ @@ -0,0 +1,39 @@ +#include +using namespace std; + +int linear_search(int num,int low, int high,int a[]) +{ +if(high<1) +{ +return -1; +} +if(a[low]==num) +{ +return low; +} +else if(a[high]==num) +{ +return high; +} +else +{ +return linear_search(num,low+1,high-1,a); +} +} + int main() +{ +int i,a[100],num,position,n; cout<<"Enter the length of the array:"; cin>>n; cout<<"Enter the elements of the array:"; for(i=0;i>a[i]; +} +cout<<"Enter the element to be searched:"; cin>>num; +position=linear_search(num,0,n+1,a); +if(position!=-1) +{ +cout<<"The given element is present a t "< +int main() +{ + int array[100], search, c, n; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d integer(s)\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter a number to search\n"); + scanf("%d", &search); + + for (c = 0; c < n; c++) + { + if (array[c] == search) /* If required element is found */ + { + printf("%d is present at location %d.\n", search, c+1); + break; + } + } + if (c == n) + printf("%d isn't present in the array.\n", search); + + return 0; +} diff --git a/Linear_search in c b/Linear_search in c new file mode 100644 index 00000000..71f8e9f4 --- /dev/null +++ b/Linear_search in c @@ -0,0 +1,22 @@ +#include +void main(){ + int a[100],n,key,i; + printf("enter number of elements"); + scanf("%d",&n); + printf("enter the elements"); + for (i = 0; i < n ; i++) + { + scanf("%d",&a[i]); + } + printf("enter key value"); + scanf("%d",&key); + for (i = 0; i < n; i++) + { + + if (a[i]==key) + { + printf("element found at %d position",i+1); + } + } + +} diff --git a/LinkedHashSet Class In java b/LinkedHashSet Class In java new file mode 100644 index 00000000..88d42598 --- /dev/null +++ b/LinkedHashSet Class In java @@ -0,0 +1,28 @@ +import java.util.LinkedHashSet; +public class LinkedHashSetExample { + public static void main(String args[]) { + // LinkedHashSet of String Type + LinkedHashSet lhset = new LinkedHashSet(); + + // Adding elements to the LinkedHashSet + lhset.add("Z"); + lhset.add("PQ"); + lhset.add("N"); + lhset.add("O"); + lhset.add("KK"); + lhset.add("FGH"); + System.out.println(lhset); + + // LinkedHashSet of Integer Type + LinkedHashSet lhset2 = new LinkedHashSet(); + + // Adding elements + lhset2.add(99); + lhset2.add(7); + lhset2.add(0); + lhset2.add(67); + lhset2.add(89); + lhset2.add(66); + System.out.println(lhset2); + } +} diff --git a/LinkedList.c b/LinkedList.c new file mode 100644 index 00000000..34427a82 --- /dev/null +++ b/LinkedList.c @@ -0,0 +1,39 @@ +struct Node +{ + int data; + struct Node *next; +}; +void push(struct Node** head_ref, int new_data) +{ + struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); + new_node->data = new_data; + new_node->next = (*head_ref); + (*head_ref) = new_node; +} +void deleteNode(struct Node **head_ref, int position) +{ + if (*head_ref == NULL) + return; + struct Node* temp = *head_ref; + if (position == 0) + { + *head_ref = temp->next; + free(temp); + return; + } + for (int i=0; temp!=NULL && inext; + if (temp == NULL || temp->next == NULL) + return; + struct Node *next = temp->next->next; + free(temp->next); + temp->next = next; +} +void printList(struct Node *node) +{ + while (node != NULL) + { + printf(" %d ", node->data); + node = node->next; + } +} diff --git a/Matrices Addition b/Matrices Addition new file mode 100644 index 00000000..89a2fc40 --- /dev/null +++ b/Matrices Addition @@ -0,0 +1,39 @@ +#include +#define N 4 + +// This function adds A[][] and B[][], and stores +// the result in C[][] +void add(int A[][N], int B[][N], int C[][N]) +{ + int i, j; + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + C[i][j] = A[i][j] + B[i][j]; +} + +int main() +{ + int A[N][N] = { {1, 1, 1, 1}, + {2, 2, 2, 2}, + {3, 3, 3, 3}, + {4, 4, 4, 4}}; + + int B[N][N] = { {1, 1, 1, 1}, + {2, 2, 2, 2}, + {3, 3, 3, 3}, + {4, 4, 4, 4}}; + + int C[N][N]; // To store result + int i, j; + add(A, B, C); + + printf("Result matrix is \n"); + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + printf("%d ", C[i][j]); + printf("\n"); + } + + return 0; +} diff --git a/Matrix addtion b/Matrix addtion new file mode 100644 index 00000000..d8eac647 --- /dev/null +++ b/Matrix addtion @@ -0,0 +1,32 @@ +#include +void main() +{ + int a[100][100],b[100][100],row,col,sum[100][100],i,j; + printf("Enter no of rows and cols"); + scanf("%d%d",&row,&col); + printf("Enter Matrix A"); + for(i=0;i +using namespace std; + +int main() +{ + int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; + + cout << "Enter rows and columns for first matrix: "; + cin >> r1 >> c1; + cout << "Enter rows and columns for second matrix: "; + cin >> r2 >> c2; + + while (c1!=r2) + { + cout << "Error! column of first matrix not equal to row of second."; + + cout << "Enter rows and columns for first matrix: "; + cin >> r1 >> c1; + + cout << "Enter rows and columns for second matrix: "; + cin >> r2 >> c2; + } + + cout << endl << "Enter elements of matrix 1:" << endl; + for(i = 0; i < r1; ++i) + for(j = 0; j < c1; ++j) + { + cout << "Enter element a" << i + 1 << j + 1 << " : "; + cin >> a[i][j]; + } + + cout << endl << "Enter elements of matrix 2:" << endl; + for(i = 0; i < r2; ++i) + for(j = 0; j < c2; ++j) + { + cout << "Enter element b" << i + 1 << j + 1 << " : "; + cin >> b[i][j]; + } + for(i = 0; i < r1; ++i) + for(j = 0; j < c2; ++j) + { + mult[i][j]=0; + } + + for(i = 0; i < r1; ++i) + for(j = 0; j < c2; ++j) + for(k = 0; k < c1; ++k) + { + mult[i][j] += a[i][k] * b[k][j]; + } + + cout << endl << "Output Matrix: " << endl; + for(i = 0; i < r1; ++i) + for(j = 0; j < c2; ++j) + { + cout << " " << mult[i][j]; + if(j == c2-1) + cout << endl; + } + + return 0; +} diff --git a/Matrix multiplications b/Matrix multiplications new file mode 100644 index 00000000..566c725d --- /dev/null +++ b/Matrix multiplications @@ -0,0 +1,42 @@ +#include +#define N 4 + +// This function multiplies mat1[][] and mat2[][], +// and stores the result in res[][] +void multiply(int mat1[][N], int mat2[][N], int res[][N]) +{ + int i, j, k; + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) { + res[i][j] = 0; + for (k = 0; k < N; k++) + res[i][j] += mat1[i][k] * mat2[k][j]; + } + } +} + +int main() +{ + int mat1[N][N] = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 }, + { 4, 4, 4, 4 } }; + + int mat2[N][N] = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 }, + { 4, 4, 4, 4 } }; + + int res[N][N]; // To store result + int i, j; + multiply(mat1, mat2, res); + + printf("Result matrix is \n"); + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) + printf("%d ", res[i][j]); + printf("\n"); + } + + return 0; +} diff --git a/Menu Driven-Calculate Basic Arithmatic Operations b/Menu Driven-Calculate Basic Arithmatic Operations new file mode 100644 index 00000000..912479e2 --- /dev/null +++ b/Menu Driven-Calculate Basic Arithmatic Operations @@ -0,0 +1,47 @@ +#include + +int main() +{ + float num1,num2,ans; + int opt; + + //taking user input + do + { + printf("\nEnter the First Number : "); + scanf("%f",&num1); + printf("\nEnter the Second Number : "); + scanf("%f",&num2); + + //Displaying menu + printf("\n-----Main Menu----\n1.Addition"); + printf("\n2.Subtraction\n3.Multiply\n4.Divide\n5.Exit"); + printf("\nEnter your choice : "); + scanf("%d",&opt); + switch(opt) + { + case 1: + ans = num1+num2; + printf("\nThe addition of 2 numbers is : %f",ans); + break; + case 2: + ans = num1-num2; + printf("\nThe differnce of 2 numbers is : %f",ans); + break; + case 3: + ans = num1*num2; + printf("\nThe product of 2 numbers is : %f",ans); + break; + case 4: + ans = num1/num2; + printf("\nThe division of 2 numbers is : %f",ans); + break; + case 5: + break; + default: //error message for wrong choice + printf("\nYou Entered Wrong Choice\n"); + break; + } + }while(opt!=5); + return 0; +} diff --git a/Merge sort b/Merge sort new file mode 100644 index 00000000..42bf452f --- /dev/null +++ b/Merge sort @@ -0,0 +1,69 @@ +#include +void mergeSort(int[],int,int); +void merge(int[],int,int,int); +void main () +{ + int a[10]= {10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; + int i; + mergeSort(a,0,9); + printf("printing the sorted elements"); + for(i=0;i<10;i++) + { + printf("\n%d\n",a[i]); + } + +} +void mergeSort(int a[], int beg, int end) +{ + int mid; + if(begmid) + { + while(j<=end) + { + temp[index] = a[j]; + index++; + j++; + } + } + else + { + while(i<=mid) + { + temp[index] = a[i]; + index++; + i++; + } + } + k = beg; + while(kval > j->val) + { + k->next = j; + j = j->next; + k = k->next; + } + else + { + k->next = i; + i = i->next; + k = k->next; + } + } + + while(i) + { + k->next = i; + i = i->next; + k = k->next; + } + while(j) + { + k->next = j; + j = j->next; + k = k->next; + } + return newlist->next; + + } +}; diff --git a/MergeArray.cpp b/MergeArray.cpp new file mode 100644 index 00000000..a8d2f05c --- /dev/null +++ b/MergeArray.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int *merge(int arr1[],int s1,int arr2[],int s2) +{ + int i,j,k; + i=j=k=0; + int *arr=new int[15]; + while(i + +using namespace std; + + + +#include +using namespace std; +typedef long long int ll; +ll e = 1e9+7; +ll check(ll x) +{ + ll sum=0; + while(x!=0) + { + sum=sum+ x%10; + x=x/10; + } + + if(sum%5==0) + { + return 1; + } + else + { + return 0; + } + +} +int main() { + ll n,k; + cin>>n; + cin>>k; + ll i = n+1; + ll count=1;ll answer ; + while(count<=k) + { + ll ram=check(i); + if(ram==1) + { + count++; + answer = i ; + } + + + i++; + } + cout< +void main() +{ +printf("Welcome to DataFlair tutorials!\n\n"); +int matrix[10][10]; +int no_of_rows, no_of_columns; +int iteration1, iteration2; +printf("Enter the number of rows of the matrix: "); +scanf("%d",&no_of_rows); +printf("Enter the number of columns of the matrix: "); +scanf("%d",&no_of_columns); +printf("Enter the elements of the matrix:\n"); +for(iteration1 = 0; iteration1 < no_of_rows; iteration1++ ) +{ +for(iteration2 = 0; iteration2 < no_of_columns; iteration2++) +{ +scanf("%d", &matrix[iteration1][iteration2]); +} +} +printf("The matrix is:\n"); +for(iteration1 = 0; iteration1 < no_of_rows; iteration1++ ) +{ +for(iteration2 = 0; iteration2 < no_of_columns; iteration2++) +{ +printf("%d\t",matrix[iteration1][iteration2]); +} +printf("\n"); +} +} diff --git a/Multiplication of 2-D Array b/Multiplication of 2-D Array new file mode 100644 index 00000000..85832866 --- /dev/null +++ b/Multiplication of 2-D Array @@ -0,0 +1,88 @@ +/*Multiplication of 2-D Array using C Programming*/ +/*Author :- Rajan Gautam*/ + +#include +#include + +void multiplication(void); + +int A[30][30], B[30][30], C[30][30]; + +int i, j, k, m, n, sum = 0; +//Inside the main function +int main() +{ + //We are defining the size of arrays here + printf("Enter Number of Rows : "); + scanf("%d", &m); + printf("Enter Number of Columns : "); + scanf("%d", &n); + printf("Enter Array Element for First Matrix : \n"); + for(i = 0; i +#include + +void multiplication(void); + +int A[30][30], B[30][30], C[30][30]; + +int i, j, k, m, n, sum = 0; +//Inside the main function +int main() +{ + //We are defining the size of arrays here + printf("Enter Number of Rows : "); + scanf("%d", &m); + printf("Enter Number of Columns : "); + scanf("%d", &n); + printf("Enter Array Element for First Matrix : \n"); + for(i = 0; i +#include +int main(){ +int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; +system("cls"); +printf("enter the number of row="); +scanf("%d",&r); +printf("enter the number of column="); +scanf("%d",&c); +printf("enter the first matrix element=\n"); +for(i=0;i list + + Return the list : [greatest_common_divisor, multiplicative_inverse(1), multiplicative_inverse (if multiplicative_inverse(1) is negative)] + + >>> extended_euclidean(10, 23) + [1, [7]] + + >>> extended_euclidean(11, 2) + [1, [-5, -3]] + """ + #Initalizing table of Extended Euclidean algorithm + table = {'quotent':0, 'remainder_temp1':max(p, q), 'remainder_temp2':min(p, q), 'remainder':0, 'multiplicative_inverse1':0, 'multiplicative_inverse2':1, 'multiplicative_inverse':0} + + while(True): + try: #Break condition for loop + table['remainder'] = table['remainder_temp1'] % table['remainder_temp2'] + if table['remainder'] < 0: + break + except: + break + #From here we calculate value of q + table['quotent'] = (table['remainder_temp1'] - table['remainder'])/table['remainder_temp2'] + + table['multiplicative_inverse'] = table['multiplicative_inverse1'] - table['multiplicative_inverse2'] * table['quotent'] + + #From here we shift values about column to right + table['remainder_temp1'] = table['remainder_temp2'] + table['remainder_temp2'] = table['remainder'] + + table['multiplicative_inverse1'] = table['multiplicative_inverse2'] + table['multiplicative_inverse2'] = table['multiplicative_inverse'] + + if table['multiplicative_inverse1'] > 0: #Returning [greatest_common_divisor, [multiplicative inverse(s)]] + return [int(table['remainder_temp1']), [int(table['multiplicative_inverse1'])]] + return [int(table['remainder_temp1']), [int(table['multiplicative_inverse1']), q + int(table['multiplicative_inverse1'])]] + +if __name__=='__main__': + p = int(input("ENter p: ")) + q = int(input("ENter q: ")) + + result = extended_euclidean(p, q) + + print(result[1]) diff --git a/Multiply 2D or 3D matrices b/Multiply 2D or 3D matrices new file mode 100644 index 00000000..e16f8ab0 --- /dev/null +++ b/Multiply 2D or 3D matrices @@ -0,0 +1,19 @@ +X = [[12,7,3], + [4,5,6], + [7,8,9]] + +Y = [[5,8,1,2], + [6,7,3,0], + [4,5,9,1]] + +result = [[0,0,0,0], + [0,0,0,0], + [0,0,0,0]] + +for i in range(len(X)): + for j in range(len(Y[0])): + for k in range(len(Y)): + result[i][j] += X[i][k] * Y[k][j] + +for r in result: + print(r) diff --git a/Multiply Two Matrices Using Multi-dimensional Arrays b/Multiply Two Matrices Using Multi-dimensional Arrays new file mode 100644 index 00000000..def03f7b --- /dev/null +++ b/Multiply Two Matrices Using Multi-dimensional Arrays @@ -0,0 +1,82 @@ +#include + +// function to get matrix elements entered by the user +void getMatrixElements(int matrix[][10], int row, int column) { + + printf("\nEnter elements: \n"); + + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("Enter a%d%d: ", i + 1, j + 1); + scanf("%d", &matrix[i][j]); + } + } +} + +// function to multiply two matrices +void multiplyMatrices(int first[][10], + int second[][10], + int result[][10], + int r1, int c1, int r2, int c2) { + + // Initializing elements of matrix mult to 0. + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + result[i][j] = 0; + } + } + + // Multiplying first and second matrices and storing it in result + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + for (int k = 0; k < c1; ++k) { + result[i][j] += first[i][k] * second[k][j]; + } + } + } +} + +// function to display the matrix +void display(int result[][10], int row, int column) { + + printf("\nOutput Matrix:\n"); + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("%d ", result[i][j]); + if (j == column - 1) + printf("\n"); + } + } +} + +int main() { + int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2; + printf("Enter rows and column for the first matrix: "); + scanf("%d %d", &r1, &c1); + printf("Enter rows and column for the second matrix: "); + scanf("%d %d", &r2, &c2); + + // Taking input until + // 1st matrix columns is not equal to 2nd matrix row + while (c1 != r2) { + printf("Error! Enter rows and columns again.\n"); + printf("Enter rows and columns for the first matrix: "); + scanf("%d%d", &r1, &c1); + printf("Enter rows and columns for the second matrix: "); + scanf("%d%d", &r2, &c2); + } + + // get elements of the first matrix + getMatrixElements(first, r1, c1); + + // get elements of the second matrix + getMatrixElements(second, r2, c2); + + // multiply two matrices. + multiplyMatrices(first, second, result, r1, c1, r2, c2); + + // display the result + display(result, r1, c2); + + return 0; +} diff --git a/Multiply Two Matrix Using Multi-dimensional Arrays b/Multiply Two Matrix Using Multi-dimensional Arrays new file mode 100644 index 00000000..d09ade57 --- /dev/null +++ b/Multiply Two Matrix Using Multi-dimensional Arrays @@ -0,0 +1,41 @@ +#include +using namespace std; +int main() { + int product[10][10], r1=2, c1=3, r2=3, c2=3, i, j, k; + int a[2][3] = { {2, 4, 1} , {2, 3, 9} }; + int b[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 9, 7} }; + if (c1 != r2) { + cout<<"Column of first matrix should be equal to row of second matrix"; + } else { + cout<<"The first matrix is:"<, end ="") + # Here space (" ") is used to print a gape after printing + # each element of R + print(R[i][j], end =" ") + print("\n", end ="") + +# First matrix. M is a list +M = [[1, 1, 1, 1], + [2, 2, 2, 2], + [3, 3, 3, 3], + [4, 4, 4, 4]] + +# Second matrix. N is a list +N = [[1, 1, 1, 1], + [2, 2, 2, 2], + [3, 3, 3, 3], + [4, 4, 4, 4]] + +# Call matrix_multiplication function +matrix_multiplication(M, N) diff --git a/No of nodes in BST b/No of nodes in BST new file mode 100644 index 00000000..7080329d --- /dev/null +++ b/No of nodes in BST @@ -0,0 +1,66 @@ +#include +using namespace std; + +int n=1; + +struct node +{ + int data; + node* left; + node* right; +}; + +struct node* getNode(int data) +{ + node* newNode=new node(); + newNode->data=data; + newNode->left=NULL; + newNode->right=NULL; + return newNode; +} + +struct node* Insert(struct node* root, int data) +{ + if (root == NULL) + return getNode(data); + + if (data < root->data) + root->left = Insert(root->left, data); + else if (data > root->data) + root->right = Insert(root->right, data); + + return root; +} + + +int CountNodes(node*root) +{ + if(root==NULL) + return 0; + if(root->left!=NULL) + { + n=n+1; + n=CountNodes(root->left); + } + if(root->right!=NULL) + { + n=n+1; + n=CountNodes(root->right); + } + return n; +} + +int main() +{ + node* root=NULL; + root=Insert(root,3); + Insert(root,4); + Insert(root,2); + Insert(root,5); + Insert(root,1); + + cout<<"Total No. of Nodes in the BST = "< endRange: + return True + mid = (startRange + endRange)//2 + print("Is the number is ", + mid, "?", end = " ") + user = input() + print(user) + if user == "Y" or user == "y": + print("Voila ! Successfully Guessed Number.") + return False + elif user == "N" or user == "n": + print("Actual number is greater than",mid, "?", end = " ") + user = input() + print(user) + if user == "Y" or user == "y": + return guessNumber(mid+1, endRange) + elif user == "N" or user == "n": + return guessNumber(startRange, mid-1) + else: + print("Invalid Input. Print 'Y'/'N'") + return guessNumber(startRange, endRange) + else: + print("Invalid Input. Print 'Y'/'N' ") + return guessNumber(startRange, endRange) +if __name__ == "__main__": + print("Number Guessing game in python") + startRange = 1 + endRange = 10 + print("Guess a number in range (1 to 10)") + out = guessNumber(startRange, endRange) + if out: + print("Bad Choices") diff --git a/PHP code for Fibonacci b/PHP code for Fibonacci new file mode 100644 index 00000000..ac5e43c3 --- /dev/null +++ b/PHP code for Fibonacci @@ -0,0 +1,20 @@ + diff --git a/Palindrome program in C b/Palindrome program in C new file mode 100644 index 00000000..f45798e8 --- /dev/null +++ b/Palindrome program in C @@ -0,0 +1,19 @@ +#include +int main() +{ +int n,r,sum=0,temp; +printf("enter the number="); +scanf("%d",&n); +temp=n; +while(n>0) +{ +r=n%10; +sum=(sum*10)+r; +n=n/10; +} +if(temp==sum) +printf("palindrome number "); +else +printf("not palindrome"); +return 0; +} diff --git a/Palindrome_number b/Palindrome_number new file mode 100644 index 00000000..9a3edbd7 --- /dev/null +++ b/Palindrome_number @@ -0,0 +1,19 @@ +#include +int main() +{ +int n,r,sum=0,temp; +printf("enter the number="); +scanf("%d",&n); +temp=n; +while(n>0) +{ +r=n%10; +sum=(sum*10)+r; +n=n/10; +} +if(temp==sum) +printf("palindrome number "); +else +printf("not palindrome"); +return 0; +} diff --git a/Passing Arrays in C b/Passing Arrays in C new file mode 100644 index 00000000..f6de3e36 --- /dev/null +++ b/Passing Arrays in C @@ -0,0 +1,8 @@ +#include +void display(int age) { +printf("%d", age); +} +int main() { +int ageArray[] = { 2, 3, 4 }; +display(ageArray[2]); //Passing array element ageArray[2] only. +return 0; diff --git a/Pattern printing Equilateral Triangle b/Pattern printing Equilateral Triangle new file mode 100644 index 00000000..9deda316 --- /dev/null +++ b/Pattern printing Equilateral Triangle @@ -0,0 +1,11 @@ +print("Print equilateral triangle Pyramid using stars ") +size = 7 +m = (2 * size) - 2 +for i in range(0, size): + for j in range(0, m): + print(end=" ") + m = m - 1 # decrementing m after each loop + for j in range(0, i + 1): + # printing full Triangle pyramid using stars + print("* ", end=' ') + print(" ") diff --git a/Perfect Number b/Perfect Number new file mode 100644 index 00000000..6a8ebd81 --- /dev/null +++ b/Perfect Number @@ -0,0 +1,40 @@ +// C++ program to check if a given number is perfect +// or not +#include +using namespace std; + +// Returns true if n is perfect +bool isPerfect(long long int n) +{ + // To store sum of divisors + long long int sum = 1; + + // Find all divisors and add them + for (long long int i=2; i*i<=n; i++) + { + if (n%i==0) + { + if(i*i!=n) + sum = sum + i + n/i; + else + sum=sum+i; + } + } + // If sum of divisors is equal to + // n, then n is a perfect number + if (sum == n && n != 1) + return true; + + return false; +} + +// Driver program +int main() +{ + cout << "Below are all perfect numbers till 10000\n"; + for (int n =2; n<10000; n++) + if (isPerfect(n)) + cout << n << " is a perfect number\n"; + + return 0; +} diff --git a/Perfect Sum probelm in java b/Perfect Sum probelm in java new file mode 100644 index 00000000..2f97396e --- /dev/null +++ b/Perfect Sum probelm in java @@ -0,0 +1,99 @@ +import java.util.ArrayList; +public class SubSet_sum_problem +{ + // dp[i][j] is going to store true if sum j is + // possible with array elements from 0 to i. + static boolean[][] dp; + + static void display(ArrayList v) + { + System.out.println(v); + } + + // A recursive function to print all subsets with the + // help of dp[][]. Vector p[] stores current subset. + static void printSubsetsRec(int arr[], int i, int sum, + ArrayList p) + { + // If we reached end and sum is non-zero. We print + // p[] only if arr[0] is equal to sun OR dp[0][sum] + // is true. + if (i == 0 && sum != 0 && dp[0][sum]) + { + p.add(arr[i]); + display(p); + p.clear(); + return; + } + + // If sum becomes 0 + if (i == 0 && sum == 0) + { + display(p); + p.clear(); + return; + } + + // If given sum can be achieved after ignoring + // current element. + if (dp[i-1][sum]) + { + // Create a new vector to store path + ArrayList b = new ArrayList<>(); + b.addAll(p); + printSubsetsRec(arr, i-1, sum, b); + } + + // If given sum can be achieved after considering + // current element. + if (sum >= arr[i] && dp[i-1][sum-arr[i]]) + { + p.add(arr[i]); + printSubsetsRec(arr, i-1, sum-arr[i], p); + } + } + + // Prints all subsets of arr[0..n-1] with sum 0. + static void printAllSubsets(int arr[], int n, int sum) + { + if (n == 0 || sum < 0) + return; + + // Sum 0 can always be achieved with 0 elements + dp = new boolean[n][sum + 1]; + for (int i=0; i p = new ArrayList<>(); + printSubsetsRec(arr, n-1, sum, p); + } + + public static void main(String args[]) + { + int arr[] = {1, 2, 3, 4, 5}; + int n = arr.length; + int sum = 10; + printAllSubsets(arr, n, sum); + } +} diff --git a/Perform addition and subtraction of Matrices b/Perform addition and subtraction of Matrices new file mode 100644 index 00000000..13a7d091 --- /dev/null +++ b/Perform addition and subtraction of Matrices @@ -0,0 +1,87 @@ +#include + +int main() +{ + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + + int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10]; + printf("\nEnter the number of rows and columns of the first matrix \n\n"); + scanf("%d%d", &m, &n); + + printf("\nEnter the %d elements of the first matrix \n\n", m*n); + for(c = 0; c < m; c++) // to iterate the rows + for(d = 0; d < n; d++) // to iterate the columns + scanf("%d", &first[c][d]); + + printf("\nEnter the %d elements of the second matrix \n\n", m*n); + for(c = 0; c < m; c++) // to iterate the rows + for(d = 0; d < n; d++) // to iterate the columns + scanf("%d", &second[c][d]); + + /* + printing the first matrix + */ + printf("\n\nThe first matrix is: \n\n"); + for(c = 0; c < m; c++) // to iterate the rows + { + for(d = 0; d < n; d++) // to iterate the columns + { + printf("%d\t", first[c][d]); + } + printf("\n"); + } + + /* + printing the second matrix + */ + printf("\n\nThe second matrix is: \n\n"); + for(c = 0; c < m; c++) // to iterate the rows + { + for(d = 0; d < n; d++) // to iterate the columns + { + printf("%d\t", second[c][d]); + } + printf("\n"); + } + + /* + finding the SUM of the two matrices + and storing in another matrix sum of the same size + */ + for(c = 0; c < m; c++) + for(d = 0; d < n; d++) + sum[c][d] = first[c][d] + second[c][d]; + + // printing the elements of the sum matrix + printf("\n\nThe sum of the two entered matrices is: \n\n"); + for(c = 0; c < m; c++) + { + for(d = 0; d < n; d++) + { + printf("%d\t", sum[c][d]); + } + printf("\n"); + } + + /* + finding the DIFFERENCE of the two matrices + and storing in another matrix difference of the same size + */ + for(c = 0; c < m; c++) + for(d = 0; d < n; d++) + diff[c][d] = first[c][d] - second[c][d]; + + // printing the elements of the diff matrix + printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n"); + for(c = 0; c < m; c++) + { + for(d = 0; d < n; d++) + { + printf("%d\t", diff[c][d]); + } + printf("\n"); + } + + printf("\n\n\t\t\tCoding is Fun !\n\n\n"); + return 0; +} diff --git a/Php_Program to multiply two matrices b/Php_Program to multiply two matrices new file mode 100644 index 00000000..41c01dac --- /dev/null +++ b/Php_Program to multiply two matrices @@ -0,0 +1,51 @@ + +edit +play_arrow + +brightness_4 + diff --git a/Please merge my pull request b/Please merge my pull request new file mode 100644 index 00000000..5c7f9e45 --- /dev/null +++ b/Please merge my pull request @@ -0,0 +1,21 @@ +#include +int main() { + double first, second, temp; + printf("Enter first number: "); + scanf("%lf", &first); + printf("Enter second number: "); + scanf("%lf", &second); + + // Value of first is assigned to temp + temp = first; + + // Value of second is assigned to first + first = second; + + // Value of temp (initial value of first) is assigned to second + second = temp; + + printf("\nAfter swapping, firstNumber = %.2lf\n", first); + printf("After swapping, secondNumber = %.2lf", second); + return 0; +} diff --git a/Powe_of_a_number b/Powe_of_a_number new file mode 100644 index 00000000..52a8a085 --- /dev/null +++ b/Powe_of_a_number @@ -0,0 +1,11 @@ +number = int(input(" Please Enter any Positive Integer : ")) +exponent = int(input(" Please Enter Exponent Value : ")) + +power = 1 +i = 1 + +while(i <= exponent): + power = power * number + i = i + 1 + +print("The Result of {0} Power {1} = {2}".format(number, exponent, power)) diff --git a/Power of Number-C b/Power of Number-C new file mode 100644 index 00000000..f77375a2 --- /dev/null +++ b/Power of Number-C @@ -0,0 +1,24 @@ +#include + +int main() +{ + int base, exp; + long long pow = 1; + int i; + + + printf("Enter base: "); + scanf("%d", &base); + printf("Enter exponent: "); + scanf("%d", &exp); + + + for(i=1; i<=exp; i++) + { + power = pow*base; + } + + printf("%d ^ %d = %lld", base, exp, pow); + + return 0; +} diff --git a/Power of a Number Using the while Loop b/Power of a Number Using the while Loop new file mode 100644 index 00000000..e7d59525 --- /dev/null +++ b/Power of a Number Using the while Loop @@ -0,0 +1,16 @@ +#include +int main() { + int base, exp; + long long result = 1; + printf("Enter a base number: "); + scanf("%d", &base); + printf("Enter an exponent: "); + scanf("%d", &exp); + + while (exp != 0) { + result *= base; + --exp; + } + printf("Answer = %lld", result); + return 0; +} diff --git a/Power of a number b/Power of a number new file mode 100644 index 00000000..a5fb3bd4 --- /dev/null +++ b/Power of a number @@ -0,0 +1,15 @@ +fun main(args: Array) { + val baseValue = 2 + var exponentVAlue = 3 + var result: Long = 1 + + while (exponentValue != 0) { + result *= baseValue.toLong() + --exponentValue + } + + println("Answer = $result") +} + +Output +Answer = 8 diff --git a/Power of a number(using function) in Python b/Power of a number(using function) in Python new file mode 100644 index 00000000..9967df76 --- /dev/null +++ b/Power of a number(using function) in Python @@ -0,0 +1,6 @@ +def pwr(x,n): + ans=x**n + return ans + +x, n = [int(x) for x in input().split()] +print(pwr(x,n)) diff --git a/Power of any number b/Power of any number new file mode 100644 index 00000000..b9a1461d --- /dev/null +++ b/Power of any number @@ -0,0 +1,30 @@ +import java.util.*; +class Solution { + + static float power(float x, int y) + { + float result; + if( y == 0) + return 1; + result = power(x, y/2); + + if (y%2 == 0) + return result * result; + else + { + if(y > 0) + return x * result * result; + else + return (result * result) / x; + } + } + + /* Program to test function power */ + public static void main(String[] args) + { + Scanner sc= new Scanner(System.in); + float x = sc.nextFloat(); + int y = sc.nextInt(); + System.out.printf("%f", power(x, y)); + } +} diff --git a/PowerOfAnyNumber b/PowerOfAnyNumber new file mode 100644 index 00000000..f3fc8f14 --- /dev/null +++ b/PowerOfAnyNumber @@ -0,0 +1,8 @@ +def power(base,exp): + if(exp==1): + return(base) + if(exp!=1): + return(base*power(base,exp-1)) +base=int(input("Enter base: ")) +exp=int(input("Enter exponential value: ")) +print("Result:",power(base,exp)) diff --git a/Power_of_Number.py b/Power_of_Number.py new file mode 100644 index 00000000..0ab65b6d --- /dev/null +++ b/Power_of_Number.py @@ -0,0 +1,8 @@ +import math + +number = int(input(" Please Enter any Positive Integer : ")) +exponent = int(input(" Please Enter Exponent Value : ")) + +power = math.pow(number, exponent) + +print("The Result of {0} Power {1} = {2}".format(number, exponent, power)) diff --git a/Prime Number using Recursion b/Prime Number using Recursion new file mode 100644 index 00000000..fdeab1d4 --- /dev/null +++ b/Prime Number using Recursion @@ -0,0 +1,33 @@ +#include +using namespace std; + +bool isPrime(int n, int d); +int main() +{ +int number; +cout <<"Enter number >= 1"; +cin >> number; +if(isPrime(number)) + { + return + cout << "Yes"; + else + return + cout << "No"; + } + return 0; +} + bool isPrime(int n, int d) + { + if(n<2) + return 0; + if(d == 1) + return true; + else + { + if(n % d == 0) + return false; + else + return isPrime(n, d - 1); + } + } diff --git a/Prime Numbers b/Prime Numbers new file mode 100644 index 00000000..f390a7ba --- /dev/null +++ b/Prime Numbers @@ -0,0 +1,23 @@ +#include +int main() { + int n, i, flag = 0; + printf("Enter a positive integer: "); + scanf("%d", &n); + for (i = 2; i <= n / 2; ++i) + { + if (n % i == 0) { + flag = 1; + break; + }} + if (n == 1) { + printf("1 is neither prime nor composite."); + } + else { + if (flag == 0) + printf("%d is a prime number.", n); + else + printf("%d is not a prime number.", n); + } + + return 0; +} diff --git a/Prime Numbers Between given Interval b/Prime Numbers Between given Interval new file mode 100644 index 00000000..51af35f8 --- /dev/null +++ b/Prime Numbers Between given Interval @@ -0,0 +1,55 @@ +import java.util.*; + +class Main { + + // driver code + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + + int a, b, i, j,flag; + + // enter lower value of interval + System.out.printf( "Enter lower bound of the interval: "); + a = sc.nextInt(); + + // enter upper value of interval + System.out.printf( "\nEnter upper bound of the interval: "); + b = sc.nextInt(); + + System.out.printf("\nPrime numbers between %d and %d are: ", a, b); + + // Explicitly handling the cases when a is less than 2 + if (a == 1) { + System.out.println(a); + a++; + if (b >= 2) { + System.out.println(a); + a++; + } + } + if (a == 2) + System.out.println(a); + + + if (a % 2 == 0) + a++; + + + for (i = a; i <= b; i = i + 2) { + + + flag = 1; + + for (j = 2; j * j <= i; ++j) { + if (i % j == 0) { + flag = 0; + break; + } + } + if (flag == 1) + System.out.println(i); + } + + } +} diff --git a/Prime number program in c b/Prime number program in c new file mode 100644 index 00000000..61c336b1 --- /dev/null +++ b/Prime number program in c @@ -0,0 +1,37 @@ +#include + +int main(){ + +int n,i,m=0,flag=0; + +printf("Enter the number to check prime:"); + +scanf("%d",&n); + +m=n/2; + +for(i=2;i<=m;i++) + +{ + +if(n%i==0) + +{ + +printf("Number is not prime"); + +flag=1; + +break; + +} + +} + +if(flag==0) + +printf("Number is prime"); + +return 0; + + } diff --git a/Prime_in_Range.py b/Prime_in_Range.py new file mode 100644 index 00000000..741b6682 --- /dev/null +++ b/Prime_in_Range.py @@ -0,0 +1,10 @@ +lower = int(input("Enter lower range: ")) +upper = int(input("Enter upper range: ")) + +for num in range(lower,upper + 1): + if num > 1: + for i in range(2,num): + if (num % i) == 0: + break + else: + print(num) diff --git a/Prime_number b/Prime_number new file mode 100644 index 00000000..ed9d702e --- /dev/null +++ b/Prime_number @@ -0,0 +1,19 @@ +#include +int main(){ +int n,i,m=0,flag=0; +printf("Enter the number to check prime:"); +scanf("%d",&n); +m=n/2; +for(i=2;i<=m;i++) +{ +if(n%i==0) +{ +printf("Number is not prime"); +flag=1; +break; +} +} +if(flag==0) +printf("Number is prime"); +return 0; + } diff --git a/Primee numbers between interval b/Primee numbers between interval new file mode 100644 index 00000000..8bd09df4 --- /dev/null +++ b/Primee numbers between interval @@ -0,0 +1,55 @@ +import java.util.*; + +class Main { + + // driver code + public static void main(String[] args) + { + Scanner sc = new Scanner(System.in); + + int a, b, i, j,flag; + + // enter lower value of interval + System.out.printf( "Enter lower bound of the interval: "); + a = sc.nextInt(); + + // enter upper value of interval + System.out.printf( "\nEnter upper bound of the interval: "); + b = sc.nextInt(); + + System.out.printf("\nPrime numbers between %d and %d are: ", a, b); + + // Explicitly handling the cases when a is less than 2 + if (a == 1) { + System.out.println(a); + a++; + if (b >= 2) { + System.out.println(a); + a++; + } + } + if (a == 2) + System.out.println(a); + + + if (a % 2 == 0) + a++; + + + for (i = a; i <= b; i = i + 2) { + + + flag = 1; + + for (j = 2; j * j <= i; ++j) { + if (i % j == 0) { + flag = 0; + break; + } + } + if (flag == 1) + System.out.println(i); + } + + } +} diff --git a/Print ASCII Value of Character b/Print ASCII Value of Character new file mode 100644 index 00000000..bf3322df --- /dev/null +++ b/Print ASCII Value of Character @@ -0,0 +1,9 @@ +#include +using namespace std; +int main() { + char c; + cout << "Enter character: "; + cin >> c; + + cout << "ASCII Value: " << (int)c; +} diff --git a/Print ASCII Value of a Character b/Print ASCII Value of a Character new file mode 100644 index 00000000..a40f3ee8 --- /dev/null +++ b/Print ASCII Value of a Character @@ -0,0 +1,16 @@ +/* + * C Program to print ASCII value of a character + */ +#include +#include + +int main() { + char c; + printf("Enter a Character\n"); + scanf("%c",&c); + /*Prints the ASCII value of character as integer */ + printf("ASCII value of %c = %d",c,c); + + getch(); + return 0; +} diff --git a/Print ASCII value of a character b/Print ASCII value of a character new file mode 100644 index 00000000..430e833a --- /dev/null +++ b/Print ASCII value of a character @@ -0,0 +1,2 @@ +n=input() +print(ord(n)) diff --git a/Print Factorial Series b/Print Factorial Series new file mode 100644 index 00000000..e8a41591 --- /dev/null +++ b/Print Factorial Series @@ -0,0 +1,9 @@ +def fibonacci(n): + if n <= 1: + return n + else: + return fibonacci(n-1) + fibonacci(n-2) + +n=int(input()) +for i in range(n): + print(fibonacci(i),end=" ") diff --git a/Print Fibonacci Series b/Print Fibonacci Series new file mode 100644 index 00000000..734f8d2b --- /dev/null +++ b/Print Fibonacci Series @@ -0,0 +1,19 @@ +#include +using namespace std; +int main() { + int n; + cout << "Enter number of terms: "; + cin >> n; + + int a = 0; + int b = 1; + int c = 0; + + for (int i=0; i + +//function to check leap year +int checkLeapYear(int year) +{ + if( (year % 400==0)||(year%4==0 && year%100!=0) ) + return 1; + else + return 0; +} + +int main() +{ + int i,n1,n2; + + printf("Enter Starting Value "); + scanf("%d",&n1); + + printf("Enter Ending Value "); + scanf("%d",&n2); + + printf("Leap years from %d to %d:\n",n1,n2); + for(i=n1;i<=n2;i++) + { + if(checkLeapYear(i)) + printf("%d\t",i); + } + + return 0; +} diff --git a/Print the Fibonacci sequence b/Print the Fibonacci sequence new file mode 100644 index 00000000..9f6b1b3d --- /dev/null +++ b/Print the Fibonacci sequence @@ -0,0 +1,23 @@ +# Program to display the Fibonacci sequence up to n-th term + +nterms = int(input("How many terms? ")) + +# first two terms +n1, n2 = 0, 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence:") + while count < nterms: + print(n1) + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/Print_fibonacci_sequence_in_go.go b/Print_fibonacci_sequence_in_go.go new file mode 100644 index 00000000..b9c92e43 --- /dev/null +++ b/Print_fibonacci_sequence_in_go.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "strconv" +) + +func A(n int) int { + f := make([]int, n+1, n+2) + if n < 2 { + f = f[0:2] + } + f[0] = 0 + f[1] = 1 + for i := 2; i <= n; i++ { + f[i] = f[i-1] + f[i-2] + } + return f[n] +} + +func B(n int) int { + if n <= 1 { + return n + } + return B(n-1) + B(n-2) +} + +func main() { + for i := 0; i <= 9; i++ { + fmt.Print(strconv.Itoa(A(i)) + " ") + } + fmt.Println("") + for i := 0; i <= 9; i++ { + fmt.Print(strconv.Itoa(B(i)) + " ") + } + fmt.Println("") +} diff --git a/Printing power of a number using python b/Printing power of a number using python new file mode 100644 index 00000000..6671644c --- /dev/null +++ b/Printing power of a number using python @@ -0,0 +1,5 @@ +n = int(input()) # taking input of number +# taking input power value to be counted of n +p = int(input()) +# printing power +print(n**p) diff --git a/Program find factorial of number b/Program find factorial of number new file mode 100644 index 00000000..d3f4b213 --- /dev/null +++ b/Program find factorial of number @@ -0,0 +1,14 @@ +public class Factorial { + + public static void main(String[] args) { + + int n = 5, i = 1; + long factorial = 1; + while(i <= n) + { + factorial *= i; + i++; + } + System.out.printf("Factorial of %d = %d", n, factorial); + } +} diff --git a/Program to find leap years in given range b/Program to find leap years in given range new file mode 100644 index 00000000..1d50717f --- /dev/null +++ b/Program to find leap years in given range @@ -0,0 +1,20 @@ +def calNum(year) : + + return (year // 4) - (year // 100) + (year // 400); + + +def leapNum(l, r) : + + l -= 1; + num1 = calNum(r); + num2 = calNum(l); + print(num1 - num2); + + +if __name__ == "__main__" : + + l1 = 1; r1 = 400; + leapNum(l1, r1); + + l2 = 400; r2 = 2000; + leapNum(l2, r2); diff --git a/Program Calculate Length of String without Using strlen() Function b/Program Calculate Length of String without Using strlen() Function new file mode 100644 index 00000000..a20fd7c3 --- /dev/null +++ b/Program Calculate Length of String without Using strlen() Function @@ -0,0 +1,10 @@ +#include +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/Program Find Factorial of a given number b/Program Find Factorial of a given number new file mode 100644 index 00000000..d18c0983 --- /dev/null +++ b/Program Find Factorial of a given number @@ -0,0 +1,19 @@ +#include +using namespace std; + +int main() +{ + unsigned int n; + unsigned long long factorial = 1; + + cout << "Enter a positive integer: "; + cin >> n; + + for(int i = 1; i <=n; ++i) + { + factorial *= i; + } + + cout << "Factorial of " << n << " = " << factorial; + return 0; +} diff --git a/Program Program to convert decimal to binary b/Program Program to convert decimal to binary new file mode 100644 index 00000000..0a7895f1 --- /dev/null +++ b/Program Program to convert decimal to binary @@ -0,0 +1,23 @@ +#include +#include +long long convert(int n); +int main() { + int n; + printf("Enter a decimal number: "); + scanf("%d", &n); + printf("%d in decimal = %lld in binary", n, convert(n)); + return 0; +} + +long long convert(int n) { + long long bin = 0; + int rem, i = 1, step = 1; + while (n != 0) { + rem = n % 2; + printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, n, rem, n / 2); + n /= 2; + bin += rem * i; + i *= 10; + } + return bin; +} diff --git a/Program To Check whether a Triangle is Equilateral, Isosceles or Scalene b/Program To Check whether a Triangle is Equilateral, Isosceles or Scalene new file mode 100644 index 00000000..01843f7b --- /dev/null +++ b/Program To Check whether a Triangle is Equilateral, Isosceles or Scalene @@ -0,0 +1,28 @@ +# Python program + +# Function to check if the triangle +# is equilateral or isosceles or scalene +def checkTriangle(x, y, z): + + # _Check for equilateral triangle + if x == y == z: + print("Equilateral Triangle") + + # Check for isoceles triangle + elif x == y or y == z or z == x: + print("Isoceles Triangle") + + # Otherwise scalene triangle + else: + print("Scalene Triangle") + + +# Driver Code + +# Given sides of triangle +x = 8 +y = 7 +z = 9 + +# Function Call +checkTriangle(x, y, z) diff --git a/Program To Convert Binary To Decimal Number in c++ b/Program To Convert Binary To Decimal Number in c++ new file mode 100644 index 00000000..1922ea16 --- /dev/null +++ b/Program To Convert Binary To Decimal Number in c++ @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main() +{ + char binaryNumber[] = "1001"; + + cout << stoi(binaryNumber, 0, 2); + + return 0; +} diff --git a/Program To Convert Binary To Decimal Number in python b/Program To Convert Binary To Decimal Number in python new file mode 100644 index 00000000..33e28128 --- /dev/null +++ b/Program To Convert Binary To Decimal Number in python @@ -0,0 +1,8 @@ +# Python3 program to convert +# binary to decimal +n = input("Enter the binary no. : ") + +# Convert n to base 2 +s = int(n, 2) + +print(s) diff --git a/Program To Find power of any number b/Program To Find power of any number new file mode 100644 index 00000000..83383e3a --- /dev/null +++ b/Program To Find power of any number @@ -0,0 +1,14 @@ +#include +using namespace std; +long long int powerr(long long int x,long long int y){ + if(y==0) + return 1; + else + return x*powerr(x,y-1); +} +int main(){ + long long int base,power; + cin>>base; + cin>>power; + cout< 0) { + int last_digit = temp % 10; + temp = temp / 10; + + dec_value += last_digit * base1; + + base1 = base1 * 2; + } + + return dec_value; + } + + // Driver Code + public static void Main() + { + int num = 10101001; + + System.Console.Write(binaryToDecimal(num)); + } +} + diff --git a/Program for Binary To Decimal Conversion c# Code b/Program for Binary To Decimal Conversion c# Code new file mode 100644 index 00000000..344cb55b --- /dev/null +++ b/Program for Binary To Decimal Conversion c# Code @@ -0,0 +1,36 @@ +// C# program to convert +// binary to decimal + +// Function to convert +// binary to decimal +class GFG { + public static int binaryToDecimal(int n) + { + int num = n; + int dec_value = 0; + + // Initializing base1 + // value to 1, i.e 2^0 + int base1 = 1; + + int temp = num; + while (temp > 0) { + int last_digit = temp % 10; + temp = temp / 10; + + dec_value += last_digit * base1; + + base1 = base1 * 2; + } + + return dec_value; + } + + // Driver Code + public static void Main() + { + int num = 10101001; + + System.Console.Write(binaryToDecimal(num)); + } +} diff --git a/Program for armstrong number b/Program for armstrong number new file mode 100644 index 00000000..41f0a1aa --- /dev/null +++ b/Program for armstrong number @@ -0,0 +1,20 @@ +#include + +int main() +{ + int armstrong,result=0,remainder; + + printf("Enter a number:\n"); + scanf("%d",&armstrong); + int i=armstrong; + while(i!=0) + { + remainder=i%10; + result=result+remainder*remainder*remainder; + i=i/10; + } + if(result==armstrong) + printf("%d is a armstrong number",armstrong); + else + printf("%d is not a armstrong number",armstrong); +} diff --git a/Program of queue in c b/Program of queue in c new file mode 100644 index 00000000..0e5b66d8 --- /dev/null +++ b/Program of queue in c @@ -0,0 +1,87 @@ +#include +#include +struct queue +{ + int no; + struct queue *next; +}; +typedef struct queue q; +q*front=NULL,*rear=NULL; +void enque(); +void dequeue(); +void traverse(); +void main() +{ + int ch; + do{ + printf("\nPress 0 to close"); + printf("\nPress 1 to Add Element"); + printf("\nPress 2 to traverse elements"); + printf("\nPress 3 to remove "); + printf("\nEnter your choice"); + scanf("%d",&ch); + if(ch==1) + enque(); + else if (ch==2) + traverse(); + else if(ch==3) + break; + else + printf("Invalid choice"); + + + + }while(ch!=0); + +} +void enque() +{ + q*node; + node=(struct queue*)malloc(sizeof(struct queue)); + printf("Enter Number"); + scanf("%d",&node->no); + node->next=NULL; + if(front==NULL) + { + front=node; + rear=node; + + } + else{ + rear->next=node; + rear=node; + + } +} +void traverse() +{ + q *t; + if(rear==NULL) + printf("No element is in queue"); + else + { + for(t=front;t!=NULL;t=t->next) + { + printf("%d\n",t->no); + } +} +} +void dequeue() +{ + q*t; + if(front==NULL) + printf("No element in queue"); + else + { + t=front; + front=front->next; + free(t); + printf("\nElement is removed successfully"); + if(front==rear) + { + front==NULL; + rear==NULL; + } + } +} + diff --git a/Program to Check Triangle is Equilateral Isosceles or Scalene b/Program to Check Triangle is Equilateral Isosceles or Scalene new file mode 100644 index 00000000..f71123c1 --- /dev/null +++ b/Program to Check Triangle is Equilateral Isosceles or Scalene @@ -0,0 +1,25 @@ +/* C Program to Check Triangle is Equilateral Isosceles or Scalene */ + +#include + +int main() +{ + int side1, side2, side3; + + printf("\n Please Enter Three Sides of a Triangle : "); + scanf("%d%d%d", &side1, &side2, &side3); + + if(side1 == side2 && side2 == side3) + { + printf("\n This is an Equilateral Triangle"); + } + else if(side1 == side2 || side2 == side3 || side1 == side3) + { + printf("\n This is an Isosceles Triangle"); + } + else + { + printf("\n This is a Scalene Triangle"); + } + return 0; + } diff --git a/Program to Convert Binary Number to Octal by Encryptor-Sec b/Program to Convert Binary Number to Octal by Encryptor-Sec new file mode 100644 index 00000000..ba2a8920 --- /dev/null +++ b/Program to Convert Binary Number to Octal by Encryptor-Sec @@ -0,0 +1,29 @@ +#include +#include +int convert(long long bin); +int main() { + long long bin; + printf("Enter a binary number: "); + scanf("%lld", &bin); + printf("%lld in binary = %d in octal", bin, convert(bin)); + return 0; +} + +int convert(long long bin) { + int oct = 0, dec = 0, i = 0; + + // converting binary to decimal + while (bin != 0) { + dec += (bin % 10) * pow(2, i); + ++i; + bin /= 10; + } + i = 1; + + // converting to decimal to octal + while (dec != 0) { + oct += (dec % 8) * i; + dec /= 8; + i *= 10; + } + return oct; diff --git a/Program to Convert Binary to Decimal b/Program to Convert Binary to Decimal new file mode 100644 index 00000000..103a0b7b --- /dev/null +++ b/Program to Convert Binary to Decimal @@ -0,0 +1,33 @@ +#include +using namespace std; + + +int binaryToDecimal(int n) +{ + int num = n; + int dec_value = 0; + + int base = 1; + + int temp = num; + while (temp) { + int last_digit = temp % 10; + temp = temp / 10; + + dec_value += last_digit * base; + + base = base * 2; + } + + return dec_value; +} + + +int main() +{ + int num; + + cin >>num; + + cout << binaryToDecimal(num) << endl; +} diff --git a/Program to Display Fibonacci Sequence by Encryptor-Sec b/Program to Display Fibonacci Sequence by Encryptor-Sec new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/Program to Display Fibonacci Sequence by Encryptor-Sec @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/Program to Find ASCII Value of a Character by Encryptor-Sec b/Program to Find ASCII Value of a Character by Encryptor-Sec new file mode 100644 index 00000000..a73b9c24 --- /dev/null +++ b/Program to Find ASCII Value of a Character by Encryptor-Sec @@ -0,0 +1,12 @@ +#include +int main() { + char c; + printf("Enter a character: "); + scanf("%c", &c); + + // %d displays the integer value of a character + // %c displays the actual character + printf("ASCII value of %c = %d", c, c); + + return 0; +} diff --git a/Program to Find Factorial in c++ b/Program to Find Factorial in c++ new file mode 100644 index 00000000..810be643 --- /dev/null +++ b/Program to Find Factorial in c++ @@ -0,0 +1,13 @@ +#include +using namespace std; +int fact(int n) { + if ((n==0)||(n==1)) + return 1; + else + return n*fact(n-1); +} +int main() { + int n = 5; + cout<<"Factorial of "< +int main() { + int r, c, a[100][100], b[100][100], sum[100][100], i, j; + printf("Enter the number of rows (between 1 and 100): "); + scanf("%d", &r); + printf("Enter the number of columns (between 1 and 100): "); + scanf("%d", &c); + + printf("\nEnter elements of 1st matrix:\n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("Enter element a%d%d: ", i + 1, j + 1); + scanf("%d", &a[i][j]); + } + + printf("Enter elements of 2nd matrix:\n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("Enter element a%d%d: ", i + 1, j + 1); + scanf("%d", &b[i][j]); + } + + // adding two matrices + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + sum[i][j] = a[i][j] + b[i][j]; + } + + // printing the result + printf("\nSum of two matrices: \n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("%d ", sum[i][j]); + if (j == c - 1) { + printf("\n\n"); + } + } + + return 0; +} diff --git a/Program to add sum off all numbers In an array b/Program to add sum off all numbers In an array new file mode 100644 index 00000000..90d1570d --- /dev/null +++ b/Program to add sum off all numbers In an array @@ -0,0 +1,19 @@ +''' +Python code to calculate the sum of an array... +''' + + +def arraySum(array): + sum = 0 + for element in array: + sum+=element + + return sum + + + + +if __name__="__main__": + array = map(int,input().split()) + result = arraySum(array) + print(result) diff --git a/Program to add two numbers in pyhton b/Program to add two numbers in pyhton new file mode 100644 index 00000000..401a4da5 --- /dev/null +++ b/Program to add two numbers in pyhton @@ -0,0 +1,4 @@ +x = input("Type a number: ") +y = input("Type another number: ") +sum = int(x) + int(y) +print("The sum is: ",sum) diff --git a/Program to calculate area and perimeter of equilateral triangle b/Program to calculate area and perimeter of equilateral triangle new file mode 100644 index 00000000..debb74de --- /dev/null +++ b/Program to calculate area and perimeter of equilateral triangle @@ -0,0 +1,33 @@ +// Java Program to find area and +// perimeter of equilateral triangle +import java.io.*; + +class GFG +{ + // Function to calculate + // Area of equilateral triangle + static float area_equi_triangle(float side) + { + + return (float)(((Math.sqrt(3)) / 4) * + side * side); + } + + // Function to calculate + // Perimeter of equilateral + // triangle + static float peri_equi_triangle(float side) + { + return 3 * side; + } + + // Driver Code + public static void main(String arg[]) + { + float side = 4; + System.out.print("Area of Equilateral Triangle:"); + System.out.println(area_equi_triangle(side)); + System.out.print("Perimeter of Equilateral Triangle:"); + System.out.println(peri_equi_triangle(side)); + } +} diff --git a/Program to calculate count of vowels & consonants in string by Encryptor-Sec b/Program to calculate count of vowels & consonants in string by Encryptor-Sec new file mode 100644 index 00000000..653a5cec --- /dev/null +++ b/Program to calculate count of vowels & consonants in string by Encryptor-Sec @@ -0,0 +1,11 @@ +vcount = 0; +ccount = 0; +str = str(input('Enter a String: ')) +str = str.lower(); +for i in range(0,len(str)): + if str[i] in ('a',"e","i","o","u"): + vcount = vcount + 1; + elif (str[i] >= 'a' and str[i] <= 'z'): + ccount = ccount + 1; +print("Total Vowels :",vcount); +print("Total consonant :",ccount); diff --git a/Program to calculate count of vowels & consonants in string using C++ b/Program to calculate count of vowels & consonants in string using C++ new file mode 100644 index 00000000..79411dd6 --- /dev/null +++ b/Program to calculate count of vowels & consonants in string using C++ @@ -0,0 +1,12 @@ +##Program to calculate count of vowels & consonants in string +main(){ +int n,vov=0,con=0; +string s; +cin>>s; +n=s.length(); +for(int i=0;i +int main() +{ + // This variable is to store the input number + int num; + + printf("Enter an integer: "); + scanf("%d",&num); + + // Modulus (%) returns remainder + if ( num%2 == 0 ) + printf("%d is an even number", num); + else + printf("%d is an odd number", num); + + return 0; +} diff --git a/Program to check triangle is equilateral, isosceles or scalene by Encryptor b/Program to check triangle is equilateral, isosceles or scalene by Encryptor new file mode 100644 index 00000000..67c0dcb0 --- /dev/null +++ b/Program to check triangle is equilateral, isosceles or scalene by Encryptor @@ -0,0 +1,28 @@ +# Python3 program for the above approach + +# Function to check if the triangle +# is equilateral or isosceles or scalene +def checkTriangle(x, y, z): + + # _Check for equilateral triangle + if x == y == z: + print("Equilateral Triangle") + + # Check for isoceles triangle + elif x == y or y == z or z == x: + print("Isoceles Triangle") + + # Otherwise scalene triangle + else: + print("Scalene Triangle") + + +# Driver Code + +# Given sides of triangle +x = 8 +y = 7 +z = 9 + +# Function Call +checkTriangle(x, y, z) diff --git a/Program to check triangle is equilateral, isosceles ot scalene b/Program to check triangle is equilateral, isosceles ot scalene new file mode 100644 index 00000000..b6f7226a --- /dev/null +++ b/Program to check triangle is equilateral, isosceles ot scalene @@ -0,0 +1,23 @@ + +# Function to check if the triangle +# is equilateral or isosceles or scalene +def checkTriangle(x, y, z): + + # _Check for equilateral triangle + if x == y == z: + print("Equilateral Triangle") + + # Check for isoceles triangle + elif x == y or y == z or z == x: + print("Isoceles Triangle") + + # Otherwise scalene triangle + else: + print("Scalene Triangle") + + +x = 8 +y = 7 +z = 9 + +checkTriangle(x, y, z) diff --git a/Program to classify a number as prime number or composite, b/Program to classify a number as prime number or composite, new file mode 100644 index 00000000..f540b7f5 --- /dev/null +++ b/Program to classify a number as prime number or composite, @@ -0,0 +1,24 @@ +#include +#include +int main() +{ +int flag=0,i,num; +clrscr(); + +prontf("\n Enter any number:"); +scanf("%d",&num); +for(i=2; i +#include +#include +#include + +using namespace std; + +int main() +{ + int b,d=0,i=0,s; + cin>>b; + while(b!=0) + { + + s=b%10; + b/=10; + if(s!=0&&s!=1) + { + break; + } + d=d+(s*pow(2,i++)); + } + if(s!=0&&s!=1) + cout<<"not a binary"; + else + cout<<"Decimal Conversion is : "< +#include + +int binary_to_octal(long int binary) +{ +int octal = 0, decimal = 0, i = 0; + +while(binary != 0) +{ +decimal += (binary%10) * pow(2,i); +++i; +binary/=10; +} + +i = 1; + +while (decimal != 0) +{ +octal += (decimal % 8) * i; +decimal /= 8; +i *= 10; +} + +return octal; +} + +int main() +{ +long int binary; + +printf(“\nEnter a binary number: “); +scanf(“%lld”, &binary); + +printf(“\nOctal Equivalent : %d\n”, binary_to_octal(binary)); + +return 0; +} diff --git a/Program to convert binary digits to octal digits b/Program to convert binary digits to octal digits new file mode 100644 index 00000000..a300ba46 --- /dev/null +++ b/Program to convert binary digits to octal digits @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int b,d=0,i=0,s; + cin>>b; + while(b!=0) + { + + s=b%10; + b/=10; + if(s!=0&&s!=1) + { + break; + } + d=d+(s*pow(8,i++)); + } + if(s!=0&&s!=1) + cout<<"not a binary"; + else + cout<<"Decimal Conversion is : "< +#include +#include + +int main() +{ + char binaryNumber[] = "1001"; + int bin, dec = 0; + + bin = atoi(binaryNumber); + + for(int i = 0; bin; i++, bin /= 10) + if (bin % 10) + dec += pow(2, i); + + printf("%d", dec); + + return 0; +} diff --git a/Program to convert binary to decimal number Pre-define Function/ProgramToConvertBinaryToDecimalNumber.cpp b/Program to convert binary to decimal number Pre-define Function/ProgramToConvertBinaryToDecimalNumber.cpp new file mode 100644 index 00000000..6ef2283f --- /dev/null +++ b/Program to convert binary to decimal number Pre-define Function/ProgramToConvertBinaryToDecimalNumber.cpp @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main() +{ + char binaryNumber[] = "1001"; + + cout << stoi(binaryNumber, 0, 2); + + return 0; +} diff --git a/Program to convert binary to decimal number Pre-define Function/ProgramToConvertBinaryToDecimalNumber.java b/Program to convert binary to decimal number Pre-define Function/ProgramToConvertBinaryToDecimalNumber.java new file mode 100644 index 00000000..17601ec0 --- /dev/null +++ b/Program to convert binary to decimal number Pre-define Function/ProgramToConvertBinaryToDecimalNumber.java @@ -0,0 +1,7 @@ +class ConBintoDEC { + public static void main(String args[]) + { + String binaryNumber = "1001"; + System.out.println(Integer.parseInt(binaryNumber, 2)); + } +} diff --git a/Program to convert binary to decimal number Pre-define Function/ProgramToConvertBinaryToDecimalNumber.py b/Program to convert binary to decimal number Pre-define Function/ProgramToConvertBinaryToDecimalNumber.py new file mode 100644 index 00000000..ce80d835 --- /dev/null +++ b/Program to convert binary to decimal number Pre-define Function/ProgramToConvertBinaryToDecimalNumber.py @@ -0,0 +1,9 @@ +# Python3 program to convert +# binary to decimal +n = input("Enter the binary no. : ") + +# Convert n to base 2 +s = int(n, 2) + +print(s) + diff --git a/Program to convert binary to decimal number in Java b/Program to convert binary to decimal number in Java new file mode 100644 index 00000000..650775b6 --- /dev/null +++ b/Program to convert binary to decimal number in Java @@ -0,0 +1,21 @@ +public class BinaryToDecimalExample3{ +public static int getDecimal(int binary){ + int decimal = 0; + int n = 0; + while(true){ + if(binary == 0){ + break; + } else { + int temp = binary%10; + decimal += temp*Math.pow(2, n); + binary = binary/10; + n++; + } + } + return decimal; +} +public static void main(String args[]){ +System.out.println("Decimal of 1010 is: "+getDecimal(1010)); +System.out.println("Decimal of 10101 is: "+getDecimal(10101)); +System.out.println("Decimal of 11111 is: "+getDecimal(11111)); +}} diff --git a/Program to convert binary to decimal number using c b/Program to convert binary to decimal number using c new file mode 100644 index 00000000..6b73a730 --- /dev/null +++ b/Program to convert binary to decimal number using c @@ -0,0 +1,21 @@ +#include +#include +int convert(long long n); +int main() { + long long n; + printf("Enter a binary number: "); + scanf("%lld", &n); + printf("%lld in binary = %d in decimal", n, convert(n)); + return 0; +} + +int convert(long long n) { + int dec = 0, i = 0, rem; + while (n != 0) { + rem = n % 10; + n /= 10; + dec += rem * pow(2, i); + ++i; + } + return dec; +} diff --git a/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.c b/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.c new file mode 100644 index 00000000..f9fda78b --- /dev/null +++ b/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.c @@ -0,0 +1,19 @@ +#include +#include +#include + +int main() +{ + char binaryNumber[] = "1001"; + int bin, dec = 0; + + bin = atoi(binaryNumber); + + for(int i = 0; bin; i++, bin /= 10) + if (bin % 10) + dec += pow(2, i); + + printf("%d", dec); + + return 0; +} diff --git a/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.cpp b/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.cpp new file mode 100644 index 00000000..6ef2283f --- /dev/null +++ b/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.cpp @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main() +{ + char binaryNumber[] = "1001"; + + cout << stoi(binaryNumber, 0, 2); + + return 0; +} diff --git a/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.java b/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.java new file mode 100644 index 00000000..17601ec0 --- /dev/null +++ b/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.java @@ -0,0 +1,7 @@ +class ConBintoDEC { + public static void main(String args[]) + { + String binaryNumber = "1001"; + System.out.println(Integer.parseInt(binaryNumber, 2)); + } +} diff --git a/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.py b/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.py new file mode 100644 index 00000000..ce80d835 --- /dev/null +++ b/Program to convert binary to decimal number/Pre-define/ProgramToConvertBinaryToDecimalNumber.py @@ -0,0 +1,9 @@ +# Python3 program to convert +# binary to decimal +n = input("Enter the binary no. : ") + +# Convert n to base 2 +s = int(n, 2) + +print(s) + diff --git a/Program to convert binary to decimal number/ProgramToConvertBinaryToDecimalNumber.cpp b/Program to convert binary to decimal number/ProgramToConvertBinaryToDecimalNumber.cpp new file mode 100644 index 00000000..60a4b6b8 --- /dev/null +++ b/Program to convert binary to decimal number/ProgramToConvertBinaryToDecimalNumber.cpp @@ -0,0 +1,19 @@ +#include +#include +int main(){ +int a[10],n,i; +system ("cls"); +printf("Enter the number to convert: "); +scanf("%d",&n); +for(i=0;n>0;i++) +{ +a[i]=n%2; +n=n/2; +} +printf("\nBinary of Given Number is="); +for(i=i-1;i>=0;i--) +{ +printf("%d",a[i]); +} +return 0; +} diff --git a/Program to convert binary to decimal number/ProgramToConvertBinaryToDecimalNumber.java b/Program to convert binary to decimal number/ProgramToConvertBinaryToDecimalNumber.java new file mode 100644 index 00000000..7b83e4dd --- /dev/null +++ b/Program to convert binary to decimal number/ProgramToConvertBinaryToDecimalNumber.java @@ -0,0 +1,35 @@ +// Java program to convert +// binary to decimal + +// Function to convert +// binary to decimal +class GFG { + static int binaryToDecimal(int n) + { + int num = n; + int dec_value = 0; + + // Initializing base + // value to 1, i.e 2^0 + int base = 1; + + int temp = num; + while (temp > 0) { + int last_digit = temp % 10; + temp = temp / 10; + + dec_value += last_digit * base; + + base = base * 2; + } + + return dec_value; + } + + // Driver Code + public static void main(String[] args) + { + int num = 10101001; + System.out.println(binaryToDecimal(num)); + } +} diff --git a/Program to convert binary to decimal number/ProgramToConvertBinaryToDecimalNumber.py b/Program to convert binary to decimal number/ProgramToConvertBinaryToDecimalNumber.py new file mode 100644 index 00000000..7219ebe8 --- /dev/null +++ b/Program to convert binary to decimal number/ProgramToConvertBinaryToDecimalNumber.py @@ -0,0 +1,25 @@ +# Python3 program to convert +# binary to decimal + +# Function to convert +# binary to decimal +def binaryToDecimal(n): + num = n; + dec_value = 0; + + # Initializing base + # value to 1, i.e 2 ^ 0 + base = 1; + + temp = num; + while(temp): + last_digit = temp % 10; + temp = int(temp / 10); + + dec_value += last_digit * base; + base = base * 2; + return dec_value; + +# Driver Code +num = int(input("Enter the binary no. : ")) +print(binaryToDecimal(num)); diff --git a/Program to convert binary to decimal numberdef binaryToDecimal(n): num = n; dec_value = 0; # Initializing base # value to 1, i.e 2 ^ 0 base = 1; temp = num; while(temp): last_digit = temp % 10; temp = int(temp/Program to convert binary to decimal number b/Program to convert binary to decimal numberdef binaryToDecimal(n): num = n; dec_value = 0; # Initializing base # value to 1, i.e 2 ^ 0 base = 1; temp = num; while(temp): last_digit = temp % 10; temp = int(temp/Program to convert binary to decimal number index 84f768b5..2d64742f 100644 --- a/Program to convert binary to decimal numberdef binaryToDecimal(n): num = n; dec_value = 0; # Initializing base # value to 1, i.e 2 ^ 0 base = 1; temp = num; while(temp): last_digit = temp % 10; temp = int(temp/Program to convert binary to decimal number +++ b/Program to convert binary to decimal numberdef binaryToDecimal(n): num = n; dec_value = 0; # Initializing base # value to 1, i.e 2 ^ 0 base = 1; temp = num; while(temp): last_digit = temp % 10; temp = int(temp/Program to convert binary to decimal number @@ -1,17 +1,30 @@ -def binaryToDecimal(n): - num = n; - dec_value = 0; - - base = 1; - - temp = num; - while(temp): - last_digit = temp % 10; - temp = int(temp / 10); - - dec_value += last_digit * base; - base = base * 2; - return dec_value; +class GFG { + public static int binaryToDecimal(int n) + { + int num = n; + int dec_value = 0; -num = 10101001; -print(binaryToDecimal(num)); + // Initializing base1 + // value to 1, i.e 2^0 + int base1 = 1; + + int temp = num; + while (temp > 0) { + int last_digit = temp % 10; + temp = temp / 10; + + dec_value += last_digit * base1; + + base1 = base1 * 2; + } + + return dec_value; + } + + public static void Main() + { + int num = 10101001; + + System.Console.Write(binaryToDecimal(num)); + } +} diff --git a/Program to convert binary to decimal.c b/Program to convert binary to decimal.c new file mode 100644 index 00000000..b721e0bc --- /dev/null +++ b/Program to convert binary to decimal.c @@ -0,0 +1,20 @@ + +#include +#include +#include + +int main() +{ + char binaryNumber[] = "1001"; + int bin, decimal = 0; + + bin = atoi(binaryNumber); + + for(int i = 0; bin; i++, bin /= 10) + if (bin % 10) + decimal += pow(2, i); + + printf("%d", decimal); + + return 0; +} diff --git a/Program to convert binary to octal number In C Language b/Program to convert binary to octal number In C Language new file mode 100644 index 00000000..c1bff256 --- /dev/null +++ b/Program to convert binary to octal number In C Language @@ -0,0 +1,29 @@ +#include +int convert(long long bin); +int main() { + long long bin; + printf("Enter a binary number: "); + scanf("%lld", &bin); + printf("%lld in binary = %d in octal", bin, convert(bin)); + return 0; +} + +int convert(long long bin) { + int oct = 0, dec = 0, i = 0; + + // converting binary to decimal + while (bin != 0) { + dec += (bin % 10) * pow(2, i); + ++i; + bin /= 10; + } + i = 1; + + // converting to decimal to octal + while (dec != 0) { + oct += (dec % 8) * i; + dec /= 8; + i *= 10; + } + return oct; +} diff --git a/Program to convert binary to octal number and vice versa b/Program to convert binary to octal number and vice versa new file mode 100644 index 00000000..13605b52 --- /dev/null +++ b/Program to convert binary to octal number and vice versa @@ -0,0 +1,28 @@ +#include +#include +int convert(long long bin); +int main() { + long long bin; + printf("Enter a binary number: "); + scanf("%lld", &bin); + printf("%lld in binary = %d in octal", bin, convert(bin)); + return 0; +} + +int convert(long long bin) { + int oct = 0, dec = 0, i = 0; + + while (bin != 0) { + dec += (bin % 10) * pow(2, i); + ++i; + bin /= 10; + } + i = 1; + + while (dec != 0) { + oct += (dec % 8) * i; + dec /= 8; + i *= 10; + } + return oct; +} diff --git a/Program to convert binary to octal number with python b/Program to convert binary to octal number with python new file mode 100644 index 00000000..89b6528f --- /dev/null +++ b/Program to convert binary to octal number with python @@ -0,0 +1,71 @@ +# Python3 implementation to convert a binary number +# to octal number + +# function to create map between binary +# number and its equivalent octal +def createMap(bin_oct_map): + bin_oct_map["000"] = '0' + bin_oct_map["001"] = '1' + bin_oct_map["010"] = '2' + bin_oct_map["011"] = '3' + bin_oct_map["100"] = '4' + bin_oct_map["101"] = '5' + bin_oct_map["110"] = '6' + bin_oct_map["111"] = '7' + +# Function to find octal equivalent of binary +def convertBinToOct(bin): + l = len(bin) + + # length of string before '.' + t = -1 + if '.' in bin: + t = bin.index('.') + len_left = t + else: + len_left = l + + # add min 0's in the beginning to make + # left substring length divisible by 3 + for i in range(1, (3 - len_left % 3) % 3 + 1): + bin = '0' + bin + + # if decimal point exists + if (t != -1): + + # length of string after '.' + len_right = l - len_left - 1 + + # add min 0's in the end to make right + # substring length divisible by 3 + for i in range(1, (3 - len_right % 3) % 3 + 1): + bin = bin + '0' + + # create dictionary between binary and its + # equivalent octal code + bin_oct_map = {} + createMap(bin_oct_map) + i = 0 + octal = "" + + while (True) : + + # one by one extract from left, substring + # of size 3 and add its octal code + octal += bin_oct_map[bin[i:i + 3]] + i += 3 + if (i == len(bin)): + break + + # if '.' is encountered add it to result + if (bin[i] == '.'): + octal += '.' + i += 1 + + # required octal number + return octal + +# Driver Code +bin = "1111001010010100001.010110110011011" +print("Octal number = ", + convertBinToOct(bin)) diff --git a/Program to convert lower case to upper and upper case to lower b/Program to convert lower case to upper and upper case to lower new file mode 100644 index 00000000..65416059 --- /dev/null +++ b/Program to convert lower case to upper and upper case to lower @@ -0,0 +1,16 @@ +#include +#include +int main() +{ + char ch; + clrscr(); + printf("\n Enter any charecter:"); + scanf("%c",&ch); + + if (ch>= 'A'&& ch<='Z') + printf("\n The entered charecter was in upper case. In lower case it is : %c",(ch+32)); + + else + printf("\n The entered charecter was in lower case.In upper case it is : %c",(ch-32)); + return 0; + } diff --git a/Program to count sum of all numbers in array .. b/Program to count sum of all numbers in array .. new file mode 100644 index 00000000..63a985c9 --- /dev/null +++ b/Program to count sum of all numbers in array .. @@ -0,0 +1,28 @@ +#include +using namespace std; + +// Returns number of pairs in arr[0..n-1] with sum equal +// to 'sum' +int getPairsCount(int arr[], int n, int sum) +{ + int count = 0; // Initialize result + + // Consider all possible pairs and check their sums + for (int i=0; i>n; +int s[n]; +for(int j=0;j>s[j];} +long long int sum=0; +for(int i=0;i>n; +int s[n]; +for(int j=0;j>s[j];} +long long int sum=0; +for(int i=0;i + + +int main() +{ + int a[1000],i,n,sum=0; + + printf("Enter size of the array : "); + scanf("%d",&n); + + printf("Enter elements in array : "); + for(i=0; i= 'a' && ch <= 'z') || + (ch >= 'A' && ch <= 'Z') ) { + + // To handle upper case letters + ch = Character.toLowerCase(ch);; + + if (ch == 'a' || ch == 'e' || ch == 'i' || + ch == 'o' || ch == 'u') + vowels++; + else + consonant++; + } + else if (ch >= '0' && ch <= '9') + digit++; + else + specialChar++; + } + + System.out.println("Vowels: " + vowels); + System.out.println("Consonant: " + consonant); + System.out.println("Digit: " + digit); + System.out.println("Special Character: " + specialChar); + } + + // Driver function. + static public void main (String[] args) + { + String str = "geeks for geeks121"; + + countCharacterType(str); + } +} diff --git a/Program to count vowels, consonants b/Program to count vowels, consonants new file mode 100644 index 00000000..c9c1d5b1 --- /dev/null +++ b/Program to count vowels, consonants @@ -0,0 +1,31 @@ +#include +int main() { + char line[150]; + int vowels, consonant, digit, space; + + vowels = consonant = digit = space = 0; + + printf("Enter a line of string: "); + fgets(line, sizeof(line), stdin); + + for (int i = 0; line[i] != '\0'; ++i) { + if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || + line[i] == 'o' || line[i] == 'u' || line[i] == 'A' || + line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || + line[i] == 'U') { + ++vowels; + } else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) { + ++consonant; + } else if (line[i] >= '0' && line[i] <= '9') { + ++digit; + } else if (line[i] == ' ') { + ++space; + } + } + + printf("Vowels: %d", vowels); + printf("\nConsonants: %d", consonant); + printf("\nDigits: %d", digit); + printf("\nWhite spaces: %d", space); + return 0; +} diff --git a/Program to find factorial b/Program to find factorial new file mode 100644 index 00000000..802cf83e --- /dev/null +++ b/Program to find factorial @@ -0,0 +1,7 @@ +def factorial(n): + if n==0 or n==1: + return 1 + else: + return n*factorial(n-1) +n=int(input()) +print(factorial(n)) diff --git a/Program to find factorial In C Language b/Program to find factorial In C Language new file mode 100644 index 00000000..07b09ea2 --- /dev/null +++ b/Program to find factorial In C Language @@ -0,0 +1,19 @@ +#include +int main() { + int n, i; + unsigned long long fact = 1; + printf("Enter an integer: "); + scanf("%d", &n); + + // shows error if the user enters a negative integer + if (n < 0) + printf("Error! Factorial of a negative number doesn't exist."); + else { + for (i = 1; i <= n; ++i) { + fact *= i; + } + printf("Factorial of %d = %llu", n, fact); + } + + return 0; +} diff --git a/Program to find factorial in JavaScript b/Program to find factorial in JavaScript new file mode 100644 index 00000000..eb719217 --- /dev/null +++ b/Program to find factorial in JavaScript @@ -0,0 +1,19 @@ +// take input from the user +let number = parseInt(prompt('Enter a positive integer: ')); + +// if negative +if (number < 0) { + console.log('Error! Factorial for negative number does not exist.'); +} + +else if (number === 0) { + console.log(`The factorial of ${number} is 1.`); +} + +else { + let fact = 1; + for (i = 1; i <= number; i++) { + fact *= i; + } + console.log(`The factorial of ${number} is ${fact}.`); +} diff --git a/Program to find factorial in JavaScript.js b/Program to find factorial in JavaScript.js new file mode 100644 index 00000000..eb719217 --- /dev/null +++ b/Program to find factorial in JavaScript.js @@ -0,0 +1,19 @@ +// take input from the user +let number = parseInt(prompt('Enter a positive integer: ')); + +// if negative +if (number < 0) { + console.log('Error! Factorial for negative number does not exist.'); +} + +else if (number === 0) { + console.log(`The factorial of ${number} is 1.`); +} + +else { + let fact = 1; + for (i = 1; i <= number; i++) { + fact *= i; + } + console.log(`The factorial of ${number} is ${fact}.`); +} diff --git a/Program to find factorial of number in java b/Program to find factorial of number in java new file mode 100644 index 00000000..e0b784fd --- /dev/null +++ b/Program to find factorial of number in java @@ -0,0 +1,13 @@ + public class FactorialProgram{ + public static void main(String args[]){ + + int num=5; + long factorial =1; + + while(num > 0) { + factorial *= num; + num--; + } + System.out.println("Factorial is : " +factorial); + } + } diff --git a/Program to find factorial of number using C++ b/Program to find factorial of number using C++ new file mode 100644 index 00000000..2dba75a8 --- /dev/null +++ b/Program to find factorial of number using C++ @@ -0,0 +1,10 @@ +##Program to find factorial of number +main(){ +int n,fact=1; +cin>>n; +if(n==0) +cout<<"1"; +if(n>1){ +for(int i=1;i<=n;i++){ +fact=fact*i;} +cout<>n; +if(n==0) +cout<<"1"; +if(n>1){ +for(int i=1;i<=n;i++){ +fact=fact*i;} +cout< + +int main() { + int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; + int loop, largest; + + largest = array[0]; + + for(loop = 1; loop < 10; loop++) { + if( largest < array[loop] ) + largest = array[loop]; + } + + printf("Largest element of array is %d", largest); + + return 0; +} diff --git a/Program to find largest element in an array b/Program to find largest element in an array new file mode 100644 index 00000000..98a77125 --- /dev/null +++ b/Program to find largest element in an array @@ -0,0 +1,22 @@ +#include +int main() { + int i, n; + float arr[100]; + printf("Enter the number of elements (1 to 100): "); + scanf("%d", &n); + + for (i = 0; i < n; ++i) { + printf("Enter number%d: ", i + 1); + scanf("%f", &arr[i]); + } + + // storing the largest number to arr[0] + for (i = 1; i < n; ++i) { + if (arr[0] < arr[i]) + arr[0] = arr[i]; + } + + printf("Largest element = %.2f", arr[0]); + + return 0; +} diff --git a/Program to find largest element in array In C_Lang b/Program to find largest element in array In C_Lang new file mode 100644 index 00000000..98a77125 --- /dev/null +++ b/Program to find largest element in array In C_Lang @@ -0,0 +1,22 @@ +#include +int main() { + int i, n; + float arr[100]; + printf("Enter the number of elements (1 to 100): "); + scanf("%d", &n); + + for (i = 0; i < n; ++i) { + printf("Enter number%d: ", i + 1); + scanf("%f", &arr[i]); + } + + // storing the largest number to arr[0] + for (i = 1; i < n; ++i) { + if (arr[0] < arr[i]) + arr[0] = arr[i]; + } + + printf("Largest element = %.2f", arr[0]); + + return 0; +} diff --git a/Program to find largest element in array in Java Using Eclipse b/Program to find largest element in array in Java Using Eclipse new file mode 100644 index 00000000..965f1fe1 --- /dev/null +++ b/Program to find largest element in array in Java Using Eclipse @@ -0,0 +1,22 @@ +import java .io.*; +import java.util.*; + +class GFG +{ + // returns maximum in arr[] of size n + static int largest(int []arr, + int n) + { + Arrays.sort(arr); + return arr[n - 1]; + } + + // Driver code + static public void main (String[] args) + { + int []arr = {10, 324, 45, + 90, 9808}; + int n = arr.length; + System.out.println(largest(arr, n)); + } +} diff --git a/Program to find largest element in array using C++ b/Program to find largest element in array using C++ new file mode 100644 index 00000000..cb472b6d --- /dev/null +++ b/Program to find largest element in array using C++ @@ -0,0 +1,14 @@ +##Program to find largest element in array +main(){ +int n; +cout<<"Enter length of arrays" +cin>>n; +int s[n]; +for(int i=0;i>s[i];} +int max=s[0] +for(int j=0;j>n; +int s[n]; +for(int i=0;i>s[i];} +int max=s[0] +for(int j=0;j +int main() { + int i, n; + float arr[100]; + printf("Enter the number of elements (1 to 100): "); + scanf("%d", &n); + + for (i = 0; i < n; ++i) { + printf("Enter number%d: ", i + 1); + scanf("%f", &arr[i]); + } + + // storing the largest number to arr[0] + for (i = 1; i < n; ++i) { + if (arr[0] < arr[i]) + arr[0] = arr[i]; + } + + printf("Largest element = %.2f", arr[0]); + + return 0; +} diff --git a/Program to find largest element in array using c++ b/Program to find largest element in array using c++ new file mode 100644 index 00000000..1e909f66 --- /dev/null +++ b/Program to find largest element in array using c++ @@ -0,0 +1,30 @@ +#include +using namespace std; + +int main() +{ + int i, n; + float arr[100]; + + cout << "Enter total number of elements(1 to 100): "; + cin >> n; + cout << endl; + + // Store number entered by the user + for(i = 0; i < n; ++i) + { + cout << "Enter Number " << i + 1 << " : "; + cin >> arr[i]; + } + + // Loop to store largest number to arr[0] + for(i = 1;i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + cout << "Largest element = " << arr[0]; + + return 0; +} diff --git a/Program to find larget number in array b/Program to find larget number in array new file mode 100644 index 00000000..9b282b67 --- /dev/null +++ b/Program to find larget number in array @@ -0,0 +1,17 @@ +public class Largest { + + public static void main(String[] args) { + + + int[] numFirstArray = { 3,6,8,9,6,2}; + + int largest = numFirstArray[0]; + + for (int num: numFirstArray) { + if(largest < num) + largest = num; + } + + System.out.format("Largest element in array : "+ largest); + } +} diff --git a/Program to find leap years b/Program to find leap years new file mode 100644 index 00000000..b101f209 --- /dev/null +++ b/Program to find leap years @@ -0,0 +1,18 @@ +Input = [2001, 2002, 2003, 2004, 2005, 2006, + 2007, 2008, 2009, 2010, 2011, 2012] + +# Find whether it is leap year or not +def checkYear(year): + return (((year % 4 == 0) and + (year % 100 != 0)) or + (year % 400 == 0)) + +# Answer Initialization +Answer = 0 + +for elem in Input: + if checkYear(elem): + Answer = Answer + 1 + +# Printing +print("No of leap years are:", Answer) diff --git a/Program to find lenght of a string in c b/Program to find lenght of a string in c new file mode 100644 index 00000000..a20fd7c3 --- /dev/null +++ b/Program to find lenght of a string in c @@ -0,0 +1,10 @@ +#include +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/Program to find lenght of a string using python b/Program to find lenght of a string using python new file mode 100644 index 00000000..301fe632 --- /dev/null +++ b/Program to find lenght of a string using python @@ -0,0 +1,7 @@ +str = input("Enter a string: ") + +# counter variable to count the character in a string +counter = 0 +for s in str: + counter = counter+1 +print("Length of the input string is:", counter) diff --git a/Program to find length of String b/Program to find length of String new file mode 100644 index 00000000..dbca6eb2 --- /dev/null +++ b/Program to find length of String @@ -0,0 +1,18 @@ +/*lex program to find the length of a string*/ + +%{ + #include + int length; +%} + +/* Rules Section*/ +%% + [a-z A-Z 0-9]+ {length=yyleng; } +%% + +int main() + { + yylex(); + printf("length of given string is : %d", length); + return 0; + } diff --git a/Program to find length of the string in javaScript b/Program to find length of the string in javaScript new file mode 100644 index 00000000..8b3e5d40 --- /dev/null +++ b/Program to find length of the string in javaScript @@ -0,0 +1,5 @@ +// Length of the string + +var text = 'Some text'; + +console.log(text.length); diff --git a/Program to find power of any number by Encryptor-Sec b/Program to find power of any number by Encryptor-Sec new file mode 100644 index 00000000..e7d59525 --- /dev/null +++ b/Program to find power of any number by Encryptor-Sec @@ -0,0 +1,16 @@ +#include +int main() { + int base, exp; + long long result = 1; + printf("Enter a base number: "); + scanf("%d", &base); + printf("Enter an exponent: "); + scanf("%d", &exp); + + while (exp != 0) { + result *= base; + --exp; + } + printf("Answer = %lld", result); + return 0; +} diff --git a/Program to find power of any number in C Language. b/Program to find power of any number in C Language. new file mode 100644 index 00000000..e7d59525 --- /dev/null +++ b/Program to find power of any number in C Language. @@ -0,0 +1,16 @@ +#include +int main() { + int base, exp; + long long result = 1; + printf("Enter a base number: "); + scanf("%d", &base); + printf("Enter an exponent: "); + scanf("%d", &exp); + + while (exp != 0) { + result *= base; + --exp; + } + printf("Answer = %lld", result); + return 0; +} diff --git a/Program to find power of any number using c b/Program to find power of any number using c new file mode 100644 index 00000000..cd8ae7c1 --- /dev/null +++ b/Program to find power of any number using c @@ -0,0 +1,19 @@ +#include +int power(int n1, int n2); +int main() { + int base, a, result; + printf("Enter base number: "); + scanf("%d", &base); + printf("Enter power number(positive integer): "); + scanf("%d", &a); + result = power(base, a); + printf("%d^%d = %d", base, a, result); + return 0; +} + +int power(int base, int a) { + if (a != 0) + return (base * power(base, a - 1)); + else + return 1; +} diff --git a/Program to find smallest element in an array b/Program to find smallest element in an array new file mode 100644 index 00000000..9bf488b4 --- /dev/null +++ b/Program to find smallest element in an array @@ -0,0 +1,29 @@ +include +int main() +{ + int a[10], Size, i, Smallest, Position; + + printf("\nPlease Enter the size of an array \n"); + scanf("%d",&Size); + + printf("\nPlease Enter %d elements of an array: \n", Size); + for(i=0; i a[i]) + { + Smallest = a[i]; + Position = i; + } + } + + printf("\nSmallest element in an Array = %d", Smallest); + printf("\nIndex position of the Smallest element = %d", Position); + + return 0; +} diff --git a/Program to find smallest element in array b/Program to find smallest element in array new file mode 100644 index 00000000..5c5c4407 --- /dev/null +++ b/Program to find smallest element in array @@ -0,0 +1,26 @@ +#include + +int main() { + int a[30], i, num, smallest; + + printf("\nEnter no of elements :"); + scanf("%d", &num); + + + for (i = 0; i < num; i++) + scanf("%d", &a[i]); + + + smallest = a[0]; + + for (i = 0; i < num; i++) { + if (a[i] < smallest) { + smallest = a[i]; + } + } + + + printf("\nSmallest Element : %d", smallest); + + return (0); +} diff --git a/Program to find smallest element in array In C_Lang b/Program to find smallest element in array In C_Lang new file mode 100644 index 00000000..fadd0c00 --- /dev/null +++ b/Program to find smallest element in array In C_Lang @@ -0,0 +1,26 @@ +#include + +int main() { + int a[30], i, num, smallest; + + printf("\nEnter no of elements :"); + scanf("%d", &num); + + //Read n elements in an array + for (i = 0; i < num; i++) + scanf("%d", &a[i]); + + //Consider first element as smallest + smallest = a[0]; + + for (i = 0; i < num; i++) { + if (a[i] < smallest) { + smallest = a[i]; + } + } + + // Print out the Result + printf("\nSmallest Element : %d", smallest); + + return (0); + } diff --git a/Program to find smallest element in array using C++ b/Program to find smallest element in array using C++ new file mode 100644 index 00000000..b79eafdf --- /dev/null +++ b/Program to find smallest element in array using C++ @@ -0,0 +1,14 @@ +##Program to find smallest element in array +main(){ +int n; +cout<<"Enter length of arrays" +cin>>n; +int s[n]; +for(int i=0;i>s[i];} +int min=s[0] +for(int j=0;js[i]) +max=s[i]; +} +cout< num) + smallest = num; + } + + System.out.format("Smallest element in array : "+ smallest); + } +} diff --git a/Program to find sum of all elements in an array b/Program to find sum of all elements in an array new file mode 100644 index 00000000..60e55679 --- /dev/null +++ b/Program to find sum of all elements in an array @@ -0,0 +1,26 @@ +#include + + +int main() +{ + int a[1000],i,n,sum=0; + + printf("Enter size of the array : "); + scanf("%d",&n); + + printf("Enter elements in array : "); + for(i=0; i +int greater(int a,int b, int c); + +int main() +{ +int num1 , num2 , num3 , large; +printf("\n Enter the first number:"); +scanf("%d",&num1); +printf("\n Enter the second number:"); +scanf("%d",&num2); +printf("\n Enter the third number:"); +scanf("%d",&num3); + +large= greater(num1,num2,num3); +printf("\n Largest number=%d",large); +return0; +} +int greater{int a, int b, int c} +{ + if(a>b&&a>c) + return a; + if(b>a&&b>c) + return b; + else + return c; +} diff --git a/Program to find the factorial of a number. b/Program to find the factorial of a number. new file mode 100644 index 00000000..07b09ea2 --- /dev/null +++ b/Program to find the factorial of a number. @@ -0,0 +1,19 @@ +#include +int main() { + int n, i; + unsigned long long fact = 1; + printf("Enter an integer: "); + scanf("%d", &n); + + // shows error if the user enters a negative integer + if (n < 0) + printf("Error! Factorial of a negative number doesn't exist."); + else { + for (i = 1; i <= n; ++i) { + fact *= i; + } + printf("Factorial of %d = %llu", n, fact); + } + + return 0; +} diff --git a/Program to find the largest no in array in O(1) time Complexity. b/Program to find the largest no in array in O(1) time Complexity. new file mode 100644 index 00000000..611dc074 --- /dev/null +++ b/Program to find the largest no in array in O(1) time Complexity. @@ -0,0 +1,26 @@ +#include +using namespace std; +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + int size; + cout<<"Enter the size of the array"<>size; + int arr[size]; + cout<<"Enter the elements of the array"<>arr[i]; + if(arr[i]<=arr[i-1]&&i!=0) + { + arr[i] = arr[i-1]; + } + + } + cout< +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/Program to find the length of the string. b/Program to find the length of the string. new file mode 100644 index 00000000..7b09e4c4 --- /dev/null +++ b/Program to find the length of the string. @@ -0,0 +1,10 @@ +#include +int main() { + char s[] = "Hacktoberfest is on!!!."; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/Program to find the power of any number b/Program to find the power of any number new file mode 100644 index 00000000..a18f4f09 --- /dev/null +++ b/Program to find the power of any number @@ -0,0 +1,6 @@ +n=int(input("Enter number : ")) +exp=int(input("Enter power : ")) +ans=1 +for i in range(1,exp+1): + ans=ans*n +print("Value of ",n," raised to the power ",exp," is ",ans) diff --git a/Program to find the smallest element in an array b/Program to find the smallest element in an array new file mode 100644 index 00000000..441e2ce4 --- /dev/null +++ b/Program to find the smallest element in an array @@ -0,0 +1,26 @@ +#include + +int main() { + int a[30], i, num, smallest; + + printf("\nEnter no of elements :"); + scanf("%d", &num); + + //Read n elements in an array + for (i = 0; i < num; i++) + scanf("%d", &a[i]); + + //Consider first element as smallest + smallest = a[0]; + + for (i = 0; i < num; i++) { + if (a[i] < smallest) { + smallest = a[i]; + } + } + + // Print out the Result + printf("\nSmallest Element : %d", smallest); + + return (0); +} diff --git a/Program to find the smallest element in array b/Program to find the smallest element in array new file mode 100644 index 00000000..3bced32f --- /dev/null +++ b/Program to find the smallest element in array @@ -0,0 +1,24 @@ +#include + +int main() { + int a[30], i, num, smallest; + + printf("\nEnter no of elements :"); + scanf("%d", &num); + + for (i = 0; i < num; i++) + scanf("%d", &a[i]); + + smallest = a[0]; + + for (i = 0; i < num; i++) { + if (a[i] < smallest) { + smallest = a[i]; + } + } + + + printf("\nSmallest Element : %d", smallest); + + return (0); +} diff --git a/Program to find the sum of all element in an array b/Program to find the sum of all element in an array new file mode 100644 index 00000000..60e55679 --- /dev/null +++ b/Program to find the sum of all element in an array @@ -0,0 +1,26 @@ +#include + + +int main() +{ + int a[1000],i,n,sum=0; + + printf("Enter size of the array : "); + scanf("%d",&n); + + printf("Enter elements in array : "); + for(i=0; i + +// function to get matrix elements entered by the user +void getMatrixElements(int matrix[][10], int row, int column) { + + printf("\nEnter elements: \n"); + + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("Enter a%d%d: ", i + 1, j + 1); + scanf("%d", &matrix[i][j]); + } + } +} + +// function to multiply two matrices +void multiplyMatrices(int first[][10], + int second[][10], + int result[][10], + int r1, int c1, int r2, int c2) { + + // Initializing elements of matrix mult to 0. + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + result[i][j] = 0; + } + } + + // Multiplying first and second matrices and storing it in result + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + for (int k = 0; k < c1; ++k) { + result[i][j] += first[i][k] * second[k][j]; + } + } + } +} + +// function to display the matrix +void display(int result[][10], int row, int column) { + + printf("\nOutput Matrix:\n"); + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("%d ", result[i][j]); + if (j == column - 1) + printf("\n"); + } + } +} + +int main() { + int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2; + printf("Enter rows and column for the first matrix: "); + scanf("%d %d", &r1, &c1); + printf("Enter rows and column for the second matrix: "); + scanf("%d %d", &r2, &c2); + + // Taking input until + // 1st matrix columns is not equal to 2nd matrix row + while (c1 != r2) { + printf("Error! Enter rows and columns again.\n"); + printf("Enter rows and columns for the first matrix: "); + scanf("%d%d", &r1, &c1); + printf("Enter rows and columns for the second matrix: "); + scanf("%d%d", &r2, &c2); + } + + // get elements of the first matrix + getMatrixElements(first, r1, c1); + + // get elements of the second matrix + getMatrixElements(second, r2, c2); + + // multiply two matrices. + multiplyMatrices(first, second, result, r1, c1, r2, c2); + + // display the result + display(result, r1, c2); + + return 0; +} diff --git a/Program to multiply 2D or 3D Matrices in c b/Program to multiply 2D or 3D Matrices in c new file mode 100644 index 00000000..2bc95559 --- /dev/null +++ b/Program to multiply 2D or 3D Matrices in c @@ -0,0 +1,100 @@ +programming topics: + +C Arrays +C Multidimensional Arrays +This program asks the user to enter the size (rows and columns) of two matrices. + +To multiply two matrices, the number of columns of the first matrix should be equal to the number of rows of the second matrix. + +The program below asks for the number of rows and columns of two matrices until the above condition is satisfied. + +Then, the multiplication of two matrices is performed, and the result is displayed on the screen. + +To perform this, we have created three functions: + +getMatrixElements() - to take matrix elements input from the user. +multiplyMatrices() - to multiply two matrices. +display() - to display the resultant matrix after multiplication. +Multiply Matrices by Passing it to a Function +#include + +// function to get matrix elements entered by the user +void getMatrixElements(int matrix[][10], int row, int column) { + + printf("\nEnter elements: \n"); + + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("Enter a%d%d: ", i + 1, j + 1); + scanf("%d", &matrix[i][j]); + } + } +} + +// function to multiply two matrices +void multiplyMatrices(int first[][10], + int second[][10], + int result[][10], + int r1, int c1, int r2, int c2) { + + // Initializing elements of matrix mult to 0. + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + result[i][j] = 0; + } + } + + // Multiplying first and second matrices and storing it in result + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + for (int k = 0; k < c1; ++k) { + result[i][j] += first[i][k] * second[k][j]; + } + } + } +} + +// function to display the matrix +void display(int result[][10], int row, int column) { + + printf("\nOutput Matrix:\n"); + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("%d ", result[i][j]); + if (j == column - 1) + printf("\n"); + } + } +} + +int main() { + int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2; + printf("Enter rows and column for the first matrix: "); + scanf("%d %d", &r1, &c1); + printf("Enter rows and column for the second matrix: "); + scanf("%d %d", &r2, &c2); + + // Taking input until + // 1st matrix columns is not equal to 2nd matrix row + while (c1 != r2) { + printf("Error! Enter rows and columns again.\n"); + printf("Enter rows and columns for the first matrix: "); + scanf("%d%d", &r1, &c1); + printf("Enter rows and columns for the second matrix: "); + scanf("%d%d", &r2, &c2); + } + + // get elements of the first matrix + getMatrixElements(first, r1, c1); + + // get elements of the second matrix + getMatrixElements(second, r2, c2); + + // multiply two matrices. + multiplyMatrices(first, second, result, r1, c1, r2, c2); + + // display the result + display(result, r1, c2); + + return 0; +} diff --git a/Program to multiply 2D or 3D Matrices in c++ b/Program to multiply 2D or 3D Matrices in c++ new file mode 100644 index 00000000..1f2fd1f9 --- /dev/null +++ b/Program to multiply 2D or 3D Matrices in c++ @@ -0,0 +1,70 @@ +#include +using namespace std; + +int main() +{ + int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; + + cout << "Enter rows and columns for first matrix: "; + cin >> r1 >> c1; + cout << "Enter rows and columns for second matrix: "; + cin >> r2 >> c2; + + // If column of first matrix in not equal to row of second matrix, + // ask the user to enter the size of matrix again. + while (c1!=r2) + { + cout << "Error! column of first matrix not equal to row of second."; + + cout << "Enter rows and columns for first matrix: "; + cin >> r1 >> c1; + + cout << "Enter rows and columns for second matrix: "; + cin >> r2 >> c2; + } + + // Storing elements of first matrix. + cout << endl << "Enter elements of matrix 1:" << endl; + for(i = 0; i < r1; ++i) + for(j = 0; j < c1; ++j) + { + cout << "Enter element a" << i + 1 << j + 1 << " : "; + cin >> a[i][j]; + } + + // Storing elements of second matrix. + cout << endl << "Enter elements of matrix 2:" << endl; + for(i = 0; i < r2; ++i) + for(j = 0; j < c2; ++j) + { + cout << "Enter element b" << i + 1 << j + 1 << " : "; + cin >> b[i][j]; + } + + // Initializing elements of matrix mult to 0. + for(i = 0; i < r1; ++i) + for(j = 0; j < c2; ++j) + { + mult[i][j]=0; + } + + // Multiplying matrix a and b and storing in array mult. + for(i = 0; i < r1; ++i) + for(j = 0; j < c2; ++j) + for(k = 0; k < c1; ++k) + { + mult[i][j] += a[i][k] * b[k][j]; + } + + // Displaying the multiplication of two matrix. + cout << endl << "Output Matrix: " << endl; + for(i = 0; i < r1; ++i) + for(j = 0; j < c2; ++j) + { + cout << " " << mult[i][j]; + if(j == c2-1) + cout << endl; + } + + return 0; +} diff --git a/Program to multiply two matrices c# Code b/Program to multiply two matrices c# Code new file mode 100644 index 00000000..0ee8ded8 --- /dev/null +++ b/Program to multiply two matrices c# Code @@ -0,0 +1,53 @@ +// C# program to multiply two square +// matrices. +using System; + +class GFG { + + static int N = 4; + + // This function multiplies mat1[][] + // and mat2[][], and stores the result + // in res[][] + static void multiply(int[, ] mat1, + int[, ] mat2, int[, ] res) + { + int i, j, k; + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) { + res[i, j] = 0; + for (k = 0; k < N; k++) + res[i, j] += mat1[i, k] + * mat2[k, j]; + } + } + } + + // Driver code + public static void Main() + { + int[, ] mat1 = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 }, + { 4, 4, 4, 4 } }; + + int[, ] mat2 = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 }, + { 4, 4, 4, 4 } }; + + // To store result + int[, ] res = new int[N, N]; + int i, j; + multiply(mat1, mat2, res); + + Console.WriteLine("Result matrix" + + " is "); + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) + Console.Write(res[i, j] + + " "); + Console.WriteLine(); + } + } +} diff --git a/Program to print ASCII value of given character in JAVA b/Program to print ASCII value of given character in JAVA new file mode 100644 index 00000000..0ba9ad03 --- /dev/null +++ b/Program to print ASCII value of given character in JAVA @@ -0,0 +1,10 @@ +public class AsciiValue { + + public static void main(String[] args) + { + + char c = 'e'; + int ascii = c; + System.out.println("The ASCII value of " + c + " is: " + ascii); + } +} diff --git a/Program to print ASCII value of given character with python b/Program to print ASCII value of given character with python new file mode 100644 index 00000000..20166dff --- /dev/null +++ b/Program to print ASCII value of given character with python @@ -0,0 +1,9 @@ +# Python program to print +# ASCII Value of Character + +# In c we can assign different +# characters of which we want ASCII value + +c = 'g' +# print the ASCII value of assigned character in c +print("The ASCII value of '" + c + "' is", ord(c)) diff --git a/Program to print Fibonacci Series b/Program to print Fibonacci Series new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/Program to print Fibonacci Series @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/Program to print Fibonacci series in JavaScript.js b/Program to print Fibonacci series in JavaScript.js new file mode 100644 index 00000000..db2a6661 --- /dev/null +++ b/Program to print Fibonacci series in JavaScript.js @@ -0,0 +1,12 @@ +// fibonacci series + +let inputNum = prompt('Enter a Number:'); +let num1 = 0, n2 = 1, nextVal; + +nextVal = n1 + n2; + +while(nextVal <= inputNum) { + num1 = num2; + n2 = nextVal; + nextVal = n1 + n2; +} diff --git a/Program to print Fibonnaci Series Recursive approach b/Program to print Fibonnaci Series Recursive approach new file mode 100644 index 00000000..cb212a0c --- /dev/null +++ b/Program to print Fibonnaci Series Recursive approach @@ -0,0 +1,20 @@ +#include +using namespace std; +int fib(int x) { + if((x==1)||(x==0)) { + return(x); + }else { + return(fib(x-1)+fib(x-2)); + } +} +int main() { + int x , i=0; + cout << "Enter the number of terms of series : "; + cin >> x; + cout << "\nFibonnaci Series : "; + while(i < x) { + cout << " " << fib(i); + i++; + } + return 0; +} diff --git a/Program to print Fibonnaci Series by Encryptor-Sec b/Program to print Fibonnaci Series by Encryptor-Sec new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/Program to print Fibonnaci Series by Encryptor-Sec @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/Program to print Fibonnaci Series in java b/Program to print Fibonnaci Series in java new file mode 100644 index 00000000..15325705 --- /dev/null +++ b/Program to print Fibonnaci Series in java @@ -0,0 +1,21 @@ +public class Fibonacci { + + public static void main(String[] args) { + + int n = 10, n1 = 0, n2 = 1; + + System.out.print(" Fibonacci Series : "); + + for (int i = 1; i <= n; ++i) + { + + System.out.print(n1 + " "); + + int sum = n1 + n2; + n1 = n2; + n2 = sum; + + } + + } +} diff --git a/Program to print Fibonnaci Series new b/Program to print Fibonnaci Series new new file mode 100644 index 00000000..a6d632f2 --- /dev/null +++ b/Program to print Fibonnaci Series new @@ -0,0 +1,39 @@ +# Program to display the Fibonacci sequence up to n-th term + +nterms = int(input("How many terms? ")) + +# first two terms + +n1, n2 = 0, 1 + +count = 0 + +# check if the number of terms is valid + +if nterms <= 0: + + print("Please enter a positive integer") + +elif nterms == 1: + + print("Fibonacci sequence upto",nterms,":") + + print(n1) + +else: + + print("Fibonacci sequence:") + + while count < nterms: + + print(n1) + + nth = n1 + n2 + + # update values + + n1 = n2 + + n2 = nth + + count += 1 diff --git a/Program to print Fibonnaci Series using Kotlin b/Program to print Fibonnaci Series using Kotlin new file mode 100644 index 00000000..7dbd98c8 --- /dev/null +++ b/Program to print Fibonnaci Series using Kotlin @@ -0,0 +1,18 @@ +fun main(args: Array) { + + val fibCount = 5 + val feb = IntArray(fibCount) + fib[0] = 0 + fib[1] = 1 + for (i in 2 until fibCount) { + fib[i] = fib[i - 1] + fib[i - 2] + } + + for (i in 0 until fibCount) { + print(fib[i].toString() + " ") + } +} + + +Output +0 1 1 2 3 diff --git a/Program to print Fibonnaci Series using Recursion b/Program to print Fibonnaci Series using Recursion new file mode 100644 index 00000000..cb212a0c --- /dev/null +++ b/Program to print Fibonnaci Series using Recursion @@ -0,0 +1,20 @@ +#include +using namespace std; +int fib(int x) { + if((x==1)||(x==0)) { + return(x); + }else { + return(fib(x-1)+fib(x-2)); + } +} +int main() { + int x , i=0; + cout << "Enter the number of terms of series : "; + cin >> x; + cout << "\nFibonnaci Series : "; + while(i < x) { + cout << " " << fib(i); + i++; + } + return 0; +} diff --git a/Program to print Fibonnaci Series using python b/Program to print Fibonnaci Series using python new file mode 100644 index 00000000..17e24228 --- /dev/null +++ b/Program to print Fibonnaci Series using python @@ -0,0 +1,21 @@ +nterms = int(input("How many terms? ")) + +# first two terms +n1, n2 = 0, 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence:") + while count < nterms: + print(n1) + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/Program to print Fibonnaci Series.js b/Program to print Fibonnaci Series.js new file mode 100644 index 00000000..db2a6661 --- /dev/null +++ b/Program to print Fibonnaci Series.js @@ -0,0 +1,12 @@ +// fibonacci series + +let inputNum = prompt('Enter a Number:'); +let num1 = 0, n2 = 1, nextVal; + +nextVal = n1 + n2; + +while(nextVal <= inputNum) { + num1 = num2; + n2 = nextVal; + nextVal = n1 + n2; +} diff --git a/Program to print all prime numbers in given range b/Program to print all prime numbers in given range new file mode 100644 index 00000000..1d875ac8 --- /dev/null +++ b/Program to print all prime numbers in given range @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +using namespace std; + +bool isPrime(int n) +{ + // Corner case + if (n <= 1) + return false; + + // Check from 2 to n-1 + for (int i = 2; i < n; i++) + if (n % i == 0) + return false; + + return true; +} +int main() +{ + int n,i; + cin>>n; + + for(i=2;i<=n;i++) + { + + if(isPrime(i)) + cout< +using namespace std; + +// Function to print first N prime numbers +void print_first_N_primes(int N) +{ + // Declare the variables + int i, j, flag; + + // Print display message + cout << "\nPrime numbers between 1 and " + << N << " are:\n"; + + // Traverse each number from 1 to N + // with the help of for loop + for (i = 1; i <= N; i++) { + + // Skip 0 and 1 as they are + // niether prime nor composite + if (i == 1 || i == 0) + continue; + + // flag variable to tell + // if i is prime or not + flag = 1; + + for (j = 2; j <= i / 2; ++j) { + if (i % j == 0) { + flag = 0; + break; + } + } + + // flag = 1 means i is prime + // and flag = 0 means i is not prime + if (flag == 1) + cout << i << " "; + } +} + +// Driver code +int main() +{ + + int N = 100; + + print_first_N_primes(N); + + return 0; +} diff --git a/Program to print n number of Fibonnaci Series b/Program to print n number of Fibonnaci Series new file mode 100644 index 00000000..67b69f3c --- /dev/null +++ b/Program to print n number of Fibonnaci Series @@ -0,0 +1,25 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + int n,f=0,s=1,t,i; + cin>>n; + cout< +int main() +{ + int num1, num2, flag_var, i, j; + + /* Ask user to input the from/to range + * like 1 to 100, 10 to 1000 etc. + */ + printf("Enter two range(input integer numbers only):"); + //Store the range in variables using scanf + scanf("%d %d", &num1, &num2); + + //Display prime numbers for input range + printf("Prime numbers from %d and %d are:\n", num1, num2); + for(i=num1+1; i +using namespace std; + +// Function for print prime +// number in given range +void primeInRange(int L, int R) +{ + int flag; + + // Traverse each number in the + // interval with the help of for loop + for (int i = L; i <= R; i++) { + + // Skip 0 and 1 as they are + // niether prime nor composite + if (i == 1 || i == 0) + continue; + + // flag variable to tell + // if i is prime or not + flag = 1; + + // Iterate to check if i is prime + // or not + for (int j = 2; j <= i / 2; ++j) { + if (i % j == 0) { + flag = 0; + break; + } + } + + // flag = 1 means i is prime + // and flag = 0 means i is not prime + if (flag == 1) + cout << i << " "; + } +} + +// Driver Code +int main() +{ + int L,R; + // Given Range + cout<<"Enter the beginning number of the list"<>L; + cout<<"Enter the last number of the list"<>R; + + // Function Call + primeInRange(L, R); + + return 0; +} diff --git a/Program to print prime numbers in given range using c b/Program to print prime numbers in given range using c new file mode 100644 index 00000000..c64ec6b2 --- /dev/null +++ b/Program to print prime numbers in given range using c @@ -0,0 +1,77 @@ + #include + + #include + + + + void main() + + { + + int num1, num2, i, j, flag, temp, count = 0; + + + + printf("Enter the value of num1 and num2 \n"); + + scanf("%d %d", &num1, &num2); + + if (num2 < 2) + + { + + printf("There are no primes upto %d\n", num2); + + exit(0); + + } + + printf("Prime numbers are \n"); + + temp = num1; + + if ( num1 % 2 == 0) + + { + + num1++; + + } + + for (i = num1; i <= num2; i = i + 2) + + { + + flag = 0; + + for (j = 2; j <= i / 2; j++) + + { + + if ((i % j) == 0) + + { + + flag = 1; + + break; + + } + + } + + if (flag == 0) + + { + + printf("%d\n", i); + + count++; + + } + + } + + printf("Number of primes between %d & %d = %d\n", temp, num2, count); + + } diff --git a/Program to print prime numbers in given range using python b/Program to print prime numbers in given range using python new file mode 100644 index 00000000..7f9e0749 --- /dev/null +++ b/Program to print prime numbers in given range using python @@ -0,0 +1,13 @@ +lower = 900 +upper = 1000 + +print("Prime numbers between", lower, "and", upper, "are:") + +for num in range(lower, upper + 1): + # all prime numbers are greater than 1 + if num > 1: + for i in range(2, num): + if (num % i) == 0: + break + else: + print(num) diff --git a/Program to print the Fibonacci series using python. b/Program to print the Fibonacci series using python. new file mode 100644 index 00000000..9f6b1b3d --- /dev/null +++ b/Program to print the Fibonacci series using python. @@ -0,0 +1,23 @@ +# Program to display the Fibonacci sequence up to n-th term + +nterms = int(input("How many terms? ")) + +# first two terms +n1, n2 = 0, 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence:") + while count < nterms: + print(n1) + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/Program to print the fibonacci series -C++ b/Program to print the fibonacci series -C++ new file mode 100644 index 00000000..9c82a47a --- /dev/null +++ b/Program to print the fibonacci series -C++ @@ -0,0 +1,33 @@ +#include +using namespace std; + +int main() +{ + int n, t1 = 0, t2 = 1, nextTerm = 0; + + cout << "Enter the number of terms: "; + cin >> n; + + cout << "Fibonacci Series: "; + + for (int i = 1; i <= n; ++i) + { + // Prints the first two terms. + if(i == 1) + { + cout << " " << t1; + continue; + } + if(i == 2) + { + cout << t2 << " "; + continue; + } + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + + cout << nextTerm << " "; + } + return 0; +} diff --git a/Program to print the position of the smallest of n numbers using array. b/Program to print the position of the smallest of n numbers using array. new file mode 100644 index 00000000..81cfd076 --- /dev/null +++ b/Program to print the position of the smallest of n numbers using array. @@ -0,0 +1,22 @@ + +#include +#include +int main() +{ +int i, n , arr[20], small=1234, pos = -1; +clrscr(); + +printf("\n Enter the number of elements in the array; "); +scanf("%d",&n); +for(i=0;i +int main() +{ +int n; +scanf(“%d”,&n); +int arr[n]; +int i; +for(i = 0; i < n; i++) +{ +scanf(“%d”,&arr[i]); +} +printf(“Reversed array is:\n”); +for(i = n-1; i >= 0; i–) +{ +printf(“%d\n”,arr[i]); +} +return 0; +} diff --git a/Program to reverse the array element b/Program to reverse the array element new file mode 100644 index 00000000..716c3363 --- /dev/null +++ b/Program to reverse the array element @@ -0,0 +1,30 @@ +#include +using namespace std; + +int main() +{ + int Arr[100],n,temp,i,j; + + cout<<"Enter number of elements you want to insert "; + cin>>n; + + for(i=0;i>Arr[i]; + } + + for(i=0,j=n-1;i +int main() +{ + int a[5],i,j,t; + printf("enter 6 values"); + for(i=0;i<=4;i++) + { + scanf("%d",&a[i]); + } + for(i=0,j=4;i"); + for(i=0;i<=4;i++) + { + printf("%d\n",a[i]); + } + return 0; +} diff --git a/Program to sort given array (ascending & descending) b/Program to sort given array (ascending & descending) new file mode 100644 index 00000000..a491f86e --- /dev/null +++ b/Program to sort given array (ascending & descending) @@ -0,0 +1,129 @@ + +#include +using namespace std; +void merge(int *a,int *x,int *y,int s,int e) +{ + int i=s; + int k=s; + int mid=(s+e)/2; + int j=mid+1; + while(i<=mid && j<=e) + { + if(x[i]>=y[j]) + { + a[k]=y[j]; + j++; + k++; + } + else + { + a[k]=x[i]; + k++; + i++; + } + } + while(i<=mid) + { + a[k]=x[i]; + i++; + k++; + } + while(j<=e) + { + a[k]=y[j]; + j++; + k++; + } + +} +void sort1(int *a,int s,int e) +{ + if(s==e) + { + return; + } + int x[100],y[100]; + int mid=(s+e)/2; + for(int i=s;i<=mid;i++) + { + x[i]=a[i]; + } + for(int i=mid+1;i<=e;i++) + { + y[i]=a[i]; + } + sort1(x,s,mid); + sort1(y,mid+1,e); + merge(a,x,y,s,e); + +} +int main() +{ + int a[100],n; + cout<<"Enter length of array"<>n; + cout<<"Enter elements"<>a[i]; + } + sort1(a,0,n-1); + for(int i=0;i +#include + + +int main() +{ + int a[100],n,i,j; + printf("Array size: "); + scanf("%d",&n); + printf("Elements: "); + + for(i=0;i a[i]) + { + int tmp = a[i]; + a[i] = a[j]; + a[j] = tmp; + } + } + } + printf("\n\nAscending : "); + for (int i = 0; i < n; i++) + { + printf(" %d ", a[i]); + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (a[j] < a[i]) + { + int tmp = a[i]; + a[i] = a[j]; + a[j] = tmp; + } + } + } + printf("\n\nDescending : "); + for (int i = 0; i < n; i++) + { + printf(" %d ", a[i]); + } + + return 0; +getch(); +} diff --git a/Program to subtract 2D or 3D Matrices b/Program to subtract 2D or 3D Matrices new file mode 100644 index 00000000..6d2570ca --- /dev/null +++ b/Program to subtract 2D or 3D Matrices @@ -0,0 +1,37 @@ +int main() +{ + int rowCount, columnCount, i, j; + int firstMatrix[10][10], secondMatrix[10][10], resultMatrix[10][10]; + + printf("Number of rows of matrices to be subtracted : "); + scanf("%d", &rowCount); + + printf("Number of columns matrices to be subtracted : "); + scanf("%d", &columnCount); + + printf("Elements of first matrix : \n"); + + for (i = 0; i < rowCount; i++) + for (j = 0; j < columnCount; j++) + scanf("%d", &firstMatrix[i][j]); + + printf("Elements of second matrix : \n"); + + for (i = 0; i < rowCount; i++) + for (j = 0; j < columnCount; j++) + scanf("%d", &secondMatrix[i][j]); + + printf("Difference of entered matrices : \n"); + + for (i = 0; i < rowCount; i++) + { + for (j = 0; j < columnCount; j++) + { + resultMatrix[i][j] = firstMatrix[i][j] - secondMatrix[i][j]; + printf("%d\t",resultMatrix[i][j]); + } + printf("\n"); + } + + return 0; +} diff --git a/ProgramToCountVowelsInAStringByIterative.java b/ProgramToCountVowelsInAStringByIterative.java new file mode 100644 index 00000000..f69686a3 --- /dev/null +++ b/ProgramToCountVowelsInAStringByIterative.java @@ -0,0 +1,31 @@ +// Java program to count vowels in a string +public class anXcode { + + // Function to check the Vowel + static boolean isVowel(char ch) + { + ch = Character.toUpperCase(ch); + return (ch=='A' || ch=='E' || ch=='I' || + ch=='O' || ch=='U'); + } + + // Returns count of vowels in str + static int countVowels(String str) + { + int count = 0; + for (int i = 0; i < str.length(); i++) + if (isVowel(str.charAt(i))) // Check for vowel + ++count; + return count; + } + + // Driver code + public static void main(String args[]) + { + //string object + String str = "abc de"; + + // Total numbers of Vowel + System.out.println(countVowels(str)); + } +} diff --git a/ProgramToCountVowelsInAStringByItretive.cpp b/ProgramToCountVowelsInAStringByItretive.cpp new file mode 100644 index 00000000..aa755821 --- /dev/null +++ b/ProgramToCountVowelsInAStringByItretive.cpp @@ -0,0 +1,32 @@ +// C++ program to count vowels in a string +#include +using namespace std; + +// Function to check the Vowel +bool isVowel(char ch) +{ + ch = toupper(ch); + return (ch=='A' || ch=='E' || ch=='I' || + ch=='O' || ch=='U'); +} + +// Returns count of vowels in str +int countVowels(string str) +{ + int count = 0; + for (int i=0; i +using namespace std; + +// Function to check the Vowel +bool isVowel(char ch) +{ + ch = toupper(ch); + return (ch=='A' || ch=='E' || ch=='I' || + ch=='O' || ch=='U'); +} + +// to count total number of vowel from 0 to n +int countVovels(string str, int n) +{ + if (n == 1) + return isVowel(str[n-1]); + + return countVovels(str, n-1) + isVowel(str[n-1]); +} + +// Main Calling Function +int main() +{ + // string object + string str = "abc de"; + + // Total numbers of Vowel + cout << countVovels(str, str.length()) << endl; + return 0; +} diff --git a/ProgramToMultiply2DMatrices b/ProgramToMultiply2DMatrices new file mode 100644 index 00000000..40781374 --- /dev/null +++ b/ProgramToMultiply2DMatrices @@ -0,0 +1,25 @@ +# Program to multiply two matrices using nested loops + +# 3x3 matrix +X = [[12,7,3], + [4 ,5,6], + [7 ,8,9]] +# 3x4 matrix +Y = [[5,8,1,2], + [6,7,3,0], + [4,5,9,1]] +# result is 3x4 +result = [[0,0,0,0], + [0,0,0,0], + [0,0,0,0]] + +# iterate through rows of X +for i in range(len(X)): + # iterate through columns of Y + for j in range(len(Y[0])): + # iterate through rows of Y + for k in range(len(Y)): + result[i][j] += X[i][k] * Y[k][j] + +for r in result: + print(r) diff --git a/ProgramToPrintFibonnacciSeries b/ProgramToPrintFibonnacciSeries new file mode 100644 index 00000000..9f6b1b3d --- /dev/null +++ b/ProgramToPrintFibonnacciSeries @@ -0,0 +1,23 @@ +# Program to display the Fibonacci sequence up to n-th term + +nterms = int(input("How many terms? ")) + +# first two terms +n1, n2 = 0, 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence:") + while count < nterms: + print(n1) + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/ProgramToReverseArray b/ProgramToReverseArray new file mode 100644 index 00000000..d1f71189 --- /dev/null +++ b/ProgramToReverseArray @@ -0,0 +1,3 @@ +l = list(map(int,input().split())) +l.reverse() +print(l) diff --git a/Python 3 Program to add 2D Matrices b/Python 3 Program to add 2D Matrices new file mode 100644 index 00000000..6f88fb87 --- /dev/null +++ b/Python 3 Program to add 2D Matrices @@ -0,0 +1,51 @@ +#getting dimension of matrix + +print "enter n for nxn matrix" +n = input() + +matrix1 = [] +matrix2 = [] + +#taking elements of first matrix + +print "Enter elements of first matrix" +for i in range(0,n): + + #taking elements of first column + + print "Enter elements of ",i,"column, seperated by space" + + #raw_input().split() will split the string + #'1 2 34'.split() will give ['1', '2', '34'] + #map will convert its elements into integers [1, 2, 34] + + matrix1.append(map(int,raw_input().split())) + +print "Matrix 1 is",matrix1 + +#taking elements of second matrix + +print "Enter elements of second matrix" +for i in range(0,n): + #Similar to input taken for 1 matrix + + print "Enter elements of ",i,"column, seperated by space" + matrix2.append(map(int,raw_input().split())) + +print "Matrix 2 is",matrix2 + +#adding + +add_matrix = [] +for i in range(0,n): + a = [] + for j in range(0,n): + + + #making a addition matrix's column to append + #making a 1D matrix with elements as sum of elements of + #respective columns of both matrices + + a.append(matrix1[i][j]+matrix2[i][j]) + add_matrix.append(a) +print "Addition of matrix is",add_matrix diff --git a/Python 3 Program to print prime numbers in given range b/Python 3 Program to print prime numbers in given range new file mode 100644 index 00000000..781fb42c --- /dev/null +++ b/Python 3 Program to print prime numbers in given range @@ -0,0 +1,7 @@ +low = int(input("Enter lower range: ")) +high = int(input("Enter upper range: ")) +for num in range(low, high + 1): + if(num > 1): + for i in range(2,num): + if (num % i) == 0: #checking whether the value is not primebreakelse: + print(num, end = " ") diff --git a/Python Matrix addition b/Python Matrix addition new file mode 100644 index 00000000..c9206ec8 --- /dev/null +++ b/Python Matrix addition @@ -0,0 +1,14 @@ +# Program to add two matrices +# using zip() + +X = [[1,2,3], + [4 ,5,6], + [7 ,8,9]] + +Y = [[9,8,7], + [6,5,4], + [3,2,1]] + +result = [map(sum, zip(*t)) for t in zip(X, Y)] + +print(result) diff --git a/Python Program to Find Factorial of Number b/Python Program to Find Factorial of Number new file mode 100644 index 00000000..03a704de --- /dev/null +++ b/Python Program to Find Factorial of Number @@ -0,0 +1,23 @@ +# This Python Program finds the factorial of a number + +def factorial(num): + """This is a recursive function that calls + itself to find the factorial of given number""" + if num == 1: + return num + else: + return num * factorial(num - 1) + + +# We will find the factorial of this number +num = int(input("Enter a Number: ")) + +# if input number is negative then return an error message +# elif the input number is 0 then display 1 as output +# else calculate the factorial by calling the user defined function +if num < 0: + print("Factorial cannot be found for negative numbers") +elif num == 0: + print("Factorial of 0 is 1") +else: + print("Factorial of", num, "is: ", factorial(num)) diff --git a/Python Program to Find Factorial of Number Using Recursion b/Python Program to Find Factorial of Number Using Recursion new file mode 100644 index 00000000..bd73658d --- /dev/null +++ b/Python Program to Find Factorial of Number Using Recursion @@ -0,0 +1,17 @@ +# Factorial of a number using recursion + +def recur_factorial(n): + if n == 1: + return n + else: + return n*recur_factorial(n-1) + +num = 7 + +# check if the number is negative +if num < 0: + print("Sorry, factorial does not exist for negative numbers") +elif num == 0: + print("The factorial of 0 is 1") +else: + print("The factorial of", num, "is", recur_factorial(num)) diff --git a/Python Program to check Triangles b/Python Program to check Triangles new file mode 100644 index 00000000..03eae8fd --- /dev/null +++ b/Python Program to check Triangles @@ -0,0 +1,24 @@ +#code +#enter the values +print("Input lengths of the triangle sides: ") +x = int(input("x: ")) +y = int(input("y: ")) +z = int(input("z: ")) + +#providing the conditions +if x == y == z: + print("Equilateral triangle") +elif x==y or y==z or z==x: + print("Isosceles triangle") +else: + print("Scalene triangle") + +#output +x: 6 +y: 8 +z: 12 +Scalene triangle +x: 7 +y: 7 +z: 7 +Equilateral triangle diff --git a/Python Program to convert binary to decimal number b/Python Program to convert binary to decimal number new file mode 100644 index 00000000..9d1c476b --- /dev/null +++ b/Python Program to convert binary to decimal number @@ -0,0 +1,21 @@ + +# Function calculates the decimal equivalent +# to given binary number + +def binaryToDecimal(binary): + + binary1 = binary + decimal, i, n = 0, 0, 0 + while(binary != 0): + dec = binary % 10 + decimal = decimal + dec * pow(2, i) + binary = binary//10 + i += 1 + print(decimal) + + +# Driver code +if __name__ == '__main__': + binaryToDecimal(100) + binaryToDecimal(101) + binaryToDecimal(1001) diff --git a/Python Program to convert binary to octal number b/Python Program to convert binary to octal number new file mode 100644 index 00000000..4f3d31b1 --- /dev/null +++ b/Python Program to convert binary to octal number @@ -0,0 +1,3 @@ +n=input("Input the binary number that you want to convert: ") +a=int(n,2) +print("The octal rep. of the given binary number is: ", oct(a)) diff --git a/Python Program to count sum of all numbers in array b/Python Program to count sum of all numbers in array new file mode 100644 index 00000000..3dc66ab2 --- /dev/null +++ b/Python Program to count sum of all numbers in array @@ -0,0 +1,7 @@ +#Initialize array. +arr = [1, 2, 3, 4, 5]; +sum = 0; +#Loop through the array to calculate sum of elements. +for i in range(0, len(arr)): +sum = sum + arr[i]; +print("Sum of all the elements of an array: " + str(sum)); diff --git a/Python Program to find largest element in an array b/Python Program to find largest element in an array new file mode 100644 index 00000000..93776b51 --- /dev/null +++ b/Python Program to find largest element in an array @@ -0,0 +1,23 @@ +# Python3 program to find maximum +# in arr[] of size n + +# python function to find maximum +# in arr[] of size n +def largest(arr,n): + + # Initialize maximum element + max = arr[0] + + # Traverse array elements from second + # and compare every element with + # current max + for i in range(1, n): + if arr[i] > max: + max = arr[i] + return max + +# Driver Code +arr = [10, 324, 45, 90, 9808] +n = len(arr) +Ans = largest(arr,n) +print ("Largest in given array is",Ans) diff --git a/Python Program to find smallest element in array b/Python Program to find smallest element in array new file mode 100644 index 00000000..df9050e8 --- /dev/null +++ b/Python Program to find smallest element in array @@ -0,0 +1,11 @@ +# Python program to find smallest +# number in a list + +# list of numbers +list1 = [10, 20, 4, 45, 99] + +# sorting the list +list1.sort() + +# printing the first element +print("Smallest element is:", *list1[:1]) diff --git a/Python Program to find sum of array b/Python Program to find sum of array new file mode 100644 index 00000000..5280ddea --- /dev/null +++ b/Python Program to find sum of array @@ -0,0 +1,20 @@ +# Python 3 code to find sum +# of elements in given array +def _sum(arr,n): + + # return sum using sum + # inbuilt sum() function + return(sum(arr)) + +# driver function +arr=[] +# input values to list +arr = [12, 3, 4, 15] + +# calculating length of array +n = len(arr) + +ans = _sum(arr,n) + +# display sum +print ('Sum of the array is ', ans) diff --git a/Python Program to print Fibonacci Series b/Python Program to print Fibonacci Series new file mode 100644 index 00000000..2e75fa3b --- /dev/null +++ b/Python Program to print Fibonacci Series @@ -0,0 +1,9 @@ +n=int(input('Input the number: ')) +def fib(a=0,b=1): + if b>=n+1: + return a + else: + print( a ,end=' ') + a,b=b,a+b + return fib(a,b) +print(fib()) diff --git a/Python _Code Print number of leap years from given list of years b/Python _Code Print number of leap years from given list of years new file mode 100644 index 00000000..cb6331dc --- /dev/null +++ b/Python _Code Print number of leap years from given list of years @@ -0,0 +1,22 @@ +# Python code to finding number of +# leap years in list of years. + +# Input list initialization +Input = [2001, 2002, 2003, 2004, 2005, 2006, + 2007, 2008, 2009, 2010, 2011, 2012] + +# Find whether it is leap year or not +def checkYear(year): + return (((year % 4 == 0) and + (year % 100 != 0)) or + (year % 400 == 0)) + +# Answer Initialization +Answer = 0 + +for elem in Input: + if checkYear(elem): + Answer = Answer + 1 + +# Printing +print("No of leap years are:", Answer) diff --git a/Python arithmetic operations Example b/Python arithmetic operations Example new file mode 100644 index 00000000..99c09e6b --- /dev/null +++ b/Python arithmetic operations Example @@ -0,0 +1,16 @@ +>>> a = 12 +>>> b = 3 +>>> addition = a + b +>>> subtraction = a - b +>>> multiplication = a * b +>>> division = a / b +>>> modulus = a % b +>>> exponent = a ** b +>>> Floor_Division = a // b +>>> print("Addition of two numbers 12 and 3 is : ", addition) +>>> print("Subtracting Number 3 from 12 is : ", subtraction) +>>> print("Multiplication of two numbers 12 and 3 is : ", multiplication) +>>> print("Division of two numbers 12 and 3 is : ", division) +>>> print("Modulus of two numbers 12 and 3 is : ", modulus) +>>> print("Exponent of two numbers 12 and 3 is : ", exponent) +>>> print("Floor Division of two numbers 12 and 3 is : ", Floor_Division) diff --git a/Python program for Fibonnaci Series b/Python program for Fibonnaci Series new file mode 100644 index 00000000..6a2d20d0 --- /dev/null +++ b/Python program for Fibonnaci Series @@ -0,0 +1,22 @@ +# Python3 script to print Fibonacci series + + +def fib(num): + if num == 1: + return 0 + elif num == 2: + return 1 + else: + return fib(num - 1) + fib(num - 2) + + +while True: + try: + n = int(input("Enter your number:\t")) + if n > 0: + print("The fibonacci series number is: ", fib(n)) + break + else: + raise Exception("Invalid Input. Try Again") + except Exception as e: + pass diff --git a/Python program for factorial b/Python program for factorial new file mode 100644 index 00000000..3ffad650 --- /dev/null +++ b/Python program for factorial @@ -0,0 +1,19 @@ +# Python program to find the factorial of a number provided by the user. + + +num = 7 + +# To take input from the user +num = int(input("Enter a number: ")) + +factorial = 1 + +# check if the number is negative, positive or zero +if num < 0: + print("Sorry, factorial does not exist for negative numbers") +elif num == 0: + print("The factorial of 0 is 1") +else: + for i in range(1,num + 1): + factorial = factorial*i + print("The factorial of",num,"is",factorial) diff --git a/Python program for factorial of number b/Python program for factorial of number new file mode 100644 index 00000000..1bab9651 --- /dev/null +++ b/Python program for factorial of number @@ -0,0 +1,17 @@ +# Python3 script to print Factorial of a number + + +def fact(num): + if num == 1 or num == 0: + return 1 + elif num == 2: + return 2 + else: + return num * fact(num - 1) + + +n = int(input("Enter your number:\t")) +if n >= 0: + print("The factorial of {} is: {}".format(n, fact(n))) +else: + raise Exception("Invalid Input. Try Again") diff --git a/Python program to add matrices b/Python program to add matrices new file mode 100644 index 00000000..f3a01780 --- /dev/null +++ b/Python program to add matrices @@ -0,0 +1,13 @@ +# Program to add two matrices + +X = [[1,2,3], + [4 ,5,6], + [7 ,8,9]] + +Y = [[9,8,7], + [6,5,4], + [3,2,1]] + +result = [map(sum, zip(*t)) for t in zip(X, Y)] + +print(result) diff --git a/Python program to convert binary to decimal conversion b/Python program to convert binary to decimal conversion new file mode 100644 index 00000000..b1da451e --- /dev/null +++ b/Python program to convert binary to decimal conversion @@ -0,0 +1,28 @@ +#code +# function to convert decimal to binary +def decToBinary(n): + + # array to store binary number + binaryNum = [0] * n; + + # counter for binary array + i = 0; + while (n > 0): + + # storing remainder + # in binary array + binaryNum[i] = n % 2; + n = int(n / 2); + i += 1; + + # printing binary array + # in reverse order + for j in range(i - 1, -1, -1): + print(binaryNum[j], end = ""); + +# driver Code +n = 23; +decToBinary(n); + +#output +10111 diff --git a/Python program to find Area of triangle b/Python program to find Area of triangle new file mode 100644 index 00000000..800dfba3 --- /dev/null +++ b/Python program to find Area of triangle @@ -0,0 +1,12 @@ +# Python Program to find the area of triangle + +a = float(input('Enter first side: ')) +b = float(input('Enter second side: ')) +c = float(input('Enter third side: ')) + +# calculate the semi-perimeter +s = (a + b + c) / 2 + +# calculate the area +area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 +print('The area of the triangle is %0.2f' %area) diff --git a/Python program to find Compound Interest b/Python program to find Compound Interest new file mode 100644 index 00000000..db53bcc6 --- /dev/null +++ b/Python program to find Compound Interest @@ -0,0 +1,13 @@ +def findCompoundInterest(P, R, t, n): + return P * pow((1 + R/(100 * n)), n*t); + + +P = float(input("Enter principal amount : ")) +R = float(input("Enter annual rate of interest : ")) +t = float(input("Enter time in years : ")) +n = float(input("Enter number of compounding periods per year : ")) + +A = findCompoundInterest(P,R,t,n) + +print("Total amount : {}".format(A)) +print("Compound interest : {}".format(A-P)) diff --git a/Python program to find Simple Instrest b/Python program to find Simple Instrest new file mode 100644 index 00000000..f31a1ad2 --- /dev/null +++ b/Python program to find Simple Instrest @@ -0,0 +1,5 @@ +principle=float(input("Enter the principle amount:")) +time=int(input("Enter the time(years):")) +rate=float(input("Enter the rate:")) +simple_interest=(principle*time*rate)/100 +print("The simple interest is:",simple_interest) diff --git a/Python program to find largest element in array b/Python program to find largest element in array new file mode 100644 index 00000000..e33c5118 --- /dev/null +++ b/Python program to find largest element in array @@ -0,0 +1,11 @@ +def largestFun(arr,n): #function to find the largest element in an array with two arguements - array and size of array. + maxNum = arr[0] #maximum element initialized + for i in range(1, n): #Traverse through the array to find the largest element + if arr[i] > maxNum: + maxNum = arr[i] + return maxNum + +arr = [13430, 33424, 45000, 56390, 938408] #Array intialized +n = len(arr) #Finding the length of array +largestElement = largestFun(arr,n) #Calling the function to find the largest element in the array. +print ("Largest element in the given array is",largestElement) #Printing output. diff --git a/Python program to find largest element in the array b/Python program to find largest element in the array new file mode 100644 index 00000000..68957817 --- /dev/null +++ b/Python program to find largest element in the array @@ -0,0 +1,24 @@ + +# Python program to find largest element in the array +# in arr[] of size n + +# python function to find maximum +# in arr[] of size n +def largest(arr,n): + + # Initialize maximum element + max = arr[0] + + # Traverse array elements from second + # and compare every element with + # current max + for i in range(1, n): + if arr[i] > max: + max = arr[i] + return max + +# Driver Code +arr = [10, 324, 45, 90, 9808] +n = len(arr) +Ans = largest(arr,n) +print ("Largest in given array is",Ans) diff --git a/Python program to multiply two matrices b/Python program to multiply two matrices new file mode 100644 index 00000000..613293ce --- /dev/null +++ b/Python program to multiply two matrices @@ -0,0 +1,22 @@ +#code +#giving input +X = [[12,7,3], +[4 ,5,6], +[7 ,8,9]] +Y = [[5,8,1,2], +[6,7,3,0], +[4,5,9,1]] +#providing empty matrice to store result +result = [[0,0,0,0], +[0,0,0,0], +[0,0,0,0]] +for i in range(len(X)): + for j in range(len(Y[0])): + for k in range(len(Y)): + result[i][j] += X[i][k] * Y[k][j] +print(np.array(result)) + +#Output +[[114 160 60 27] +[ 74 97 73 14] +[119 157 112 23]] diff --git a/Python program to print ASCII value of given character b/Python program to print ASCII value of given character new file mode 100644 index 00000000..46f3c4d2 --- /dev/null +++ b/Python program to print ASCII value of given character @@ -0,0 +1,2 @@ +character=input() +print("The ASCII value of the given character is: ",ord(character)) diff --git a/Python program to reverse an array b/Python program to reverse an array new file mode 100644 index 00000000..09caa7eb --- /dev/null +++ b/Python program to reverse an array @@ -0,0 +1,19 @@ +#code +# function to reverse A[] +def reverseList(A, start, end): + while start < end: + A[start], A[end] = A[end], A[start] + start += 1 + end -= 1 + +# driver function +A = [1, 2, 3, 4, 5, 6] +print(A) +reverseList(A, 0, 5) +print("Reversed list is") +print(A) + +#output +1 2 3 4 5 6 +Reversed array is +6 5 4 3 2 1 diff --git a/Python script to sort and reverse the Array b/Python script to sort and reverse the Array new file mode 100644 index 00000000..841af9cd --- /dev/null +++ b/Python script to sort and reverse the Array @@ -0,0 +1,15 @@ +# Python3 script to reverse the array + + +def rev(ls): + return ls[::-1] + + +num = int(input("Enter the length of your array:\t")) +arr = list(map(int, input().strip().split())) + +print("Your original array is: ", arr) +print("The reversed array is: ", rev(arr)) + +print("Sorted Array is: ", sorted(arr)) +print("Sorted Array in descending order is: ", rev(sorted(arr))) diff --git a/Python | Print number of leap years from given list of years b/Python | Print number of leap years from given list of years new file mode 100644 index 00000000..cb6331dc --- /dev/null +++ b/Python | Print number of leap years from given list of years @@ -0,0 +1,22 @@ +# Python code to finding number of +# leap years in list of years. + +# Input list initialization +Input = [2001, 2002, 2003, 2004, 2005, 2006, + 2007, 2008, 2009, 2010, 2011, 2012] + +# Find whether it is leap year or not +def checkYear(year): + return (((year % 4 == 0) and + (year % 100 != 0)) or + (year % 400 == 0)) + +# Answer Initialization +Answer = 0 + +for elem in Input: + if checkYear(elem): + Answer = Answer + 1 + +# Printing +print("No of leap years are:", Answer) diff --git a/Python: Largest element in an array b/Python: Largest element in an array new file mode 100644 index 00000000..a9bda037 --- /dev/null +++ b/Python: Largest element in an array @@ -0,0 +1,12 @@ +def largest(arr,n): + + max = arr[0] + for i in range(1, n): + if arr[i] > max: + max = arr[i] + return max + +arr = [10, 54, 45, 90, 1000] +n = len(arr) +Ans = largest(arr,n) +print ("Largest in given array is",Ans) diff --git a/Python_SocketProramming.py b/Python_SocketProramming.py new file mode 100644 index 00000000..fd4c1f8b --- /dev/null +++ b/Python_SocketProramming.py @@ -0,0 +1,56 @@ +import socket # Import socket module + +port = 60000 # Reserve a port for your service. +s = socket.socket() # Create a socket object +host = socket.gethostname() # Get local machine name +s.bind((host, port)) # Bind to the port +s.listen(5) # Now wait for client connection. + +print 'Server listening....' + +while True: + conn, addr = s.accept() # Establish connection with client. + print 'Got connection from', addr + data = conn.recv(1024) + print('Server received', repr(data)) + + filename='mytext.txt' + f = open(filename,'rb') + l = f.read(1024) + while (l): + conn.send(l) + print('Sent ',repr(l)) + l = f.read(1024) + f.close() + + print('Done sending') + conn.send('Thank you for connecting') + conn.close() + + +# client.py + +import socket # Import socket module + +s = socket.socket() # Create a socket object +host = socket.gethostname() # Get local machine name +port = 60000 # Reserve a port for your service. + +s.connect((host, port)) +s.send("Hello server!") + +with open('received_file', 'wb') as f: + print 'file opened' + while True: + print('receiving data...') + data = s.recv(1024) + print('data=%s', (data)) + if not data: + break + # write data to a file + f.write(data) + +f.close() +print('Successfully get the file') +s.close() +print('connection closed') diff --git a/QueueUsing2Stacks1 (1).cpp b/QueueUsing2Stacks1 (1).cpp new file mode 100644 index 00000000..495b3b3e --- /dev/null +++ b/QueueUsing2Stacks1 (1).cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +struct node +{ +int data; +struct node *next; +}; + + +struct myStack{ +struct node *top; +void push(int item) +{ +struct node *t= new node; +t->data=item; +t->next=top; +top=t; +//cout<<"pushed "<data; + top=top->next; + delete p; + } + return item; +} +bool isEmpty(){if (top==NULL) return true; else return false;} +}; +myStack s1,s2; + +void insertQ(int item) +{ +s1.push(item); +cout<<"inserted in Queue "< +int main() { + char line[150]; + + printf("Enter a string: "); + fgets(line, sizeof(line), stdin); // take input + + + for (int i = 0, j; line[i] != '\0'; ++i) { + + // enter the loop if the character is not an alphabet + // and not the null character + while (!(line[i] >= 'a' && line[i] <= 'z') && !(line[i] >= 'A' && line[i] <= 'Z') && !(line[i] == '\0')) { + for (j = i; line[j] != '\0'; ++j) { + + // if jth element of line is not an alphabet, + // assign the value of (j+1)th element to the jth element + line[j] = line[j + 1]; + } + line[j] = '\0'; + } + } + printf("Output String: "); + puts(line); + return 0; +} diff --git a/Remove linked list elements of a particular value b/Remove linked list elements of a particular value new file mode 100644 index 00000000..1c01ad93 --- /dev/null +++ b/Remove linked list elements of a particular value @@ -0,0 +1,48 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* removeElements(ListNode* head, int val) { + ListNode * temp = head, *prev = NULL; + if(head == NULL) + { + return head; + } + if(head->next == NULL && temp->val == val) + { + head = temp->next; + return NULL; + + } + //For handling cases when the particular value elements come in first positions, as then head needs to be changed to remove those elements. For example [1,1,1,1], val = 1 + while(head && head->val == val) + { + head = head->next; + } + temp = head; + //If an element is equal to the value then set the address in the previous node as the address of next node. + while(temp != NULL) + { + if(temp->val == val) + { + prev->next = temp->next; + temp = temp->next; + } + else + { + prev = temp; + temp = temp->next; + } + } + return head; + //Modified linked list would be returned. + } +}; diff --git a/Replace all 0(s) with 5(s) in user accepted number b/Replace all 0(s) with 5(s) in user accepted number new file mode 100644 index 00000000..f88bba57 --- /dev/null +++ b/Replace all 0(s) with 5(s) in user accepted number @@ -0,0 +1,8 @@ +number = input('Enter a number: ') +lis = [int(digit) for digit in number] +for i in lis: + if i == 0: + i = 5 + print(i, end="") + else: + print(i, end="") diff --git a/Reverse a Number using a while loop in Java b/Reverse a Number using a while loop in Java new file mode 100644 index 00000000..840b430c --- /dev/null +++ b/Reverse a Number using a while loop in Java @@ -0,0 +1,21 @@ +public class ReverseNumber { + + public static void main(String[] args) { + + int num = 1234, reversed = 0; + + while(num != 0) { + + int digit = num % 10; + + reversed = reversed * 10 + digit; + + num /= 10; + + } + + System.out.println("Reversed Number: " + reversed); + + } + +} diff --git a/Reverse an Array using While Loop b/Reverse an Array using While Loop new file mode 100644 index 00000000..10a0fe0c --- /dev/null +++ b/Reverse an Array using While Loop @@ -0,0 +1,36 @@ +/* C Program to Reverse an Array using While loop */ +#include + +int main() +{ + int a[100], i, j, Size, Temp; + + printf("\nPlease Enter the size of an array: "); + scanf("%d",&Size); + + //Inserting elements into the array + for (i = 0; i < Size; i++) + { + scanf("%d", &a[i]); + } + + j = i - 1; // Assigning j to Last array element + i = 0; // Assigning i to first array element + + while (i < j) + { + Temp = a[i]; + a[i] = a[j]; + a[j] = Temp; + i++; + j--; + } + + printf("\nResult of an Reverse array is: "); + for (i = 0; i < Size; i++) + { + printf("%d \t", a[i]); + } + + return 0; +} diff --git a/Reverse an Array using python b/Reverse an Array using python new file mode 100644 index 00000000..b72fde36 --- /dev/null +++ b/Reverse an Array using python @@ -0,0 +1,14 @@ +def arrayRev(array): + length = len(array) + revArray = [] + i=length-1 + while i >= 0: + revArray.append(array[i]) + i-=1 + return revArray + +if __name__=="__main__": + array = map(int,input().split()) + array = list(array) + result = arrayRev(array) + print(result) diff --git a/Reverse an array b/Reverse an array new file mode 100644 index 00000000..f0d5339a --- /dev/null +++ b/Reverse an array @@ -0,0 +1,44 @@ +#include +using namespace std; + +void rvereseArray(int arr[], int start, int end) +{ + while (start < end) + { + int temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + start++; + end--; + } +} + +/* Utility function to print an array */ +void printArray(int arr[], int size) +{ + for (int i = 0; i < size; i++) + cout << arr[i] << " "; + + cout << endl; +} + +/* Driver function to test above functions */ +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6}; + + int n = sizeof(arr) / sizeof(arr[0]); + + // To print original array + printArray(arr, n); + + // Function calling + rvereseArray(arr, 0, n-1); + + cout << "Reversed array is" << endl; + + // To print the Reversed array + printArray(arr, n); + + return 0; +} diff --git a/Reverse an array in Java b/Reverse an array in Java new file mode 100644 index 00000000..5c430612 --- /dev/null +++ b/Reverse an array in Java @@ -0,0 +1,26 @@ + + +public class reverseArray { + + + static void reverse(int a[], int n) + { + int[] b = new int[n]; + int j = n; + for (int i = 0; i < n; i++) { + b[j - 1] = a[i]; + j = j - 1; + } + + System.out.println("Reversed array is: \n"); + for (int k = 0; k < n; k++) { + System.out.println(b[k]); + } + } + + public static void main(String[] args) + { + int [] arr = {10, 20, 30, 40, 50}; + reverse(arr, arr.length); + } +} diff --git a/Reverse an array using pointers b/Reverse an array using pointers new file mode 100644 index 00000000..f9abab55 --- /dev/null +++ b/Reverse an array using pointers @@ -0,0 +1,57 @@ + +#include +using namespace std; + +// Function to swap two memory contents +void swap(int* a, int* b) +{ + int temp = *a; + *a = *b; + *b = temp; +} + +// Function to reverse the array through pointers +void reverse(int array[], int array_size) +{ + + // pointer1 pointing at the beginning of the array + int *pointer1 = array, + + // pointer2 pointing at end of the array + *pointer2 = array + array_size - 1; + while (pointer1 < pointer2) { + swap(pointer1, pointer2); + pointer1++; + pointer2--; + } +} + +// Function to print the array +void print(int* array, int array_size) +{ + + // Length pointing at end of the array + int *length = array + array_size, + + // Position pointing to the beginning of the array + *position = array; + cout << "Array = "; + for (position = array; position < length; position++) + cout << *position << " "; +} + +// Driver function +int main() +{ + + // Array to hold the values + int array[] = { 2, 4, -6, 5, 8, -1 }; + + cout << "Original "; + print(array, 6); + + cout << "Reverse "; + reverse(array, 6); + print(array, 6); + return 0; +} diff --git a/Reverse array in half time complexity b/Reverse array in half time complexity new file mode 100644 index 00000000..f8adbdfa --- /dev/null +++ b/Reverse array in half time complexity @@ -0,0 +1,31 @@ +#include +using namespace std; +void reverses(int arr[],int n) +{ + + for(int i=0;i<(n+1)/2;i++) + { + swap(arr[i],arr[n-i-1]); + } +} +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + int size; + cout<<"Enter the size of the array"<>size; + int arr[size]; + cout<<"Enter the elements of the array"<>arr[i]; + } + reverses(arr,size); + for(int i=0;i + +/* Function to reverse arr[] from start to end*/ +void rvereseArray(int arr[], int start, int end) +{ + int temp; + while (start < end) + { + temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + start++; + end--; + } +} + +/* Utility that prints out an array on a line */ +void printArray(int arr[], int size) +{ +int i; +for (i=0; i < size; i++) + printf("%d ", arr[i]); + +printf("\n"); +} + +/* Driver function to test above functions */ +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6}; + int n = sizeof(arr) / sizeof(arr[0]); + printArray(arr, n); + rvereseArray(arr, 0, n-1); + printf("Reversed array is \n"); + printArray(arr, n); + return 0; +} diff --git a/Reverse the given array in Python b/Reverse the given array in Python new file mode 100644 index 00000000..296871f5 --- /dev/null +++ b/Reverse the given array in Python @@ -0,0 +1,9 @@ +array=[1,3,5,4,8,6,2,4] #input array +i=0 +j=len(array)-1 + +while i<=j: # two-pointer approach + array[i],array[j]=array[j],array[i] + i+=1 + j-=1 +print("Array in reversed form is",array) #o/p is [4, 2, 6, 8, 4, 5, 3, 1] diff --git a/ReverseOfAnArray b/ReverseOfAnArray new file mode 100644 index 00000000..ae158afb --- /dev/null +++ b/ReverseOfAnArray @@ -0,0 +1,31 @@ +import java.util.*; +class ReverseOfAnArray { + public static void main(String []args){ + Scanner sc = new Scanner(System.in); + + System.out.print("Enter array size: "); + int n = sc.nextInt(); + + int[] arr = new int[n]; + for (int i=0; i + +using namespace std; + +int main() +{ + int numOfElements; + cin >> numOfElements; + int arr[numOfElements]; + for(int i = 0; i < numOfElements; i++) + cin >> arr[i]; + for(int i = 0; i < numOfElements/2; i++) + { + swap(arr[i], arr[numOfElements-i-1]); + } + for(int i = 0; i < numOfElements; i++) + { + cout << arr[i] << " "; + } + return 0; +} diff --git a/Reversing array using python b/Reversing array using python new file mode 100644 index 00000000..edc3b4ad --- /dev/null +++ b/Reversing array using python @@ -0,0 +1,5 @@ +# taking input of array +l = list(map(int,input().split())) +# reversing the array +l = l[::-1] +print(l) diff --git a/Reversing the array b/Reversing the array new file mode 100644 index 00000000..e363d7bb --- /dev/null +++ b/Reversing the array @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +int main() +{ + int n; + cout << "Enter the size of array: "; + cin >> n; + int arr[n]; + cout << "Enter array elements: "; + + for(int i=0; i> arr[i]; + + + cout << "Entered array is: "; + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + + reverse(arr, arr + n); + + cout << "\nReversed Array: "; + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + return 0; +} diff --git a/Sales by match hackerrank in c b/Sales by match hackerrank in c new file mode 100644 index 00000000..91528eff --- /dev/null +++ b/Sales by match hackerrank in c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + +// Complete the sockMerchant function below. +int sockMerchant(int n, int* ar) { + int i,j,result=0,count=0,c=0,div=0; + // int *b; + + + for(i=0;i +using namespace std; +const int N = 2e6; +map my[N]; +void update(int st, int lo, int hi, int pos, int val){ + my[st][val]++; + if(lo == hi){ + return; + } + int l = 2*st, r = l+1, mid = (lo+hi)/2; + if(pos>mid) update(r, mid+1, hi, pos, val); + else update(l, lo, mid, pos, val); +} +long long query(int st, int lo, int hi, int x, int y, int val){ + if(lo == x and hi == y){ + return my[st][val]; + } + int l = 2*st, r = l+1, mid = (lo+hi)/2; + if(x>mid) return query(r, mid+1, hi, x, y, val); + else if(y<=mid) return query(l, lo, mid, x, y, val); + return query(l ,lo , mid, x, mid, val) + query(r, mid+1, hi, mid+1, y, val); +} + +int main() { + int n, i, q, k, l, t, r; + cin >> n >> q; + while(q--){ + cin >> t >> l >> r; + if(t == 1){ + //add l to a[r] + update(1, 0, n-1, r-1, l); + } + else{ + //count of k from a[l] to a[r]. + cin >> k; + cout << query(1, 0, n-1, l-1, r-1, k) << endl; + } + } + return 0; +} diff --git a/Selection sort in c b/Selection sort in c new file mode 100644 index 00000000..5af936d3 --- /dev/null +++ b/Selection sort in c @@ -0,0 +1,37 @@ +#include +void main(){ +void sw(int* , int* ); + int i,j,n,min,a[100]; + printf("enter number of elements"); + scanf("%d",&n); + printf("enter the elements"); + for (i = 0; i < n ; i++) + { + scanf("%d",&a[i]); + } + for ( i = 0; i < n ; i++) + { + min=i; + for ( j = i+1; j < n; j++) + { + if (a[j] +using namespace std; + +void SieveOfEratosthenes(int n) +{ + + bool prime[n+1]; + memset(prime, true, sizeof(prime)); + + for (int p=2; p*p<=n; p++) + { + // If prime[p] is not changed, then it is a prime + if (prime[p] == true) + { + + for (int i=p*p; i<=n; i += p) + prime[i] = false; + } + } + + // Print all prime numbers + for (int p=2; p<=n; p++) + if (prime[p]) + cout << p << " "; +} + + +int main() +{ + int n = 30; + cout << "Following are the prime numbers smaller " + << " than or equal to " << n << endl; + SieveOfEratosthenes(n); + return 0; +} diff --git a/Simple Calculator in Bash Language Using Ubuntu Terminal b/Simple Calculator in Bash Language Using Ubuntu Terminal new file mode 100644 index 00000000..9928200a --- /dev/null +++ b/Simple Calculator in Bash Language Using Ubuntu Terminal @@ -0,0 +1,28 @@ +# !/bin/bash + +# Take user Input +echo "Enter Two numbers : " +read a +read b + +# Input type of operation +echo "Enter Choice :" +echo "1. Addition" +echo "2. Subtraction" +echo "3. Multiplication" +echo "4. Division" +read ch + +# Switch Case to perform +# calulator operations +case $ch in +1)res=`echo $a + $b | bc` +;; +2)res=`echo $a - $b | bc` +;; +3)res=`echo $a \* $b | bc` +;; +4)res=`echo "scale=2; $a / $b" | bc` +;; +esac +echo "Result : $res" diff --git a/Simple interest floor and ceiling value b/Simple interest floor and ceiling value new file mode 100644 index 00000000..5d93203d --- /dev/null +++ b/Simple interest floor and ceiling value @@ -0,0 +1,10 @@ +import math +p=3500 +r=3.15 +t=2.5 +interest=((p*t*r)/100) +print("The interest is",round(interest)) +ceil=math.ceil(interest) +print("The celing value is",ceil) +floor=math.floor(interest) +print("The floor value is",floor) diff --git a/SinglyLinkedList.cpp b/SinglyLinkedList.cpp new file mode 100644 index 00000000..65b54ef2 --- /dev/null +++ b/SinglyLinkedList.cpp @@ -0,0 +1,237 @@ +#include + +using namespace std; + +class Node { + public: + int key; + int data; + Node * next; + + Node() { + key = 0; + data = 0; + next = NULL; + } + Node(int k, int d) { + key = k; + data = d; + } +}; + +class SinglyLinkedList { + public: + Node * head; + + SinglyLinkedList() { + head = NULL; + } + SinglyLinkedList(Node * n) { + head = n; + } + + // 1. CHeck if node exists using key value + Node * nodeExists(int k) { + Node * temp = NULL; + + Node * ptr = head; + while (ptr != NULL) { + if (ptr -> key == k) { + temp = ptr; + } + ptr = ptr -> next; + + } + return temp; + } + + // 2. Append a node to the list + void appendNode(Node * n) { + if (nodeExists(n -> key) != NULL) { + cout << "Node Already exists with key value : " << n -> key << ". Append another node with different Key value" << endl; + } else { + if (head == NULL) { + head = n; + cout << "Node Appended" << endl; + } else { + Node * ptr = head; + while (ptr -> next != NULL) { + ptr = ptr -> next; + } + ptr -> next = n; + cout << "Node Appended" << endl; + } + } + + } + // 3. Prepend Node - Attach a node at the start + void prependNode(Node * n) { + if (nodeExists(n -> key) != NULL) { + cout << "Node Already exists with key value : " << n -> key << ". Append another node with different Key value" << endl; + } else { + n -> next = head; + head = n; + cout << "Node Prepended" << endl; + } + } + + // 4. Insert a Node after a particular node in the list + void insertNodeAfter(int k, Node * n) { + Node * ptr = nodeExists(k); + if (ptr == NULL) { + cout << "No node exists with key value: " << k << endl; + } else { + if (nodeExists(n -> key) != NULL) { + cout << "Node Already exists with key value : " << n -> key << ". Append another node with different Key value" << endl; + } else { + n -> next = ptr -> next; + ptr -> next = n; + cout << "Node Inserted" << endl; + } + } + } + + // 5. Delete node by unique key + void deleteNodeByKey(int k) { + if (head == NULL) { + cout << "Singly Linked List already Empty. Cant delete" << endl; + } else if (head != NULL) { + if (head -> key == k) { + head = head -> next; + cout << "Node UNLINKED with keys value : " << k << endl; + } else { + Node * temp = NULL; + Node * prevptr = head; + Node * currentptr = head -> next; + while (currentptr != NULL) { + if (currentptr -> key == k) { + temp = currentptr; + currentptr = NULL; + } else { + prevptr = prevptr -> next; + currentptr = currentptr -> next; + } + } + if (temp != NULL) { + prevptr -> next = temp -> next; + cout << "Node UNLINKED with keys value : " << k << endl; + } else { + cout << "Node Doesn't exist with key value : " << k << endl; + } + } + } + + } + // 6th update node + void updateNodeByKey(int k, int d) { + + Node * ptr = nodeExists(k); + if (ptr != NULL) { + ptr -> data = d; + cout << "Node Data Updated Successfully" << endl; + } else { + cout << "Node Doesn't exist with key value : " << k << endl; + } + + } + + // 7th printing + void printList() { + if (head == NULL) { + cout << "No Nodes in Singly Linked List"; + } else { + cout << endl << "Singly Linked List Values : "; + Node * temp = head; + + while (temp != NULL) { + cout << "(" << temp -> key << "," << temp -> data << ") --> "; + temp = temp -> next; + } + } + + } + +}; + +int main() { + + SinglyLinkedList s; + int option; + int key1, k1, data1; + do { + cout << "\nWhat operation do you want to perform? Select Option number. Enter 0 to exit." << endl; + cout << "1. appendNode()" << endl; + cout << "2. prependNode()" << endl; + cout << "3. insertNodeAfter()" << endl; + cout << "4. deleteNodeByKey()" << endl; + cout << "5. updateNodeByKey()" << endl; + cout << "6. print()" << endl; + cout << "7. Clear Screen" << endl << endl; + + cin >> option; + Node * n1 = new Node(); + //Node n1; + + switch (option) { + case 0: + break; + case 1: + cout << "Append Node Operation \nEnter key & data of the Node to be Appended" << endl; + cin >> key1; + cin >> data1; + n1 -> key = key1; + n1 -> data = data1; + s.appendNode(n1); + //cout<> key1; + cin >> data1; + n1 -> key = key1; + n1 -> data = data1; + s.prependNode(n1); + break; + + case 3: + cout << "Insert Node After Operation \nEnter key of existing Node after which you want to Insert this New node: " << endl; + cin >> k1; + cout << "Enter key & data of the New Node first: " << endl; + cin >> key1; + cin >> data1; + n1 -> key = key1; + n1 -> data = data1; + + s.insertNodeAfter(k1, n1); + break; + + case 4: + + cout << "Delete Node By Key Operation - \nEnter key of the Node to be deleted: " << endl; + cin >> k1; + s.deleteNodeByKey(k1); + + break; + case 5: + cout << "Update Node By Key Operation - \nEnter key & NEW data to be updated" << endl; + cin >> key1; + cin >> data1; + s.updateNodeByKey(key1, data1); + + break; + case 6: + s.printList(); + + break; + case 7: + system("clear"); + break; + default: + cout << "Enter Proper Option number " << endl; + } + + } while (option != 0); + + return 0; +} \ No newline at end of file diff --git a/Smallest element in an array. b/Smallest element in an array. new file mode 100644 index 00000000..520ab540 --- /dev/null +++ b/Smallest element in an array. @@ -0,0 +1,20 @@ + + +#Initialize array +arr = [25, 11, 7, 75, 56]; + +#Initialize min with the first element of the array. + +min = arr[0]; + +#Loop through the array +for i in range(0, len(arr)): + #Compare elements of array with min + if(arr[i] < min): + min = arr[i]; + +print("Smallest element present in given array: " + str(min)); + + + + diff --git a/Smallest element in array.py b/Smallest element in array.py new file mode 100644 index 00000000..de939d35 --- /dev/null +++ b/Smallest element in array.py @@ -0,0 +1,14 @@ +#Initialize array +arr = [25, 11, 7, 75, 56]; + +#Initialize min with the first element of the array. + +min = arr[0]; + +#Loop through the array +for i in range(0, len(arr)): + #Compare elements of array with min + if(arr[i] < min): + min = arr[i]; + +print("Smallest element present in given array: " + str(min)); diff --git a/Smallest_element_in_1Darray b/Smallest_element_in_1Darray new file mode 100644 index 00000000..9c5cc31a --- /dev/null +++ b/Smallest_element_in_1Darray @@ -0,0 +1,17 @@ +lst = [] +# number of elements as input +n = int(input("Enter number of elements : ")) +# iterating till the range +for i in range(0, n): + ele = int(input()) + + lst.append(ele) # adding the element +def smallest(lst,n): + # Initialize smallest element + min = lst[0] + for i in range(1, n): + if lst[i] < min: + min = lst[i] + return min +Ans = smallest(lst,n) +print ("Smallest in given array is",Ans) diff --git a/Sorting of array elements using quick sort in java b/Sorting of array elements using quick sort in java new file mode 100644 index 00000000..a85f628f --- /dev/null +++ b/Sorting of array elements using quick sort in java @@ -0,0 +1,50 @@ +class QuickSort +{ + int partition(int arr[], int low, int high) + { + int pivot = arr[high]; + int i = (low-1); + for (int j=low; j1 2 3 4----> 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 + <----5 6 7 8 <----- => + ----->9 10 11 12----> + <----13 14 15 16<----- + +*/ + +#include +using namespace std; + + +void spiral_print(int arr[20][20], int row, int col) +{ + int i; + int start_row = 0; + int start_col = 0; + int end_row = row - 1; + int end_col = col - 1; + + while(start_row <= end_row && start_col <= end_col) + { + + // First Row + for (int i = start_col; i <= end_col; i++) + cout << arr[start_row][i]<<" "; + start_row++; + + // + for (int i = start_row; i <= end_row; i++) + cout< start_row) + { + for(i = end_col; i >= start_col; i--) + cout< start_col) + { + for(i = end_row; i >= start_row; i--) + cout << arr[i][start_col]<<" "; + start_col++; + } + } +} +int main() +{ +int arr[20][20], row, col; + int i, j; + cout<<"Enter the size of 2D-ARRAY row-wise: \n"; + cin>>row; + cout<<"Enter the size of 2D-ARRAY column-wise: \n"; + cin>>col; + + cout<<"Enter the values for 2D-ARRAY: \n"; + for(i = 0; i < row; i++) + { + for(j = 0; j < col; j++) + { + cout<<"arr["<>arr[i][j]; + } + } + + spiral_print(arr, row, col); +} diff --git a/String Operation using Java b/String Operation using Java new file mode 100644 index 00000000..f045450e --- /dev/null +++ b/String Operation using Java @@ -0,0 +1,37 @@ +/*Given two strings, append them together (known as "concatenation") and return the result. However, if the strings are different lengths, omit chars from the longer string so it is the same length as the shorter string. So "Hello" and "Hi" yield "loHi". The strings may be any length. + + +minCat("Hello", "Hi") → "loHi" +minCat("Hello", "java") → "ellojava" +minCat("java", "Hello") → "javaello"*/ + +package zzz; +import java.util.Scanner; +import java.util.ArrayList; +public class practice { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Scanner sc = new Scanner(System.in); + String a =sc.nextLine(); + String b[] =a.split(" "); + int b1[] = new int[b.length]; + int j = 0; + for(int i=0;i +using namespace std; + + +int sum(int arr[], int n) +{ + int sum = 0; + + for (int i = 0; i < n; i++) + sum = sum + arr[i]; + + return sum; +} + + +int main() +{ + int Array1[] = {99, 99, 99, 99}; + int N = sizeof(Array1) / sizeof(Array1[0]); + cout << "Sum of given array is " << sum(Array1, N); + return 0; +} diff --git a/Sum of elements in a array.. b/Sum of elements in a array.. new file mode 100644 index 00000000..b8f40776 --- /dev/null +++ b/Sum of elements in a array.. @@ -0,0 +1,16 @@ +#include + +int main() { + int arry{10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}: + int sum, loop; + + sum = 0; + + for(loop = 9; loop >= 0; loop--) { + sum = sum + array[loop]; + } + + printf("sum of array is %d.", sum); + + return 0; +} diff --git a/Sum of elements in an array b/Sum of elements in an array new file mode 100644 index 00000000..5839023e --- /dev/null +++ b/Sum of elements in an array @@ -0,0 +1,23 @@ +class Test +{ + static int arr[] = {12,3,4,15}; + + // method for sum of elements in an array + static int sum() + { + int sum = 0; // initialize sum + int i; + + // Iterate through all elements and add them to sum + for (i = 0; i < arr.length; i++) + sum += arr[i]; + + return sum; + } + + // Driver method + public static void main(String[] args) + { + System.out.println("Sum of given array is " + sum()); + } + } diff --git a/Sum of elements in array b/Sum of elements in array new file mode 100644 index 00000000..a41c0e06 --- /dev/null +++ b/Sum of elements in array @@ -0,0 +1,27 @@ +#include + #include + + +int main() +{ + int a[1000],i,n,sum=0; + + printf("Enter size of the array : "); + scanf("%d",&n); + + printf("Enter elements in array : "); + for(i=0; i +int main() +{ +int array [10] = {1, 2, 3, 4, 5, 6, 7, 8,9,0}; +int sum, Loop; +sum= 0; +for( loop = 9; loop >= 0; loop--) +{ +sum = sum+ array[loop]; + +} +printr("Sum of array is %d.", sum); + +return 0; + +} diff --git a/SumOfAllNumbers_In_Array using c b/SumOfAllNumbers_In_Array using c new file mode 100644 index 00000000..f540922b --- /dev/null +++ b/SumOfAllNumbers_In_Array using c @@ -0,0 +1,29 @@ + #include + #include + + int sum_array(int arr[],int n,int i) + { + if(i + Example : Suppose you want to use how to use the concept for in java, + /search for in java + ''') + diff --git a/Telegram_Bot_Using_Syntaxdb_api/Token.py b/Telegram_Bot_Using_Syntaxdb_api/Token.py new file mode 100644 index 00000000..126526fa --- /dev/null +++ b/Telegram_Bot_Using_Syntaxdb_api/Token.py @@ -0,0 +1,26 @@ +from options import * +import os + +if __name__=='__main__': + TOKEN = '<809112078:AAEOBQvRY0eIJVNyMdXQjZwYQy-K_RfZs38>' + PORT = int(os.environ.get('PORT', '8443')) + + updater = Updater(TOKEN) + + + + #logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.INFO) + + #logger = logging.getLogger(__name__) + + updater = Updater(token=TOKEN) + + dispatcher = updater.dispatcher + + dispatcher.add_handler(CommandHandler('start',start)) + + dispatcher.add_handler(CommandHandler('help',help)) + + dispatcher.add_handler(CommandHandler('search', search, pass_args = True)) + + updater.start_polling() \ No newline at end of file diff --git a/Telegram_Bot_Using_Syntaxdb_api/syntaxdb_api.py b/Telegram_Bot_Using_Syntaxdb_api/syntaxdb_api.py new file mode 100644 index 00000000..bb32eb5b --- /dev/null +++ b/Telegram_Bot_Using_Syntaxdb_api/syntaxdb_api.py @@ -0,0 +1,41 @@ +import requests +import json + +class Syntaxdb(): + def __init__(self, choice=0, search_query="",language=""): + '''choice = 0 => concepts in any language''' + '''choice = 1 => all concepts of particular language''' + if(choice == 0): + self.url = "https://syntaxdb.com/api/v1/concepts/search?q="+search_query + else: + self.url = "https://syntaxdb.com/api/v1/languages/"+language+"/concepts" + + def getResponse(self): + '''Returns the reponse got from the url''' + return requests.get(self.url) + + def getJson(self,response): + '''Pass in the requests reponse and returns the json data''' + return response.json() + + def getContent(self): + '''Pass the json data and returns list''' + '''if choice is concept, the summary will have only one item in list''' + '''if choice is is 1, the summary will have multiple items in list''' + response = self.getResponse() + json_response = self.getJson(response) + summary = [] + for data in json_response: + concept = data['concept_search'] + syntax = data['syntax'] + description = data['description'] + notes = data['notes'] + example = data['example'] + summary+=["\nCONCEPT\n"+concept+"\n\nSYNTAX\n"+syntax+"\n\nDESCRIPTION\n"+ description+"\n\nNOTES\n"+notes+"\n\nEXAMPLE\n"+example] + return summary + + def getAllConcepts(self): + '''Pass the langauge and a list of all concepts are returned''' + response = self.getResponse() + json_response = self.getJson(response) + return self.getContent(json_response) \ No newline at end of file diff --git a/This code is for finding the length of a string b/This code is for finding the length of a string new file mode 100644 index 00000000..99be0d52 --- /dev/null +++ b/This code is for finding the length of a string @@ -0,0 +1,15 @@ +# Python code to demonstrate string length +# using for loop + +# Returns length of string +def findLen(str): + counter = 0 + for i in str: + counter += 1 + return counter + + +str = "Shubham" +print(findLen(str)) + +# This code is contributed by Shubham diff --git a/To find length of a string. b/To find length of a string. new file mode 100644 index 00000000..fb0675b1 --- /dev/null +++ b/To find length of a string. @@ -0,0 +1,9 @@ +def findLen(str): + if not str: + return 0 + else: + some_random_str = 'py' + return ((some_random_str).join(str)).count(some_random_str) + 1 + +str = "geeks" +print(findLen(str)) diff --git a/To find the Fibonacci series in C b/To find the Fibonacci series in C new file mode 100644 index 00000000..93fb0d5b --- /dev/null +++ b/To find the Fibonacci series in C @@ -0,0 +1,20 @@ +#include +void main() + +{ + long long int num,i,a=-1,b=1,c; + + printf("Enter a number:"); + scanf("%d",&num); + + for(i=0;i<=num;i++) + + { + c=a+b; + printf("%lld ",c); + a=b; + b=c; + } + + getch(); +} diff --git a/To find the largest element in an array b/To find the largest element in an array new file mode 100644 index 00000000..f9d7bf15 --- /dev/null +++ b/To find the largest element in an array @@ -0,0 +1,20 @@ +#include +int main() { + int i, n; + float arr[100]; + printf("Enter the number of elements (1 to 100): "); + scanf("%d", &n); + + for (i = 0; i < n; ++i) { + printf("Enter number%d: ", i + 1); + scanf("%f", &arr[i]); + } + for (i = 1; i < n; ++i) { + if (arr[0] < arr[i]) + arr[0] = arr[i]; + } + + printf("Largest element = %.2f", arr[0]); + + return 0; +} diff --git a/To find the length of a string b/To find the length of a string new file mode 100644 index 00000000..c8f5c534 --- /dev/null +++ b/To find the length of a string @@ -0,0 +1,11 @@ + +#include +int main() { + char s[] = "I love programming"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git "a/To find the length of any string like \"Shubham\"" "b/To find the length of any string like \"Shubham\"" new file mode 100644 index 00000000..acd0b0e3 --- /dev/null +++ "b/To find the length of any string like \"Shubham\"" @@ -0,0 +1,15 @@ +# Python code to demonstrate string length +# using for loop + +# Returns length of string +def findLen(str): + counter = 0 + for i in str: + counter += 1 + return counter + + +str = "Shubham" +print(findLen(str)) + +# This code is contributed by Shubham Kumar Sharma diff --git a/To find the length of the string b/To find the length of the string new file mode 100644 index 00000000..a20fd7c3 --- /dev/null +++ b/To find the length of the string @@ -0,0 +1,10 @@ +#include +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/To obtain the Fibonacci series b/To obtain the Fibonacci series new file mode 100644 index 00000000..2ae3e317 --- /dev/null +++ b/To obtain the Fibonacci series @@ -0,0 +1,14 @@ +#include +int main() { + + printf("enter the value"); + scanf("%d",&n); + for(i=1;i<=n;i++) + { + printf("%d",a); + c=a+b; + a=b; + b=c; + } + return 0; +}int n,i,a=0,b=1,c; diff --git a/To-Reverse-the-array-in-java b/To-Reverse-the-array-in-java new file mode 100644 index 00000000..4bda7b15 --- /dev/null +++ b/To-Reverse-the-array-in-java @@ -0,0 +1,23 @@ +import java.util.Scanner; +class ReverseNumberWhile +{ + public static void main(String args[]) + { + int num=0; + int reversenum =0; + System.out.println("Input your number and press enter: "); + + Scanner in = new Scanner(System.in); + + num = in.nextInt(); + + while( num != 0 ) + { + reversenum = reversenum * 10; + reversenum = reversenum + num%10; + num = num/10; + } + + System.out.println("Reverse of input number is: "+reversenum); + } +} diff --git a/Triangular Number pattern b/Triangular Number pattern new file mode 100644 index 00000000..dbb2ce6d --- /dev/null +++ b/Triangular Number pattern @@ -0,0 +1,18 @@ +import java.util.Scanner; + +public class TraingularNumberPattern { + public static void main(String[] args) { + + + Scanner scan=new Scanner(System.in); + int n=scan.nextInt(); + for(int i=1;i<=n;i++) { + for(int j=1;j<=i;j++) { + System.out.print(i); + } + System.out.println(); + } + + + } +} diff --git a/Triangular Star Pattern b/Triangular Star Pattern new file mode 100644 index 00000000..0061d988 --- /dev/null +++ b/Triangular Star Pattern @@ -0,0 +1,18 @@ +import java.util.Scanner; + +public class TraingularStarPattern { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Scanner scan=new Scanner(System.in); + int n=scan.nextInt(); + for(int i=1;i<=n;i++) { + for(int j=1;j<=i;j++) { + System.out.print("*"); + } + System.out.println(); + } + + } + +} diff --git a/Two integer sum. b/Two integer sum. new file mode 100644 index 00000000..0230c685 --- /dev/null +++ b/Two integer sum. @@ -0,0 +1,22 @@ +// Program for Java Assignment Operators +package JavaOperators; + +import java.util.Scanner; + +public class AssignmentOperators { + private static Scanner sc; + public static void main(String[] args) { + int a, Sum; + sc = new Scanner(System.in); + System.out.println(" Please Enter any integer Value: "); + a = sc.nextInt(); + System.out.println(" Please Enter any integer Value for Total: "); + Sum = sc.nextInt(); + + System.out.format(" Value of Sum = %d \n", Sum += a ); + System.out.format(" Value of Sum = %d \n", Sum -= a ); + System.out.format(" Value of Sum = %d \n", Sum *= a ); + System.out.format(" Value of Sum = %d \n", Sum /= a ); + System.out.format(" Value of Sum = %d \n", Sum %= a ); + } +} diff --git a/URL_Shortner.py b/URL_Shortner.py new file mode 100644 index 00000000..eae3376d --- /dev/null +++ b/URL_Shortner.py @@ -0,0 +1,29 @@ +from __future__ import with_statement + +import contextlib + +try: + from urllib.parse import urlencode + +except ImportError: + from urllib import urlencode +try: + from urllib.request import urlopen + +except ImportError: + from urllib2 import urlopen + +import sys + + +def make_tiny(url): + request_url = ('http://tinyurl.com/api-create.php?' + urlencode({'url':url})) + with contextlib.closing(urlopen(request_url)) as response: + return response.read().decode('utf-8 ') + +def main(): + for tinyurl in map(make_tiny, sys.argv[1:]): + print(tinyurl) + +if __name__ == '__main__': + main() diff --git a/Using comments in a program in c b/Using comments in a program in c new file mode 100644 index 00000000..2675d09b --- /dev/null +++ b/Using comments in a program in c @@ -0,0 +1,18 @@ +#include + +int main() +{ + // Single line comment in a C program + + printf("Writing comments is very useful.\n"); + + /* + * Multi-line comment syntax + * Comments help us to understand a program later easily. + * Will you write comments while writing programs? + */ + + printf("Good luck C programmer.\n"); + + return 0; +} diff --git a/Wave Print in 2D-Array b/Wave Print in 2D-Array new file mode 100644 index 00000000..d72cd6ab --- /dev/null +++ b/Wave Print in 2D-Array @@ -0,0 +1,61 @@ +/* +1 4 7 10 1 6 7 12 +2 5 8 11 => 2 5 8 11 +3 6 9 12 3 4 9 10 +*/ + + +#include +using namespace std; + +int main() +{ + int arr[20][20], row, col; + int i, j; + cout<<"Enter the size of 2D-ARRAY row-wise: \n"; + cin>>row; + cout<<"Enter the size of 2D-ARRAY column-wise: \n"; + cin>>col; + + cout<<"Enter the values for 2D-ARRAY: \n"; + for(i = 0; i < row; i++) + { + for(j = 0; j < col; j++) + { + cout<<"arr["<>arr[i][j]; + } + } + for(j = 0; j < col; j++) + { + for(i = 0; i < row; i++) + { + cout<= 0; i--) + { + cout< +using namespace std; + +int main() +{ + char x; + cin>>x; + + cout<<"the ASCII value of "< +using namespace std; +int main() +{ + int first[20], second[20], sum[20], c, n; + + cout << "Enter the number of elements in the array "; + cin >> n; + + cout << "Enter elements of first array" << endl; + + for (c = 0; c < n; c++) + cin >> first[c]; + + cout << "Enter elements of second array" << endl; + + for (c = 0; c < n; c++) + cin >> second[c]; + + cout << "Sum of elements of the arrays:" << endl; + + for (c = 0; c < n; c++) { + sum[c] = first[c] + second[c]; + cout << sum[c] << endl; + } + return 0; +} diff --git a/add two given Matrices_Java b/add two given Matrices_Java new file mode 100644 index 00000000..21608f90 --- /dev/null +++ b/add two given Matrices_Java @@ -0,0 +1,28 @@ +public class JavaExample { + public static void main(String[] args) { + int rows = 2, columns = 4; + + // Declaring the two matrices as multi-dimensional arrays + int[][] MatrixA = { {1, 1, 1, 1}, {2, 3, 5, 2} }; + int[][] MatrixB = { {2, 3, 4, 5}, {2, 2, 4, -4} }; + + /* Declaring a matrix sum, that will be the sum of MatrixA + * and MatrixB, the sum matrix will have the same rows and + * columns as the given matrices. + */ + int[][] sum = new int[rows][columns]; + for(int i = 0; i < rows; i++) { + for (int j = 0; j < columns; j++) { + sum[i][j] = MatrixA[i][j] + MatrixB[i][j]; + } + } + // Displaying the sum matrix + System.out.println("Sum of the given matrices is: "); + for(int i = 0; i < rows; i++) { + for (int j = 0; j < columns; j++) { + System.out.print(sum[i][j] + " "); + } + System.out.println(); + } + } +} diff --git a/adding the array elements b/adding the array elements new file mode 100644 index 00000000..3bad39e9 --- /dev/null +++ b/adding the array elements @@ -0,0 +1,14 @@ +#include +int main() +{ + int a[100],sum=0,n; + scanf("%d",&n); + for(int i=1;i +int main() +{ + int x, y, z; + + printf("Enter two numbers to add\n"); + scanf("%d%d", &x, &y); + + z = x + y; + + printf("Sum of the numbers = %d\n", z); + + return 0; +} diff --git a/arithmetic operations b/arithmetic operations new file mode 100644 index 00000000..2e394f66 --- /dev/null +++ b/arithmetic operations @@ -0,0 +1,14 @@ +a=int(input("Enter number 1 : ")) +b=int(input("Enter number 2 : ")) +s=a+b +m=a-b; +multi=a*b +div=a/b +mod=a%b +p=a//b +print("Sum : ",s) +print("Subtraction : ",m) +print("Multiplication : ",multi) +print("Division : ",div); +print("Modulus operator(gives remainder) : ",mod) +print("Floor division : ",p) diff --git a/arithmetic_oprations_in_java.java b/arithmetic_oprations_in_java.java new file mode 100644 index 00000000..e1d49c13 --- /dev/null +++ b/arithmetic_oprations_in_java.java @@ -0,0 +1,28 @@ +import java.util.Scanner; + +class Test +{ +public static void main(String args[]) +{ + int num1, num2; + int sum, sub, mult, mod; + float div; + Scanner op=new Scanner(System.in); + System.out.print("Enter 1st no: "); + num1=op.nextInt(); + System.out.print("Enter 2nd no:"); + num2=op.nextInt(); + + sum = num1 + num2; + sub = num1 - num2; + mult = num1 * num2; + div = (float)num1 / num2; + mod = num1 % num2; + + System.out.println("SUM = "+sum); + System.out.println("DIFF = "+sub); + System.out.println("PRODUCT = "+mult); + System.out.println("QUOTIENT = "+div); + System.out.println("MODULUS = "+mod); + } +} diff --git a/array reverse b/array reverse new file mode 100644 index 00000000..d53801a3 --- /dev/null +++ b/array reverse @@ -0,0 +1,38 @@ +// Iterative C program to reverse an array +#include + +/* Function to reverse arr[] from start to end*/ +void rvereseArray(int arr[], int start, int end) +{ + int temp; + while (start < end) + { + temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + start++; + end--; + } +} + +/* Utility that prints out an array on a line */ +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + + printf("\n"); +} + +/* Driver function to test above functions */ +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6}; + int n = sizeof(arr) / sizeof(arr[0]); + printArray(arr, n); + rvereseArray(arr, 0, n-1); + printf("Reversed array is \n"); + printArray(arr, n); + return 0; +} diff --git a/array sorting in python.py b/array sorting in python.py new file mode 100644 index 00000000..5bcbb0ad --- /dev/null +++ b/array sorting in python.py @@ -0,0 +1,19 @@ +arr = [5, 2, 8, 7, 1]; +temp = 0; + +print("Elements of original array: "); +for i in range(0, len(arr)): + print(arr[i], end=" "); + +for i in range(0, len(arr)): + for j in range(i+1, len(arr)): + if(arr[i] > arr[j]): + temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + +print(); + +print("The final sorted array is: "); +for i in range(0, len(arr)): + print(arr[i], end=" "); diff --git a/array_reverser_by_ridwankustanto.go b/array_reverser_by_ridwankustanto.go new file mode 100644 index 00000000..41a9c207 --- /dev/null +++ b/array_reverser_by_ridwankustanto.go @@ -0,0 +1,36 @@ +package main + +import ( + "flag" + "fmt" + "strings" +) + +func main() { + + defaultArr := "1,31,october,hacktoberfest" + fmt.Println("Default array value: ", defaultArr) + + var ( + arr, arrReversed []interface{} + ) + + str := flag.String("array", defaultArr, "set of array that want to be reversed. must be separated by ',' between the items") + flag.Parse() + + arrStr := strings.Split(*str, ",") + for _, item := range arrStr { + arr = append(arr, item) + } + + fmt.Println("set of array: ", arr) + + arrLenght := len(arr) + + for i := arrLenght-1; i >= 0; i-- { + arrReversed = append(arrReversed, arr[i]) + } + + fmt.Println("set of reversed array: ", arrReversed) + +} diff --git a/bin_to_dec.cpp b/bin_to_dec.cpp new file mode 100644 index 00000000..a1c55f6d --- /dev/null +++ b/bin_to_dec.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +void BinaryToDecimal(int n) +{ + int decimal = 0; + int base = 1; + int temp = n; + while (temp) + { + int lastDigit = temp % 10; + temp = temp/10; + decimal += lastDigit*base; + base = base*2; + } + cout<<"Decimal conversion of "<>m; + BinaryToDecimal(m); + return 0; +} diff --git a/binary - decimal b/binary - decimal new file mode 100644 index 00000000..6b73a730 --- /dev/null +++ b/binary - decimal @@ -0,0 +1,21 @@ +#include +#include +int convert(long long n); +int main() { + long long n; + printf("Enter a binary number: "); + scanf("%lld", &n); + printf("%lld in binary = %d in decimal", n, convert(n)); + return 0; +} + +int convert(long long n) { + int dec = 0, i = 0, rem; + while (n != 0) { + rem = n % 10; + n /= 10; + dec += rem * pow(2, i); + ++i; + } + return dec; +} diff --git a/binary search in c b/binary search in c new file mode 100644 index 00000000..9378bd3b --- /dev/null +++ b/binary search in c @@ -0,0 +1,37 @@ +#include +int main() +{ + int c, first, last, middle, n, search, array[100]; + + 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]); + + printf("Enter value to find\n"); + scanf("%d", &search); + + first = 0; + last = n - 1; + middle = (first+last)/2; + + while (first <= last) { + if (array[middle] < search) + first = middle + 1; + else if (array[middle] == search) { + printf("%d found at location %d.\n", search, middle+1); + break; + } + else + last = middle - 1; + + middle = (first + last)/2; + } + if (first > last) + printf("Not found! %d isn't present in the list.\n", search); + + return 0; +} diff --git a/binary search in c++ b/binary search in c++ new file mode 100644 index 00000000..03dc859c --- /dev/null +++ b/binary search in c++ @@ -0,0 +1,42 @@ +// C++ program to implement recursive Binary Search +#include +using namespace std; + +// A recursive binary search function. It returns +// location of x in given array arr[l..r] is present, +// otherwise -1 +int binarySearch(int arr[], int l, int r, int x) +{ + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the middle + // itself + if (arr[mid] == x) + return mid; + + // If element is smaller than mid, then + // it can only be present in left subarray + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + // Else the element can only be present + // in right subarray + return binarySearch(arr, mid + 1, r, x); + } + + // We reach here when element is not + // present in array + return -1; +} + +int main(void) +{ + int arr[] = { 2, 3, 4, 10, 40 }; + int x = 10; + int n = sizeof(arr) / sizeof(arr[0]); + int result = binarySearch(arr, 0, n - 1, x); + (result == -1) ? cout << "Element is not present in array" + : cout << "Element is present at index " << result; + return 0; +} diff --git a/binary search tree b/binary search tree new file mode 100644 index 00000000..b21004d1 --- /dev/null +++ b/binary search tree @@ -0,0 +1,101 @@ +# Create a node +class Node: + def __init__(self, key): + self.key = key + self.left = None + self.right = None + + +# Inorder traversal +def inorder(root): + if root is not None: + # Traverse left + inorder(root.left) + + # Traverse root + print(str(root.key) + "->", end=' ') + + # Traverse right + inorder(root.right) + + +# Insert a node +def insert(node, key): + + # Return a new node if the tree is empty + if node is None: + return Node(key) + + # Traverse to the right place and insert the node + if key < node.key: + node.left = insert(node.left, key) + else: + node.right = insert(node.right, key) + + return node + + +# Find the inorder successor +def minValueNode(node): + current = node + + # Find the leftmost leaf + while(current.left is not None): + current = current.left + + return current + + +# Deleting a node +def deleteNode(root, key): + + # Return if the tree is empty + if root is None: + return root + + # Find the node to be deleted + if key < root.key: + root.left = deleteNode(root.left, key) + elif(key > root.key): + root.right = deleteNode(root.right, key) + else: + # If the node is with only one child or no child + if root.left is None: + temp = root.right + root = None + return temp + + elif root.right is None: + temp = root.left + root = None + return temp + + # If the node has two children, + # place the inorder successor in position of the node to be deleted + temp = minValueNode(root.right) + + root.key = temp.key + + # Delete the inorder successor + root.right = deleteNode(root.right, temp.key) + + return root + + +root = None +root = insert(root, 8) +root = insert(root, 5) +root = insert(root, 1) +root = insert(root, 6) +root = insert(root, 7) +root = insert(root, 10) +root = insert(root, 14) +root = insert(root, 4) + +print("Inorder traversal: ", end=' ') +inorder(root) + +print("\nDelete 10") +root = deleteNode(root, 10) +print("Inorder traversal: ", end=' ') +inorder(root) diff --git a/binary to decimal conversion b/binary to decimal conversion new file mode 100644 index 00000000..70886cf5 --- /dev/null +++ b/binary to decimal conversion @@ -0,0 +1,8 @@ + +class Solution { + public static void main(String args[]) + { + String binaryNumber = "1001"; + System.out.println(Integer.parseInt(binaryNumber, 2)); + } +} diff --git a/binary to decimal conversion using c b/binary to decimal conversion using c new file mode 100644 index 00000000..5ada4dcd --- /dev/null +++ b/binary to decimal conversion using c @@ -0,0 +1,19 @@ +#include + +int main() +{ + int num, b_val, d_val = 0, base = 1, rem; + + printf("Enter a binary number(1s and 0s) \n"); + scanf("%d", &num); /* maximum five digits */ + binary_val = num; + while (num > 0) + { + rem = num % 10; + d_val = d_val + rem * base; + num = num / 10 ; + base = base * 2; + } + printf("The Binary number is = %d \n", b_val); + printf("Its decimal equivalent is = %d \n", d_val); +} diff --git a/binary to decimal converter b/binary to decimal converter new file mode 100644 index 00000000..aa3e14fd --- /dev/null +++ b/binary to decimal converter @@ -0,0 +1,20 @@ +#include +int convert(long long n); +int main() { + long long n; + printf("Enter a binary number: "); + scanf("%lld", &n); + printf("%lld in binary = %d in decimal", n, convert(n)); + return 0; +} + +int convert(long long n) { + int dec = 0, i = 0, rem; + while (n != 0) { + rem = n % 10; + n /= 10; + dec += rem * pow(2, i); + ++i; + } + return dec; +} diff --git a/binary to octal b/binary to octal new file mode 100644 index 00000000..96cf36b9 --- /dev/null +++ b/binary to octal @@ -0,0 +1,30 @@ +#include +#include +int convert(long long bin); +int main() { + long long bin; + printf("Enter a binary number: "); + scanf("%lld", &bin); + printf("%lld in binary = %d in octal", bin, convert(bin)); + return 0; +} + +int convert(long long bin) { + int oct = 0, dec = 0, i = 0; + + // converting binary to decimal + while (bin != 0) { + dec += (bin % 10) * pow(2, i); + ++i; + bin /= 10; + } + i = 1; + + // converting to decimal to octal + while (dec != 0) { + oct += (dec % 8) * i; + dec /= 8; + i *= 10; + } + return oct; +} diff --git a/binary to octal code b/binary to octal code new file mode 100644 index 00000000..e67c7da4 --- /dev/null +++ b/binary to octal code @@ -0,0 +1,34 @@ +import java.util.*; +public class Main +{ +public static int binary_to_octal( int binary) +{ +int octal = 0, decimal = 0, i = 0; + +while(binary != 0) +{ +decimal += (binary%10) * Math.pow(2,i); +++i; +binary/=10; +} + +i = 1; + +while (decimal != 0) +{ +octal += (decimal % 8) * i; +decimal /= 8; +i *= 10; +} + +return octal; +} + +public static void main(String[] args) +{ +Scanner sc = new Scanner(System.in); +System.out.print(“Enter the binary number : “); +int binary = sc.nextInt(); +System.out.print(“\nEquivalent octal number : ” + binary_to_octal(binary)); +} +} diff --git a/binaryToDecimal.py b/binaryToDecimal.py new file mode 100644 index 00000000..6d3e8a04 --- /dev/null +++ b/binaryToDecimal.py @@ -0,0 +1,39 @@ +binnum = int(input("Enter a binary number ")) + +final = int(0) + + +#128 +if (binnum >= 10000000): + binnum = int(binnum-10000000) + final = int(final+128) +#64 +if (binnum >= 1000000): + binnum = int(binnum-1000000) + final = int(final+64) +#32 +if (binnum >= 100000): + binnum = int(binnum-100000) + final = int(final+32) +#16 +if (binnum >= 10000): + binnum = int(binnum-10000) + final = int(final+16) +#8 +if (binnum >= 1000): + binnum = int(binnum-1000) + final = int(final+8) +#4 +if (binnum >= 100): + binnum = int(binnum-100) + final = int(final+4) +#2 +if (binnum >= 10): + binnum = int(binnum-10) + final = int(final+2) +#1 +if (binnum >= 1): + binnum = int(binnum-1) + final = int(final+1) + +print(final) diff --git a/binary_search_algo b/binary_search_algo new file mode 100644 index 00000000..8afb3afc --- /dev/null +++ b/binary_search_algo @@ -0,0 +1,27 @@ +#include +using namespace std; +int binarysearch(int arr[], int num, int start, int end) +{ while(start<=end){ + int middle = (start+end)/2; + if(arr[middle] == num) + return middle; + if(arr[middle]>num) + end=middle-1; + else + start=middle+1; + + } + return -1; +} + +int main() +{ + int arr[]={1,2,6,7,8,10}; + int num; + cin>>num; + int n = sizeof(arr)/ sizeof(arr[0]); + int ans=binarysearch(arr,num,0,n-1); + cout< 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] + " "); + } + + } +} diff --git a/bubbleSort.py b/bubbleSort.py new file mode 100644 index 00000000..609564c6 --- /dev/null +++ b/bubbleSort.py @@ -0,0 +1,26 @@ +# Python program for implementation of Bubble Sort + +def bubbleSort(arr): + n = len(arr) + + # Traverse through all array elements + for i in range(n-1): + # range(n) also work but outer loop will repeat one time more than needed. + + # Last i elements are already in place + for j in range(0, n-i-1): + + # traverse the array from 0 to n-i-1 + # Swap if the element found is greater + # than the next element + if arr[j] > arr[j+1] : + arr[j], arr[j+1] = arr[j+1], arr[j] + +# Driver code to test above +arr = [64, 34, 25, 12, 22, 11, 90] + +bubbleSort(arr) + +print ("Sorted array is:") +for i in range(len(arr)): + print ("%d" %arr[i]), diff --git a/c for linearsearch b/c for linearsearch new file mode 100644 index 00000000..8e625c7c --- /dev/null +++ b/c for linearsearch @@ -0,0 +1,29 @@ +#include +int main() +{ + int array[100], search, c, n; + + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d integer(s)\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter a number to search\n"); + scanf("%d", &search); + + for (c = 0; c < n; c++) + { + if (array[c] == search) /* If required element is found */ + { + printf("%d is present at location %d.\n", search, c+1); + break; + } + } + if (c == n) + printf("%d isn't present in the array.\n", search); + + return 0; +} diff --git a/c program to find lenght of text b/c program to find lenght of text new file mode 100644 index 00000000..c90dfe30 --- /dev/null +++ b/c program to find lenght of text @@ -0,0 +1,17 @@ +#include +#include + +int main() +{ + char Str[100]; + int Length; + + printf("\n Please Enter any String \n"); + gets (Str); + + Length = strlen(Str); + + printf("Length of a String = %d\n", Length); + + return 0; +} diff --git a/c program to print leap year b/c program to print leap year new file mode 100644 index 00000000..8e44022c --- /dev/null +++ b/c program to print leap year @@ -0,0 +1,29 @@ +#include +int main() +{ + int startYear, endYear, i; + + //print a string about the program + printf("Print leap years between two given years \n"); + + //get the start year from user + printf("Enter Start year: "); + scanf("%d", &startYear); + + //get the end year from user + printf("Enter End year: "); + scanf("%d", &endYear); + + printf("Leap years:\n"); + + //loop through between the start and end year + for(i=startYear; i <= endYear; i++){ + + //check if the (i) year is a Leap year if yes print + if( (0 == i % 4) && (0 != i % 100) || (0 == i % 400) ){ + printf("%d\n", i); + } + } + + return 0; +} diff --git a/c# Count of Leap Years in a given year range b/c# Count of Leap Years in a given year range new file mode 100644 index 00000000..dd515fda --- /dev/null +++ b/c# Count of Leap Years in a given year range @@ -0,0 +1,36 @@ +// C# implementation to find the +// count of leap years in given +// range of the year +using System; + +class GFG +{ + +// Function to calculate the number +// of leap years in range of (1, year) +static int calNum(int year) +{ + return (year / 4) - (year / 100) + + (year / 400); +} + +// Function to calculate the number +// of leap years in given range +static void leapNum(int l, int r) +{ + l--; + int num1 = calNum(r); + int num2 = calNum(l); + Console.Write(num1 - num2 +"\n"); +} + +// Driver Code +public static void Main(String[] args) +{ + int l1 = 1, r1 = 400; + leapNum(l1, r1); + + int l2 = 400, r2 = 2000; + leapNum(l2, r2); +} +} diff --git a/c# Prime No. b/c# Prime No. new file mode 100644 index 00000000..f5b5ed02 --- /dev/null +++ b/c# Prime No. @@ -0,0 +1,61 @@ +// C# program to find the prime numbers +// between a given interval +using System; + +class GFG{ + +// Driver code +public static void Main(string[] args) +{ + + // Declare the variables + int a, b, i, j, flag; + + // Ask user to enter lower value of interval + Console.WriteLine("Enter lower bound of " + + "the interval: "); + + // Take input + a = int.Parse(Console.ReadLine()); + + // Ask user to enter upper value of interval + Console.WriteLine("\nEnter upper bound " + + "of the interval: "); + + // Take input + b = int.Parse(Console.ReadLine()); + + // Print display message + Console.WriteLine("\nPrime numbers between " + + "{0} and {1} are: ", a, b); + + // Traverse each number in the interval + // with the help of for loop + for(i = a; i <= b; i++) + { + + // Skip 0 and 1 as they are + // niether prime nor composite + if (i == 1 || i == 0) + continue; + + // flag variable to tell + // if i is prime or not + flag = 1; + + for(j = 2; j <= i / 2; ++j) + { + if (i % j == 0) + { + flag = 0; + break; + } + } + + // flag = 1 means i is prime + // and flag = 0 means i is not prime + if (flag == 1) + Console.WriteLine(i); + } +} +} diff --git a/c# Program for Binary To Decimal Conversion b/c# Program for Binary To Decimal Conversion new file mode 100644 index 00000000..344cb55b --- /dev/null +++ b/c# Program for Binary To Decimal Conversion @@ -0,0 +1,36 @@ +// C# program to convert +// binary to decimal + +// Function to convert +// binary to decimal +class GFG { + public static int binaryToDecimal(int n) + { + int num = n; + int dec_value = 0; + + // Initializing base1 + // value to 1, i.e 2^0 + int base1 = 1; + + int temp = num; + while (temp > 0) { + int last_digit = temp % 10; + temp = temp / 10; + + dec_value += last_digit * base1; + + base1 = base1 * 2; + } + + return dec_value; + } + + // Driver Code + public static void Main() + { + int num = 10101001; + + System.Console.Write(binaryToDecimal(num)); + } +} diff --git a/c# program to reverse an array or string b/c# program to reverse an array or string new file mode 100644 index 00000000..59be7d45 --- /dev/null +++ b/c# program to reverse an array or string @@ -0,0 +1,44 @@ +// Iterative C# program to reverse an +// array +using System; + +class GFG { + + /* Function to reverse arr[] from + start to end*/ + static void rvereseArray(int []arr, + int start, int end) + { + int temp; + + while (start < end) + { + temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + start++; + end--; + } + } + + /* Utility that prints out an + array on a line */ + static void printArray(int []arr, + int size) + { + for (int i = 0; i < size; i++) + Console.Write(arr[i] + " "); + + Console.WriteLine(); + } + + // Driver function + public static void Main() + { + int []arr = {1, 2, 3, 4, 5, 6}; + printArray(arr, 6); + rvereseArray(arr, 0, 5); + Console.Write("Reversed array is \n"); + printArray(arr, 6); + } +} diff --git a/c++ program to find the largest number b/c++ program to find the largest number new file mode 100644 index 00000000..1e909f66 --- /dev/null +++ b/c++ program to find the largest number @@ -0,0 +1,30 @@ +#include +using namespace std; + +int main() +{ + int i, n; + float arr[100]; + + cout << "Enter total number of elements(1 to 100): "; + cin >> n; + cout << endl; + + // Store number entered by the user + for(i = 0; i < n; ++i) + { + cout << "Enter Number " << i + 1 << " : "; + cin >> arr[i]; + } + + // Loop to store largest number to arr[0] + for(i = 1;i < n; ++i) + { + // Change < to > if you want to find the smallest element + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + cout << "Largest element = " << arr[0]; + + return 0; +} diff --git a/c++_Program to find sum of elements in a given array b/c++_Program to find sum of elements in a given array new file mode 100644 index 00000000..5feb4b6f --- /dev/null +++ b/c++_Program to find sum of elements in a given array @@ -0,0 +1,27 @@ +/* C++ Program to find sum of elements +in a given array */ +#include +using namespace std; + +// function to return sum of elements +// in an array of size n +int sum(int arr[], int n) +{ + int sum = 0; // initialize sum + + // Iterate through all elements + // and add them to sum + for (int i = 0; i < n; i++) + sum += arr[i]; + + return sum; +} + +// Driver code +int main() +{ + int arr[] = {12, 3, 4, 15}; + int n = sizeof(arr) / sizeof(arr[0]); + cout << "Sum of given array is " << sum(arr, n); + return 0; +} diff --git a/c_length_String b/c_length_String new file mode 100644 index 00000000..5d3de3c2 --- /dev/null +++ b/c_length_String @@ -0,0 +1,18 @@ +// C program to find the length of string +#include +#include + +int main() +{ + char Str[1000]; + int i; + + printf("Enter the String: "); + scanf("%s", Str); + + for (i = 0; Str[i] != '\0'; ++i); + + printf("Length of Str is %d", i); + + return 0; +} diff --git a/c_program Program to check triangle is equilateral, isosceles ot scalene b/c_program Program to check triangle is equilateral, isosceles ot scalene new file mode 100644 index 00000000..44d601a4 --- /dev/null +++ b/c_program Program to check triangle is equilateral, isosceles ot scalene @@ -0,0 +1,26 @@ +#include +int main() +{ + int sidea, sideb, sidec; //are three sides of a triangle + + /* + * Reads all sides of a triangle + */ + printf("Input three sides of triangle: "); + scanf("%d %d %d", &sidea, &sideb, &sidec); + + if(sidea==sideb && sideb==sidec) //check whether all sides are equal + { + printf("This is an equilateral triangle.\n"); + } + else if(sidea==sideb || sidea==sidec || sideb==sidec) //check whether two sides are equal + { + printf("This is an isosceles triangle.\n"); + } + else //check whether no sides are equal + { + printf("This is a scalene triangle.\n"); + } + + return 0; +} diff --git a/calculate aveg of array in c b/calculate aveg of array in c new file mode 100644 index 00000000..11d2e663 --- /dev/null +++ b/calculate aveg of array in c @@ -0,0 +1,24 @@ +#include +int main() { + int n, i; + float num[100], sum = 0.0, avg; + + printf("Enter the numbers of elements: "); + scanf("%d", &n); + + while (n > 100 || n < 1) { + printf("Error! number should in range of (1 to 100).\n"); + printf("Enter the number again: "); + scanf("%d", &n); + } + + for (i = 0; i < n; ++i) { + printf("%d. Enter number: ", i + 1); + scanf("%f", &num[i]); + sum += num[i]; + } + + avg = sum / n; + printf("Average = %.2f", avg); + return 0; +} diff --git a/calculate average using array b/calculate average using array new file mode 100644 index 00000000..5b50f483 --- /dev/null +++ b/calculate average using array @@ -0,0 +1,18 @@ +#include + +int main() { + int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; + int sum, loop; + float avg; + + sum = avg = 0; + + for(loop = 0; loop < 10; loop++) { + sum = sum + array[loop]; + } + + avg = (float)sum / loop; + printf("Average of array values is %.2f", avg); + + return 0; +} diff --git a/calculate power of number b/calculate power of number new file mode 100644 index 00000000..ee40cf16 --- /dev/null +++ b/calculate power of number @@ -0,0 +1,14 @@ +#include +#include +using namespace std; + +int main() +{ + int p,e; + cout<<"enter the element and the power respectively"; + cin>>e>>p; + + cout<<"the "<info=item; + new_node->link=new_node; + if(y==NULL) + return new_node; + new_node->link=y->link; + y->link=new_node; + return y; + +} + NODE insert_rear(int item,NODE y) +{ + NODE new_node=getnode(); + new_node->info=item; + new_node->link=new_node; + if(y==NULL) + return new_node; + new_node->link=y->link; + y->link=new_node; + y=new_node; + return y; + +} + +NODE delete_front(NODE y) +{ + if(y==NULL) + { + printf("LinkedList doesn't exsists\n"); + return y; + } + NODE temp=y->link; + y->link=temp->link; + free(temp); + return y; +} +NODE delete_rear(NODE y) +{ + NODE cur,prev; + if(y==NULL) + { + printf("Linked list doesn't exsists\n"); + return y; + } + cur=y->link; + prev=NULL; + while(cur!=y) + { + prev=cur; + cur=cur->link; + } + prev->link=y->link; + y=prev; + free(cur); + return y; +} +void display(NODE y) +{ + NODE cur=y->link; + while(cur!=y) + { + printf("%d ",(*cur).info); + cur=cur->link; + } + printf("%d\n",cur->info); +} +int main() +{ + NODE last=NULL; + last=insert_front(8,last); + last=insert_front(7,last); + last=insert_front(6,last); + + display(last); + + last=insert_rear(9,last); + last=insert_rear(10,last); + last=insert_rear(11,last); + + display(last); + + + last=delete_front(last); + display(last); + + last=delete_rear(last); + display(last); + + return 0; + +} + diff --git a/code for ASCII b/code for ASCII new file mode 100644 index 00000000..e84fc638 --- /dev/null +++ b/code for ASCII @@ -0,0 +1,11 @@ +#include +using namespace std; + +int main() +{ + char x; + cin>>x; + + cout<<"the ASCII value of "< +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/command line arguments in c b/command line arguments in c new file mode 100644 index 00000000..1abf1267 --- /dev/null +++ b/command line arguments in c @@ -0,0 +1,12 @@ +#include +int main(int argc, char *argv[]) +{ + int c; + + printf("Number of command line arguments passed: %d\n", argc); + + for (c = 0; c < argc; c++) + printf("%d argument is %s\n", c + 1, argv[c]); + + return 0; +} diff --git a/convert Integers into a Strings using c++ b/convert Integers into a Strings using c++ new file mode 100644 index 00000000..efe3cb38 --- /dev/null +++ b/convert Integers into a Strings using c++ @@ -0,0 +1,18 @@ +#include +#include +#include +template +std::string to_string(T t, std::ios_base & (*f)(std::ios_base&)) +{ + std::ostringstream oss; + oss << f << t; + return oss.str(); +} +int main() +{ + // the second parameter of to_string() should be one of + // std::hex, std::dec or std::oct + std::cout<(123456, std::hex)<(123456, std::oct)< +using namespace std; + +// Function to convert binary to decimal +int binaryToDecimal(int n) +{ + int num = n; + int dec_value = 0; + + // Initializing base value to 1, i.e 2^0 + int base = 1; + + int temp = num; + while (temp) { + int last_digit = temp % 10; + temp = temp / 10; + + dec_value += last_digit * base; + + base = base * 2; + } + + return dec_value; +} + +// Driver program to test above function +int main() +{ + int num; + cout<<"Enter the binary no. : "; + cin>>num; + + cout << binaryToDecimal(num) << endl; +} diff --git a/convert binary to decimal number b/convert binary to decimal number new file mode 100644 index 00000000..18c2f126 --- /dev/null +++ b/convert binary to decimal number @@ -0,0 +1,8 @@ +b_num = list(input("Input a binary number: ")) +value = 0 + +for i in range(len(b_num)): + digit = b_num.pop() + if digit == '1': + value = value + pow(2, i) +print("The decimal value of the number is", value) diff --git a/convert decimal to binary b/convert decimal to binary new file mode 100644 index 00000000..e3ac7e19 --- /dev/null +++ b/convert decimal to binary @@ -0,0 +1,19 @@ +#include +#include +int main(){ +int a[10],n,i; +system ("cls"); +printf("Enter the number to convert: "); +scanf("%d",&n); +for(i=0;n>0;i++) +{ +a[i]=n%2; +n=n/2; +} +printf("\nBinary of Given Number is="); +for(i=i-1;i>=0;i--) +{ +printf("%d",a[i]); +} +return 0; +} diff --git a/count vowels and consonants b/count vowels and consonants new file mode 100644 index 00000000..4049aa8c --- /dev/null +++ b/count vowels and consonants @@ -0,0 +1,12 @@ +string = str(input('Enter a String: ')) +vowels = ['a','e','i','o','u'] +characters = [] +count_v = 0 +count_c = 0 +for ch in string.lower(): + if ch.isalpha(): + if ch in vowels: + count_v += 1 + else: + count_c += 1 +print(' Number of Vowels: {} \n Number of Consonants: {}'.format(count_v, count_c)) diff --git a/counting vowels and consonants in a random input string b/counting vowels and consonants in a random input string new file mode 100644 index 00000000..311b1ccb --- /dev/null +++ b/counting vowels and consonants in a random input string @@ -0,0 +1,13 @@ +s = input() # taking input string +l = len(s) # calculate length of string +# creating list of vowels +num = ['a','A','e','E','i','I','o','O','u','U'] +count_vowels = 0 +count_consonants = 0 +for i in s: + if i in num: + count_vowels += 1 + elif 64 +using namespace std; + +int main() +{ + unsigned int n; + unsigned long long factorial = 1; + + cout << "Enter a positive integer: "; + cin >> n; + + for(int i = 1; i <=n; ++i) + { + factorial *= i; + } + + cout << "Factorial of " << n << " = " << factorial; + return 0; +} diff --git a/create circular queue b/create circular queue new file mode 100644 index 00000000..66f6c2b7 --- /dev/null +++ b/create circular queue @@ -0,0 +1,110 @@ +#include +# define MAX 5 +int cqueue_arr[MAX]; +int front = -1; +int rear = -1; +void insert(int item) +{ +if((front == 0 && rear == MAX-1) || (front == rear+1)) +{ +printf("Queue Overflow n"); +return; +} +if(front == -1) +{ +front = 0; +rear = 0; +} +else +{ +if(rear == MAX-1) +rear = 0; +else +rear = rear+1; +} +cqueue_arr[rear] = item ; +} +void deletion() +{ +if(front == -1) +{ +printf("Queue Underflown"); +return ; +} +printf("Element deleted from queue is : %dn",cqueue_arr[front]); +if(front == rear) +{ +front = -1; +rear=-1; +} +else +{ +if(front == MAX-1) +front = 0; +else +front = front+1; +} +} +void display() +{ +int front_pos = front,rear_pos = rear; +if(front == -1) +{ +printf("Queue is emptyn"); +return; +} +printf("Queue elements :n"); +if( front_pos <= rear_pos ) +while(front_pos <= rear_pos) +{ +printf("%d ",cqueue_arr[front_pos]); +front_pos++; +} +else +{ +while(front_pos <= MAX-1) +{ +printf("%d ",cqueue_arr[front_pos]) +front_pos++; +} +front_pos = 0; +while(front_pos <= rear_pos) +{ +printf("%d ",cqueue_arr[front_pos]); +front_pos++; +} +} +printf("n"); +} +int main() +{ +int choice,item; +do +{ +printf("1.Insertn"); +printf("2.Deleten"); +printf("3.Displayn"); +printf("4.Quitn"); +printf("Enter your choice : "); +scanf("%d",&choice); +switch(choice) +{ +case 1 : +printf("Input the element for insertion in queue : "); +scanf("%d", &item); +insert(item); +break; +case 2 : +deletion(); +break; +case 3: +display(); +break; +case 4: +break; +default: +printf("Wrong choicen"); +} +}while(choice!=4); +return 0; +} diff --git a/create power of numbers b/create power of numbers new file mode 100644 index 00000000..e7d59525 --- /dev/null +++ b/create power of numbers @@ -0,0 +1,16 @@ +#include +int main() { + int base, exp; + long long result = 1; + printf("Enter a base number: "); + scanf("%d", &base); + printf("Enter an exponent: "); + scanf("%d", &exp); + + while (exp != 0) { + result *= base; + --exp; + } + printf("Answer = %lld", result); + return 0; +} diff --git a/create_power_of_any_number b/create_power_of_any_number new file mode 100644 index 00000000..26c3b012 --- /dev/null +++ b/create_power_of_any_number @@ -0,0 +1,30 @@ +import java.util.*; +class Solution { + + static float power(float x, int y) + { + float result; + if( y == 0) + return 1; + result = power(x, y/2); + + if (y%2 == 0) + return result * result; + else + { + if(y > 0) + return x * result * result; + else + return (result * result) / x; + } + } + + /* Program to test function power */ + public static void main(String[] args) + { + Scanner sc= new Scanner(System.in); + float x = sc.nextFloat(); + int y = sc.nextInt(); + System.out.printf("%f", power(x, y)); + } +} diff --git a/css/style.css b/css/style.css new file mode 100644 index 00000000..ceb57611 --- /dev/null +++ b/css/style.css @@ -0,0 +1,50 @@ +* +{ + margin: 0px; + padding: 0px; + font-family: century Gothic; + } + + header { + background-image: url(../2.jpg); + height: 100vh; + background-size: cover; + background-position: center; +} + +ul{ + float: right; + list-style-type: none; + margin-top: 25px; +} + +ul li{ + display: inline-block; +} + +ul li a{ + text-decoration: none; + color: #000; + padding: 5px 20px; + border: 1px solid #000; + transition: 0.6s ease; + } + ul li a:hover{ + background-color: #fff; + color: #000; + } + +.title{ + position: absolute; + top: 50%; + left: 50%; + transform: translate(-50%,-50%); +} +.title h1{ + color: #000; + font-size: 70px; +} + +ul li ul li{ + display: none; +} \ No newline at end of file diff --git a/deleting an element in the array b/deleting an element in the array new file mode 100644 index 00000000..af3b20ab --- /dev/null +++ b/deleting an element in the array @@ -0,0 +1,44 @@ +// C++ program to remove a given element from an array +#include +using namespace std; + +// This function removes an element x from arr[] and +// returns new size after removal (size is reduced only +// when x is present in arr[] +int deleteElement(int arr[], int n, int x) +{ +// Search x in array +int i; +for (i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + + int n, j; + int i=0,RightDiagonalSum=0,LeftDiagonalSum=0, firstarray, secondarray; + + scanf("%d",&n); + int a[n][n]; + + for(int firstarray = 0; firstarray < n; firstarray++) + { + for(int secondarray = 0; secondarray < n; secondarray++) + { + + scanf("%d",&a[firstarray][secondarray]); + } + } + + while(i{ + + const aux = []; + for (var i = 0; i < x; i++){ + aux.push(Math.floor((Math.random() * y)+1)); + } + console.log(aux); +} + diceRoller(4,20); diff --git a/elements sum in array b/elements sum in array new file mode 100644 index 00000000..b0efbb78 --- /dev/null +++ b/elements sum in array @@ -0,0 +1,23 @@ +#include + +// function to return sum of elements +// in an array of size n +int sum(int arr[], int n) +{ + int sum = 0; // initialize sum + + // Iterate through all elements + // and add them to sum + for (int i = 0; i < n; i++) + sum += arr[i]; + + return sum; +} + +int main() +{ + int arr[] = {12, 3, 4, 15}; + int n = sizeof(arr) / sizeof(arr[0]); + printf("Sum of given array is %d", sum(arr, n)); + return 0; +} diff --git a/factorial of a number in javascript.js b/factorial of a number in javascript.js new file mode 100644 index 00000000..953fb80b --- /dev/null +++ b/factorial of a number in javascript.js @@ -0,0 +1,46 @@ + + + + + + Factorial of a number using JavaScript + + + + + +

+

+ + + +

+

+ + + + + diff --git a/factorial of any number b/factorial of any number new file mode 100644 index 00000000..6e8b10e0 --- /dev/null +++ b/factorial of any number @@ -0,0 +1,17 @@ +#include +using namespace std; + +long facto(long n) +{ + if (n == 0) + return 1; + return n * facto(n - 1); +} + +int main() +{ + long num ; + cin>>num; + cout << "Factorial of "<< num << " is = " << facto(num) << endl; + return 0; +} diff --git a/factorial of number java b/factorial of number java new file mode 100644 index 00000000..c156c1f7 --- /dev/null +++ b/factorial of number java @@ -0,0 +1,14 @@ +import java.io.*; +public class factorial +{ +public static void main(String args[]) +{ +Scanner sc= new Scanner(System.in) +{ +int n=sc.nextInt() //size of array +int fact=1; +for(int i=0;i +int main() +{ + int i,fact=1,number; + printf("Enter a number: "); + scanf("%d",&number); + for(i=1;i<=number;i++){ + fact=fact*i; + } + printf("Factorial of %d is: %d",number,fact); +return 0; +} diff --git a/factorial of numbers b/factorial of numbers new file mode 100644 index 00000000..07b09ea2 --- /dev/null +++ b/factorial of numbers @@ -0,0 +1,19 @@ +#include +int main() { + int n, i; + unsigned long long fact = 1; + printf("Enter an integer: "); + scanf("%d", &n); + + // shows error if the user enters a negative integer + if (n < 0) + printf("Error! Factorial of a negative number doesn't exist."); + else { + for (i = 1; i <= n; ++i) { + fact *= i; + } + printf("Factorial of %d = %llu", n, fact); + } + + return 0; +} diff --git a/factorial program in c b/factorial program in c new file mode 100644 index 00000000..3637389b --- /dev/null +++ b/factorial program in c @@ -0,0 +1,12 @@ +#include +int main() +{ + int i,factorial=1,num; + printf("Enter a number to find factorial: "); + scanf("%d",&num); + for(i=1;i<=num;i++){ + factorial=factorial*i; + } + printf("Factorial of %d is: %d",num,factorial); +return 0; +} diff --git a/factorial.py b/factorial.py new file mode 100644 index 00000000..07df8ef3 --- /dev/null +++ b/factorial.py @@ -0,0 +1,5 @@ +n=int(input("Enter number ")) +fact=1 +for i in range(1,n+1): + fact=fact*i +print("Factorial is ",fact) diff --git a/factorial1.py b/factorial1.py new file mode 100644 index 00000000..9c436024 --- /dev/null +++ b/factorial1.py @@ -0,0 +1,10 @@ +num = int(input("Enter a number: ")) +factorial = 1 +if num < 0: + print("Error!!") +elif num == 0: + print("The factorial of 0 is 1") +else: + for i in range(1,num + 1): + factorial = factorial*i + print("The factorial of",num,"is",factorial) diff --git a/factorial_dp.cpp b/factorial_dp.cpp new file mode 100644 index 00000000..1298ea9d --- /dev/null +++ b/factorial_dp.cpp @@ -0,0 +1,20 @@ +#include +using namspace std; +vectorarr; +int main() +{ +long long int val; //upto which term +cin>>val; +arr.push_back(1); + +for(long long int i=1;i= 0 : + sys.stdout.write(str(res[i])) + sys.stdout.flush() + i = i - 1 + + +# This function multiplies x with the number +# represented by res[]. res_size is size of res[] +# or number of digits in the number represented +# by res[]. This function uses simple school +# mathematics for multiplication. This function +# may value of res_size and returns the new value +# of res_size +def multiply(x, res,res_size) : + + carry = 0 # Initialize carry + + # One by one multiply n with individual + # digits of res[] + i = 0 + while i < res_size : + prod = res[i] *x + carry + res[i] = prod % 10; # Store last digit of + # 'prod' in res[] + # make sure floor division is used + carry = prod//10; # Put rest in carry + i = i + 1 + + # Put carry in res and increase result size + while (carry) : + res[res_size] = carry % 10 + # make sure floor division is used + # to avoid floating value + carry = carry // 10 + res_size = res_size + 1 + + return res_size + +# Driver program +factorial(100) + +#This code is contributed by Nikita Tiwari. diff --git a/factorial_of_a_number.c b/factorial_of_a_number.c new file mode 100644 index 00000000..6095a083 --- /dev/null +++ b/factorial_of_a_number.c @@ -0,0 +1,12 @@ +#include +int main() +{ + int i,fact=1,number; + printf("Enter a number: "); + scanf("%d",&number); + for(i=1;i<=number;i++){ + fact=fact*i; + } + printf("Factorial of %d is: %d",number,fact); +return 0; +} diff --git a/factorialno b/factorialno new file mode 100644 index 00000000..c4b104c9 --- /dev/null +++ b/factorialno @@ -0,0 +1,15 @@ +#include +#include + + int main() + { + int i,fact=1,n; + printf("Enter A Number"); + scanf("%d",&n); + for(i=1,i<=n,i++) + { + fact=fact*i; + } + printf("Factorial of %d is %d",n,fact); + return 0; + } diff --git a/fibonacci series in c b/fibonacci series in c new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/fibonacci series in c @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/fibonacci series in c++ using recursion b/fibonacci series in c++ using recursion new file mode 100644 index 00000000..cb212a0c --- /dev/null +++ b/fibonacci series in c++ using recursion @@ -0,0 +1,20 @@ +#include +using namespace std; +int fib(int x) { + if((x==1)||(x==0)) { + return(x); + }else { + return(fib(x-1)+fib(x-2)); + } +} +int main() { + int x , i=0; + cout << "Enter the number of terms of series : "; + cin >> x; + cout << "\nFibonnaci Series : "; + while(i < x) { + cout << " " << fib(i); + i++; + } + return 0; +} diff --git a/fibonacci series in java using recursion b/fibonacci series in java using recursion new file mode 100644 index 00000000..46cd166f --- /dev/null +++ b/fibonacci series in java using recursion @@ -0,0 +1,19 @@ +//Using Recursion +public class FibonacciCalc{ + public static int fibonacciRecursion(int n){ + if(n == 0){ + return 0; + } + if(n == 1 || n == 2){ + return 1; + } + return fibonacciRecursion(n-2) + fibonacciRecursion(n-1); + } + public static void main(String args[]) { + int maxNumber = 10; + System.out.print("Fibonacci Series of "+maxNumber+" numbers: "); + for(int i = 0; i < maxNumber; i++){ + System.out.print(fibonacciRecursion(i) +" "); + } + } +} diff --git a/fibonacci series in javascript.js b/fibonacci series in javascript.js new file mode 100644 index 00000000..74b81843 --- /dev/null +++ b/fibonacci series in javascript.js @@ -0,0 +1,12 @@ + diff --git a/fibonacci series in python using recursion b/fibonacci series in python using recursion new file mode 100644 index 00000000..be9335c8 --- /dev/null +++ b/fibonacci series in python using recursion @@ -0,0 +1,17 @@ +# Python program to display the Fibonacci sequence + +def recur_fibo(n): + if n <= 1: + return n + else: + return(recur_fibo(n-1) + recur_fibo(n-2)) + +nterms = 10 + +# check if the number of terms is valid +if nterms <= 0: + print("Plese enter a positive integer") +else: + print("Fibonacci sequence:") + for i in range(nterms): + print(recur_fibo(i)) diff --git a/fibonacci series using recursion b/fibonacci series using recursion new file mode 100644 index 00000000..428f5d5d --- /dev/null +++ b/fibonacci series using recursion @@ -0,0 +1,16 @@ +//Fibonacci Series using Recursion +#include +int fib(int n) +{ + if (n <= 1) + return n; + return fib(n-1) + fib(n-2); +} + +int main () +{ + int n = 9; + printf("%d", fib(n)); + getchar(); + return 0; +} diff --git a/fibonacci_recursion.cpp b/fibonacci_recursion.cpp new file mode 100644 index 00000000..1c3c686d --- /dev/null +++ b/fibonacci_recursion.cpp @@ -0,0 +1,22 @@ +// This program will help you to find fibonacci serise using recursion. + +#include +using namespace std; +int fib(int x) { + if((x==1)||(x==0)) { + return(x); + }else { + return(fib(x-1)+fib(x-2)); + } +} +int main() { + int x , i=0; + cout << "Enter the number of terms of series : "; + cin >> x; + cout << "\nFibonnaci Series : "; + while(i < x) { + cout << " " << fib(i); + i++; + } + return 0; +} diff --git a/fibonacci_series.py b/fibonacci_series.py new file mode 100644 index 00000000..fcc98ccb --- /dev/null +++ b/fibonacci_series.py @@ -0,0 +1,11 @@ +n=int(input("Enter limit : ")) +f=0 +s=1 +print(f,end=' ') +print(s,end=' ') +for i in range(1,limit-1): + t=f+s + print(t,end=' ') + f=s + s=t + diff --git a/fibonacci_series_java b/fibonacci_series_java new file mode 100644 index 00000000..0dc63143 --- /dev/null +++ b/fibonacci_series_java @@ -0,0 +1,17 @@ +import java.util.*; + +class Solution { + +static int fib(int n) { +double result = (1 + Math.sqrt(5)) / 2; +return (int) Math.round(Math.pow(result, n) + / Math.sqrt(5)); +} + +// Driver Code +public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + int n=sc.nextInt(); + System.out.println(fib(n)); + } +} diff --git a/find Power of a number in javascript.js b/find Power of a number in javascript.js new file mode 100644 index 00000000..fe1c863e --- /dev/null +++ b/find Power of a number in javascript.js @@ -0,0 +1,18 @@ + + + + +

Click the button to display the result of 5*5*5.

+ + + +

+ + + + + diff --git a/find a small element in array b/find a small element in array new file mode 100644 index 00000000..441e2ce4 --- /dev/null +++ b/find a small element in array @@ -0,0 +1,26 @@ +#include + +int main() { + int a[30], i, num, smallest; + + printf("\nEnter no of elements :"); + scanf("%d", &num); + + //Read n elements in an array + for (i = 0; i < num; i++) + scanf("%d", &a[i]); + + //Consider first element as smallest + smallest = a[0]; + + for (i = 0; i < num; i++) { + if (a[i] < smallest) { + smallest = a[i]; + } + } + + // Print out the Result + printf("\nSmallest Element : %d", smallest); + + return (0); +} diff --git a/find factorial of number b/find factorial of number new file mode 100644 index 00000000..6ca575ce --- /dev/null +++ b/find factorial of number @@ -0,0 +1,13 @@ +#include +using namespace std; +int main() +{ + int i,fact=1,number; + cout<<"Enter any Number: "; + cin>>number; + for(i=1;i<=number;i++){ + fact=fact*i; + } + cout<<"Factorial of " < +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/find the minimum and maximum element of an Array b/find the minimum and maximum element of an Array new file mode 100644 index 00000000..fcb66969 --- /dev/null +++ b/find the minimum and maximum element of an Array @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main() +{ + // Get the array + int arr[] = { 1, 45, 54, 71, 76, 12 }; + + // Compute the sizes + int n = sizeof(arr) / sizeof(arr[0]); + + // Print the array + cout << "Array: "; + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + + // Find the minimum element + cout << "\nMin Element = " + << *min_element(arr, arr + n); + + // Find the maximum element + cout << "\nMax Element = " + << *max_element(arr, arr + n); + return 0; +} diff --git a/find the sum of all element of array b/find the sum of all element of array new file mode 100644 index 00000000..54f92e0b --- /dev/null +++ b/find the sum of all element of array @@ -0,0 +1,28 @@ +#include +#define MAX_SIZE 100 + +int main() +{ + int arr[MAX_SIZE]; + int i, n, sum=0; + + /*input size of the array*/ + printf("Enter size of the array: "); + scanf("%d", &n); + + /*input element in array*/ + printf("Enter %d element in the aray: ",n); + for(i=0; i max) { + max = array[i]; + } + + } + return max; +} + +//Testing +const array = [1,2,4,3,12,8,4,9]; +maxx= maxValue(array); +console.log(maxx); diff --git a/findSmallestElementArray b/findSmallestElementArray new file mode 100644 index 00000000..09517409 --- /dev/null +++ b/findSmallestElementArray @@ -0,0 +1,18 @@ +function minValue(array){ + //console.log(array.length); + min = array[0]; + //console.log(min); + for (i=0; i +int main() +{ + int n, i, c, a = 1; + + printf("Enter the number of rows of Floyd's triangle to print\n"); + scanf("%d", &n); + + for (i = 1; i <= n; i++) + { + for (c = 1; c <= i; c++) + { + printf("%d ", a); // Please note space after %d + a++; + } + printf("\n"); + } + + return 0; +} diff --git a/fputs in C b/fputs in C new file mode 100644 index 00000000..ceee3ab5 --- /dev/null +++ b/fputs in C @@ -0,0 +1,17 @@ +#include +int main() +{ +FILE *f = NULL; +f = fopen("aticle.txt", "w"); +if(f == NULL) +{ +printf("Error in creating the file\n"); +exit(1); +} +fputs("Writing the first Line in the file.",f); +fputs("Writing the Second Line in the file.",f); +fclose(f); +puts("Writing the first Line on the output screen."); +puts("Writing the second Line on the output screen."); +return 0; +} diff --git a/function program inc b/function program inc new file mode 100644 index 00000000..ad9287dd --- /dev/null +++ b/function program inc @@ -0,0 +1,19 @@ +#include +void my_function(); // Declaring a function + +int main() +{ + printf("Main function.\n"); + + my_function(); // Calling the function + + printf("Back in function main.\n"); + + return 0; +} + +// Defining the function +void my_function() +{ + printf("Welcome to my function. Feel at home.\n"); +} diff --git a/gcd-lcm b/gcd-lcm new file mode 100644 index 00000000..6a8d9484 --- /dev/null +++ b/gcd-lcm @@ -0,0 +1,27 @@ + +#include +#include +int main() +{ + int x,y; + std::cout<<"Enter the first number: "; + std::cin>>x; + std::cout<<"Enter the 2nd number: "; + std::cin>>y; + int a,b,rem; + + a=x; + b=y; + while(b!=0){ + rem=a%b; + a=b; + b=rem; + } + int gcd,lcm; + + gcd=a; + lcm=(x*y)/gcd; + std::cout<=r) + { + System.out.println("The value of "+n+"p"+r+" is : "+p.permutation(n,r)); + System.out.println("The value of "+n+"c"+r+" is : "+p.combination(n,r)); + } + else + System.out.println("n value should be greater than or equals to r value"); + + } +} diff --git a/interface_in_java.java b/interface_in_java.java new file mode 100644 index 00000000..f5576b4b --- /dev/null +++ b/interface_in_java.java @@ -0,0 +1,42 @@ + +// A simple interface +interface Player +{ + final int id = 10; + int move(); +} +//To implement an interface we use keyword: implements + + +// Java program to demonstrate working of +// interface. +import java.io.*; + +// A simple interface +interface In1 +{ + // public, static and final + final int a = 10; + + // public and abstract + void display(); +} + +// A class that implements the interface. +class TestClass implements In1 +{ + // Implementing the capabilities of + // interface. + public void display() + { + System.out.println("Geek"); + } + + // Driver Code + public static void main (String[] args) + { + TestClass t = new TestClass(); + t.display(); + System.out.println(a); + } +} diff --git a/java Program for factorial of a number b/java Program for factorial of a number new file mode 100644 index 00000000..de045537 --- /dev/null +++ b/java Program for factorial of a number @@ -0,0 +1,15 @@ +import java.io.*; +import java.utils.*; +public class factorial +{ + public static void main(String args[]) + { + Scanner sc= new Scanner(System.in); + int n= sc.nextInt(); + int fact=1; + for(int i=1;i<=n;i++) + fact=fact*i; + System.out.println("the factorial is"+fact); + } +} + diff --git a/java Program to count sum of all numbers in array b/java Program to count sum of all numbers in array new file mode 100644 index 00000000..6b5fe696 --- /dev/null +++ b/java Program to count sum of all numbers in array @@ -0,0 +1,17 @@ +import java.utils.*; +public class sum +{ +public static void main(String args[]) +{ +Scanner sc= new Scanner(System.in); +int n= sc.nextInt(); //size +int arr[]= new int[n]; +count=0; +for(int i=0;i='a'&&ch<='z')||(ch>='A'&&ch<='Z')) + System.out.println(ch+" is a Consonant"); + else + System.out.println("Input is not an alphabet"); + } + } diff --git a/kandale algo.txt b/kandale algo.txt new file mode 100644 index 00000000..6d4fb7e3 --- /dev/null +++ b/kandale algo.txt @@ -0,0 +1,40 @@ +Lets take the example: + {-2, -3, 4, -1, -2, 1, 5, -3} + + max_so_far = max_ending_here = 0 + + for i=0, a[0] = -2 + max_ending_here = max_ending_here + (-2) + Set max_ending_here = 0 because max_ending_here < 0 + + for i=1, a[1] = -3 + max_ending_here = max_ending_here + (-3) + Set max_ending_here = 0 because max_ending_here < 0 + + for i=2, a[2] = 4 + max_ending_here = max_ending_here + (4) + max_ending_here = 4 + max_so_far is updated to 4 because max_ending_here greater + than max_so_far which was 0 till now + + for i=3, a[3] = -1 + max_ending_here = max_ending_here + (-1) + max_ending_here = 3 + + for i=4, a[4] = -2 + max_ending_here = max_ending_here + (-2) + max_ending_here = 1 + + for i=5, a[5] = 1 + max_ending_here = max_ending_here + (1) + max_ending_here = 2 + + for i=6, a[6] = 5 + max_ending_here = max_ending_here + (5) + max_ending_here = 7 + max_so_far is updated to 7 because max_ending_here is + greater than max_so_far + + for i=7, a[7] = -3 + max_ending_here = max_ending_here + (-3) + max_ending_here = 4 \ No newline at end of file diff --git a/largest element of the array b/largest element of the array new file mode 100644 index 00000000..34a9b0c4 --- /dev/null +++ b/largest element of the array @@ -0,0 +1,12 @@ +#include +int main() +{ + int a[100],n,max=0; + scanf("%d",&n); + for(int i=0;i>max){max=a[i];} + } + printf("%d",max); + } diff --git a/largest no in array using java b/largest no in array using java new file mode 100644 index 00000000..62c59050 --- /dev/null +++ b/largest no in array using java @@ -0,0 +1,20 @@ +import java.io.*; +public class largest +{ +public static void main(String args[]) +{ +Scanner sc= new Scanner(System.in) +{ +int n=sc.nextInt() //size of array +int arr[]= new int[n] //array creation +for(int i=0;imax) +max=arr[i]; +} +System.out.println("the maximum no is"+max); +} +} diff --git a/largest_element_in_1Darray b/largest_element_in_1Darray new file mode 100644 index 00000000..1dd2a78b --- /dev/null +++ b/largest_element_in_1Darray @@ -0,0 +1,17 @@ +lst = [] +# number of elements as input +n = int(input("Enter number of elements : ")) +# iterating till the range +for i in range(0, n): + ele = int(input()) + + lst.append(ele) # adding the element +def largest(lst,n): + # Initialize maximum element + max = lst[0] + for i in range(1, n): + if lst[i] > max: + max = lst[i] + return max +Ans = largest(lst,n) +print ("Largest in given array is",Ans) diff --git a/left rotate array using c b/left rotate array using c new file mode 100644 index 00000000..ade267a3 --- /dev/null +++ b/left rotate array using c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(){ +int x,d,n,i,a[1000000],j; + +scanf("%d",&n); + +scanf("%d",&d); + +for(i=0;i +#include + +int main() +{ + char Str[1000]; + int i; + + printf("Enter the String: "); + scanf("%s", Str); + + for (i = 0; Str[i] != '\0'; ++i); + + printf("Length of Str is %d", i); + + return 0; +} diff --git a/lengthOfString.py b/lengthOfString.py new file mode 100644 index 00000000..38c8b0fc --- /dev/null +++ b/lengthOfString.py @@ -0,0 +1,3 @@ +ans = input("Enter a string to find the length: ") +fin = len(ans) +print(fin) diff --git a/length_of_string b/length_of_string new file mode 100644 index 00000000..77f67220 --- /dev/null +++ b/length_of_string @@ -0,0 +1,2 @@ +sl = str(input("Enter string:")) +print('Length of',sl,'is',len(sl)) diff --git a/linear search b/linear search new file mode 100644 index 00000000..412fdcf7 --- /dev/null +++ b/linear search @@ -0,0 +1,18 @@ +def linear_search(alist, key): + """Return index of key in alist. Return -1 if key not present.""" + for i in range(len(alist)): + if alist[i] == key: + return i + return -1 + + +alist = input('Enter the list of numbers: ') +alist = alist.split() +alist = [int(x) for x in alist] +key = int(input('The number to search for: ')) + +index = linear_search(alist, key) +if index < 0: + print('{} was not found.'.format(key)) +else: + print('{} was found at index {}.'.format(key, index)) diff --git a/linkedlist.c b/linkedlist.c new file mode 100644 index 00000000..a410d87a --- /dev/null +++ b/linkedlist.c @@ -0,0 +1,233 @@ +#include +#include + +struct node { + int data; + struct node *link; + +}; +struct node *head,*newnode,*temp; + +int main(){ + int i,n; + + head =0; + printf("enter number of nodes :"); + scanf("%d",&n); + for(i=0;idata); + newnode->link=0; + + if(head ==0){ + + head =temp=newnode; + } + else{ + temp->link=newnode; + temp=newnode; + } + + + } + temp=head; + while(temp!=0){ + + printf("\t%d->",temp->data); + temp=temp->link; + } + int ch; + + + printf("\nOperations on singly linked list\n"); + + printf("\n1.Insert node at first"); + printf("\n2.Insert node at last"); + printf("\n3.Insert node at position"); + printf("\n4.Delete Node from any Position"); + printf("\n5.Display List in Reverse"); + printf("\n6.Exit\n"); + + + while (ch!=6) + { + + printf("\nEnter your choice"); + scanf("%d", &ch); + + switch (ch) + { + case 1: + printf("...Inserting node at first\n"); + insbegin();temp=head; + printf("\n list :"); + disp(); + break; + case 2: + printf("...Inserting node at last\n"); + insend();temp=head; + printf("\n list :"); + disp(); + break; + case 3: + printf("...Inserting node at position\n"); + inspos();temp=head; + printf("\n list :"); + disp(); + break; + case 4: + printf("...Deleting Node from any Position\n"); + del();temp=head; + printf("\n list :"); + disp(); + break; + case 5: + printf("Displaying List in Reverse\n"); + rev();temp=head; + printf("\n list :"); + disp(); + break; + case 6: + printf("Exiting\n"); + return 0; + break; + default: + printf("...Invalid Choice...\n"); + break; + } + + } + + +} +void disp() + + { + while(temp!=0){ + + printf("\t%d->",temp->data); + temp=temp->link; + } + } + + +void insbegin(){ + newnode=(struct node *)malloc(sizeof(struct node)); + printf("enter data to be inserted:"); + scanf("%d",&newnode->data); + newnode->link=0; + temp=head; + if(head ==0){ + + head =temp=newnode; + } + else{ + + newnode->link=temp->link; + head=newnode; + } + +} +void insend(){ + newnode=(struct node *)malloc(sizeof(struct node)); + printf("\nenter data to be inserted at end:"); + scanf("%d",&newnode->data); + newnode->link=0; + temp=head; + while(temp->link!=0){ + temp=temp->link; + } + temp->link=newnode; + temp=newnode; + +} +void inspos(){ + int i,c; + printf("\nenter the position at which node is to be inserted :"); + scanf("%d",&c); + newnode=(struct node *)malloc(sizeof(struct node)); + printf("\nenter data to be inserted :"); + scanf("%d",&newnode->data); + + temp=head; + for(i=1;ilink; + +} +newnode->link=temp->link; + temp->link=newnode; + + + + +} + + +void delbegin(){ + temp = head; + head = head->link; + free(temp); + + + +} + + + + + + +void dellast(){ + struct node *prev; + temp =head ; + while(temp->link!=0){ + prev=temp; + + temp=temp->link; + } + prev->link=0; + free(temp); +} + + + + + + + +void del(){ + int c,i=1 ; + struct node *prev; + printf("\nenter the node you want to delete :"); + scanf("%d",&c); + + temp=head; + while(ilink; + + i++;} + + prev->link=temp->link; + free(temp); +} + + + +void rev(){ + struct node *nextnode,*current,*prev; + prev=0; + current=nextnode=head; + while(nextnode!=0){ + nextnode=nextnode->link; + current->link=prev; + prev=current; + current = nextnode; + + } + head =prev; + +} + diff --git a/linkedlist.cpp b/linkedlist.cpp new file mode 100644 index 00000000..20abeed9 --- /dev/null +++ b/linkedlist.cpp @@ -0,0 +1,45 @@ +#include +using namespace std; + +class Node { +public: + int data; + Node* next; +}; + +// This function prints contents of linked list +// starting from the given node +void printList(Node* n) +{ + while (n != NULL) { + cout << n->data << " "; + n = n->next; + } +} + +// Driver code +int main() +{ + Node* head = NULL; + Node* second = NULL; + Node* third = NULL; + + // allocate 3 nodes in the heap + head = new Node(); + second = new Node(); + third = new Node(); + + head->data = 1; // assign data in first node + head->next = second; // Link first node with second + + second->data = 2; // assign data to second node + second->next = third; + + third->data = 3; // assign data to third node + third->next = NULL; + + printList(head); + + return 0; +} + diff --git a/matrices_multiplication b/matrices_multiplication new file mode 100644 index 00000000..5c5b8779 --- /dev/null +++ b/matrices_multiplication @@ -0,0 +1,16 @@ +A=[[2,3,4],[5,6,2],[1,2,1]] +print("First matrix:") +for x in A: + print(x) +B=[[1,1,2],[2,2,2],[4,0,2]] +print("Second matrix: ") +for x in B: + print(x) +Product=[[0,0,0],[0,0,0],[0,0,0]] +for i in range(len(A)): + for j in range(len(B[0])): + for k in range(len(B)): + Product[i][j]+=A[i][k]*B[k][j] +print("Product of two matrices: ") +for x in Product: + print(x) diff --git a/matrix multiplication b/matrix multiplication new file mode 100644 index 00000000..7ec5e0be --- /dev/null +++ b/matrix multiplication @@ -0,0 +1,43 @@ +// C program to multiply two square matrices. +#include +#define N 4 + +// This function multiplies mat1[][] and mat2[][], +// and stores the result in res[][] +void multiply(int mat1[][N], int mat2[][N], int res[][N]) +{ + int i, j, k; + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) { + res[i][j] = 0; + for (k = 0; k < N; k++) + res[i][j] += mat1[i][k] * mat2[k][j]; + } + } +} + +int main() +{ + int mat1[N][N] = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 }, + { 4, 4, 4, 4 } }; + + int mat2[N][N] = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 }, + { 4, 4, 4, 4 } }; + + int res[N][N]; // To store result + int i, j; + multiply(mat1, mat2, res); + + printf("Result matrix is \n"); + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) + printf("%d ", res[i][j]); + printf("\n"); + } + + return 0; +} diff --git a/matrix multiplication 2D b/matrix multiplication 2D new file mode 100644 index 00000000..311eb90a --- /dev/null +++ b/matrix multiplication 2D @@ -0,0 +1,108 @@ +#include + +// function to get matrix elements entered by the user +void getMatrixElements(int matrix[][10], int row, int column) { + + printf("\nEnter elements: \n"); + + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("Enter a%d%d: ", i + 1, j + 1); + scanf("%d", &matrix[i][j]); + } + } +} + +// function to multiply two matrices +void multiplyMatrices(int first[][10], + int second[][10], + int result[][10], + int r1, int c1, int r2, int c2) { + + // Initializing elements of matrix mult to 0. + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + result[i][j] = 0; + } + } + + // Multiplying first and second matrices and storing it in result + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + for (int k = 0; k < c1; ++k) { + result[i][j] += first[i][k] * second[k][j]; + } + } + } +} + +// function to display the matrix +void display(int result[][10], int row, int column) { + + printf("\nOutput Matrix:\n"); + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("%d ", result[i][j]); + if (j == column - 1) + printf("\n"); + } + } +} + +int main() { + int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2; + printf("Enter rows and column for the first matrix: "); + scanf("%d %d", &r1, &c1); + printf("Enter rows and column for the second matrix: "); + scanf("%d %d", &r2, &c2); + + // Taking input until + // 1st matrix columns is not equal to 2nd matrix row + while (c1 != r2) { + printf("Error! Enter rows and columns again.\n"); + printf("Enter rows and columns for the first matrix: "); + scanf("%d%d", &r1, &c1); + printf("Enter rows and columns for the second matrix: "); + scanf("%d%d", &r2, &c2); + } + + // get elements of the first matrix + getMatrixElements(first, r1, c1); + + // get elements of the second matrix + getMatrixElements(second, r2, c2); + + // multiply two matrices. + multiplyMatrices(first, second, result, r1, c1, r2, c2); + + // display the result + display(result, r1, c2); + + return 0; +} +Output + +Enter rows and column for the first matrix: 2 +3 +Enter rows and column for the second matrix: 3 +2 + +Enter elements: +Enter a11: 2 +Enter a12: -3 +Enter a13: 4 +Enter a21: 53 +Enter a22: 3 +Enter a23: 5 + +Enter elements: +Enter a11: 3 +Enter a12: 3 +Enter a21: 5 +Enter a22: 0 +Enter a31: -3 +Enter a32: 4 + +Output Matrix: +-21 22 +159 179 diff --git a/matriz in python b/matriz in python new file mode 100644 index 00000000..bf61c2d7 --- /dev/null +++ b/matriz in python @@ -0,0 +1,11 @@ +matriz = []; +quantidade_linhas = 2; +quantidade_colunas = 2; + +for l in range(quantidade_linhas): + colunas = []; + for c in range(quantidade_colunas): + quantidade = int(input("Informe o elemento [{}][{}]:".format(l, c))); + colunas.append(quantidade); + + matriz.append(colunas); diff --git a/max_gcd_of_numbers.cpp b/max_gcd_of_numbers.cpp new file mode 100644 index 00000000..5a2aa328 --- /dev/null +++ b/max_gcd_of_numbers.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +int max_gcd(int num) { + int m, res, i, a, cnt; + + m = num; + res = 0; + i = m; + + while (i > 0) { + a = i; + cnt = 0; + while (a <= m) { + cnt += 1; + a += i; + } + if (cnt >= 2) { + res = i; + break; + } + i -= 1; + } + return res; +} + +int main() { + int test_case, num; + cin >> test_case; + for (int i = 0; i < test_case; i++) { + cin >> num; + cout << max_gcd(num) << "\n"; + } + return 0; +} diff --git a/multiplication of matrices b/multiplication of matrices new file mode 100644 index 00000000..0dba24e7 --- /dev/null +++ b/multiplication of matrices @@ -0,0 +1,84 @@ +#include + +// function to get matrix elements entered by the user +void getMatrixElements(int matrix[][10], int row, int column) { + + printf("\nEnter elements: \n"); + + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("Enter a%d%d: ", i + 1, j + 1); + scanf("%d", &matrix[i][j]); + } + } +} + +// function to multiply two matrices +void multiplyMatrices(int first[][10], + int second[][10], + int result[][10], + int r1, int c1, int r2, int c2) { + + // Initializing elements of matrix mult to 0. + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + result[i][j] = 0; + } + } + + // Multiplying first and second matrices and storing it in result + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + for (int k = 0; k < c1; ++k) { + result[i][j] += first[i][k] * second[k][j]; + } + } + } +} + +// function to display the matrix +void display(int result[][10], int row, int column) { + + printf("\nOutput Matrix:\n"); + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("%d ", result[i][j]); + if (j == column - 1) + printf("\n"); + } + } +} + +int main() { + int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2; + printf("Enter rows and column for the first matrix: "); + scanf("%d %d", &r1, &c1); + printf("Enter rows and column for the second matrix: "); + scanf("%d %d", &r2, &c2); + + // Taking input until + // 1st matrix columns is not equal to 2nd matrix row + while (c1 != r2) { + printf("Error! Enter rows and columns again.\n"); + printf("Enter rows and columns for the first matrix: "); + scanf("%d%d", &r1, &c1); + printf("Enter rows and columns for the second matrix: "); + scanf("%d%d", &r2, &c2); + } + + // get elements of the first matrix + getMatrixElements(first, r1, c1); + + // get elements of the second matrix + getMatrixElements(second, r2, c2); + + // multiply two matrices. + multiplyMatrices(first, second, result, r1, c1, r2, c2); + + // display the result + display(result, r1, c2); + + return 0; +} + + diff --git a/new of elements in array b/new of elements in array new file mode 100644 index 00000000..45c0659b --- /dev/null +++ b/new of elements in array @@ -0,0 +1,19 @@ +#include +using namespace std; + +int x,y; +int tabel[3][5] = { + {1, 2, 3, 4, 5}, + {2, 4, 6, 8, 10}, + {3, 6, 9, 12, 15} + }; + +int main () +{ + for (x=0; x<3; x++) + for (y=0; y<5; y++) + { + cout << tabel[x][y] << " "; + } + cout << "\n"; +} diff --git a/patterns b/patterns new file mode 100644 index 00000000..8fed681c --- /dev/null +++ b/patterns @@ -0,0 +1,168 @@ +#include +#include + +void upper_tri(); +void lower_tri(); +void right_upperhalf(); +void right_lowerhalf(); +void pyramid(); + +void main() +{int ch; + while(1) +{ printf("\n"); + printf("Choose pattern\n1.Upper triangular\n2.Lower triangular\n3.Right Upper triangular\n4.Right Lower triangular\n5.Pyramid \t\t"); + scanf("%d",&ch); + switch(ch) + { + case 1:upper_tri(); + break; + case 2:lower_tri(); + break; + case 3:right_upperhalf(); + break; + case 4:right_lowerhalf(); + break; + case 5:pyramid(); + default:exit(0); + } +} +} + + +/* upper + +12345 +1234 +123 +12 +1 + +*/ +void upper_tri() +{ int i,j; +printf("\n"); + for(i=5;i>=1;i--)// Rows + { + for(j=1;j<=i;j++)//Col. + { + printf("%d",j); + + } + printf("\n"); + } + +} + + +/* Lower + +1 +12 +123 +1234 +12345 + +*/ +void lower_tri() +{int i,j; +printf("\n"); + for(i=1;i<=5;i++) + { + for(j=1;j<=i;j++) + { + printf("%d",j); + + } + printf("\n"); + } + +} + + +/* right upperhalf + +12345 + 1234 + 123 + 12 + 1 + +*/ +void right_upperhalf() +{ int i,j,k; +printf("\n"); + for(i=5;i>=1;i--)//rows + { + for(j=i;j<5;j++)// Spaces + { + printf(" "); + + } + for(k=1;k<=i;k++)//cols + { + printf("%d",k); + + } + printf("\n"); + } + +} + +/* right lowerhalf + + 1 + 12 + 123 + 1234 +12345 + + +*/ + +void right_lowerhalf() +{int i,j,k; +printf("\n"); + for(i=1;i<=5;i++)//Rows + { + for(j=i;j<5;j++)// Spaces + { + printf(" "); + + } + for(k=1;k<=i;k++)//Cols. + { + printf("%d",k); + + } + printf("\n"); + } +} + +/* Pyramid + * + *** + ***** + ******* + ********* +*/ + +void pyramid() +{int i,j,k,l=40; +printf("\n"); + for(i=1;i<=5;i++)//Rows + { + for(j=1;j<=l-i;j++)// Spaces + { + printf(" "); + + } + for(k=1;k<=2*i-1;k++)//Cols. + { + printf("*"); + + } + printf("\n"); + } + +} diff --git a/perform To perform arithmetic operations on two integers. b/perform To perform arithmetic operations on two integers. new file mode 100644 index 00000000..e1bd137d --- /dev/null +++ b/perform To perform arithmetic operations on two integers. @@ -0,0 +1,9 @@ +Read two input integers using input() or raw_input(). +Addition operation using + operator, num1 + num2 adds 2 numbers. +Subtraction operation using - operator, num1 - num2 right hand operand from left hand operand. +Multiplication operation using * operator, num1 * num2 multiplies 2 numbers. +Division operation using / operator, num1 / num2 divides left hand operand by right hand operand. +Floor Division operation using // operator, num1 // num2 divides left hand operand by right hand operand, here it removes the values after decimal point. +Modulus % operator when applied returns the remainder when left hand operand is divided by right hand operand num1 % num2. +Exponential operation using ** operator, num1 ** num2 returns value of num1 num2 +Print the result of each operation. diff --git a/perform all arithmetic operations b/perform all arithmetic operations new file mode 100644 index 00000000..ea23e48f --- /dev/null +++ b/perform all arithmetic operations @@ -0,0 +1,39 @@ + +/** + * C program to perform all arithmetic operations + */ + +#include + +int main() +{ + int num1, num2; + int sum, sub, mult, mod; + float div; + + /* + * Input two numbers from user + */ + printf("Enter any two numbers: "); + scanf("%d%d", &num1, &num2); + + /* + * Perform all arithmetic operations + */ + sum = num1 + num2; + sub = num1 - num2; + mult = num1 * num2; + div = (float)num1 / num2; + mod = num1 % num2; + + /* + * Print result of all arithmetic operations + */ + printf("SUM = %d\n", sum); + printf("DIFFERENCE = %d\n", sub); + printf("PRODUCT = %d\n", mult); + printf("QUOTIENT = %f\n", div); + printf("MODULUS = %d", mod); + + return 0; +} diff --git a/persistent Segment trees b/persistent Segment trees new file mode 100644 index 00000000..9a7a9867 --- /dev/null +++ b/persistent Segment trees @@ -0,0 +1,49 @@ +// query l to r range for the no of integers between x and y +#include +using namespace std; +int T = 1; +const int N = 1e6; +const int MX = N; +struct node{ + int l, r, cnt; +}t[100*MX]; +int root[N], a[N]; +int build(int lo, int hi){ + int id = T++; + if(lo == hi) return id; + int mid = (lo+hi)/2; + t[id].l = build(lo, mid); + t[id].r = build(mid+1, hi); + return id; +} +int update(int rt, int lo, int hi, int val){ + int id = T++; + t[id] = t[rt]; t[id].cnt++; + if(lo == hi) return id; + int mid = (lo+hi)/2; + if(val <= mid) t[id].l = update(t[rt].l, lo, mid, val); + else t[id].r = update(t[rt].r, mid+1, hi, val); + return id; +} +int query(int rt, int lo, int hi, int x, int y){ + if(x==lo and y==hi) return t[rt].cnt; + int mid = (lo+hi)/2; + if(y <= mid) return query(t[rt].l, lo, mid, x, y); + else if (x > mid) return query(t[rt].r, mid+1, hi, x, y); + return query(t[rt].l, lo, mid, x, mid) + query(t[rt].r, mid+1, hi, mid+1, y); +} +int main() { + int i, n, q; + cin >> n >> q; + for(i = 0; i < n; i++) cin >> a[i+1]; + root[0] = build(0, MX); + for(i = 1; i <= n; i++){ + root[i] = update(root[i-1], 0, MX, a[i]); + } + while(q--){ + int l, r, x, y; + cin >> l >> r >> x >> y; + cout << query(root[r], 0, MX, x, y) - query(root[l-1], 0, MX, x, y) << endl; + } + return 0; +} diff --git a/plus minus b/plus minus new file mode 100644 index 00000000..5a9ea052 --- /dev/null +++ b/plus minus @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(){ + int t; + float p=0,n=0,z=0; + scanf("%d",&t); + int arr[t]; + for(int i = 0; i < t;i++){ + scanf("%d",&arr[i]); + if(arr[i]>0) + p++; + else if(arr[i]<0) + n++; + else + z++; + } + printf("%.5f\n%.5f\n%.5f",(float)p/t,(float)n/t,(float)z/t); + return 0; +} diff --git a/power of a number in python b/power of a number in python new file mode 100644 index 00000000..59f8573c --- /dev/null +++ b/power of a number in python @@ -0,0 +1,7 @@ +import math + +base_number = float(input("Enter the base number")) +exponent = float(input("Enter the exponent")) + +power = math.pow(base_number,exponent) +print("Power is =",power) diff --git a/power of number b/power of number new file mode 100644 index 00000000..df1bc45c --- /dev/null +++ b/power of number @@ -0,0 +1,35 @@ +using System; +public class RecExercise15 +{ + + public static void Main() + { + int bNum,pwr; + int result; + Console.Write("\n\n Recursion : Calculate power of any number :\n"); + Console.Write("------------------------------------------------\n"); + + Console.Write(" Input the base value : "); + bNum = Convert.ToInt32(Console.ReadLine()); + + Console.Write(" Input the exponent : "); + pwr = Convert.ToInt32(Console.ReadLine()); + + result=CalcuOfPower(bNum,pwr);//called the function CalcuOfPower + + Console.Write(" The value of {0} to the power of {1} is : {2} \n\n",bNum,pwr,result); + } + +public static int CalcuOfPower( int x,int y) + { + if (y == 0) + return 1; + else + return x * CalcuOfPower( x, y - 1 ); + } +} + +Copy +Sample Output: + + Recursion diff --git a/power.py b/power.py new file mode 100644 index 00000000..cf73cd50 --- /dev/null +++ b/power.py @@ -0,0 +1,4 @@ +base = int(input("Enter your base number: ")) +power = int(input("Enter your power/exponent: ")) + +print(base**power) diff --git a/power_of_number b/power_of_number new file mode 100644 index 00000000..859177b5 --- /dev/null +++ b/power_of_number @@ -0,0 +1,7 @@ +import math + +base_number = float(input("Enter the base number: ")) +exponent = float(input("Enter the exponent: ")) + +power = math.pow(base_number,exponent) +print("Power is =",power) diff --git a/pressmebuttongame in c b/pressmebuttongame in c new file mode 100644 index 00000000..2ea417d3 --- /dev/null +++ b/pressmebuttongame in c @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include +union REGS i, o; +int left = 265, top = 250; + +void initialize_graphics_mode() +{ + int gd = DETECT, gm, error; + + initgraph(&gd,&gm,"C:\\TC\\BGI"); + + error = graphresult(); + + if (error != grOk) + { + perror("Error "); + printf("Press any key to exit...\n"); + getch(); + exit(EXIT_FAILURE); + } +} + +void showmouseptr() +{ + i.x.ax = 1; + int86(0x33, &i, &o); +} + +void hidemouseptr() +{ + i.x.ax = 2; + int86(0x33, &i, &o); +} + +void getmousepos(int *x,int *y) +{ + i.x.ax = 3; + int86(0x33, &i, &o); + + *x = o.x.cx; + *y = o.x.dx; +} + +void draw_bar() +{ + hidemouseptr(); + setfillstyle(SOLID_FILL,CYAN); + bar(190,180,450,350); + showmouseptr(); +} + +void draw_button(int x, int y) +{ + hidemouseptr(); + setfillstyle(SOLID_FILL,MAGENTA); + bar(x,y,x+100,y+30); + moveto(x+5,y); + setcolor(YELLOW); + outtext("Press me"); + showmouseptr(); +} + +void draw() +{ + settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); + outtextxy(155,451,"www.programmingsimplified.com"); + setcolor(BLUE); + rectangle(0,0,639,450); + setcolor(RED); + outtextxy(160,25,"Try to press the \"Press me\" button"); + outtextxy(210,50,"Press escape key to exit"); + setfillstyle(XHATCH_FILL,GREEN); + setcolor(BLUE); + bar(1,1,75,449); + bar(565,1,638,449); + showmouseptr(); + draw_bar(); + draw_button(left,top); +} + +void initialize() +{ + initialize_graphics_mode(); + + if( !initmouse() ) + { + closegraph(); + printf("Unable to initialize the mouse"); + printf("Press any key to exit...\n"); + getch(); + exit(EXIT_SUCCESS); + } + + draw(); +} + +int initmouse() +{ + i.x.ax = 0; + int86(0x33, &i, &o); + return ( o.x.ax ); +} + +void get_input() +{ + int x, y; + + while(1) + { + getmousepos(&x,&y); + + /* mouse pointer in left of button */ + + if( x >= (left-3) && y >= (top-3) && y <= (top+30+3) && x < left ) + { + draw_bar(); + left = left + 4; + + if (left > 350) + left = 190; + + draw_button(left,top); + } + + /* mouse pointer in right of button */ + + else if (x<=(left+100+3)&&y>=(top-3)&&y<=(top+30+3)&&x>(left+100)) + { + draw_bar(); + left = left - 4; + + if (left < 190) + left = 350; + + draw_button(left,top); + } + + /* mouse pointer above button */ + + else if(x>(left-3) && y>=(top-3) && y<(top) && x<= (left+100+3)) + { + draw_bar(); + top = top + 4; + + if (top > 320) + top = 180; + + draw_button(left,top); + } + + /* mouse pointer below button */ + + else if (x>(left-3)&&y>(top+30)&&y<=(top+30+3)&&x<=(left+100+3)) + { + draw_bar(); + top = top - 4; + + if (top < 180) + top = 320; + + draw_button(left,top); + } + + if (kbhit()) + { + if (getkey() == 1) + exit(EXIT_SUCCESS); + } + } +} + +int getkey() +{ + i.h.ah = 0; + int86(22, &i, &o); + + return( o.h.ah ); +} + +main() +{ + initialize(); + + get_input(); + return 0; +} diff --git a/prime_number_inrange_Manas b/prime_number_inrange_Manas new file mode 100644 index 00000000..06f5afdf --- /dev/null +++ b/prime_number_inrange_Manas @@ -0,0 +1,15 @@ +# Python program to print all +# prime number in an interval +#number should be greater than 1 +start = 11 +end = 25 + +for i in range(start,end): +if i>1: + for j in range(2,i): + if(i % j==0): + break + else: + print(i) + +# Code contributed by Manas diff --git a/print Fibonnaci Series using C b/print Fibonnaci Series using C new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/print Fibonnaci Series using C @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/print a factorial number in c b/print a factorial number in c new file mode 100644 index 00000000..44bca104 --- /dev/null +++ b/print a factorial number in c @@ -0,0 +1,13 @@ +void main() +{ + int a, b, n, fact; + int i, j; + printf("Enter the number"); + scanf("%d %d",&a,&b); + for(i=0;i +using namespace std; + +void print_subs(string input, string output) { + if (input.length() == 0) { + cout << output << endl; + return; + } + + print_subs(input.substr(1), output); + print_subs(input.substr(1), output + input[0]); +} + +int main() { + string input; + cin >> input; + string output = ""; + print_subs(input, output); +} diff --git a/printing the reverse of the array b/printing the reverse of the array new file mode 100644 index 00000000..4892daf6 --- /dev/null +++ b/printing the reverse of the array @@ -0,0 +1,14 @@ +#include +using namespace std; + +int main() +{ + int A[10] = {1,2,3,4,5,6,7,8,9,10}; + + cout<<"the reversed array is : "; + for(int i=9;i>=0;i--) + { + cout< +#include +#include + +int generateRandomNumber(int n) +{ + srand(time(NULL)); //srand takes seed as an input and is defined inside stdlib.h + return rand() % n; +} +//Create Rock, Paper & Scissors Game +// Player 1: rock +// Player 2 (computer): scissors -->player 1 gets 1 point + +// rock vs scissors - rock wins +// paper vs scissors - scissors wins +// paper vs rock - paper wins +// Write a C program to allow user to play this game three times with computer. Log the scores of computer and the player. Display the name of the winner at the end +// Notes: You have to display name of the player during the game. Take users name as an input from the user. + +int main() +{ + printf("The random number between 0 to 5 is %d\n", generateRandomNumber(5)); + return 0; +} diff --git a/program to calculate the no. of vowels and consonants b/program to calculate the no. of vowels and consonants new file mode 100644 index 00000000..5c597a7b --- /dev/null +++ b/program to calculate the no. of vowels and consonants @@ -0,0 +1,32 @@ +#include +#include +#include + +#define str_size 100 +int main() +{ + char str[str_size]; + int i, len, vowel, cons; + + printf("Input the string : "); + fgets(str, sizeof str, stdin); + + vowel = 0; + cons = 0; + len = strlen(str); + + for(i=0; i='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')) + { + cons++; + } + } + printf("\nThe total number of vowel in the string is : %d\n", vowel); + printf("The total number of consonant in the string is : %d\n\n", cons); +} diff --git a/program to calculate the power of num b/program to calculate the power of num new file mode 100644 index 00000000..e019a0a9 --- /dev/null +++ b/program to calculate the power of num @@ -0,0 +1,16 @@ +#include +int main() { + int b, ex; + long long result = 1; + printf("Enter a base number: "); + scanf("%d", &b); + printf("Enter an exponent: "); + scanf("%d", &ex); + + while (ex != 0) { + result *= b; + --exp; + } + printf("Answer = %lld", result); + return 0; +} diff --git a/program to find factorial of number b/program to find factorial of number new file mode 100644 index 00000000..ae621fea --- /dev/null +++ b/program to find factorial of number @@ -0,0 +1,14 @@ +public class Factorial { + + public static void main(String[] args) { + + int num = 10; + long factorial = 1; + for(int i = 1; i <= num; ++i) + { + // factorial = factorial * i; + factorial *= i; + } + System.out.printf("Factorial of %d = %d", num, factorial); + } +} diff --git a/program to find largest element in array b/program to find largest element in array new file mode 100644 index 00000000..b1d0629c --- /dev/null +++ b/program to find largest element in array @@ -0,0 +1,46 @@ +#include + +int main() +{ + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int a[50], size, i, big, small; + + printf("\nEnter the size of the array: "); + scanf("%d", &size); + + printf("\n\nEnter the %d elements of the array: \n\n", size); + for(i = 0; i < size; i++) + scanf("%d", &a[i]); + + big = a[0]; + /* + from 2nd element to the last element + find the bigger element than big and + update the value of big + */ + for(i = 1; i < size; i++) + { + if(big < a[i]) + { + big = a[i]; + } + } + printf("\n\nThe largest element is: %d", big); + + small = a[0]; + /* + from 2nd element to the last element + find the smaller element than small and + update the value of small + */ + for(i = 1; i < size; i++) + { + if(small>a[i]) + { + small = a[i]; + } + } + printf("\n\nThe smallest element is: %d", small); + printf("\n\n\t\t\tCoding is Fun !\n\n\n"); + return 0; +} diff --git a/program to find length of a given string b/program to find length of a given string new file mode 100644 index 00000000..f2edae7d --- /dev/null +++ b/program to find length of a given string @@ -0,0 +1,3 @@ +# testing len() +str1 = "Welcome to python" +print("The length of the string is :", len(str1)) diff --git a/program to find length of a string b/program to find length of a string new file mode 100644 index 00000000..a20fd7c3 --- /dev/null +++ b/program to find length of a string @@ -0,0 +1,10 @@ +#include +int main() { + char s[] = "Programming is fun"; + int i; + + for (i = 0; s[i] != '\0'; ++i); + + printf("Length of the string: %d", i); + return 0; +} diff --git a/program to find length of a string in java b/program to find length of a string in java new file mode 100644 index 00000000..49ea7f0a --- /dev/null +++ b/program to find length of a string in java @@ -0,0 +1,12 @@ +public class LengthExample2 { + public static void main(String[] args) { + String str = "Javatpoint"; + if(str.length()>0) { + System.out.println("String is not empty and length is: "+str.length()); + } + str = ""; + if(str.length()==0) { + System.out.println("String is empty now: "+str.length()); + } + } +} diff --git a/program to print Fibonacci series in python b/program to print Fibonacci series in python new file mode 100644 index 00000000..9f6b1b3d --- /dev/null +++ b/program to print Fibonacci series in python @@ -0,0 +1,23 @@ +# Program to display the Fibonacci sequence up to n-th term + +nterms = int(input("How many terms? ")) + +# first two terms +n1, n2 = 0, 1 +count = 0 + +# check if the number of terms is valid +if nterms <= 0: + print("Please enter a positive integer") +elif nterms == 1: + print("Fibonacci sequence upto",nterms,":") + print(n1) +else: + print("Fibonacci sequence:") + while count < nterms: + print(n1) + nth = n1 + n2 + # update values + n1 = n2 + n2 = nth + count += 1 diff --git a/program to print the ASCII value of a character b/program to print the ASCII value of a character new file mode 100644 index 00000000..a73b9c24 --- /dev/null +++ b/program to print the ASCII value of a character @@ -0,0 +1,12 @@ +#include +int main() { + char c; + printf("Enter a character: "); + scanf("%c", &c); + + // %d displays the integer value of a character + // %c displays the actual character + printf("ASCII value of %c = %d", c, c); + + return 0; +} diff --git a/program to reverse an array b/program to reverse an array new file mode 100644 index 00000000..5ec9fb89 --- /dev/null +++ b/program to reverse an array @@ -0,0 +1,14 @@ +# Python program to reverse an array + +def reverseList(A, start, end): + while start < end: + A[start], A[end] = A[end], A[start] + start += 1 + end -= 1 + +# Input To check above function +A = [1, 2, 3, 4, 5, 6] +print(A) +reverseList(A, 0, 5) +print("Reversed list is") +print(A) diff --git a/python program to find leap year between two given years b/python program to find leap year between two given years new file mode 100644 index 00000000..87af4c8f --- /dev/null +++ b/python program to find leap year between two given years @@ -0,0 +1,14 @@ +#Print a string about the program +print ("Print leap year between two given years") +#Get the input start and end year from user +print ("Enter start year") +startYear = int(input()) +print ("Enter last year") +endYear = int(input()) + +print ("List of leap years:") +#loop through between the start and end year +for year in range(startYear, endYear): + #check if the year is a Leap year if yes print + if (0 == year % 4) and (0 != year % 100) or (0 == year % 400): + print (year) diff --git a/python program to print ascii value b/python program to print ascii value new file mode 100644 index 00000000..29316095 --- /dev/null +++ b/python program to print ascii value @@ -0,0 +1,5 @@ +# ASCII Value of Character + +c = 'g' +# print the ASCII value of assigned character in c +print("The ASCII value of '" + c + "' is", ord(c)) diff --git a/python-pyramid-pattern.py b/python-pyramid-pattern.py new file mode 100644 index 00000000..6c9ceaae --- /dev/null +++ b/python-pyramid-pattern.py @@ -0,0 +1,36 @@ + +play_arrow + +brightness_4 +# Python 3.x code to demonstrate star pattern + +# Function to demonstrate printing pattern triangle +def triangle(n): + + # number of spaces + k = 2*n - 2 + + # outer loop to handle number of rows + for i in range(0, n): + + # inner loop to handle number spaces + # values changing acc. to requirement + for j in range(0, k): + print(end=" ") + + # decrementing k after each loop + k = k - 1 + + # inner loop to handle number of columns + # values changing acc. to outer loop + for j in range(0, i+1): + + # printing stars + print("* ", end="") + + # ending line after each row + print("\r") + +# Driver Code +n = 5 +triangle(n) diff --git a/rev_arr.py b/rev_arr.py new file mode 100644 index 00000000..c1d5ff41 --- /dev/null +++ b/rev_arr.py @@ -0,0 +1,14 @@ +''' +Program to reverse the array +Created by Shouvik Dutta +Pls Merge my PR +''' +def rev(l): + ans=[] + for i in range(len(l)-1,-1,-1): + ans.append(l[i]) + return ans +if name==main: + l=[1,2,3,4,5,6] + ans=rev(l) + print(*ans) diff --git a/reverse an array b/reverse an array new file mode 100644 index 00000000..c34ee78d --- /dev/null +++ b/reverse an array @@ -0,0 +1,46 @@ +// Iterative C++ program to reverse an array +#include +using namespace std; + +/* Function to reverse arr[] from start to end*/ +void rvereseArray(int arr[], int start, int end) +{ + while (start < end) + { + int temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + start++; + end--; + } +} + +/* Utility function to print an array */ +void printArray(int arr[], int size) +{ + for (int i = 0; i < size; i++) + cout << arr[i] << " "; + + cout << endl; +} + +/* Driver function to test above functions */ +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6}; + + int n = sizeof(arr) / sizeof(arr[0]); + + // To print original array + printArray(arr, n); + + // Function calling + rvereseArray(arr, 0, n-1); + + cout << "Reversed array is" << endl; + + // To print the Reversed array + printArray(arr, n); + + return 0; +} diff --git a/reverse an array. b/reverse an array. new file mode 100644 index 00000000..53823671 --- /dev/null +++ b/reverse an array. @@ -0,0 +1,23 @@ +//reverse of an array +#include +int main() +{ + int a[5],i,j,t; + printf("enter 6 values"); + for(i=0;i<=4;i++) + { + scanf("%d",&a[i]); + } + for(i=0,j=4;i"); + for(i=0;i<=4;i++) + { + printf("%d\n",a[i]); + } + return 0; +} diff --git a/rogram to convert binary to decimal number b/rogram to convert binary to decimal number new file mode 100644 index 00000000..f9030cb5 --- /dev/null +++ b/rogram to convert binary to decimal number @@ -0,0 +1,37 @@ +// Java program to convert +// binary to decimal + +// Function to convert +// binary to decimal +class GFG { + static int binaryToDecimal(int n) + { + int num = n; + int dec_value = 0; + + // Initializing base + // value to 1, i.e 2^0 + int base = 1; + + int temp = num; + while (temp > 0) { + int last_digit = temp % 10; + temp = temp / 10; + + dec_value += last_digit * base; + + base = base * 2; + } + + return dec_value; + } + + // Driver Code + public static void main(String[] args) + { + int num = 10101001; + System.out.println(binaryToDecimal(num)); + } +} + +// This code is contributed by mits. diff --git a/series in c b/series in c new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/series in c @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/show basic Arithmetic Operations and role of Typecasting b/show basic Arithmetic Operations and role of Typecasting new file mode 100644 index 00000000..4226dc5b --- /dev/null +++ b/show basic Arithmetic Operations and role of Typecasting @@ -0,0 +1,23 @@ +#include + +int main() +{ + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int a, b, add, subtract, multiply; + float divide; + + printf("Enter two integers: \n"); + scanf("%d%d", &a, &b); + + add = a+b; + subtract = a-b; + multiply = a*b; + divide = a/b; + + printf("\nAddition of the numbers = %d\n", add); + printf("Subtraction of 2nd number from 1st = %d\n", subtract); + printf("Multiplication of the numbers = %d\n", multiply); + printf("Dividing 1st number from 2nd = %f\n", divide); + printf("\n\n\t\t\tCoding is Fun !\n\n\n"); + return 0; +} diff --git a/smallest element in array b/smallest element in array new file mode 100644 index 00000000..4e665c58 --- /dev/null +++ b/smallest element in array @@ -0,0 +1,17 @@ +#include + +int main() { + int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; + int loop, smallest; + + smallest = array[0]; + + for(loop = 1; loop < 10; loop++) { + if( smallest > array[loop] ) + smallest = array[loop]; + } + + printf("Smallest element of array is %d", smallest); + + return 0; +} diff --git a/smallest element in the array.c b/smallest element in the array.c new file mode 100644 index 00000000..441e2ce4 --- /dev/null +++ b/smallest element in the array.c @@ -0,0 +1,26 @@ +#include + +int main() { + int a[30], i, num, smallest; + + printf("\nEnter no of elements :"); + scanf("%d", &num); + + //Read n elements in an array + for (i = 0; i < num; i++) + scanf("%d", &a[i]); + + //Consider first element as smallest + smallest = a[0]; + + for (i = 0; i < num; i++) { + if (a[i] < smallest) { + smallest = a[i]; + } + } + + // Print out the Result + printf("\nSmallest Element : %d", smallest); + + return (0); +} diff --git a/snake_game.c b/snake_game.c new file mode 100644 index 00000000..2b556237 --- /dev/null +++ b/snake_game.c @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include +#include + +check(); +end(); +win(); +int m[500],n[500],con=20; +clock_t start,stop; + void main(void) +{ + +int gd=DETECT,gm,ch,maxx,maxy,x=13,y=14,p,q,spd=100; + +initgraph(&gd,&gm,"..\bgi"); + +setcolor(WHITE); +settextstyle(3,0,6); +outtextxy(200,2," SNAKE 2 "); +settextstyle(6,0,2); +outtextxy(20,80," Use Arrow Keys To Direct The Snake "); +outtextxy(20,140," Avoid The Head Of Snake Not To Hit Any Part Of Snake +"); +outtextxy(20,160," Pick The Beats Untill You Win The Game "); +outtextxy(20,200," Press 'Esc' Anytime To Exit "); +outtextxy(20,220," Press Any Key To Continue "); +ch=getch(); +if(ch==27) exit(0); +cleardevice(); +maxx=getmaxx(); +maxy=getmaxy(); + +randomize(); + +p=random(maxx); +int temp=p%13; +p=p-temp; +q=random(maxy); +temp=q%14; +q=q-temp; + + + + start=clock(); +int a=0,i=0,j,t; +while(1) +{ + + setcolor(WHITE); + setfillstyle(SOLID_FILL,con+5); + circle(p,q,5); + floodfill(p,q,WHITE); + + if( kbhit() ) + { + ch=getch(); if(ch==0) ch=getch(); + if(ch==72&& a!=2) a=1; + if(ch==80&& a!=1) a=2; + if(ch==75&& a!=4) a=3; + if(ch==77&& a!=3) a=4; + } + else + { + if(ch==27 + ) break; + } + + if(i<20){ + m[i]=x; + n[i]=y; + i++; + } + + if(i>=20) + + { + for(j=con;j>=0;j--){ + m[1+j]=m[j]; + n[1+j]=n[j]; + } + m[0]=x; + n[0]=y; + + setcolor(WHITE); + setfillstyle(SOLID_FILL,con); + circle(m[0],n[0],8); + floodfill(m[0],n[0],WHITE); + + setcolor(WHITE); + for(j=1;j=5) spd=spd-5; else spd=5; + if(con>490) win(); + p=random(maxx); temp=p%13; p=p-temp; + q=random(maxy); temp=q%14; q=q-temp; + } + if(a==1) y = y-14; if(y<0) { temp=maxy%14;y=maxy-temp;} + if(a==2) y = y+14; if(y>maxy) y=0; + if(a==3) x = x-13; if(x<0) { temp=maxx%13;x=maxx-temp;} + if(a==4) x = x+13; if(x>maxx) x=0; + if(a==0){ y = y+14 ; x=x+13; } + } + + } + + +check(){ + int a; + for(a=1;a +int main() +{ + char s[100]; + int l; + scanf("%s",&s); + l=strlen(s); + printf("%d",l); + return 0; + } + diff --git a/string length in javascript.js b/string length in javascript.js new file mode 100644 index 00000000..ff0a9037 --- /dev/null +++ b/string length in javascript.js @@ -0,0 +1,12 @@ + + + Program to find string length + + + + + + diff --git a/sum of elements b/sum of elements new file mode 100644 index 00000000..89f34037 --- /dev/null +++ b/sum of elements @@ -0,0 +1,13 @@ +#include + +int main() { +int array [10] ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; +int sum,loop; + +sum=0 ; +for{loop = 9; loop>=0; loop--) { +sum = sum +array[loop]; +} +print("sum of array is %d.",sum) +return 0; +} diff --git a/sum of elements in a array b/sum of elements in a array new file mode 100644 index 00000000..b8f40776 --- /dev/null +++ b/sum of elements in a array @@ -0,0 +1,16 @@ +#include + +int main() { + int arry{10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}: + int sum, loop; + + sum = 0; + + for(loop = 9; loop >= 0; loop--) { + sum = sum + array[loop]; + } + + printf("sum of array is %d.", sum); + + return 0; +} diff --git a/sum of elements in a array... b/sum of elements in a array... new file mode 100644 index 00000000..b8f40776 --- /dev/null +++ b/sum of elements in a array... @@ -0,0 +1,16 @@ +#include + +int main() { + int arry{10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}: + int sum, loop; + + sum = 0; + + for(loop = 9; loop >= 0; loop--) { + sum = sum + array[loop]; + } + + printf("sum of array is %d.", sum); + + return 0; +} diff --git a/sum of elements in a array.... b/sum of elements in a array.... new file mode 100644 index 00000000..b8f40776 --- /dev/null +++ b/sum of elements in a array.... @@ -0,0 +1,16 @@ +#include + +int main() { + int arry{10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}: + int sum, loop; + + sum = 0; + + for(loop = 9; loop >= 0; loop--) { + sum = sum + array[loop]; + } + + printf("sum of array is %d.", sum); + + return 0; +} diff --git a/sum of elements in a array..... b/sum of elements in a array..... new file mode 100644 index 00000000..b8f40776 --- /dev/null +++ b/sum of elements in a array..... @@ -0,0 +1,16 @@ +#include + +int main() { + int arry{10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}: + int sum, loop; + + sum = 0; + + for(loop = 9; loop >= 0; loop--) { + sum = sum + array[loop]; + } + + printf("sum of array is %d.", sum); + + return 0; +} diff --git a/sum of elements in a array...... b/sum of elements in a array...... new file mode 100644 index 00000000..b8f40776 --- /dev/null +++ b/sum of elements in a array...... @@ -0,0 +1,16 @@ +#include + +int main() { + int arry{10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}: + int sum, loop; + + sum = 0; + + for(loop = 9; loop >= 0; loop--) { + sum = sum + array[loop]; + } + + printf("sum of array is %d.", sum); + + return 0; +} diff --git a/sum of elements in an array b/sum of elements in an array new file mode 100644 index 00000000..b8f40776 --- /dev/null +++ b/sum of elements in an array @@ -0,0 +1,16 @@ +#include + +int main() { + int arry{10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}: + int sum, loop; + + sum = 0; + + for(loop = 9; loop >= 0; loop--) { + sum = sum + array[loop]; + } + + printf("sum of array is %d.", sum); + + return 0; +} diff --git a/sum of elements in an array. b/sum of elements in an array. new file mode 100644 index 00000000..b8f40776 --- /dev/null +++ b/sum of elements in an array. @@ -0,0 +1,16 @@ +#include + +int main() { + int arry{10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}: + int sum, loop; + + sum = 0; + + for(loop = 9; loop >= 0; loop--) { + sum = sum + array[loop]; + } + + printf("sum of array is %d.", sum); + + return 0; +} diff --git a/sum of elements in the array b/sum of elements in the array new file mode 100644 index 00000000..b8f40776 --- /dev/null +++ b/sum of elements in the array @@ -0,0 +1,16 @@ +#include + +int main() { + int arry{10} = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}: + int sum, loop; + + sum = 0; + + for(loop = 9; loop >= 0; loop--) { + sum = sum + array[loop]; + } + + printf("sum of array is %d.", sum); + + return 0; +} diff --git a/sum of even number b/sum of even number new file mode 100644 index 00000000..0f54ef56 --- /dev/null +++ b/sum of even number @@ -0,0 +1,28 @@ + +# python 3 implementation to +# find sum of even numbers +# at even indices + +# Function to calculate sum +# of even numbers at even indices +def sum_even_and_even_index(arr,n): + + i = 0 + sum = 0 + +# calculating sum of even +# number at even index + for i in range(0,n,2): + if (arr[i] % 2 == 0) : + sum += arr[i] + + # required sum + return sum + + +# Driver program to test above +arr = [5, 6, 12, 1, 18, 8] +n = len(arr) +print("Sum of even numbers at ", + "even indices is ", + sum_even_and_even_index(arr, n)) diff --git a/sumofarraynumbers.c b/sumofarraynumbers.c new file mode 100644 index 00000000..a0c4900f --- /dev/null +++ b/sumofarraynumbers.c @@ -0,0 +1,46 @@ +#include +#include + +int main (){ +int i ,j, a,b,n,arr[20][20],h,v; +int count=0; + +scanf("%d",&h); + +scanf("%d",&v); + +printf("enter the elements"); +for (i=0;i +#include +int main() +{ + int n, i, flag = 1; + + // Ask user for input + printf("Enter a number: \n"); + + // Store input number in a variable + scanf("%d", &n); + + // Iterate from 2 to n/2 + for (i = 2; i <= sqrt(n); i++) { + + // If n is divisible by any number between + // 2 and n/2, it is not prime + if (n % i == 0) { + flag = 0; + break; + } + } + + if(n<=1) + flag=0; + else if(n==2) + flag=1; + + if (flag == 1) { + printf("%d is a prime number", n); + } + else { + printf("%d is not a prime number", n); + } + + return 0; +} diff --git a/to find factorial of number/c# b/to find factorial of number/c# new file mode 100644 index 00000000..b9984f17 --- /dev/null +++ b/to find factorial of number/c# @@ -0,0 +1,22 @@ +using System; + +class Test { + // method to find factorial + // of given number + static int factorial(int n) + { + if (n == 0) + return 1; + + return n * factorial(n - 1); + } + + + public static void Main() + { + int num = 5; + Console.WriteLine("Factorial of " + + num + " is " + factorial(5)); + } +} + diff --git a/to find factorial of number/in java b/to find factorial of number/in java index ae621fea..4d431984 100644 --- a/to find factorial of number/in java +++ b/to find factorial of number/in java @@ -1,14 +1,18 @@ -public class Factorial { - - public static void main(String[] args) { - - int num = 10; - long factorial = 1; - for(int i = 1; i <= num; ++i) - { - // factorial = factorial * i; - factorial *= i; - } - System.out.printf("Factorial of %d = %d", num, factorial); - } -} +class Factorial { + + int factorial(int n) + { + + // single line to find factorial + return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); + } + + public static void main(String args[]) + { + Factorial obj = new Factorial(); + int num = 5; + System.out.println( + "Factorial of " + num + + " is " + obj.factorial(num)); + } +} diff --git a/to find factorial of number/in php b/to find factorial of number/in php new file mode 100644 index 00000000..a72922ab --- /dev/null +++ b/to find factorial of number/in php @@ -0,0 +1,16 @@ + diff --git a/to find the largest element in an array b/to find the largest element in an array new file mode 100644 index 00000000..75ccc198 --- /dev/null +++ b/to find the largest element in an array @@ -0,0 +1,14 @@ +# largest function +def largest(arr,n): + #maximum element + max = arr[0] + # traverse the whole loop + for i in range(1, n): + if arr[i] > max: + max = arr[i] + return max +# Driver Code +arr = [23,1,32,67,2,34,12] +n = len(arr) +Ans = largest(arr,n) +print ("Largest element given in array is",Ans) diff --git a/to find the length of a string b/to find the length of a string new file mode 100644 index 00000000..e876e50f --- /dev/null +++ b/to find the length of a string @@ -0,0 +1,8 @@ +# User inputs the string and it gets stored in variable str +str = input("Enter a string: ") + +# counter variable to count the character in a string +counter = 0 +for s in str: + counter = counter+1 +print("Length of the input string is:", counter) diff --git a/total number of vowels and consonants in a string Program_java b/total number of vowels and consonants in a string Program_java new file mode 100644 index 00000000..d06700c1 --- /dev/null +++ b/total number of vowels and consonants in a string Program_java @@ -0,0 +1,28 @@ +public class CountVowelConsonant { + public static void main(String[] args) { + + //Counter variable to store the count of vowels and consonant + int vCount = 0, cCount = 0; + + //Declare a string + String str = "This is a really simple sentence"; + + //Converting entire string to lower case to reduce the comparisons + str = str.toLowerCase(); + + for(int i = 0; i < str.length(); i++) { + //Checks whether a character is a vowel + if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') { + //Increments the vowel counter + vCount++; + } + //Checks whether a character is a consonant + else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') { + //Increments the consonant counter + cCount++; + } + } + System.out.println("Number of vowels: " + vCount); + System.out.println("Number of consonants: " + cCount); + } +} diff --git a/using if else control instructions b/using if else control instructions new file mode 100644 index 00000000..cb1c16fb --- /dev/null +++ b/using if else control instructions @@ -0,0 +1,15 @@ +#include +int main() +{ + int n; + + printf("Enter a number\n"); + scanf("%d", &n); + + if (n > 0) + printf("Greater than zero.\n"); + else + printf("Less than or equal to zero.\n"); + + return 0; +} diff --git a/using structures in C programming b/using structures in C programming new file mode 100644 index 00000000..0abfb534 --- /dev/null +++ b/using structures in C programming @@ -0,0 +1,20 @@ +#include +#include +struct game +{ + char game_name[50]; + int number_of_players; +}; // Note the semicolon + +int main() +{ + struct game g; + + strcpy(g.game_name, "Cricket"); + g.number_of_players = 11; + + printf("Name of game: %s\n", g.game_name); + printf("Number of players: %d\n", g.number_of_players); + + return 0; +} diff --git a/valleys counting b/valleys counting new file mode 100644 index 00000000..95496ec1 --- /dev/null +++ b/valleys counting @@ -0,0 +1,36 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(){ + int n,i,u=1,d=-1,sum=0,countv=0,counth=0; + + scanf("%d",&n); + char steps[n]; + + scanf("%s",steps); + + for(i=0;i=0 and time<12: + speak("good morning sir") + elif time>=12 and time<18: + speak("good afternoon sir") + else: + speak("good evening sir") + speak( "I am your virtual assistant jarvis . please tell me how may i help you") + + +def takeCommand(): + r=sr.Recognizer() + with sr.Microphone() as source: + print("listening.....") + r.pause_threshold=1 + r.adjust_for_ambient_noise(source, duration=5) + audio=r.listen(source) + + try: + print("Recognizing.....") + stat=r.recognize_google(audio, language="en-in") + print(f"User said:> {stat}\n") + + except Exception as e: + #print(e) + + print("Say that again please!!") + return "None" + return stat +def sendemail(to,content): + e_mail=lib.smtplib.SMTP('smtp.gmail.com',587) + e_mail.ehlo() + e_mail.starttls() + e_mail.login('tggv9986@gmail.com', 'mypass') + e_mail.sendemail('tggv9986@gmail.com',to,content) + e_mail.close() +if __name__ == "__main__": + wishme() + while True: + stat=takeCommand().lower() + + if 'wikipedia' in stat: + speak('searching wikipedia') + stat=stat.replace('wikipedia' , "") + results=wikipedia.summary(stat, sentences=3) + speak('According to wikipedia') + print(results) + speak(results) + + elif 'open youtube' in stat: + speak('okay') + webbrowser.open('youtube.com') + + elif 'my youtube channel' in stat: + speak("okay") + webbrowser.open('youtube.com/c/thegreatgrandvideos') + + elif 'google classroom' in stat: + speak("okay") + webbrowser.open('https://classroom.google.com/c/NjQzNTUyODU0MTFa') + + elif 'google' in stat: + speak("okay") + webbrowser.open('www.google.co.in') + + elif 'play music' in stat: + musicfile='"E:\\YouTube\\NCS Music & Sound Effect' + songs=os.listdir(musicfile) + print(songs) + os.startfile(os.path.join(musicfile ,songs[0])) + + elif'time' in stat: + ntime=datetime.datetime().strftime('%H:%M:%S') + + elif"open vs code" in stat: + vscode='C:\\Users\\ABHISHEK RANJAN\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Visual Studio Code\\Visual Studio Code.lnk' + os.startfile(vscode) + + elif'open filmora' in stat: + filmora="C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Wondershare\\Filmora9\\Wondershare Filmora9.lnk" + os.startfile(filmora) + + elif'open zoom' in stat: + zoomm="C:\\Users\\ABHISHEK RANJAN\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Zoom\\Start Zoom.lnk" + os.startfile(zoomm) + + elif 'open droidcam' in stat: + d_cam="C:\\Users\\ABHISHEK RANJAN\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\DroidCam\\DroidCam Client.lnk" + os.startfile(d_cam) + + elif "what's up" in stat or 'how are you' in stat: + stMsgs = ['Just doing my thing!', 'I am fine!', 'I am nice and full of energy'] + speak(random.choice(stMsgs)) + + elif 'nothing' in stat or 'abort' in stat or 'stop' in stat: + speak('okay') + speak('Bye Sir, have a good day.') + sys.exit() + + elif 'hello' in stat: + speak('Hello Sir') + + + elif'email to ashutosh' in stat: + try: + speak("what should i mail") + mes=takeCommand() + to='ashuranjan567@gmail.com' + sendemail(to, mes) + except Exception as e: + #print(e) + speak("sorry sir email has not been send i committed a mistake") + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/whether a Triangle is Equilateral, Isosceles or Scalene b/whether a Triangle is Equilateral, Isosceles or Scalene new file mode 100644 index 00000000..a995ee18 --- /dev/null +++ b/whether a Triangle is Equilateral, Isosceles or Scalene @@ -0,0 +1,26 @@ +# Function to check if the triangle +# is equilateral or isosceles or scalene +def checkTriangle(x, y, z): + + # _Check for equilateral triangle + if x == y == z: + print("Equilateral Triangle") + + # Check for isoceles triangle + elif x == y or y == z or z == x: + print("Isoceles Triangle") + + # Otherwise scalene triangle + else: + print("Scalene Triangle") + + +# Driver Code + +# Given sides of triangle +x = 8 +y = 7 +z = 9 + +# Function Call +checkTriangle(x, y, z) diff --git a/xepdiem.py b/xepdiem.py new file mode 100644 index 00000000..5e8e02db --- /dev/null +++ b/xepdiem.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + + +definition = { + "0":6, + "1":2, + "2":5, + "3":5, + "4":4, + "5":5, + "6":6, + "7":3, + "8":7, + "9":6, + "+":2, + "-":1, + "=":2 +} + +bienThe = { + "0":[0,6,8,9], + "1":[1,7], + "2":[2,3], + "3":[2,3,5,9], + "4":[4], + "5":[3,5,6,9], + "6":[0,5,6,8,9], + "7":[1,7], + "8":[0,6,8,9], + "9":[0,3,5,6,8,9] +} + +def soDiem(data): + queDiem = 0 + for char in data: + if char in definition : + queDiem = queDiem + definition[char] + return(queDiem) + +def demKiTu(x): + number = [] + for i in data: + try: + if i in definition: + number.append(i) + except: + continue + return(number) + +def soSanh(a,b): + for i in a: + if i in b: + b.remove(i) + if len(b) in [1,2]: + return(True) + else: + return(False) + +print("\t\t\t","Author: Qh3xg3m") + +while True: + print() + data = str(input("\t> Input: ")) + output = [] + for i in bienThe[demKiTu(data)[0]]: + for j in bienThe[demKiTu(data)[2]]: + String = "{0} {1} {2} = {3}".format(i,"+",j,i+j) + if i+j in range(10): + output.append(String) + String = "{0} {1} {2} = {3}".format(i,"-",j,i-j) + if i-j in range(10): + output.append(String) + for i in output: + temp = i.split(" ") + if soDiem(data)==soDiem(i) and soSanh(data,temp)==True: + print("\t",i) + else: + continue +