You are on page 1of 6

Programming Fundamentals

Assignment 1

Name: Muhammad Farhan

Registration Number: SP20-BSE-049

Class: Section 2A

Submitted to: Mam Sadia Maqbool


Question No # 1:
Ans:

package javaapplication7;

import java.util.Scanner;

public class JavaApplication7 {

public static void main(String[] args) {

int x = 0;

int correct_count = 0;

int count = 2;

while (true){

int n1 = 10 + (int)(Math.random() * 90);

int n2 = 10 + (int)(Math.random() * 90);

int ans = n1 + n2;

System.out.println("What is " + n1 + "+" + n2 + "?" );

System.out.print("Your answer: ");

Scanner input = new Scanner(System.in);

int g = input.nextInt();

if (g == ans){

count = 1;
if (count == 1){

correct_count += 1;

System.out.println("Correct! you have gotten " + correct_count + " correct in a


row.");

if (correct_count == 3){

System.out.println("Congratulations! you mastered in addition.");

break;

else{

System.out.println("incorrect. The possible answer is " + ans);

count = 0;

correct_count = 0;

}
}

Question No # 2:
Ans:

package javaapplication6;

import java.util.Scanner;

public class JavaApplication6 {

public static void main(String[] args) {

int n = 0;
System.out.println("entre an integer: ");

Scanner input = new Scanner(System.in);

n = input.nextInt();

int i;

for (i = 1 ; n != 1 ; i++){

if(n%2==0){

int x = n;

n = n/2;

System.out.println(i + ". " + x + " is even so i take half : " + n);

else{

int y = n;

n = 3*n+1;

System.out.println(i + ". " + y + " is odd so i make 3n+1 : " + n);

System.out.println( "the process took "+(i-1)+" steps to reach 1");


}

You might also like