Fundamentals of Computer Programming - Unit Wise Questions
1. Draw the flow chart for finding largest of three numbers and write an algorithm and explain it.
Algorithm:
1. Start
2. Input A,B,C
3. If (A>B) and (A>C) then print “A is greater”.
Else if (B>A) and (B>C) then print “B is greater”.
Else print “C is greater”.
4. Stop
Flowchart:
AI is thinking...
3. Write a program for the interest charged in installments for following case. A cassette player costs Rs. 2000. A shopkeeper sells it for Rs. 100 down payment and Rs. 100 for 21 more months. What is the monthly interest charged?
#include<stdio.h>
#include<conio.h>
void main()
{
float c_price, t_price, t_interest, m_interest; //cost price, total price, total interest, monthly interest
c_price=2000;
t_price=100+(100*21); //down payment Rs. 100 and Rs. 100 for 21 installments
t_interest=(t_price - c_price)/c_price*100;
m_interest=t_interest/22; //payment is for 22 months
printf("\\nThe monthly interest charged is %f", m_interest); //result is 0.45% per month
getch();
}
AI is thinking...
2. Find the value of “a” in each of the following statements:
int i=3 , j=4 ,k=8
float a=4.5 ,b=6.5,c=3.5
(a) a = b- i/k +c/k
(b) a = (b-k)/j + (j + c)/k
(c) a = c-(( i + j)/(k + i))*b
(d) a = c – i + j / k+ i * b
(e) a = c + j % 2 +b
(f) a = (b + 1) % (c + 1).
a) a = b-i/k+c/k = 6.5-3/8+3.5/8 = 6.5-0+0.4375 = 6.9375
b) a = (b-k)/j+(j+c)/k = (6.5-8)/4+(4+3.5)/8 = 0.5625
c) a = c-((i+j)/(k+i))*b = 3.5-((3+4)/(8+3))*6.5 = 3.5-0*6.5 = 3.5
d) a = c-i+j/k+i*b = 3.5-3+4/8+3 *6.5 = 3.5-3+0+19.5 = 0.5 + 19.5 = 20
e) a = c+j%2+b = 3.5+4%2+6.5 = 3.5+0+6.5 = 10
f) a = (b+1)%(c+1) = (6.5+1)%(3.5+1) = 7.5%4.5 = Not valid
AI is thinking...
2. Determine which of the following are valid identifiers? If invalid, explain why?
(a) record1 (b)record
(c) file_3 (d) return
(e) #tax (f) name
(g) goto (h) name and address
(i) name-and-address (j) 123-45-6789
(k) Void (l) name_address
AI is thinking...
3. Write a program to find the factorial of a given integer.
AI is thinking...
7. Write a program to count the number of words in a sentence.
AI is thinking...
4. Write a program that uses a “for” loop to compute and prints the sum of a given numbers of squares.
#include<stdio.h>
int main()
{
int i, n, sum=0;
printf("Enter n value: ");
scanf("%d", &n);
for(i=0; i<=n; i++)
{
sum += (i*i);
}
printf("Sum of squares of first %d natural numbers = %d", n, sum);
return 0;
}
AI is thinking...
5. Write a program to obtain the product of the following matrices and explain it:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3]={{3,5,7},{2,-3,4,{4,5,2}};
int b[3][2]={{7,6},{6,-5},{4,3}};
int i, j, k, c[3][2];
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("%d", c[i][j]);
}
printf("\\n");
}
getch();
}
AI is thinking...
3. Define printf() function, header file and main function. Find the value of following expression. Use the value initially assigned to the variables for each expressions. [4.5+1.5]
int a = 8, b = 5;
float x = 0.005, y = -0.01;
1) 2 * ((a / 5) + 4 * (b – 3)) % (a + b - 2));
2) (x > y) && (a > 0) || (b < 5);
3) (a > b)?a:b;
printf() function
printf is an output function which has been defined in stdio.h file. What ever is put inside the function printf between two double quotes is printed on the screen.
E.g. printf(“Welcome to C programming”);
Output: Welcome to C programming
Header file
A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.
You request to use a header file in your program by including it with the C preprocessing directive #include, like you have seen inclusion of stdio.h header file, which comes along with your compiler.
main function
Every C-programs needs to have the main function. Each main function contains 2 parts.
Declaration part: The declaration part declares all the variables used in the executable part.
Executable part: There is at least one statement in the executable part. These two parts must appear between the opening and closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function is the logical end of the program.
E.g.
int main( )
{
int a=10;
printf(“%d”, a);
return 0;
}
Now,
Given,
int a = 8, b = 5;
float x = 0.005, y = -0.01;
1) 2 * ((a / 5) + 4 * (b – 3)) % (a + b - 2));
= 2*((8/5)+4*(5-3))%(8+5-2)
= 2*(1+4*2)%11
= 2*9 % 11
= 18 % 11
= 7
2) (x > y) && (a > 0) || (b < 5);
= (0.005>-0.01)&&(8>0)||(5<5)
= (1)&&(1)||(0)
= 1|| 0
= 1
3) (a > b)?a:b;
= (8>5)?8:5;
= 8 [Using ternary principle)
AI is thinking...
6. Write a function to add, subtract, multiply, and divide two complex numbers (x +iy) and (c + id).
AI is thinking...
6. Write a function to multiply two x matrices.
AI is thinking...
8. What is a pointer and explain its applications? Write a program that uses pointers to copy an array of double.
OR
Define a pointer. Write a function that is passed an array of n pointers to the maximum of the floats.
AI is thinking...
9. Define a structure of employee having data members name, address, age, and salary. Take data for employee in an array dynamically and find the average salary.
AI is thinking...
7. Write a program which will read a line and delete from it all occurrences of the word
“that”.
AI is thinking...
8. Discuss different file openings modes. Write a program to read all the numbers from the input file “value.dat” and store only even numbers in an output file named as “result.res”.(2+4)
AI is thinking...
9. Write a program to read the data file which has following details.
a. Name b. Age c. Test player d. Total run.
AI is thinking...
10. Some text file is given; create another text file replacing the following words “Ram” to “Hari”, “Sita” to “Gita”, and “Govinda” to “Shiva”.
OR
What are the uses of graphical function? Explain the basic graphical function with suitable program.
AI is thinking...
10. Write a program to create a file "RECORD.TXT" then store roll no, name and percentage of 10 students. [6]
AI is thinking...
10. Given a text file, create another text file deleting the following words “three”, “bad”, and “time”.
OR
Why do you require graphical function? Explain the basic graphical function with suitable program.
AI is thinking...
9. List any five names of graphics function. Write a program to read line of text then count no. of vowels, No. of digits and no. of spaces. [2+4]
Some names of graphics function:
initgraph(): it is one of the function that is used to initialize the computer in graphics mode. Its syntax is as
initgraph(&gdriver, &gmode, "graphics driver path");
closegraph(): It is the graphical function that is used to close the graphics mode initialized by the initgraph() function.
setcolor(color): It is a function that is used to set the color of the drawing object with given color. The color is indicated by the integer from 0 to 15 ( 16 color)
lineto(x, y): draws the line from current position to (x,y) position.
clrscr(): It is used to clear the screen and locates the curser at the beginning of the screen.
cputs(string): it writes a string given to function to the user defined window screen.
putch(char) : it writes a character given to function to the user defined area of window.
line(a, b, c, d): draws line from (a,b) to (c,d).
circle(x, y, r): prints circle with center (x,y) and radius r.
rectangle(a, b, c, d): prints rectangle where (a,b) is upper left coordinate and (c,d) is lower right co-ordinates of rectangle.
Program to read line of text then count no. of vowels, No. of digits and no. of spaces:
#include <stdio.h>
void main()
{
char str[200];
int i, vowels=0, digits=0, spaces=0;
printf("Enter a string\\n");
gets(str);
for(i=0;str[i]!='\\0';i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||str[i]=='o' || str[i]=='u' || str[i]=='A' ||str[i]=='E' || str[i]=='I' || str[i]=='O' ||str[i]=='U')
{
vowels++;
}
else if(str[i]>='0' && str[i]<='9')
{
digits++;
}
else if (str[i]==' ')
{
spaces++;
}
}
printf("\\nVowels = %d",vowels);
printf("\\nDigits = %d",digits);
printf("\\nWhite spaces = %d",spaces);
}
AI is thinking...