2013 Computer Science 2nd Year Solved Exercise Chapter # 3 Programs Chapter # 4 Chapter # 5 Chapter # 6 Chapter # 7 Important Work Sheets Aumir Shabbir Pakistan School & College Muscat 3/4/2013
Chap # 3 Q#1. Write a program to find the sum of positive odd numbers and the product of positive even numbers less than or equal to 30. void main(void) int p,i,j,sum=0; while(j<=30) p=i*j; sum+=p; i=i+2; j=j+2; printf("the sum is equal to %d",sum); Q#2. Write a program that reads an integer and prints its table in descending order using for loop. void main(void) int n,i,s; printf("enter any no to get its table"); scanf("%d",&n); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 2 of 35
for(i=10;i>=0;i--) s=i*n; printf("%d*%d=%d\n",n,i,s); Q#2. Write a program that prints the square of all the numbers from 1 to 10. void main(void) int i; printf("number\tsquare\n"); for(i=1;i<=10;i++) printf("%d\t%d\n",i,i*i); Output Number. Square. 1 1 2 4 3 9...... 10 100 Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 3 of 35
Chap # 4 Q#2. Write a program that reads temperature and prints a message as given below. Temperature Message t>35 It is hot! t 20, t 35 Nice Day! T<20 It is cold! void main(void) int temp; printf("enter the temperature"); scanf("%d",&temp); if(temp>35) printf("it is hot!"); if(temp>=20 && temp<=35) printf("nice day"); if(temp<20) printf("it is cold!"); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 4 of 35
Q#3. Write a program that reads a phrase and prints the number of upper-case and lower-case in it. void main(void) char phrase[50]; int i,upper,lower; upper=0; lower=0; printf("enter any Phrase\n"); gets(phrase); for(i=0;i<strlen(phrase);i++) if(phrase[i]>=97 && phrase[i]<=122) lower=lower+1; if(phrase[i]>=65 && phrase[i]<=90) upper=upper+1; printf("the no of uppercase letter(s)= %d\n",upper); printf("the no of lowercase letter(s)= %d\n",lower); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 5 of 35
Q#4. Write a program that reads three numbers and prints the largest. void main(void) int a,b,c; printf("enter any three no.s and get the largest no."); scanf("%d %d %d",&a,&b,&c); if(a>b && a>c) printf("%d is the largest no ",a); else if(b>a && b>c) printf("%d is the largest no",b); else if(c>a && c>b) printf("%d is the largest no",c); else printf("all no. are equal"); Q#5. A class of 35 students took an Examination in which marks range from 0 to 100. Write a program which finds, a) The average marks. b) The number of students failed (Marks below 50). c) The number of students who scored 100 marks. void main() Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 6 of 35
int a[5],i,j=0,k=0,avg=0,sum=0; printf("enter the marks of 35 Students the range in 0-100\n if you exceed from this range then the program will be terminated"); for(i=0;i<5;i++) scanf("%d",&a[i]); if(a[i]<0 a[i]>100) printf("the range in 0-100 dont exceed"); exit(1); //for average for(i=0;i<5;i++) sum+=a[i]; avg=sum/5; printf("the average is=%d\n",avg); //for the no of students failed (marks below 50) for(i=0;i<5;i++) if(a[i]<50) j++; if(a[i]==100) k++; printf("there are %d no of stdents are failed\n",j); printf("there are %d no of stdents scored 100 mark\n",k); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 7 of 35
Q#6. Write a program that prints all odd positive integers less than 100 skipping those that are exactly divisible by 7. void main(void) int i; i=1; while(i<=99) if(i%7!=0) printf("%d",i); i=i+2; Q#8. Write a program that reads the coefficients, a, b and c of the quadratic equation, ax 2 + bx + c = 0 And prints the real solution of x, using the following formula, Note that if, x = b± b 2 4ac 2a b2 4ac = 0 Then there is only one real solution. If it is greater than zero than there are two real solutions and if it is less than zero then print the message, NO REAL SOLUTION. Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 8 of 35
#include<math.h> void main(void) int a,b,c; double d=1.0,ans=1.0,e=2.0,sq=1.0,solution=1.0,solution2=1.0; printf("enter the coefficients of the quadratic equation\n"); scanf("%d%d%d",&a,&b,&c); sq=b*b; d=sq-(4*a*c); ans=pow(d,1/e); if(ans==0.0) printf("there is only one real solution\n"); solution=(-b+ans)/2*a; printf("the Solution is =%lf",solution); if(ans>0.0) printf("there are two solution\n"); solution=(-b+ans)/2*a; solution2=(-b-ans)/2*a; printf("the 1st Solution is =%lf\n",solution); printf("the 2nd solution is =%lf\n",solution2); if(ans<0.0) printf("no real Solution\n"); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 9 of 35
Chap # 5 Q#5. Write a program that reads n integers and print the smallest along with its subscript value in the list. void main() int a[5],i,temp; printf("enter any 5 no."); for(i=0;i<5;i++) scanf("%d",&a[i]); temp=a[0]; for(i=0;i<5;i++) if(a[i]<temp) temp=a[i]; printf("the smallest no. is %d",temp); Q#6. Write a program that reads two integer arrays, a and b having 5 elements each and prints the sum of the products as given below, Sum = a[0] * b[0] + a[1] * b[1] +.+ a[4] * b[4] void main() Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 10 of 35
int a[4],b[4],sum=0,i; printf("enter values in 1st array"); for(i=0;i<5;i++) scanf("%d",a[i]); printf("enter values in 2nd array"); for(i=0;i<5;i++) scanf("%d",b[i]); for(i=0;i<5;i++) sum=sum+a[i]*b[i]; printf("the sum is=%d",sum); Q#7. Write a program that reads n floating point numbers and prints the sum positive numbers. void main() float a[10],sum=0; int i; printf("enter any 10 no.s"); for(i=0;i<10;i++) Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 11 of 35
scanf("%f",&a[i]); for(i=0;i<10;i++) if(a[i]>0) sum=sum+a[i]; printf("the sum of Positive no.s is=%f",sum); Q#8. For a floating point array x whose size is n, find the geometric mean. GM = n x 1. x 2. x 3 x n #include<math.h> void main(void) float a[10]; double p=1.0,gm,e=10.0; int i; printf("enter no. to get its geometric mean"); for (i=0;i<10;i++) scanf("%f",&a[i]); for (i=0;i<10;i++) p=p*a[i]; gm=pow(p,1/e); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 12 of 35
printf("the GM is= %lf",gm); Q#9. Write codes that will print the following patterns. a) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 void main() int i,j; for(i=1;i<=5;i++) print("\n"); for(j=1;j<=i;j++) printf("%d\t",j); b) 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 13 of 35
void main() int i,j; for(i=5;i>=1;i--) printf("\n"); for(j=1;j<=i;j++) printf("%d\t",j); c) 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 void main() int i,j; for(i=1;i<=5;i++) printf("\n"); for(j=i;j<=5;j++) printf("%d\t",j); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 14 of 35
d) * * * * * * * * * * * * * * * void main() int i,j; for(i=1;i<=5;i++) printf("\n"); for(j=1;j<=i;j++) printf("*\t"); Q#10. For two dimensional array x that has r rows and c columns, print the sum and average of each row. void main() int a[5][5],sum=0,i,j; float avg=1; printf("fill the Two dimensional array"); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 15 of 35
for(i=0;i<5;i++) for(j=0;j<5;j++) scanf("%d",&a[i][j]); for(i=0;i<5;i++) for(j=0;j<5;j++) sum=sum+a[i][j]; avg=sum/5; printf("the sum of %d row is=%d\n",i+1,sum); printf("the average is=%f\n",avg); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 16 of 35
Chap # 6 Q#2. Write a program that prints the larger of two numbers entered from the keyboard. Use a function to do the actual comparison of the two numbers. Pass the two numbers to the function as arguments and have the function return the answer with return (). int comparison(int,int); void main(void) int a,b,c; printf("enter any two no. to get the larger"); scanf("%d%d",&a,&b); c=comparison(a,b); printf("%d is the larger no.",c); int comparison(int x,int y) if(x>y) return x; else return y; Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 17 of 35
Q#3. Write a program using a function to calculate the area of a rectangle. int area(int,int); void main(void) int height,width,ans; printf("enter height of the rectangle\n"); scanf("%d",&height); printf("enter the width of the rectangle\n"); scanf("%d",&width); ans=area(height,width); printf("the area of the rectangle is %d",ans); int area(int a,int b) int c; c=a*b; return c; Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 18 of 35
Q#4. Write a program that produces the following table of temperature in Centigrade and Fahrenheit from 0 degrees to 50 degrees centigrade. Use a function for conversion. Centigrade Fahrenheit 0 32 5 10... 50 void conversion(); void main(void) int a,b,c; printf("--------------------------------\n"); printf("centigrade\t"); printf("fahrenheit\n"); printf("---------------------------------\n"); conversion(); void conversion() int c,f; for(c=0;c<=50;c=c+5) f=(9*c/5)+32; Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 19 of 35
printf("%d\t\t\t%d\n",c,f); Q#5. Write a program that reads a phrase and prints the number of lower-case letters in it using a function for counting. void couting(char[]); void main() char phrase[50]; printf("enter any phrase\n"); gets(phrase); couting(phrase); void couting(char p[]) int i,lower=0; for(i=0;i<strlen(p);i++) if(p[i]>=97 && p[i]<=122) lower++; printf("the no of lowercase letter(s)= %d",lower); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 20 of 35
Q#6. Write a program that reads n floating point number in an array and prints their product using a function. void product(float[]); void main(void) float a[5]; int i; printf("enter no. in the array to get it product\n"); for(i=0;i<5;i++) scanf("%f",&a[i]); product(a); void product(float a[]) float ans=1.0; int i; for(i=0;i<5;i++) ans=ans*a[i]; printf("the Product is %f",ans); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 21 of 35
Q#7. Write a program that reads numbers in two integer arrays, X and Y, of size m and n and prints them in ascending order using a function. void ascending(int[],int[]); void main(void) int i,a[5],b[5]; printf("enter the values in the 1st array\n"); for(i=0;i<5;i++) scanf("%d",&a[i]); printf("enter the values in the 2nd array\n"); for(i=0;i<5;i++) scanf("%d",&b[i]); ascending(a,b); void ascending(int a[],int b[]) int i,temp,j; temp=a[0]; for(j=4;j>=1;j--) for(i=0;i<j;i++) if(a[i]>a[i+1]) temp=a[i]; a[i]=a[i+1]; Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 22 of 35
a[i+1]=temp; for(j=4;j>=1;j--) for(i=0;i<j;i++) if(b[i]>b[i+1]) temp=b[i]; b[i]=b[i+1]; b[i+1]=temp; printf("the 1st array in ascending order\n"); for(j=0;j<5;j++) printf("%d\t",a[j]); printf("\nthe 2nd array in ascending order\n"); for(j=0;j<5;j++) printf("%d\t",b[j]); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 23 of 35
Chap # 7 Q#3. Write a program that will read a C source file and verify that the number of right and left braces in the file is equal. Use getc() function to read the file. #include<string.h> void main(void) FILE *fptr; int i,r,l; char ch,string[81]; r=0; l=0; fptr=fopen("student.txt","w"); while(strlen(gets(string))>0) fputs(string,fptr); fclose(fptr); fptr=fopen("student.txt","r"); if(fptr==null) puts("can not open file"); exit(); while((ch=fgetc(fptr))!=eof) if(ch=='(') l++; Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 24 of 35
if(ch==')') r++; if(l==r) printf("the Left and right brackets are equal "); else printf("the Left and Right brackets are not equal",l,r); fclose(fptr); Q#4. Write a program that will read names and marks of six subjects of five students and stores them in a file called result.txt. #include<string.h> void main(void) FILE *fptr; int i,j; char ch,name[40]; float marks; fptr=fopen("result.txt","w"); for(i=1;i<=5;i++) printf("enter the Name of student(%d)\n",i); scanf("%s",&name); fprintf(fptr,"%s",name); for(j=1;j<=6;j++) printf("the marks of %d subject",j); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 25 of 35
scanf("%f",&marks); fprintf(fptr,"%d",marks); fclose(fptr); Q#5. Write a program that will read names and marks of six subjects of five students from the result.txt file created in the previous question and prints each student s name along with his total and average marks. #include<string.h> void main(void) FILE *fptr; int i,j; char ch,name[40]; float avg=0,total=0,marks; fptr=fopen("result.txt","w"); for(i=1;i<=5;i++) printf("enter the Name of student(%d)\n",i); scanf("%s",&name); fprintf(fptr,"%s",name); for(j=1;j<=6;j++) printf("the marks of %d subject",j); scanf("%f",&marks); total=total+marks; fprintf(fptr,"%f",marks); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 26 of 35
avg=total/6; printf("the Total Marks are = %f\n",total); printf("the averge is = %f\n",avg); fprintf(fptr,"%f%f",total,avg); fclose(fptr); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 27 of 35
Important worksheets Format Specifiers There are many format specifiers defined in C. Take a look at the following list: %i or %d int %c char %f float %lf double %s string Note: %lf stands for long float. Let s take a look at an example of printf formatted output: main() int a,b; float c,d; a = 15; b = a / 2; printf("%d\n",b); printf("%3d\n",b); printf("%03d\n",b); c = 15.3; d = c / 3; printf("%3.2f\n",d); Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 28 of 35
Output of the source above: 7 7 007 5.10 As you can see in the first printf statement we print a decimal. In the second printf statement we print the same decimal, but we use a width (%3d) to say that we want three digits (positions) reserved for the output. The result is that two space characters are placed before printing the character. In the third printf statement we say almost the same as the previous one. Print the output with a width of three digits, but fill the space with 0. In the fourth printf statement we want to print a float. In this printf statement we want to print three position before the decimal point (called width) and two positions behind the decimal point (called precision)... Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 29 of 35
Syntax for getch () in C : variable_name = getch() accepts only single character from keyboard. The character entered through getch() is not displayed in the screen (monitor). Syntax for getche() in C : variable_name = getche(); Like getch(), getche() also accepts only single character, but unlike getch(), getche() displays the entered character in the screen. Syntax for getchar() in C : variable_name = getchar(); getchar() accepts one character type data from the keyboard. Syntax for gets() in C : gets(variable_name); gets() accepts any line of string including spaces from the standard Input device (keyboard). gets() stops reading character from keyboard only when the enter key is pressed. The unformatted output statements in C are putch, putchar and puts. Syntax for putch in C : putch(variable_name); putch displays any alphanumeric characters to the standard output device. It displays only one character at a time. Syntax for putchar in C : putchar(variable_name); putchar displays one character at a time to the Monitor. Syntax for puts in C : puts(variable_name); puts displays a single / paragraph of text to the standard output device. Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 30 of 35
Sample program : gets_puts.c #include<conio.h> void main() char a[20]; gets(a); puts(a); Program Algorithm / Explanation 1. header file is included because, the C in-built statements gets and puts we used in this program comes under stdio.h header files. 2. #include<conio.h> is used because the C in-built function getch() comes under conio.h header files. 3. main () function is the place where C program execution begins. 4. Array a[] of type char size 20 is declared. 5. gets is used to receive user input to the array. gets stops receiving user input only when the Newline character (Enter Key) is interrupted. 6. puts is used to display them back in the monitor. Output : Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 31 of 35
Array Array is a collection of homogenous data stored under unique name. The values in an array are called as 'elements of an array.' These elements are accessed by numbers called as 'subscripts or index numbers.' Arrays may be of any variable type. Array is also called as 'subscripted variable.' Types of an Array: 1.One / Single Dimensional Array 2. Two Dimensional Array Single / One Dimensional Array: The array which is used to represent and store data in a linear form is called as 'single or one dimensional array.' Syntax: <Data-type> <array_name> [size]; Example: int a[3] = 2, 3, 5; char ch[20] = "TechnoExam" ; float stax[3] = 5003.23, 1940.32, 123.20 ; Total Size (in Bytes): Total size = length of array * size of data type In above example, a is an array of type integer which has storage size of 3 elements. The total size would be 3 * 2 = 6 bytes. Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 32 of 35
Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 33 of 35
Features: Array size should be positive number only. String array always terminates with null character ('\0'). Array elements are countered from 0 to n-1. Useful for multiple reading of elements (numbers). Disadvantages: There is no easy method to initialize large number of array elements. It is difficult to initialize selected elements Two Dimensional Array The array which is used to represent and store data in a tabular form is called as 'two dimensional array.' Such type of array specially used to represent data in a matrix form. The following syntax is used to represent two dimensional array. Syntax: <data-type> <array_nm> [row_subscript][column-subscript]; Example: int a[3][3]; In above example, a is an array of type integer which has storage size of 3 * 3 matrix. The total size would be 3 * 3 * 2 = 18 bytes. It is also called as 'multidimensional array. Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 34 of 35
Limitations of two dimensional array: 1. We cannot delete any element from an array. 2. If we don t know that how many elements have to be stored in a memory in advance, then there will be memory wastage if large array size is specified. Aumir Shabbir Pakistan School Muscat (Class: XII) Page # 35 of 35