|
Question 1.
Is interfaces in Java, a kind of polymorphism? justify with a java program.
|
|
Question 2.
Question 1. Question 5. Write a program which has the following seven functions. The functions should be:
•main() this calls the other 6 functions
•fget_long() a function which returns a long data type from a file
•fget_short() a function which returns a short integer variable from a file
•fget_float() a function which returns a floating point variable from a file
•fprt_long() a function which prints its single, long argument into a file
•fprt_short() a function which prints its single, short argument into a file
•fprt_float() a function which prints its single, floating point argument into a file.
You should use fscanf() to get the values of the variables from the input (the file) and fprintf() to print the values to the other file. Pay attention to using the correct format for each of the data types.
|
|
Question 3.
Question 5. Write a program which has the following seven functions. The functions should be:
•main() this calls the other 6 functions
•fget_long() a function which returns a long data type from a file
•fget_short() a function which returns a short integer variable from a file
•fget_float() a function which returns a floating point variable from a file
•fprt_long() a function which prints its single, long argument into a file
•fprt_short() a function which prints its single, short argument into a file
•fprt_float() a function which prints its single, floating point argument into a file.
You should use fscanf() to get the values of the variables from the input (the file) and fprintf() to print the values to the other file. Pay attention to using the correct format for each of the data types.
|
|
Question 4.
Write a program to print this triangle:
*
***
*****
*******
*********
************
Answer:
include<stdio.h>
include<conio.h>
void main()
{
int i;
for(i=1;i<=5;i++)
{
for(j=1;j<=I;i++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}
|
|
Question 5.
Write a program to compute the average of the ten numbers 1, 4, 9, ..., 81, 100 (i.e., the average of the squares of the numbers from 1 to 10).
Answer:
include<stdio.h>
include<conio.h>
void main()
{
int average, sum=0, i;
for (i=1;i<=10;i++)
{
sum = sum + i * i;
}
average = sum/10;
printf(“average is %d”, average);
getch();
}
|
|
Question 6.
Write a chequebook balancing program of an user’s account. It will use getline() to read a line, which will contain either the word “withdrawl” or “deposit”. The next line will contain the amount of the withdrawl or deposit. After reading each pair of lines, the program should compute and print the new balance. You can declare the variable to hold the running balance to be datatype float, and you can use the function atof() to convert an amount string read by getline() into a floating-point number. When the program reaches end-of-file, it should exit.
For example, given the input
1. deposit
2. 100
3. withdrawl
4. 12.34
5. withdrawl
6. 49.00
7. deposit
8. 7.01
the program should print something like
balance: 100.00
balance: 87.66
balance: 38.66
balance: 45.67
Answer:
include<stdio.h>
include<conio.h>
void main()
{
float balance, final balance = 0;
int byte_read;
int nbyte = 10;
char * my_string, * my_balance;
puts (“user’s account”);
for (i=1;i<4;i++)
while(byte_read= get line (&my_string, &nbyte, Stdin))!=-1)
{
printf(“%s”, mu_balance);
balance = atof (my_balance);
}
final balance = final_balance + balance;
printf(“balance = %f”, final_balance);
exit();
}
|
|
Question 7.
Using Pointers,
a. Write a line-reversing function.
b. Write the character-counting function.
c. Write the string-concatenation program
d. Write the string-replacing function
Answer: (a)
int char_count (char str [])
{
int l=0;
char * ptr;
ptr = &str[0];
while(*ptr ! = ‘10’)
{
l++
ptr++;
}
return l;
(b)
void line_reverse (char str [])
{
int I = 0;
char * ptr, str 2[20];
l = strlen(str);
ptr = & str [l – 1];
while (l > =0)
{
str 2 [i] = *ptr;
i++;
str--;
}
str [i] = ‘10’;
puts (str2);
}
(c)
void string_concate (char * str1, char * str2, char * str3);
{
while (*str1 ! = ‘10’)
{
*str3 = *str1;
str3++;
str1++;
}
while (*str2 ! = ‘10’)
{
*str3 = 8str2;
str3++;
str2++;
}
str3 = ‘10’;
}
|
|
Question 8.
Write a program which has the following seven functions. The functions should be:
•main() this calls the other 6 functions
•fget_long() a function which returns a long data type from a file
•fget_short() a function which returns a short integer variable from a file
•fget_float() a function which returns a floating point variable from a file
•fprt_long() a function which prints its single, long argument into a file
•fprt_short() a function which prints its single, short argument into a file
•fprt_float() a function which prints its single, floating point argument into a file.
You should use fscanf() to get the values of the variables from the input (the file) and fprintf() to print the values to the other file. Pay attention to using the correct format for each of the data types.
|
|
Question 9.
Write a function celsius() to convert degrees Fahrenheit to degrees Celsius.
Note: The conversion formula is °C = 5/9 * (°F - 32)
Answer:
float celcius (float f)
{
int c;
c = (5/9) * (f=32);
return c;
}
|
|
Question 10.
Write a program in C that prompts the user with the following lines:
a) Add two integers
c) Compare two integers for the larger
t) Test an integer for odd or even
q) Quit
Answer:
include<stdio.h>
include<conio.h>
void main()
{
int a,b,c, choice;
printf(1. Add two integers”);
printf(“\n 2. Compare two integers for the larger”);
printf(“\n 3. Test an integers for odd or even”);
printf(“\n 4. Quit);
printf(“enter your choice);
scanf(“%d”, &choice);
switch(“choice”)
{
case 1: printf(“enter a, b”);
scanf(“%d%d”, &a, &b);
c= a+b;
printf(“sum is %d”, c);
break;
case 2: printf(“enter a, b);
scanf(“%d%d”, &a, &b)
if(a>b)
printf(“Lagrest is %d”, a);
else
printf(“largest is %d”, b);
break;
case 3: print(“Enter the number”);
scanf(“%d”, & num);
if(num %2==0)
printf(“number is even”);
else
printf(“number is odd);
break;
case 4:
exit();
default:
printf(“Invalid choice”);
}
getch();
}
|
|
Question 11.
Write a simple program to find out how many of the numbers from 1 to 10 is greater than 4.
Answer:
include<stdio.h>
include<conio.h>
void main()
{
int i, count = 0;
for(i=1;i<=10;i++)
{
if(i>4)
count++;
}
printf (“number greater than 4 is %d”, count);
getch();
}
|