You are on page 1of 7

C Example Practical Questions

#include<stdio.h> //including the standard input output library which provides the functionalities for printf, scanf etc //declaring and initialising variables int sum=0; int max=0; int min=0; int minimumTuner=0; //All the functions except the main functions should be indicated begining of the program by using its function header void findSum(int inputValue); void findMax(int inputValue); void findMin(int inputValue); void closePro(); main(){ int inputValue=0; printf("Enter User Input ....\n"); scanf("%d",&inputValue); //Gets user inputs //min should be initialized with a value that user input if(minimumTuner==0){ min=inputValue; minimumTuner=1; }

if(inputValue==0 || inputValue<0){ closePro(); return 0; //To exit the program } findSum(inputValue); //callling to below functions. Variable inside the paranthesis is the input parameter for the function. findMin(inputValue); findMax(inputValue); main(); //re-starting the program by calling to main function } //function to find sum. variable inside the paranthesis is to grab the value void findSum(int inputValue){ sum=sum+inputValue; } //function to find max. void findMax(int inputValue){ if(inputValue>max){ max=inputValue; } } //function to find min void findMin(int inputValue){ if(inputValue<min){ min=inputValue; } } //function to print results void closePro(){ printf("Sum is ......%d\n",sum); //%d to indicate an integer and related variable is after the comma. \n is to put a new line printf("Maximum is ......%d\n",max); printf("Minimum is ......%d\n",min); }

#include<stdio.h> int total=0; void calGeometricSeries(int initialValue,int commonRatio,int numberOfRatio); void findTotal(int geometricItem); main(){ int initialValue=0; int commonRatio=0; int numberOfTerms=0; //Gets the user inputs. %d indicate a integer and relavent variable where the value should be stored is mentioned after the comma.

printf("Enter the initial value ....\n"); scanf("%d",&initialValue); printf("Enter the common ratio ....\n"); scanf("%d",&commonRatio); printf("Enter the number of terms ....\n"); scanf("%d",&numberOfTerms); calGeometricSeries(initialValue,commonRatio,numberOfTerms); //According to our logic 'calGeometricSeries' function requires three inputs so we input them here and call it printf("\nSum of first %d terms....%d\n",numberOfTerms,total); } //'calGeometricSeries' function grabs the above mentioned input parameters through parameter list void calGeometricSeries(int initialValue,int commonRatio,int numberOfTerms){ int factor=1; int geometricItem=0; printf("Geometric Series...."); while(numberOfTerms>0){ if(geometricItem==0){ geometricItem=initialValue; }else{ factor=factor*commonRatio; geometricItem=factor*initialValue; } printf("%d, ",geometricItem); findTotal(geometricItem); numberOfTerms--; } } void findTotal(int geometricItem){ total=total+geometricItem; }

#include<stdio.h> int codeIdentifier(int noOfHours); float salaryCalculator(int code); main(){ int noOfHours=0; printf("Enter the number of hours...."); scanf("%d",&noOfHours); printf("Code %d\n",codeIdentifier(noOfHours)); printf("Salary %f\n",salaryCalculator(codeIdentifier(noOfHours))); } //this function returns a integer type value that is why we have used int front of the function name. If nothing is returned then use 'void'. Returning a value means function provides the result value at the place of it is called. int codeIdentifier(int noOfHours){ if(noOfHours>0 && noOfHours<31){ return 1; }else if(noOfHours>30 && noOfHours<51){ return 2; }else if(noOfHours>50 && noOfHours<71){

return 3; }else if(noOfHours>70 && noOfHours<101){ return 4; } } float salaryCalculator(int code){ float salary=0; switch(code){ case 1: salary=1500+1500*0.2; break; case 2: salary=2000+2000*0.25; break; case 3: salary=2500+2500*0.30; break; case 4: salary=3000+3000*0.35; break; } return salary; }

You might also like