Posts

Showing posts from 2006

Calculation of Sum, Average and Standard Deviation using Functions and Pointers.

Write a function that receives 5 integers and returns the sum, average and standard deviation of these numbers. Call this function from main() and print the results in main(). #include <stdio.h> #include <math.h> int calc ( float a , float b , float c , float d , float e , float * sum , float * avg , float * sd ) ; int main ( ) { float a , b , c , d , e , sum = 0.0 , avg = 0.0 ; float sd = 0.0 ; printf ( "Enter Five Numbers:" ) ; scanf ( "%f %f %f %f %f" , & a , & b , & c , & d , & e ) ; calc ( a , b , c , d , e , & sum , &amp; avg , & sd ) ; printf ( " \n Sum=%f" , sum ) ; printf ( " \n Average=%f" , avg ) ; printf ( " \n Standard Deviation=%f \n " , sd ) ; getchar ( ) ; return 0 ; } calc ( float a , float b , float c , float d , float e , float * sum , float * avg , float * sd ) { float Calc = 0.0 ; * sum = a + b + c + d + e ; * avg = * sum / 5.0 ; Calc +

Calculation of Product of Two Numbers using Function - Returns a Float

This program seems to be rather simple. But there's one little thing to be noted in this particular program. The thing is that the function in this program returns a float. The function declaration is usually given outside main..but due to some other standards that I am following, I have prototyed it inside main..but that doesn't cause much of a difference in this simple program. Write a function which receives a float and an int from main(), finds the product of these two and returns the product which is printed through main(). #include <stdio.h> main ( ) { int i ; float j , prod ; float product ( int x , float y ) ; printf ( "Enter the i(int) and j(float):" ) ; scanf ( "%d %f" , & i , & j ) ; prod = product ( i , j ) ; printf ( "Product:%f" , prod ) ; } product ( int x , float y ) { float product ; product = x * y ; return ( product ) ; } The program can be found at: Download File.

C Program - Calculation of Area and Circumference of a Circle using Pointers

The following program is one good example that illustrates how we can return more than one value in a function. The answer is certainly using Pointers. The following program demonstrates the method. Write a function that calculates both Area and Perimeter/ Circumference of the Circle, whose Radius is entered through the keyboard. #include <stdio.h> main ( ) { int radius ; float area , perimeter ; printf ( " \n Enter radius of a circle:" ) ; scanf ( "%d" , & radius ) ; areaperi ( radius , & area , & perimeter ) ; printf ( "Area=%f" , area ) ; printf ( " \n Perimeter=%f" , perimeter ) ; } areaperi ( int r , float * a , float * p ) { * a = 3.14 * r * r ; * p = 2 * 3.14 * r ; } // This Program exhibits the use of Call By Reference . The program can be found at: Download File.

Determination of Prime Factors using Functions.

A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number. For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7. #include <stdio.h> main ( ) { int number ; int prime ( int number ) ; int primefactor ( int number ) ; printf ( "Enter the number whose prime factors are to be calculated:" ) ; scanf ( "%d" , & number ) ; primefactor ( number ) ; } // The following function detects a Prime number . prime ( int num ) { int i , ifprime ; for ( i = 2 ; i < = num - 1 ; i + + ) { if ( num % i = = 0 ) { ifprime = 0 ; } else ifprime = 1 ; } return ( ifprime ) ; } // The following function prints the prime factors of a number . primefactor ( int num ) { int factor , ifprime ; for ( factor = 2 ; factor < = num ; ) { prime ( factor ) ; // so that the factors are only prime and nothing else . if ( ifprim

Calculation of A to the power of B using Functions

Write a function power(a,b), to calculate the value of a raised to b. #include <stdio.h> main ( ) { int power ( a , b ) ; int a , b , result ; printf ( "Enter the value of a and b:" ) ; scanf ( "%d %d" , & a , & b ) ; result = power ( a , b ) ; printf ( "%d raised to %d is %d" , a , b , result ) ; } power ( int a , int b ) { int calculation = 1 , calc ; for ( calc = 1 ; calc < = b ; calc + + ) { calculation = calculation * a ; continue ; } return ( calculation ) ; } The File can be found at: Download File

Conver the given year to Roman Numerals using Functions.

Write a general-purpose function to convert any given year into its roman equivalent. The following table shows the roman equivalents of decimal numbers: Decimal:........Roman 1.....................i 5....................v 10..................x 50..................l 100................c 500...............d 1000.............m Example: Roman equivalent of 1988 is mdcccclxxxviii Roman equivalent of 1525 is mdxxv This program is a big lengthy owing to the use of Case Statements. This program can also be rewritten using Arrays, which will reduce the length considerably. #include <stdio.h> main ( ) { int year ; int convert ( int year ) ; { printf ( "Note:Enter a four year digit year. \n \n " ) ; printf ( "Enter the year that you wanna convert to Roman: " ) ; scanf ( "%d" , & year ) ; if ( year > 1999 ) { printf ( "Invalid Year.Please enter again. \n \n " ) ; } } convert ( year ) ; } convert ( int year ) {

Detection of Leap year using Functions.

Any year is entered through the keyboard. Write a function to determine whether the year is a leap year or not. #include <stdio.h> main ( ) { int leap_year ( year ) ; int year , lp ; printf ( "Enter the year:" ) ; scanf ( "%d" , & year ) ; lp = leap_year ( year ) ; if ( lp ) { printf ( " \n The entered year is a leap year." ) ; } else { printf ( " \n The entered year is not a leap year." ) ; } } leap_year ( int y ) { int lp ; if ( y % 4 = = 0 ) { lp = 1 ; } else lp = 0 ; return ( lp ) ; } The file can be found at: Download File

Predict the output of the following Functions.

Predict the output of the following Code Snippets. This is an exercise based on Functions. The answers are provided at the bottom of the exercise. Be honest with yourself. :P // 1 main ( ) { printf ( " \n Only stupids use c?" ) ; display ( ) ; } display ( ) { printf ( " \n Fools too use C!" ) ; main ( ) ; } // 2 main ( ) { printf ( " \n C to it that C survives." ) ; main ( ) ; } // 3 main ( ) { int i = 45 , c ; c = check ( i ) ; printf ( " \n %d" , c ) ; } check ( int ch ) { if ( ch > = 45 ) return ( 100 ) ; else return ( 10 * 10 ) ; } // 4 main ( ) { int i = 45 , c ; c = check ( i * 1000 ) ; printf ( " \n %d" , c ) ; } check ( int ch ) { if ( ch > = 40000 ) return ( ch / 10 ) ; else return ( 10 ) ; } // 1 ) infinite loop of both the statements // 2 ) The message " C to it that C survives" is iterated infinitesimally ! // 3 ) 100 // 4 ) 4500 The file can be downloaded at:

Calculation of Factorial using Functions.

Write a function to calculate the factorial value of any integer entered through the keyboard. #include <stdio.h> main ( ) { int i , f ; int factorial ( ) ; printf ( "Enter the number to evaluate its factorial:" ) ; scanf ( "%d" , & i ) ; f = factorial ( i ) ; printf ( "%d! = %d \n " , i , f ) ; } factorial ( int num ) { int temp , fact ; for ( temp = 1 , fact = 1 ; temp < = num ; temp + + ) { fact = fact * temp ; continue ; } return ( fact ) ; } The file can be found at: Download File

Find the Errors (Switch Case)

These code snippets will help you understand the requirement of debugging a program. Without a program being debugged, they generate errors. And the heart of debugging lies in the process to find out what causes these errors. // 1 . main ( ) { int suite = 1 ; switch ( suite ) ; { case 0 : printf ( " \n Club" ) ; case 1 : printf ( " \n Diamond" ) ; } } // 2 . main ( ) { int temp ; scanf ( "%d" , & temp ) ; switch ( temp ) { case ( temp < = 20 ) : printf ( " \n Oooooooohhhhh! Damn Cool!" ) ; case ( temp > 20 & & temp < = 30 ) : printf ( " \n Rain rain here again! " ) ; case ( temp > 30 & & temp < = 40 ) : printf ( " \n Wish I am on Everest" ) ; default : printf ( " \n Good old nagpur weather" ) ; } } // 3 . main ( ) { float a = 3.5 ; switch ( a ) { case 0.5 : printf ( " \n The art of C" ) ; break ; case 1.5 : printf ( " \n The spirit of C&

Menu driven program - calculates factorial, detects a prime number, even/odd

Write a menu driven program which has the following options: 1. Factorial of a Number 2. Prime or Not 3. Odd or Even 4. Exit Make use of a switch Statement. Outline of the program: //a menu driven program main() { int choice; while (1) { printf("\n1. Factorial"); printf("\n2. Prime"); printf("\n3. Odd/ Even"); printf("\n4. Exit"); printf("\n Your Choice? : "); scanf ("%d", &choice); switch (choice) { case 1: //logic for factorial of a number break; case 2: //logic for deciding a prime number case 3: //logic for odd/even break; case 4: exit(); } } } NOTES: 1) The statement while(1) puts the entire logic in an infinite loop. This is necessary since the menu must keep reappearing on the screen once an item is selected and an appropriate action is taken. 2) ex