Thursday, July 30, 2009

Can anyone help me with my c programming project?

Write a function called ChangeNumber that take in an integer parameter num. If the number is an even number, the function adds one and returns the result else it minus one and returns the number.

Can anyone help me with my c programming project?
int ChangeNumber(int num){


if(num%2==0){


return num+1;


}else{


return num-1;


}


}


is this what you need?
Reply:thanks people for the best answers you gave me! it helps me alot! Report It

Reply:This will not be in the main() area because it is a function. Create a new function and either pass by value or pass the pointer to the variable containing the entered number. Once this is done then within the function perform a simple IF statement. The way to do this is to take the number and divide by 2 to see if a reminder exists. If so then it is Odd otherwise is it even. An easier way to do this but it is morec omplicated and requires some research is to translate the number to binary code and then look at the trailing number. A "1" indicates Odd and a "0" indicates even.
Reply:#include %26lt;stdio.h%26gt;


int ChangeNumber(int num)


{


if(num%2==0)


{


return num+1;


}


else


{


return num-1;


}


}





main()


{


int number,retval;


printf("Enter the number");


scanf("%d",%26amp;number);


retval = ChangeNumber(number);


printf("The ruturn value is %d", retval);


}
Reply:/* By manjunath.B from Bangalore malleshwaram*/


/* If this source is useful to u, E-mail me at manju_qd@yahoo.co.in*/





#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;


int changenum(int);


{


int num,x;


printf("Input a number : ");


scanf("%d",%26amp;num);





x = changenum(num);





printf("The result is %d",x);





getch ();


return 0;


}


int changenum(int num);


{


if (num%2==0)


return num+1;


else


return num-1;


}
Reply:I won't write the code but this is one of several methods you could consider. A number is even if the last digit is in the set {0,2,4,6,8}. Your function can strip off the last digit, test it for a match then perform the addition or subtraction and return the value you want.


No comments:

Post a Comment