SUBJECT-COMPUTER CLASS-12 CHAPTER 6 – Statements, Control Structures And Scope(Part-3)

Visibility Scope and Lifetime of Variables: There are two basic types of scope: local scope and global scope. A variable declared outside all functions is located into the global scope. Access to such variables can be done from anywhere in the program.These variables are located in the global pool of memory, so their lifetime coincides with the lifetime of the program. A variable declared inside a block (part of code enclosed in curly brackets) belongs to the local scope. Such a variable is not visible (and therefore not available) outside the block, in which it is declared. The most common case of local declaration is a variable declared within a function and the lifetime of such a variable is equal to the lifetime of the function.

Some Java Programs

1-

A perfect number is a positive integer that is equal to the sum of its proper positive excluding the number itself.

import java.util.Scanner; public class Perfect { public static void main(String[] args) { int n, sum = 0; Scanner s = new Scanner(System.in); System.out.print("Enter any integer you want to check:"); n = s.nextInt(); for(int i = 1; i < n; i++) { if(n % i == 0) { sum = sum + i; } } if(sum == n) { System.out.println("Given number is Perfect"); } else { System.out.println("Given number is not Perfect"); } } }

Output:

Enter any integer you want to check:6 Given number is Perfect

2- MAGIC NUMBER

A number is said to be a Magic number if the sum of its digits are calculated till a single digit is obtained by recursively adding the sum of its digits. If the single digit comes to be 1 then the number is a magic number.

Example:

199 is a magic number as 1+9+9=19 but 19 is not a single digit number so 1+9=10 and then 1+0=1 which is a single digit number and also 1.Hence it is a magic number.

/*Java program to check given number is magic number or not*/ import java.util.*; public class MagicNumber { public static void main(String args[]) { Scanner ob=new Scanner(System.in); System.out.println("Enter the number:"); int n=ob.nextInt(); int sum=0,num=n; while(num>9) { sum=num;int s=0; while(sum!=0) { s=s+(sum%10); sum=sum/10; } num=s; } if(num==1) { System.out.println(n+" is a Magic Number."); } else { System.out.println(n+" is not a Magic Number."); } }

}

OUTPUT:

Enter the number: 12 12 is not a Magic Number. Enter the number: 19 19 is a Magic Number.

3- Smith Number

A Smith number is a , the sum of whose digits is the sum of the digits of its prime factors obtained as a result of prime factorization (excluding 1). The first few such numbers are 4, 22, 27, 58, 85, 94, 121 ………………..

Examples:

1. 666

Prime factors are 2, 3, 3, and 37 Sum of the digits are (6+6+6) = 18 Sum of the digits of the factors (2+3+3+(3+7)) = 18

2. 4937775

Prime factors are 3, 5, 5, 65837 Sum of the digits are (4+9+3+7+7+7+5) = 42 Sum of the digits of the factors (3+5+5+(6+5+8+3+7)) = 42

Write a program to input a number and display whether the number is a Smith number or not.

Sample data: Input 102 Output NOT SMITH Number

Input 666 Output SMITH Number

Input 999 Output NOT SMITH Number

Programming Code:

import java.io.*;

class smith { public static void main (int n) { int r, sum = 0, s = 0, factor, counter = 0, s1 = 0, sumprime = 0,d = 0; //to find the sum of the digits for(int i=1; i<=n; i++) // checking for prime or not prime if(n%i == 0) counter++; if(counter>2)

{ s1=n; System.out.print(“Sum of digits=”); while(s1>=1) { r=s1%10;

s+=r;//sum of the digits

s1=s1/10; } System.out.println(s); factor=2; int n2=n; System.out.print(“Sum of prime factors=”); while(factor <= n2) { if(n2 % factor == 0) { if(factor<10) sumprime+=factor; else { s1=factor; while(s1>0) { d = s1%10; sumprime += d; s1 /= 10; }} n2 /= factor; } else { if(factor == 2) factor = 3; else factor += 2; }} System.out.print(sumprime); if(sumprime == s) System.out.println(“Smith Number”); else System.out.println(“Not a Smith Number”); } else System.out.println(“Prime no., it can’t be a smith number”);

}

}

Output: Number=4937775 SumofDigits=42 ofDigit=42 SumofPrimeFactors=42 It is a Smith Number

NOTE- All the answers should be given in your notebook

Q1- What are local and global variables.

Q2- Make all of the above given programs and also try them on computer without watching them from notebook.

------