NOS 120 FINAL ALL SETS QUESTIONS WITH VERIFIED
ACCURATE ANSWERS
Write a C program called fedora.c that displays the line "Fedora is a version of Linux." -
Answers -#include <stdio.h>
int main()
{
printf("Fedora is a version of Linux.\n");
}
Create a simple C program which declares the variable number as a local variable that
is an integer. - Answers -int number;
int main()
{
}
Modify the rain.c program you created in this chapter so that the total of all inches of
rain when added together is
136 inches. - Answers -#include <stdio.h>
int main()
{
int rain, total_rain = 0;
for (rain = 0; rain < 17; rain++)
{
printf("We have had %d inches of rain.\n", rain);
total_rain = total_rain + rain;
}
printf("We have had a total ");
printf("of %d inches of rain.\n", total_rain);
}
Write a program in which a prompt asks for your first name and after you enter your first
name the program displays
it. - Answers -#include <stdio.h>
int main()
{
char string[20];
printf("\nEnter your first name: ");
scanf("%s", string);
printf("Your first name is: %s\n", string);
}
,Write a program in which you enter a number and the program calculates and displays
that number cubed. - Answers -#include <stdio.h>
int main(void)
{
int num, cube;
printf("\nEnter the number to cube: ");
scanf("%d", &num);
cube = num*num*num;
printf("Your number cubed is: %d\n", cube);
}
Create a program that enables you to input a positive integer and that will then tell you if
it is a prime number or if it can be
divided by 2. - Answers -#include <stdio.h>
int main (void)
{
int num;
int count;
int signal;
printf("Enter the number to test: ");
scanf("%d", &num);
for (count=2, signal=1; (count<=(num/2)) && signal; )
{
if ((num % count) ==0)
signal = 0;
else
count++;
}
if (signal)
printf("%d is prime\n", num);
else
printf("%d has %d as a factor\n", num, count);
}
Create a program in which the user enters the outside temperature in Fahrenheit. If the
temperature is 60 or over,
print a comment that states "It is just fine outside." If the temperature is under 60, print a
comment that states "It's
not so warm outside." - Answers -#include<stdio.h>
int main (void)
{
int temp;
printf("\nEnter the outside temperature: ");
scanf("%d", &temp);
if (temp >=60)
{
, printf("It is just fine outside.\n");
}
else
{
printf("It's not so warm outside.\n");
}
}
Write a small C++ program that writes to the screen the make and model of the car or
bicycle you own (or one that
you wish to own) - Answers -#include <iostream>
using namespace std;
int main(void)
{
cout << "I own a Trek mountain bike.\n";
cout << "It was made in 2004.\n";
}
Rewrite the C program keyboard.c that you created earlier in this chapter to compile as
a C++ program. - Answers -#include<iostream>
using namespace std;
int main(void)
{
char string[50];
float my_money;
int weight;
cout << "\nEnter your First Name: ";
cin >> string;
cout << "\nEnter your Desired Monthly Income: ";
cin >> my_money;
cout << "\nEnter your friend's weight: ";
cin >> weight;
cout << "\n\n Recap\n";
cout << "I am " << string;
cout << " and I wish to have " << my_money;
cout << " per month";
cout << "\nI never would have guessed your friend weighs " << weight;
cout << "\n\n";
}
Create a C++ program that tests to see if the file accounts exists and prints a message
to say whether the file exists. - Answers -#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
, ifstream file("accounts");
if (file.fail())
cout << "Can't find the accounts file.\n";
else
{
cout << "The accounts file exits.\n";
}
}
Write a Perl script to display the line "Perl was developed by Larry Wall". - Answers -
#!/usr/bin/perl
# Program name: exercise1.pl
print ("Perl was developed by Larry Wall\n");
Write a Perl script in which you create a variable in the script to contain the name Beth
and then have the script
display "Welcome Beth." - Answers -#!/usr/bin/perl
# Program name: exercise2.pl
$name = "Beth";
print ("Welcome $name\n");
Modify the script you wrote in Exercise 2 so that you prompt for the name and then
display "Welcome name". - Answers -#!/usr/bin/perl
# Program name: exercise3.pl
print ("Enter your first name: ");
$name = <STDIN>;
print ("Welcome $name\n");
Write down four scalar variables, two of which are numeric and two that are strings. -
Answers -$value = 45;
$m = 90;
$furniture = "desk";
$car = "Ford";
Create a Perl program that uses an array of vegetables—peas, carrots, spinach, corn,
beans—and in which all of the vegetables in the array are displayed to the screen. -
Answers -#!/usr/bin/perl
# Program name: exercise5.pl
@vegetables = ("peas", "carrots", "spinach", "corn", "beans");
print ("The vegetables on my list are:\n");
print ("$vegetables[0]\n");
print ("$vegetables[1]\n");
print ("$vegetables[2]\n");
print ("$vegetables[3]\n");
print ("$vegetables[4]\n");
ACCURATE ANSWERS
Write a C program called fedora.c that displays the line "Fedora is a version of Linux." -
Answers -#include <stdio.h>
int main()
{
printf("Fedora is a version of Linux.\n");
}
Create a simple C program which declares the variable number as a local variable that
is an integer. - Answers -int number;
int main()
{
}
Modify the rain.c program you created in this chapter so that the total of all inches of
rain when added together is
136 inches. - Answers -#include <stdio.h>
int main()
{
int rain, total_rain = 0;
for (rain = 0; rain < 17; rain++)
{
printf("We have had %d inches of rain.\n", rain);
total_rain = total_rain + rain;
}
printf("We have had a total ");
printf("of %d inches of rain.\n", total_rain);
}
Write a program in which a prompt asks for your first name and after you enter your first
name the program displays
it. - Answers -#include <stdio.h>
int main()
{
char string[20];
printf("\nEnter your first name: ");
scanf("%s", string);
printf("Your first name is: %s\n", string);
}
,Write a program in which you enter a number and the program calculates and displays
that number cubed. - Answers -#include <stdio.h>
int main(void)
{
int num, cube;
printf("\nEnter the number to cube: ");
scanf("%d", &num);
cube = num*num*num;
printf("Your number cubed is: %d\n", cube);
}
Create a program that enables you to input a positive integer and that will then tell you if
it is a prime number or if it can be
divided by 2. - Answers -#include <stdio.h>
int main (void)
{
int num;
int count;
int signal;
printf("Enter the number to test: ");
scanf("%d", &num);
for (count=2, signal=1; (count<=(num/2)) && signal; )
{
if ((num % count) ==0)
signal = 0;
else
count++;
}
if (signal)
printf("%d is prime\n", num);
else
printf("%d has %d as a factor\n", num, count);
}
Create a program in which the user enters the outside temperature in Fahrenheit. If the
temperature is 60 or over,
print a comment that states "It is just fine outside." If the temperature is under 60, print a
comment that states "It's
not so warm outside." - Answers -#include<stdio.h>
int main (void)
{
int temp;
printf("\nEnter the outside temperature: ");
scanf("%d", &temp);
if (temp >=60)
{
, printf("It is just fine outside.\n");
}
else
{
printf("It's not so warm outside.\n");
}
}
Write a small C++ program that writes to the screen the make and model of the car or
bicycle you own (or one that
you wish to own) - Answers -#include <iostream>
using namespace std;
int main(void)
{
cout << "I own a Trek mountain bike.\n";
cout << "It was made in 2004.\n";
}
Rewrite the C program keyboard.c that you created earlier in this chapter to compile as
a C++ program. - Answers -#include<iostream>
using namespace std;
int main(void)
{
char string[50];
float my_money;
int weight;
cout << "\nEnter your First Name: ";
cin >> string;
cout << "\nEnter your Desired Monthly Income: ";
cin >> my_money;
cout << "\nEnter your friend's weight: ";
cin >> weight;
cout << "\n\n Recap\n";
cout << "I am " << string;
cout << " and I wish to have " << my_money;
cout << " per month";
cout << "\nI never would have guessed your friend weighs " << weight;
cout << "\n\n";
}
Create a C++ program that tests to see if the file accounts exists and prints a message
to say whether the file exists. - Answers -#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
, ifstream file("accounts");
if (file.fail())
cout << "Can't find the accounts file.\n";
else
{
cout << "The accounts file exits.\n";
}
}
Write a Perl script to display the line "Perl was developed by Larry Wall". - Answers -
#!/usr/bin/perl
# Program name: exercise1.pl
print ("Perl was developed by Larry Wall\n");
Write a Perl script in which you create a variable in the script to contain the name Beth
and then have the script
display "Welcome Beth." - Answers -#!/usr/bin/perl
# Program name: exercise2.pl
$name = "Beth";
print ("Welcome $name\n");
Modify the script you wrote in Exercise 2 so that you prompt for the name and then
display "Welcome name". - Answers -#!/usr/bin/perl
# Program name: exercise3.pl
print ("Enter your first name: ");
$name = <STDIN>;
print ("Welcome $name\n");
Write down four scalar variables, two of which are numeric and two that are strings. -
Answers -$value = 45;
$m = 90;
$furniture = "desk";
$car = "Ford";
Create a Perl program that uses an array of vegetables—peas, carrots, spinach, corn,
beans—and in which all of the vegetables in the array are displayed to the screen. -
Answers -#!/usr/bin/perl
# Program name: exercise5.pl
@vegetables = ("peas", "carrots", "spinach", "corn", "beans");
print ("The vegetables on my list are:\n");
print ("$vegetables[0]\n");
print ("$vegetables[1]\n");
print ("$vegetables[2]\n");
print ("$vegetables[3]\n");
print ("$vegetables[4]\n");