Briefing in Python Machine Learning (ML) is that field of computer science with the help of which the computer systems can provide a sense to data in much the same way as human beings do. It is a type of artificial intelligence that extracts patterns out of raw data by using an algorithm or method.... Continue Reading →
Reversing a list in python
def Reverse(lst): return [element for element in reversed(lst)] lst = [3,14,8,24,1] print(Reverse(lst))
List Length in Python
The length of a list can be calculated in Python by using various methods. The built-in len() method in Python is widely used to calculate the length of any sequential data type. It calculates the number of elements or items in a list and returns the same as the length of the list. l=[2,4,6,8,10] print("The length of... Continue Reading →
Swapping the elements in a list
def swap(list, postn1, postn2): list[postn1], list[postn2] = list[postn2], list[postn1] return list List = [24,1,11,9,30,31] pos1, pos2 = 1, 3 print(swap(List, pos1-1, pos2-1))
Interchange first and last elements in a list in python
def swap_num_List(Listofnum): size = len(Listofnum) temp = Listofnum[0] Listofnum[0] = Listofnum[size - 1] Listofnum[size - 1] = temp return Listofnum Listofnum = [34,14,8,11,10] print(swap_num_List(Listofnum))
Checking for monotonic array in Python
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))) # main A = [5,6,7,8,9,10,11,12] print(isMonotonic(A)) 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... Continue Reading →
Finding the remainder of array multiplication divided n
#include <iostream> using namespace std; int findrem(int arr[], int len, int n) { int mul = 1; for (int i = 0; i < len; i++) mul = (mul * (arr[i] % n)) % n; return mul % n; } int main() { int arr[] = { 10, 18, 45, 7, 12, 20 }; int... Continue Reading →
Splitting the array and adding the first part at the end
#include <bits/stdc++.h> using namespace std; void splitArr(int arr[], int n, int k) { for (int i = 0; i < k; i++) { int x = arr[0]; for (int j = 0; j < n - 1; ++j) arr[j] = arr[j + 1]; arr[n - 1] = x; } } int main() { int arr[]... Continue Reading →
Sum of elements of array
#include <iostream> using namespace std; int main (){ int arr[] = {6,12,4,7,9 }; int n = 7, sum = 0; for(int i = 0; i<n ; i++){ sum+=arr[i]; } cout<<"The array sum is "<<sum; return 0; }
Store and display information using structure in CPP
#include <iostream> using namespace std; struct student { char name[50]; int roll; float marks; } s[6]; int main() { cout << "Enter information of students: " << endl; for(int i = 0; i < 6; ++i) { s[i].roll = i+1; cout << "Roll number " << s[i].roll << ":" << endl; cout << "Enter name:... Continue Reading →
Sorting in Lexicographical Order
#include <iostream> using namespace std; int main() { string str[10], temp; cout << "Enter words (<=10): " << endl; for(int i = 0; i < 10; ++i) { getline(cin, str[i]); } for (int i = 0; i < 9; ++i) { for (int j = 0; j < 9 - i; ++j) { if (str[j]... Continue Reading →
Transpose of a matrix in C
#include <stdio.h> int main() { int matrix[10][10], transpose[10][10], row, col, i, j; printf("Enter rows and columns: "); scanf("%d %d", &row, &col); printf("\nEnter matrix elements:\n"); for (i = 0; i < row; ++i) for (j = 0; j < col; ++j) { printf("Enter element a%d%d: ", i + 1, j + 1); scanf("%d", &matrix[i][j]); } printf("\nEntered... Continue Reading →
Matrix Addition in C
#include <stdio.h> int main() { int row, col, mat1[100][100], mat2[100][100], matsum[100][100], i, j; printf("Enter the number of rows: "); scanf("%d", &row); printf("Enter the number of columns"); scanf("%d", &col); printf("\nEnter elements of 1st matrix:\n"); for (i = 0; i < row; ++i) for (j = 0; j < col; ++j) { printf("Element a%d%d: ", i +... Continue Reading →
Hosoya’s Triangle or Fibonacci Triangle in C
The Hosoya's triangle (originally Fibonacci triangle) is a triangular arrangement of numbers as it is shown in pascal triangle based on the Fibonacci series. #include<stdio.h> #include<stdlib.h> int main(){ int a=0,b=1,i,c,n,j; system("cls"); printf("Enter the number of lines you want:"); scanf("%d",&n); for(i=1;i<=n;i++) { a=0; b=1; printf("%d\t",b); for(j=1;j<i;j++) { c=a+b; printf("%d\t",c); a=b; b=c; } printf("\n"); } return 0; }
Triangular Number Pattern in C
#include<stdio.h> #include<stdlib.h> int main(){ int i,j,k,l,n; system("cls"); printf("enter the range="); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) { printf(" "); } for(k=1;k<=i;k++) { printf("%d",k); } for(l=i-1;l>=1;l--) { printf("%d",l); } printf("\n"); } return 0; }
Matrix Multiplication in C
#include <stdio.h> int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter number of rows and columns of matrix 1 \n"); scanf("%d%d", &m, &n); printf("Enter elements of first matrix\n"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d",... Continue Reading →
Automorphic number in C
An Automorphic number is a number whose square ends with the same digits as the original. example: 5, 6 etc. #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int num, square, temp, last; int n =0; printf("Enter a number \n"); scanf("%d",&num); square = num*num; temp = num; while(temp>0){ n++; temp = temp/10; } int den =... Continue Reading →
C program to check for a perfect number
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. example: 4: factors of 4 are 1, 2 nd 4 itself 1+2=3 , 4 is not a perfect number. #include <stdio.h> int main() { int number, rem, sum = 0, i; printf("Enter a Number\n");... Continue Reading →
C program to check whether a given number is a strong number or not
STRONG NUMBER: Strong number is a number whose sum of all digits’ factorial is equal to the number itself. example : 12 ; 1!+2! = 3 not a strong number #include <stdio.h> int factorial(int r) { int fact = 1; while(r>1) { fact = fact * r; r--; } return fact; } int check(int num)... Continue Reading →
C program to swap 2 numbers without using 3rd variable
It can be done using two methods: By using + and -By using * and / Method 1: By using + and - #include<stdio.h> int main() { int num1, num2; printf("Enter first number: ") ; scanf("%d",&num1); printf("\nEnter second number: ") ; scanf("%d",&num2); printf("\n Before swapping \n number_1= %d \n number_2 = %d",num1,num2); num1=num1+num2; num2=num1-num2; num1=num1-num2;... Continue Reading →
Python program to print all the prime numbers in a given range
The prime numbers are whole numbers greater than 1, that have only two factors – 1 and the number itself. Prime numbers are divisible only by the number 1 or itself. examples: 2*1=2 and 1*2=2 start = int (input("Enter the lower range ")) end = int (input("Enter the upper range ")) for i in range(start,... Continue Reading →
C program to check whether an alphabet is a vowel or consonant
#include <stdio.h> int main() { char let; int lcase_vowel, ucase_vowel; printf("Enter an alphabet: "); scanf("%c", &let); lcase_vowel = (let == 'a' || let == 'e' || let == 'i' || let == 'o' || let == 'u'); ucase_vowel = (let == 'A' || let == 'E' || let == 'I' || let == 'O' ||... Continue Reading →
Easy Python programs
Print the ASCII value of Characters charr =(input("Enter a charcater")) print("The ASCII value of '" + charr + "' is", ord(charr)) Find the roots of quadratic equation import cmath a = int(input("Enter the value of a ")) b = int(input("Enter the value of b ")) c = int(input("Enter the value of c ")) # calculating... Continue Reading →
Area and Circumference of circle in python
#area and circumference of circle rad=float(input("Enter the radius of the circle ")) area= 3.14*rad*rad print("Area of the circle of given radius is = ", area) circum= 2*3.14*rad print("Circumference of the circle of given radius is = ", circum)
To check for Armstrong number in C
Armstrong number is a number which is equal to the sum of cubes of its digits. e.g. 0, 1, 153 etc are the Armstrong numbers. Demonstration: 153=(1*1*1)+(5*5*5)+(3*3*3) cube of 1=1 cube of 5=125 cube of 3=27 1+125+27=153 #include<stdio.h> int main() { int n,q,sum=0,temp; printf("Enter a number="); scanf("%d",&n); temp=n; while(n>0) { q=n%10; sum=sum+(q*q*q); n=n/10; } if(temp==sum) printf("The given... Continue Reading →
Calculate Compound Interest using Python
Formula for compound interest : =final amount=initial principal balance=interest rate=number of times interest applied per time period=number of time periods elapsed def compound_interest(principle, rate, time): CI = principle * (pow((1 + rate / 100), time)) print("Compound interest : ", CI) p=int(input("Enter Principal amount: ")) r=int(input("Enter interest rate: ")) t=int(input("Enter time period: ")) compound_interest(p, r, t)
Calculate simple interest using python
Formula for simple interest is : (principal amount* rate of interest* time period)/100 SI= (P*R*T)/100 p=int(input("Enter the principal amount ")) r=int(input("Enter the interest rate per annum ")) t=int(input("Enter the time period in years ")) simple_interest=(p*r*t)/100 print("SIMPLE INTEREST = ", simple_interest)
Fibonacci Series
Fibonacci series is a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. e.g. 0, 1, 1, 2, 3, 5, 8, etc. The Rule is to write a Fibonacci series is: xn = xn−1 + xn−2 where: xn is term number "n"xn−1 is the previous term (n−1)xn−2 is the term before that (n−2)... Continue Reading →
Binomial Coefficient
Binomial coefficient is a positive integer that occur as coefficient in the Binomial Theorem. It can be defined as the coefficient of the monomial Xk in the expansion of (1 + X)n . This coefficient also occurs in the formula given below #include <stdio.h> int Coeff(int n, int k) { if (k > n) return 0; if (k... Continue Reading →
Find the Prime Factors of a Number.
# include <stdio.h> # include <math.h> void prime_Factors(int num) { while (num%2 == 0) { printf("%d ", 2); num = num/2; } // n should be odd for (int i = 3; i <= sqrt(num); i = i+2) { // While i divides n, print i and divide n while (num%i == 0) { printf("%d... Continue Reading →
Factorial of a Big Number
#include<stdio.h> int main() { int arr[200],n,count,temp,i; arr[0]=1; count=0; printf("Enter the number whose factorial you want to find: "); scanf("%d",&n); for(; n>=2; n--) { temp=0; for(i=0; i<=count; i++) { temp=(arr[i]*n)+temp; arr[i]=temp%10; temp=temp/10; } while(temp>0) { arr[++count]=temp%10; temp=temp/10; } } for(i=count; i>=0; i--) printf("%d",arr[i]); return 0; }
HCF and LCM
HCF can be defined as the Highest Common Factor. It means the greatest which can exaclty divide the given numbers without the remainder. It is also known as Greatest Common Divisor (GCD). Program to find the HCF of 2 numbers. #include <stdio.h> int main() { int num1, num2, i, hcf; printf("Enter two numbers whose HCF... Continue Reading →