Answer:
#define LSH_RL_BUFSIZE 1024
char *lsh_read_line(void)
{
int bufsize = LSH_RL_BUFSIZE;
int position = 0;
char buffer = malloc(sizeof(char) bufsize);
int c;
if (!buffer) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
while (1) {
// Read a character
c = getchar();
// If we hit EOF, replace it with a null character and return.
if (c == EOF || c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
// If we have exceeded the buffer, reallocate.
if (position >= bufsize) {
bufsize += LSH_RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer) {
fprintf(stderr, "lsh: allocation error\n");
exit(EXIT_FAILURE);
}
}
}
}
Explanation:
Answer:
import java.util.Scanner;
public class Supermarket
{
public static void main (String[] args)
{
String item;
double pounds, ounces, price,
total, unit;
Scanner scn = new Scanner(System.in);
System.out.print("Please enter the name of item ");
item = scn.nextLine();
System.out.print("Please enter the price of " + "the item per pound : ");
price = scn.nextDouble();
System.out.print("Enter the weight of " + "the item in pounds and " + "ounces respectively : ");
pounds = scn.nextDouble();
ounces = scn.nextDouble();
unit = price/16;
total = price * (pounds + ounces/16);
System.out.print("The unit price of " + "the products sold is : " + unit);
System.out.print("\nThe total cost of the " + "amount purchased is : " + total);
}
}
Explanation:
- Ask the user to enter the price of the item and the weight.
- Calculate the unit and the total price.
- Display the unit and the total price.
Answer:
//Program in C++.
// header
#include <bits/stdc++.h>
using namespace std;
// function to multiply number with 2-10
void multiplicationTable(int num)
{
cout<<"Number when multiplied with 2-10:"<<endl;
for(int a=2;a<=10;a++)
{
// multiply number with 2-10
cout << num << " * " << a << " = " << num*a << endl;
}
}
// driver function
int main()
{
// variable
int num;
cout<<"Enter a number:";
// read number from user
cin>>num;
// call the function
multiplicationTable(num);
return 0;
}
Explanation:
Read a number from user and assign it to variable "num".Call the function multiplicationTable() with parameter "num".In this function, multiply number with each from 2 to 10 and print the value.
Output:
Enter a number:5
Number when multiplied with 2-10:
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Answer:
Preprocessor directives are lines included in the code of programs preceded by a hash sign ( # ). ... The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated