Skip to main content

Posts

Showing posts from August, 2023

Change an extension to file in python

Program to change an extension of a file in python: import os import shutil def renamefiles():     filelist = os.listdir(r"source path here")     print(filelist)     os.chdir(r"source path here")     path = os.getcwd()     for filename in filelist:         if filename.endswith(".xml"):             print("Old Name - " + filename)             (prefix, sep, sffix) = filename.rpartition(".")             newfile = filename + '.ok'             if not filename.endswith(".ok"):                 os.rename(filename, newfile)                 print("New Name - " + newfile)                 os.chdir(path) # path to source directory src_dir = r'source path here' # path to destination directory...

Java Program to print Star patterns

 Java Star Pattern Programs: ================================================================= Patterns 1:  package javaPrograms; public class StarPattern { public static void main(String[] args) { //i for rows and j for columns       //row denotes the number of rows you want to print   int i, j, row=6;    //outer loop for rows   for(i=0; i<row; i++)    {    //inner loop for columns   for(j=0; j<=i; j++)    {    //prints stars    System.out.print("* ");    }    //throws the cursor in a new line after printing each line   System.out.println();    }      } } ================================================================= Pattern 2:  package javaPrograms; public class StarPattern1 { public static void main(String[] args) { for(int i=1;i<=4;i++) { ...

Java Program to check if Number is Armstrong or not

 Logic for  Armstrong   number :)  Armstrong Number An  Armstrong  number is a positive m-digit number that is equal to the sum of the m th  powers of their digits. It is also known as  pluperfect , or  Plus Perfect , or  Narcissistic  number. It is an OEIS sequence  A005188 . Let’s understand it through an example. Armstrong Number Example 1:  1 1  =  1 2:  2 1  =  2 3:  3 1  =  3 153:  1 3  + 5 3  + 3 3  = 1 + 125+ 27 =  153 125:  1 3  + 2 3  + 5 3  = 1 + 8 + 125 =  134 (Not an Armstrong Number) 1634:  1 4  + 6 4  + 3 4  + 4 4  = 1 + 1296 + 81 + 256 =  1643 //Armstrong number list:0,1,153,370,371,407,1634 //Eg 153 rest to 3(Total number of digit ) each digit if addition come original number. package javaPrograms; import java.util.Scanner; public class ArmstrongNumber { public static vo...

Java Program to reverse the Array elements

 package javaPrograms; import java.util.Scanner; public class ReverseArray { public static void main(String[] args) { Scanner sc = new Scanner (System.in); System.out.println("Enter the number of Elements::"); int n =sc.nextInt(); System.out.println("Enter the "+ n +" number of array elements"); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } System.out.println("The reverse of array Elements are as follow"); for(int i=a.length-1;i>=0;i--) { System.out.println(a[i]); } } }

Java Program to find Maximum element from the Array

 package javaPrograms; import java.util.Scanner; public class MaximumElementInArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the Length of Array::"); int n = sc.nextInt(); System.out.println("Enter the "+n+" number of Array Element::"); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] =sc.nextInt(); } int max=a[0];    for(int i=1;i<a.length;i++) {    if(a[i]>max) {    max=a[i];    }    }    System.out.println("Maximum Element is::"+max); } }

Java Prgram to find the minimum element in the Array

 package javaPrograms; import java.util.Scanner; public class ArrayMinElement { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the Length of Array::"); int n = sc.nextInt(); System.out.println("Enter the "+n+" number of Array Element::"); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] =sc.nextInt(); } int min=a[0];    for(int i=1;i<a.length;i++) {    if(a[i]<=min) {    min=a[i];    }    }    System.out.println("Minimum Element is::"+min); } }

Java program to Sort the Array in ascending order

 package javaPrograms; import java.util.Scanner; public class ArrayAscending { public static void main(String[] args) { //Create object of scanner class Scanner sc = new Scanner(System.in); System.out.println("Enter the number:"); int n= sc.nextInt(); //Declare and initialize Array and other Required variables.. int a[] = new int [n];    int i,j,temp=0;   //Now get all array elements from user System.out.println("Enter" + n   +"number of array elements:  ");//5 for(i=0;i<n;i++) { a[i]=sc.nextInt(); } //Print all array elements System.out.println("All array elements are: ");//4 6 3 2 1 8 for(i=0;i<n;i++) { System.out.println(" "+a[i]); } //Now arrange array elements in ascending order for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } //Print array ...

Java Program to check number is palindrome or not

 package javaPrograms; import java.util.Scanner; public class PalindromeNum { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number::"); int no=sc.nextInt(); int temp = no; int rev=0,rem; while(temp!=0) { rem=temp%10; rev = rev*10+rem; temp=temp/10; } if(no==rev) { System.out.println(no+" is Palindrome number"); } else { System.out.println(no+" is not palindrome number"); } } }

Java Prgram to remove duplicate character in a string

 package Java; import java.util.Scanner; public class RemoveDuplicateCharacter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the String"); String w = sc.next();   String nw = " "; System.out.println("Original String: "+w); for(int i=0;i<w.length();i++) { char ch = w.charAt(i); if(ch!=' ') { nw = nw + ch; w = w.replace(ch, ' '); } } System.out.println("New word = "+nw); } }

Java program to check string is palindrome or not

 package javaPrograms; import java.util.Scanner; public class Palindrome { public static void main(String args[])     {         Scanner sc = new Scanner(System.in);         System.out.print("Enter the string you want to check:");        String str = sc.next();        String org_str=str;         String rev="";         int len=str.length();         for(int i = len - 1; i >= 0; i--)         {             rev = rev + str.charAt(i);         }         if(org_str.equals(rev))         {             System.out.println("The string is palindrome.");         }         else         {       ...

Java Program to find factorial of a number

Java Program to find factorial of a number:  package javaPrograms; import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner sc =  new Scanner(System.in); System.out.println("Enter the number::"); int no = sc.nextInt(); int fact=1; for(int i=1;i<=no;i++) { fact = fact*i; } System.out.println(fact); } }

Java Program to check number is Prime or not

 //Prime number logic: Prime number is the number which is divisible by 1 and it self package javaPrograms; import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number::"); int n= sc.nextInt(); int temp=0; for(int i=2;i<=n-1;i++) {     //2-3 if(n%2==0) { temp= temp+1; } } if(temp==0) { System.out.println("Number is  Prime"); } else { System.out.println("Number is not Prime"); } } }

Java program to find Odd and Even number

 Below is the Java program to find find Odd and Even number. package javaPrograms; public class EvenOdd { public static void main(String[] args) { int a= 91; if(a%2==0) { System.out.println("Number is Even"); } else { System.out.println("Number is odd"); } } } =========================================================== Same program by taking an input from user: package javaPrograms; import java.util.Scanner; public class OddEvenUser { public static void main(String[] args) { int Number; Scanner sc = new Scanner(System.in); System.out.println("\n Please Enter any integer Value: "); Number = sc.nextInt(); if (Number % 2 == 0) { System.out.println("\n You have entered EVEN Number"); } else { System.out.println("\n You have entered ODD Number"); } } }

SFTP connection using Python

 Below is the code which will guide you on how to make a SFTP(Simple Mail Transfer Protocol) connection using python. In order to do this you first need to install following library to use the inbuild SFTP related functions. To install the Paramiko module, use the command: $ pip3 install paramiko after installing the library use following code: import paramiko from io import BytesIO try:     # Open a transport     sftpTransport = paramiko.Transport(('host_name', PORT_NUMBER))     sftpTransport.connect(None, 'SFTP_USER_NAME', 'SFTP_USER_PASSWORD')     # Open a sftp     sftpClient = paramiko.SFTPClient.from_transport(sftpTransport)     alive = True     print(sftpClient)     for i in sftpClient.listdir():         lstatout=str(sftpClient.lstat(i)).split()[0]         if 'd' in lstatout: print(f'{i} is a directory') except Exception as e:     print("SFTP: Erro...