Write a verilog code to Initialize a 10 bit number “num” of reg data type with a value 0 (i.e. num=10’b0000000000), now make use of for loop to get the output result as num=10’b001100110011 (Note: It is mandatory to use for loop for getting this output)
The code is attached below along with output. Comment has been added to the code for explanation, but still if you have any doubts let me know.
PPA8.java:
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class PPA8{
private static final String evenFileName = "EvenNumOut.txt";
private static final String oddFileName = "OddNumOut.txt";
private static final String primeFileName = "PrimeNumOut.txt";
public static boolean isPrime(int num) {
for (int div =2; div <= num/2; div++) {
if (num % div == 0 ) return false;
} // end of for loop
//if you get here, then num is prime
return true;
} //end of isPrime method
public static void main(String[] args){
//scanner class for taking user input
Scanner scn = new Scanner(System.in);
//open files for writing
PrintWriter evenFileWriter;
PrintWriter oddFileWriter;
PrintWriter primeFileWriter;
//try to open files for writing
try{
evenFileWriter = new PrintWriter(new FileOutputStream(evenFileName), true);
oddFileWriter = new PrintWriter(new FileOutputStream(oddFileName), true);
primeFileWriter = new PrintWriter(new FileOutputStream(primeFileName), true);
}
catch(IOException e){
// Files cannot be opened for writing exceptions are handled
System.out.println("Unable to open files for writing: " + e);
return;
}
//now get input from user
System.out.print("Enter count [400 - 950]: ");
int count = scn.nextInt();
//now if count is not within range
while(count < 400 || count > 950){
System.out.print("Count is out of range. Enter in range [400 - 950]: ");
count = scn.nextInt();
}
//now initalise variables wrt files count
int evenCount = 0;
int oddCount = 0;
int primeCount = 0;
int evenTotal = 0;
int oddTotal = 0;
int primeTotal = 0;
int num;
//create random class object
Random rand = new Random();
//start loop
for(int i = 0; i < count ; i++){
//get a randim number within range of 1000
num = rand.nextInt(1000);
//check if this number is odd
if((num % 2) == 1){
//number is odd
oddCount++;
oddTotal += num;
oddFileWriter.println(num);
}
else{
//number is even
evenCount++;
evenTotal += num;
evenFileWriter.println(num);
}
//now check if number is prime
if(isPrime(num)){
primeCount++;
primeTotal += num;
primeFileWriter.println(num);
}
}
//now calculate average for all three sets
double evenAvg = (double)evenTotal/evenCount;
double oddAvg = (double)oddTotal/oddCount;
double primeAvg = (double)primeTotal/primeCount;
//now output statistics
System.out.println("Number of Odd Numbers: " + oddCount);
System.out.println("Total value of Odd Numbers: " + oddTotal);
System.out.println("Average value of Odd Numbers: " + oddAvg);
System.out.println("Number of Even Numbers: " + evenCount);
System.out.println("Total value of Even Numbers: " + evenTotal);
System.out.println("Average value of Even Numbers: " + evenAvg);
System.out.println("Number of Prime Numbers: " + primeCount);
System.out.println("Total value of Prime Numbers: " + primeTotal);
System.out.println("Average value of Prime Numbers: " + primeAvg);
//close printwriters
oddFileWriter.close();
evenFileWriter.close();
primeFileWriter.close();
}
}
Comments
Leave a comment