❌

Reading view

There are new articles available, click to refresh the page.

Exploring Multiplication in Python: A Simple Program

In this post, we will delve into a basic Python program that demonstrates the concept of multiplication using both explicit print statements and a while loop.

Part 1: Using Print Statements

#print num series 2 4 8 16 32 
#using print statement

print(2)
print(4)
print(8)
print(16)
print(32)

In this section, we use individual print statements to display the sequence of numbers. Each number is a power of 2, starting from 21 and doubling with each subsequent number.

Part 2: Using a Variable

#print num series 2 4 8 16 32 
#using a variable

no = 2
print(no)
no = no * 2
print(no)
no = no * 2
print(no)
no = no * 2
print(no)
no = no * 2
print(no)

Here, we start with the number 2 assigned to the variable 'noβ€˜. We then repeatedly multiply 'noβ€˜ by 2 and print the result. This approach showcases the concept of updating a variable’s value based on its previous state.

Part 3: Using a While Loop

#print num series 2 4 8 16 32 
#using while loop

no = 2
while no <= 32:
    print(no)
    no = no * 2

In this final part, we use a β€˜while' loop to achieve the same result as above. The loop continues to run as long as β€˜no' is less than or equal to 32. Inside the loop, we print the current value of 'noβ€˜ and then double it. This approach is more efficient and scalable, as it reduces the need for repetitive code.

Conclusion

This simple Python program effectively demonstrates how to use basic arithmetic operations and loops to achieve a desired sequence of numbers. It’s a great starting point for beginners to understand variable manipulation and control flow in programming.


program:

#print num series 2 4 8 16 32 
#using print statement
print(2)
print(4)
print(8)
print(16)
print(32)
print()#just for space in output

#print num series 2 4 8 16 32 
#using a variable
no=2
print(no)
no=no*2
print(no)
no=no*2
print(no)
no=no*2
print(no)
no=no*2
print(no)
print()#just for space in output

#print num series 2 4 8 16 32 
#using while loop
no=2
while no<=32:
    print(no)
    no=no*2

Output:


Task – For Loop – Playing with Numbers

Armstrong, Neon, Perfect, Prime, emirP, Strong, Triangle Series
import java.util.Scanner;

public class forLoopPlayWithNos {
// USING FOR LOOP
//Code for Armstrong, Neon, Perfect, Prime, emirP, Strong Numbers
// Factorial, SumOfDigits, ReverseDigits, CountOfDigits, Power also used.
//Triangular Series : 1,3,6,10,15,21,28,....
	    // FOR PRINTING 
		private static void print(int n, String numType, boolean matched) {
			if (matched) System.out.println(n+"  is a " + numType +" Number.");
			else System.out.println(n+"  is not a " + numType +" Number.");
		}

		public static void main(String[] args) {
			// TODO Auto-generated method stub
			forLoopPlayWithNos loop = new forLoopPlayWithNos();
	        System.out.print("Enter a Number to Play = ");
	        Scanner scn = new Scanner(System.in);
	        int num = scn.nextInt();
	        scn.close();
			
	        forLoopPlayWithNos.print(num, "ArmStrong",loop.isArmstrongNo(num));
	        forLoopPlayWithNos.print(num, "Neon", loop.isNeonNo(num));
	        forLoopPlayWithNos.print(num, "Perfect",loop.isPerfectNo(num));
	        forLoopPlayWithNos.print(num, "Prime",loop.isPrimeNo(num));
	        forLoopPlayWithNos.print(num, "emirP",loop.isemirPNo(num));
	        forLoopPlayWithNos.print(num, "Strong",loop.isStrong(num));
			loop.tringangularSeries(num);
		}
		
		private boolean isArmstrongNo(int n) {
			//Sum of (each digit to the power of (No of digits)) == n
			// Ex. 371, 153 == 1^3 + 5^3 + 3^3 = 1+125+27
			int powSum=0, nod = countDigits(n);
			for (int num=n; num>0; num/=10) {
				powSum += findPower((num%10),nod);
			}
			if (n == powSum) return true;
			return false;
		}
		
		private boolean isNeonNo(int n) {
			//Neon = (Sum of digits of a squared number == number)
			// Ex 1,9,45,55
			int sqNum = n*n;
			if (n == sumOfDigits(sqNum)) return true;
			return false;
		}
		
		private boolean isPerfectNo(int n) {
			//Sum of its divisors (excluding n) == n
			//Ex. 6,28,496
			int sum=1;
			for(int i=2; i<n; i++) {
				if (n%i == 0) sum+=i; // Adding Divisors
			}
			if (sum==n) return true;
			return false;
		}
		
		private boolean isPrimeNo(int n) {
			// N number that can be divided exactly only by itself, ie n's divisors are 1 and n
			// Ex 2,3,5,7,11,13
			switch(n) {
			case 0,1:
				return false;
			case 2,3:
				return true;
			default:
				if (n%2 == 0) return false;
				else {
					for (int i=3; i*i<n; i+=2) {
						if (n%i == 0) {
							return false;
						}
					}
				}
			}
			return true;
		}
		
		private boolean isemirPNo(int n) {
			// A number and revereDigits of that number are prime
			int rev = reverseDigits(n);
			return (isPrimeNo(n) && isPrimeNo(rev))? true:false;
		}
		
		private boolean isStrong(int n) {
			//sum of the factorial of its digit is equal to number itself
			// Ex. 145 = 1!+4!+5!
			int sum = 0, num=n;
			for (num=n; num>0; num/=10) {
				sum += factorial(num%10);
			}
			return (n == sum)? true:false; 
		}
		
		private int findPower(int base, int pr) {
			// finding base to the power pr
			int res=1;
			for (; pr>=1; pr--) {
				res *= base;
			}
			return res;
		}
		
		private int reverseDigits(int num) {
			int rev = 0;
			for (; num>0; num/=10) {
				rev = rev*10 + num%10;
			}
			return rev;
		}
		private int countDigits(int num) { //Counting No. of Digits
			int cnt=0;
			for (; num>0; num/=10,cnt++); 
			return cnt;
		}
		private int sumOfDigits(int n) {
			int sum=0;
			for (; n>0; n/=10) {
				sum += n%10;
			}
			return sum;
		}
		
		private int factorial(int n) {
			int fact=1;
			for (int i=1; i<=n; i++) {
				fact *= i;
			}
			return fact;
		}	
		
		private void tringangularSeries(int n) {
			System.out.print("Tringangular Series : ");
			for (int tri=1, i=2; tri<=n; i++) {
				System.out.print(tri+" ");
				tri+=i;
			}
		}
}

OUTPUT:

Interface types in Java

java programming language an interface is a reference type, similar to a class, that can contain only constants, method declaration(method signature, nobody), default methods, static methods and nested types inside its body. Nested type simply means it can contain another interface or class inside it. variables declared in an interface are public, static & final […]

❌