Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert the following while loop to a for loop and : Write an input validation l

ID: 3682306 • Letter: C

Question

Convert the following while loop to a for loop and : Write an input validation loop that asks the user to enter a number in the range of I through 4. Write an input validation loop that asks the user to enter words "yes" or "no". Write code that does the following: Opens a file named NumberList. Txl, uses a loop to write the numbers 1 through 100 to the file, and then closes the file. Write code that does the following: Opens the NumberList file that was created by the code in Question 18. reads all the numbers from the file and displays them, and then closes the file. Modify the code you? Wrote in Question 19 so it adds all the numbers read from the file and displays their total. Write a program that asks the user for the name of a file. The program should display only the first five lines of the file's contents. If the file contains less than five lines, it should display the file's entire contents.

Explanation / Answer

//Question No:14
//Converting a while loop to a for loop
for(int count=0;i<50;count++)
{
   cout<<"count is"<<count;
}
//Question No:15
//code in .cpp file
int i,range;
for(;;)
{
cout<<" enter the range in the range of 1 through 4";
cin>>range;
if(range>0 && range <5)
{
cout<<" valid input range";
}
else
{
break;
}
}

//Question No:16
//code in .java file
import java.util.*;
for(;;)
{
System.out.println("enter the input word");
Scanner scan=new Scanner(System.in);
String input=scan.next();
if(input.equals("yes")|| input.equals("no"))
{
Systeml.out.println("valid input");
}
else
{
System.exit();
}
}

//Question No:20
//Lines.java
import java.io.*;
import java.util.*;
public class Lines
{
public static void main(String args[]) throws IOException
{
int c;
int lines=0;
   System.out.println("Enter the file name...");
   Scanner sc=new Scanner(System.in);
  
   String file=sc.next();
  
   File f = new File(file);
   FileInputStream fis = new FileInputStream(f);
while((c=fis.read())!=-1)
    {
   if(c==' ')
++lines;
   }
    System.out.println("Number of lines in the given file : " + lines);
   if(lines<5)
   {
       System.out.println("The entire file can be displayed completely");
   }
   else
   {


       System.out.println("The file cannot be read completely,only first five lines should be displayed");
}
}

}