(X) Ch-14 C programming

1. Fill in the blanks: (a) C is a strucutured programming language. (b) C language was originally developed by Dennis Ritchie in 1972. (c)printf() is a output function in C language. (d) PRINT is a output statement in QBASIC. (e) Every C program must start with main() function. (f) AND (&&) operator produces true result if all given condition are true. (g) In C language, remainder is found using % operator. (h) In C language, every line of the program must terminatewith semicolon. (i) printf() function is defined in stdio.h header file. 2. State wehter each of the following statements is True or False. (a) In C program, variables must be declared before they are used. TRUE (b) In QBASIC program, variables must be declared before they are used. FALSE (c) printf() in C is equivalent to INPUT statement in QBASIC. FALSE (d) In C language, void must return a value. FALSE (e) The total number of keywords in C language is 28. FALSE (f) % operator is used to find percentage in C language. FALSE (g) clrscr() function is defined in "stdio.h". FALSE 3. Write the purpose of the following functions/operators in C language. (a) printf() - used to display output on screen. (b) scanf() - used to get input from keyboard. (c) % - used to get remainder (d) && - used for AND operation (e) ||- used for OR operation (f) getch() - Used to halts the output screen to press any single character from keyboard. 4. Answer the following questions. (a) Define C language. Who developed it? Ans: C is a structure programming language that divides a program into many functions , which is developed by Dennis Ritchie at Bell Laboratories in 1972 AD. (b) Write the important features of C language. Ans: - It is powerful and highly portable Language. - It is a structured programming language because the program is divided into a number of functions. - It is a general purpose high level programming language. - It is an internationally standardized programming language. (c) List the basic data types with their memory consumption in C. Ans: char - 1 Byte , int - 2 Bytes float - 4 Bytes, double - 8 Bytes (d) Write the formart specifier of basic data types of C language. Ans: char - %c , int - %d float - %f , double - %lf (e) Define variable. Write down the rules for writing a variable name. Ans: An identifier that is used to represent some specified single data item that is numeric or string type is called variable. Rule for naming variables in C: - The first character of a variable name must be a character. - Both uppercase and lowercase letters are permitted. - Uppercase and lowercase letters are not interchangeable. - Space is not allowed in variable name. (f) Differentiate between local and global variables.

Ans:
Global Variable Local Variable
1. Variable declared outside a function is called Global variable. 1. Variable declared inside the procedure is called Local Variable.
2. It is visible to other functions or modules. 2. It is not visible to other nodules or functions.
3. Example:
new() { // Local variables declaration int a=92; int b; int c=a+b; } Here, a,b, and c are global variable.
3. Example:
int a; main() { a=10; } function1() { int b; b=a; } Here, variable a is called global variable.

(g) What is a global variable? Give an example of its declaration. Ans: The variable which is declared outside any function is called global variable. They are visible to other functions or modules. For example: int a; main() { a=10; } function1() { int b; b=a; } Here, variable a is called global variable. (h) What do you understand by decision making statements? List the decision making statements supported by C. Ans: Decision making statements choose among different courses of actions available during a program’s execution. C has two major decision making statements. They are: (1) if statement (2) switch statement (i) What are arithmetic operators? Write them in their hierarchy. Ans: The operators, which are used in mathematical calculations are called arithmetic operators. Hierarchy of operators: 1st : () 2nd : *,/,% 3rd : + , - (j) What is a header file? Give any two examples. Ans: The header files are system files are system files stored with extension ".h" that contain pre-defined functions. To use functions provided by "C language" we need to includes specific header file. Two examples of header files are: (i) stdio.h (ii) math.h (k) What is loop? List the different looping statements used in C language. Ans: A control structure that executes the same program statement or block of program statements repeatedly for a specified number of times or till the given condition is satisfied is called Loop. Different looping statements supported by C language are: 1. for loop 2. while loop 3. do-while loop 5. Evaluate the expressions and find out the final values for the following: (Suppose a=2,b=3,c=4) (a) s = (a+b)/3 ANSWER: S = (2+3) / 3 = 5/3 = 1.666667 (b) s= 40%(a+b+c) ANSWER: S = 40 % (2+3+4) = 40 % 9 = 4 (c) s = a/b*c/b+c*a ANSWER: S = 2/3 * 4/3 + 4 * 2= 0.666667*1.333333+8 = 0.888889 + 8 = 8.888889 (d) s=(a+b)/(a*c) ANSWER: S = (2+3) / (2*4) = 5 / 8 = 0.625000 (e) s=(a+b+c)+(a+b+c)%2 ANSWER: S = (2+3+4)+(2+3+4) % 2 = 9 + 9 %2 = 9 + 1 = 10

6. Write down C programs for the following:
a. To supply a number and then find 10% and 20% of that number and print the output.
#include<stdio.h> main() { float n,x,y; printf("Enter a number "); scanf("%f",&n); x=(n*10)/100; y=(n*20)/100; printf("Ten Percent = %0.2f ",x); printf("Twenty Percent = %0.2f ",y); }
b. To find the simple interest where P= 5000, R= 12, T=2. (hint: si = (prt)/100)
#include<stdio.h> main() { int p=5000,r=12,t=2,si; si=(p*t*r)/100; printf("Simple Interest=%d",si); }
C. To find the area of a rectangle, where the user supplies length and breadth from the keyboard.
#include<stdio.h> main() { int l,b,a; printf("Enter Length "); scanf("%d",&l); printf("Enter Breadth "); scanf("%d",&b); a=l*b; printf(" Area = %d ",a); }
d. To find area of four walls where Area=2H(L+B).
#include<stdio.h> main() { int l,b,a; printf("Enter Length "); scanf("%d",&l); printf("Enter Breadth "); scanf("%d",&b); a=2*(l+b); printf(" Area of four walls = %d ",a); }
e. To print "Pass" or "Fail" inputting percentage from the keyboard. If the percentage is >=40 then print "Pass" otherwise "Fail".
#include<stdio.h> main() { float per; printf(" Enter your percentage "); scanf("%f",&per); if(per>=40) printf("Pass"); else printf("Fail"); }
f. To input a number and then print whether number is fully divisible by 5 or not.
#include<stdio.h> main() { int n,r; printf(" Enter any number "); scanf("%d",&n); r=n%5; if (r==0) printf("%d is divisible by 5",n); else printf("%d is not divisible by 5",n); }
g. To input three numbers and to print the smallest number.
#include<stdio.h> main() { int num1,num2,num3; printf("Enter three numbers"); scanf("%d%d%d",&num1,&num2,&num3); if(num1<num2&&num1<num3) printf("%d is smallest",num1); else if(num2<num1&&num2<num3) printf("%d is smallest",num2); else printf("%d is smallest",num3); }
h. To input a number and then print whether number is fully divisible by 2 and 3 or not.
#include<stdio.h> main() { int n,r1,r2; printf("Enter a number : "); scanf("%d",&n); r1=n%2; r2=n%3; if(r1==0&&r2==0) printf(" %d is divisible by 2 and 3 ",n); else printf(" %d is not divisible by 2 and 3 ",n); }
i. To print all numbers from 100 to 1 with their sum.
#include<stdio.h> main() { int i,sum=0; for(i=100;i>=1;i--) { printf("%d\t",i); sum=sum+i; } printf("Their sum = %d ",sum); }
j. To print the following series using for, while and do-while loops:
(i) 100, 98, 96... 10th terms.
for loop while loop do-while loop
#include<stdio.h> main() { int i,a=100; for(i=0;i<10;i++) { printf("%d\n",a); a=a-2; } }
#include<stdio.h> main() { int i=0,a=100; while(i<10) { printf("%d\n",a); a=a-2; i++; } }
#include<stdio.h> main() { int i=0,a=100; do { printf("%d\n",a); a=a-2; i++; }while(i<10); }
(ii) 1,4,9, 16 up to 10th terms.
for loop while loop do-while loop
#include<stdio.h> main() { int i; for(i=1;i<=10;i++) { printf("%d\n",i*i); } }
#include<stdio.h> main() { int i=1; while(i<=10) { printf("%d\n",i*i); i++; } }
#include<stdio.h> main() { int i=1; do { printf("%d\n",i*i); i++; }while(i<10); }
(iii) 2 8 18 32 up to 10th terms
for loop while loop do-while loop
#include<stdio.h> main() { int i,a; for(i=1;i<=10;i++) { a=i*i*2; printf("%d\n",a); } }
#include<stdio.h> main() { int i=1,a; while(i<=10) { a=i*i*2; printf("%d\n",a); i++; } }
#include<stdio.h> main() { int i=1,a; do { a=i*i*2; printf("%d\n",a); i++; }while(i<=10); }
(iv) 2 2 4 6 10 16 up to 10th terms.
for loop while loop do-while loop
#include<stdio.h> main() { int a=2,b=2,c,i; for(i=0;i<10;i++) { printf("%d\n",a); c=a+b; a=b; b=c; } }
#include<stdio.h> main() { int a=2,b=2,c,i=0; while(i<10) { printf("%d\n",a); c=a+b; a=b; b=c; i++; } }
#include<stdio.h> main() { int a=2,b=2,c,i=0; do { printf("%d\n",a); c=a+b; a=b; b=c; i++; }while(i<10); }
End of Chapter-14