20631083-C-How-to-program-programs

Page 1

Program Write a program to convert digits into numbers.

Solution: #include<stdio.h> #include<conio.h> void f20(int); void f1to19(int); void main() { unsigned long int a,c,d,di,b=1000; clrscr(); printf("Enter the Amount in digit : "); scanf("%ld",&di); printf(“\n\n�); for(;di>=1;) { if(di>=1000) { for(a=0;b<=di;a++) { b=b*100; } b=b/100; for(;b>=1000;b=b/100,a--) { d=di/b; if(d>19) { c=d/10; f20(c); c=d%10; f1to19(c); } else f1to19(d); if(d!=0) { if(a>=3) printf(" Carror"); else if(a>=2&&a<3) printf(" Lakh"); else if(a<2) printf(" Thousand"); } di=di%b; } } else if(di>=100&&di<1000) { c=di/100; f1to19(c); printf(" Hundred"); di=di%100;


} else if(di<100) { if(di>19) { c=di/10; f20(c); c=di%10; f1to19(c); } else f1to19(di); di=di/100; } } getch(); } void f1to19(int n) { if(n==1) printf(" One"); else if(n==2) printf(" Two"); else if(n==3) printf(" Three"); else if(n==4) printf(" Four"); else if(n==5) printf(" Five"); else if(n==6) printf(" Six"); else if(n==7) printf(" Seven"); else if(n==8) printf(" Eight"); else if(n==9) printf(" Nine"); else if(n==10) printf(" Ten"); else if(n==11) printf(" Eleven"); else if(n==12) printf(" Twelve"); else if(n==13) printf(" Thirteen"); else if(n==14) printf(" Forteen"); else if(n==15) printf(" Fifteen"); else if(n==16) printf(" Sixteen"); else if(n==17) printf(" Seventeen"); else if(n==18) printf(" Eighteen"); else if(n==19) printf(" Ninteen"); } void f20(int n) { if(n==2) printf(" Twenty"); else if(n==3) printf(" Thirty"); else if(n==4) printf(" Forty"); else if(n==5) printf(" Fifty"); else if(n==6) printf(" Sixty"); else if(n==7) printf(" Seventy"); else if(n==8) printf(" Eighty"); else if(n==9) printf(" Ninty"); }


Chapter No : 04 Program no : 4.16 One interesting application of computers is drawing graphs and bar charts (sometimes called "histograms"). Write a program that reads five numbers (each between 1 and 30). For each number read, your program should print a line containing that number of adjacent asterisks. For example, if your program reads the number seven, it should print *******.

Solution : #include<conio.h> #include<stdio.h> void main() { int a,b,c; clrscr(); for(c=1;c<=5;c++) { printf("\nEnter the number between 1 and 30 \n"); scanf("%d",&b); if(b>1&&b<30) { for(a=1;a<=b;a++) { printf("*"); } printf("\n"); } else printf("Entered number is out of range\n"); } getch(); }

Program no : 4.23 Write a program that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range 1 through 256. If you are not familiar with these number systems, read Appendix D before you attempt this exercise.

Solution: #include<stdio.h> #include<conio.h> void main() { int a,b,no; float n; clrscr(); printf("Decimal No.

Binary Re.

Octal Re.

Hexal Re.\n");


for(a=1;a<=256;a++) { printf("%d ",a); for(n=a;n>=2;) { n=n/2; } for(;n<=a;) { b=n; no=b%2; printf("%d",no); n=n*2; } printf(" "); for(n=a;n>=8;) { n=n/8; } for(;n<=a;) { b=n; no=b%8; printf("%d",no); n=n*8; } printf(" "); for(n=a;n>=16;) { n=n/16; } for(;n<=a;) { b=n; no=b%16; printf("%d",no); n=n*16; } printf("\n"); } getch(); }

Program no : 4.28 Write a program that prints the following diamond shape. You may use printf statements that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with nested for statements) and minimize the number of printf statements.


* *** ***** ******* ********* ******* ***** *** *

Solution

:

#include<stdio.h> #include<conio.h> void main() { int a,i,j; clrscr(); printf("Enter the no of rows in odd no.s "); scanf("%d",&a); a=a/2+1; for(i=1;i<=a;i++) { for(j=1;j<=a-i;j++) { printf(" "); } for(j=1;j<=2*i-1;j++) { printf("*"); } printf("\n"); } for(i=1;i<a;i++) { for(j=1;j<=i;j++) { printf(" "); } for(j=2*a-2*i-1;j>=1;j--) { printf("*"); } printf("\n"); } getch(); }


Chapter No : 05 Program no : 5.9 A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write a program that will calculate and print the parking charges for each of three customers who parked their cars in this garage yesterday. You should enter the hours parked for each customer. Your program should print the results in a neat tabular format, and should calculate and print the total of yesterday's receipts. The program should use the function calculateCharges to determine the charge for each customer.

Solution: #include<stdio.h> #include<conio.h> float cal(float,int); void main() { float h,h1,h2,h3,c,c1,c2,c3; clrscr(); printf("Enter the hours of parking of \n"); printf("Car no:1 "); scanf("%f",&h1); printf("Car no:2 "); scanf("%f",&h2); printf("Car no:3 "); scanf("%f",&h3); printf("\n CAR HOURS CHARGE\n"); c1=cal(h1,1); c2=cal(h2,2); c3=cal(h3,3); h=h1+h2+h3; c=c1+c2+c3; printf("\n\nTotal %f %f ",h,c); getch(); } float cal(float h,int car) { int a=1; float hour,c=0; hour=h; while(a!=0) { if(h<=3) { c+=2.00; a=0; } if(h>3&&h<24) { c+=0.50;


h--; } if(h==24) { c=10.00; a=0; } } printf("\n %d return c;

%f

%f",car,hour,c);

}

Program no : 5.14 Define a function called hypotenuse that calculates the length of the hypotenuse of a right triangle when the other two sides are given. Use this function in a program to determine the length of the hypotenuse for each of the following triangles. The function should take two arguments of type double and return the hypotenuse as a double

Solution: #include<stdio.h> #include<conio.h> #include<math.h> int a=1; void cal(double , double); void main() { float as1,as2,bs1,bs2,cs1,cs2; clrscr(); printf("Triangle no:1 "); printf("\n Length of SIDE 1 : "); scanf("%f",&as1); printf(" Length of SIDE 2 : "); scanf("%f",&as2); printf("\nTriangle no:2 "); printf("\n Length of SIDE 1 : "); scanf("%f",&bs1); printf(" Length of SIDE 2 : "); scanf("%f",&bs2); printf("\nTriangle no:3 "); printf("\n Length of SIDE 1 : "); scanf("%f",&cs1); printf(" Length of SIDE 2 : "); scanf("%f",&cs2); printf("\n Triangle SIDE 1 SIDE 2 cal(as1,as2); cal(bs1,bs2); cal(cs1,cs2); getch(); } void cal(double s1,double s2)

Hypotenuse \n");


{ float fs,fs1,fs2; fs1=s1*s1; fs2=s2*s2; fs=fs1+fs2; fs=sqrt(fs); printf("\n %d a++;

%f

%f

%f ",a,s1,s2,fs);

}

Program no : 5.15 Write a function integerPower( base, exponent ) that returns the value of baseexponent

For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer, and base is an integer. Function integerPower should use for to control the calculation. Do not use any math library functions.

Solution: #include<stdio.h> #include<conio.h> void integerPower(int,int); void main() { int base,exponent; clrscr(); printf("Enter the BASE : "); scanf("%d",&base); printf("Enter the EXPONENT : "); scanf("%d",&exponent); integerPower(base,exponent); getch(); } void integerPower(int base,int exponent) { int a,power=1; for(a=1;a<=exponent;a++) { power=power*base; } printf("\n%d to the power %d = %d",base,exponent,power); }

Program no : 5.16 Write a program that inputs a series of integers and passes them one at a time to function even, which uses the remainder operator to determine if an integer is even. The function should take an integer argument and return 1 if the integer is even and 0 otherwise.


Solution: #include<stdio.h> #include<conio.h> int even(int); void main() { int a,b,c; clrscr(); for(a=1;a<=10;a++) { printf("Enter the No : "); scanf("%d",&b); c=even(b); if(c==1) printf("%d is an even integer\n\n",b); else printf("%d is not an even integer\n\n",b); } getch(); } int even(int b) { if(b%2==0) return 1; else return 0; }

Program no : 5.17 Write a function that displays at the left margin of the screen a solid square of asterisks whose side is specified in integer parameter side. For example, if side is 4, the function displays **** **** **** ****

Solution: #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter the No : "); scanf("%d",&a); printf("\n"); for(b=1;b<=a;b++)


{ for(c=1;c<=a;c++) printf("*"); printf("\n"); } getch(); }

Program no : 5.18 Modify the function created in Exercise 5.17 to form the square out of whatever character is contained in character parameter fillCharacter. Thus if side is 5 and fillCharacter is "#" then this function should print: #### #### #### ####

Solution: #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter the No : "); scanf("%d",&a); printf("\n"); for(b=1;b<=a;b++) { for(c=1;c<=a;c++) printf("#"); printf("\n"); } getch(); }

Program no : 5.19 Write program segments that accomplish each of the following: a. Calculate the integer part of the quotient when integer a is divided by integer b. b. Calculate the integer remainder when integer a is divided by integer b. c. Use the program pieces developed in a) and b) to write a function that inputs an integer between 1 and 32767 and prints it as a series of digits,with two spaces between each digit. For example, the integer 4562 should be printed as: 4 5 6 2

Solution:


#include<stdio.h> #include<conio.h> int g=1; void pieces(int); void main() { int a,b,q,r; clrscr(); printf("Enter the no a : "); scanf("%d",&a); printf("Enter the no b : "); scanf("%d",&b); q=a/b; pieces(q); g++; r=a%b; pieces(r); getch(); } void pieces(int a) { int c=1,p,b; b=a; if(g==1) printf("\nQuotient = "); if(g==2) printf("\nRemainder = "); while(c<a) { c*=10; } c/=10; while(c!=0) { p=b/c; printf("%d ",p); b=b%c; c=c/10; } if(a==0) printf("0"); }

Program no : 5.20 Write a function that takes the time as three integer arguments (for hours, minutes, and seconds) and returns the number of seconds since the last time the clock "struck 12." Use this function to calculate the amount of time in seconds between two times, both of which are within one 12-hour cycle of the clock.

Solution:


#include<stdio.h> #include<conio.h> long int sec(long int,long int,long int); void main() { long int diff,t1,t2,h1,m1,s1,h2,m2,s2; clrscr(); printf("Enter the 1st Time \n"); printf("\n Hours : "); scanf("%ld",&h1); printf(" Minutes : "); scanf("%ld",&m1); printf(" Seconds : "); scanf("%ld",&s1); t1=sec(h1,m1,s1); printf("\nEnter the 2nd Time \n"); printf("\n Hours : "); scanf("%ld",&h2); printf(" Minutes : "); scanf("%ld",&m2); printf(" Seconds : "); scanf("%ld",&s2); t2=sec(h2,m2,s2); if(t1>t2) diff=t1-t2; if(t2>=t1) diff=t2-t1; printf("\nThe amount of Seconds between two times is %ld sec.s",diff); getch(); } long int sec(long int h,long int m,long int s) { while(h>12) { h=h-12; } h=h*3600; m=m*60; s=s+m+h; return s; }

Program no : 5.21 Implement the following integer functions: a. Function celsius returns the Celsius equivalent of a Fahrenheit temperature. b. Function fahrenheit returns the Fahrenheit equivalent of a Celsius temperature. c. Use these functions to write a program that prints charts showing the Fahrenheit equivalents of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of all Fahrenheit temperatures from 32 to


212 degrees. Print the outputs in a neat tabular format that minimizes the number of lines of output while remaining readable.

Solution: #include<stdio.h> #include<conio.h> void ftoc(int); void ctof(int); void main() { int f,c=0; clrscr(); printf("Farenheit to Centigrade : Centigrade to Farenheit\n"); for(f=32;f<=212;f++) { ftoc(f); if(c<=100) ctof(c); c++; printf("\n"); } getch(); } void ftoc(int f) { float cf; cf=(f-32)* 0.555555555; printf(" %d %f",f,cf); } void ctof(int c) { float fc; fc=1.8*c+32; printf(" %d %f",c,fc); }

Program no : 5.22 Write a function that returns the smallest of three floating-point numbers.

Solution: #include<stdio.h> #include<conio.h> float con(float,int); float smallest(float,float,float); void main() { float a,b,c,s; int pa,pb,pc; clrscr();


printf("Enter three Numbers "); printf("\n\n1st Number and power of exponent : "); scanf("%f%d",&a,&pa); a=con(a,pa); printf("2nd Number and power of exponent : "); scanf("%f%d",&b,&pb); b=con(b,pb); printf("3rd Number and power of exponent : "); scanf("%f%d",&c,&pc); c=con(c,pc); s=smallest(a,b,c); printf("\nThe Smallest Number is %f",s); getch(); } float smallest(float a,float b,float c) { float small=a; if(small>b) small=b; if(small>c) small=c; return small; } float con(float n,int p) { int a,b,fp=1; if(p>0) { b=p; for(a=1;a<=b;a++) { fp=fp*10; } n=n*fp; } if(p<=0) { b= - p; for(a=1;a<=b;a++) { fp=fp*10; } n=n/fp; } return n; }

Program no : 5.23 An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 = 1 + 2 + 3. Write a function perfect that determines if


parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect. Challenge the power of your computer by testing numbers much larger than 1000.

Solution: #include<stdio.h> #include<conio.h> int perfectNumber(int); void main() { int a; clrscr(); for(a=1;a<=1000;a++) perfectNumber(a); getch(); } int perfectNumber(int a) { int b,c=0,d=0; for(b=1;c<=a&&b<a;b++) if(a%b==0) c=c+b; if(c==a) { for(b=1;d<=a&&b<a;b++) { if(a%b==0) { if(b!=1) printf("+"); printf("%d",b); d=d+b; } } printf(" = %d \n%d is a perfect number \n\n",a,a); } }

Program no : 5.24 An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. a. Write a function that determines if a number is prime. b. Use this function in a program that determines and prints all the prime numbers between 1 and 10,000. How many of these 10,000 numbers do you really have to test before being sure that you have found all the primes? c. Initially you might think that n/2 is the upper limit for which you must test to see if a number is prime, but you need go only as high as the square root of n. Why? Rewrite the program, and run it both ways. Estimate the performance improvement.


Solution: #include<stdio.h> #include<conio.h> void prime1(int); void prime2(int); void main() { int no; clrscr(); printf("Prime Number with limit n/2 for(no=1;no<=10000;no++) { prime1(no); prime2(no); } getch(); } void prime1(int no) { int a,p=1; for(a=2;a<=no/2;a++) { if(no%a==0) p++; } if(p==1) printf("\n %d",no); } void prime2(int no) { int a,p=1; for(a=2;a<=no*no;a++) { if(a==no) continue; if(no%a==0) p++; } if(p==1) printf(" %d",no); }

Prime Number with limit n*n ");

Program no : 5.25 Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367.

Solution: #include<stdio.h>


#include<conio.h> void reverse(unsigned long int); void main() { unsigned long int no; clrscr(); printf("Enter the number : "); scanf("%ld",&no); printf("\nNumber in Reverse order : "); reverse(no); getch(); } void reverse(unsigned long int a) { unsigned long int c,d=10; while(a>0) { c=a%d; printf("%ld",c); a=a/d; } }

Program no : 5.26 The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers. Write function gcd that returns the greatest common divisor of two integers.

Solution: #include<stdio.h> #include<conio.h> int gcd(int,int); void main() { int n1,n2,Gcd; clrscr(); printf("Enter the 1st No : "); scanf("%d",&n1); printf("Enter the 2nd No : "); scanf("%d",&n2); Gcd=gcd(n1,n2); printf("\nGreatest Common Divisor is %d ",Gcd); getch(); } int gcd(int n1,int n2) { int a,gcd=1; for(a=2;n1>0&&n2>0;a++) { if(n1%a==0||n2%a==0)


{ if(n1%a==0) n1=n1/a; if(n2%a==0) n2=n2/a; gcd=gcd*a; a--; } } return gcd; }

Program no : 5.27 Write a function qualityPoints that inputs a student's average and returns 4 if a student's average is 90–100, 3 if the average is 80–89, 2 if the average is 70–79, 1 if the average is 60–69, and 0 if the average is lower than 60.

Solution: #include<stdio.h> #include<conio.h> int qualityPoints(int); void main() { int a,qp; clrscr(); printf("Enter the Student's Average : "); scanf("%d",&a); qp=qualityPoints(a); printf("\nQuality Points = %d ",qp); getch(); } int qualityPoints(int a) { if(a>=90&&a<=100) return 4; if(a>=80&&a<=89) return 3; if(a>=70&&a<=79) return 2; if(a>=60&&a<=69) return 1; if(a<60) return 0; }


Name

Faisal Hafeez Roll No

CIIT/sp09-BCE-006/LHR Subject

Introduction to Computer and Programming Teacher Name

Sr. Imran Latif

COMSATS Institute of Information Technology


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.