-
1. Write a Simple C-program to print if a given number is even or odd
criteria:
You can not use % operator
time : upto thrursday 5:30 IST.
Are you sure? This action cannot be undone.
-
Re: 1. Write a Simple C-program to print if a given number is even or odd
#include<stdio.h>
void check(int n);
void main()
{
int n;
printf("\n Enter the number \n");
scanf("%d",&n);
check(n);
}
void check(int n)
{
int temp=n;
for(int i=0;i<temp/2;i++)
{
n=n-2;
}
if(n==0)
printf("%d is even",temp);
else
printf("%d is odd",temp);
}
is this correct
Are you sure? This action cannot be undone.
-
Re: 1. Write a Simple C-program to print if a given number is even or odd
and also
#include<stdio.h>
void check(int n);
void main()
{
int n;
printf("\n Enter the number \n");
scanf("%d",&n);
check(n);
}
void check(int n)
{
int temp=n,q;
q=temp/2;
n=n-(2*q);
if(n==0)
printf("%d is even",temp);
else
printf("%d is odd",temp);
}
Are you sure? This action cannot be undone.
-
Re: 1. Write a Simple C-program to print if a given number is even or odd
harishproject
and also
#include<stdio.h>
void check(int n);
void main()
{
int n;
printf("\n Enter the number \n");
scanf("%d",&n);
check(n);
}
void check(int n)
{
int temp=n,q;
q=temp/2;
n=n-(2*q);
if(n==0)
printf("%d is even",temp);
else
printf("%d is odd",temp);
}
Bravo Harish..................... Great work!
Are you sure? This action cannot be undone.
-
Here is the Second Question: Write a Simple C program that Scan an even number
Criteria:
Once user gives an input, the scanning part must check if the number is even or not and should keep prompting the user for a valid even number input. if user fails to input correct number in 5 attempts, program should terminate.
😀
Are you sure? This action cannot be undone.
-
#include<stdio.h>
#include<stdlib.h>
void check(int n);
void main()
{
int n;
for(int i=0;i<5;i++)
{
printf("\n Enter a valid even number number \n");
scanf("%d",&n);
if(n%2!=0)
continue;
else
{
printf("%d is even\n",n);
break;
}
}
exit(0);
}
Are you sure? This action cannot be undone.
-
Great Going Harish. Now try this with a do-while loop
Do while is the best technique for validating. Just try out. That is the most efficient method. I know you will do this.
Are you sure? This action cannot be undone.
-
Re: Great Going Harish. Now try this with a do-while loop
#include<stdio.h>
#include<stdlib.h>
void check(int n);
void main()
{
int n,i=1;
do
{
printf("\n Enter a valid even number number \n");
scanf("%d",&n);
i++;
if(n%2!=0)
continue;
else
{
printf("%d is even\n",n);
break;
}
}while(i<=5);
exit(0);
}
ok did intrepret your question properly?
and as i know for and do while execute same no. times here.can i know y is do while efficient?
Are you sure? This action cannot be undone.
-
H.Ali
Member •
Oct 14, 2011
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0;
do {
clrscr();
printf("enter an EVEN No.\n");
scanf("%d",&n);
i++;
continue;
}while(n%2!=0&&i<5);
if(n%2==0)
printf("the enterd even No. is %d",n);
else
printf("LOSER\n you entered ODD No. in all your 5 chances !");
getch();
}
Are you sure? This action cannot be undone.
-
Ali....that was what exactly i was looking for and that is exactly how it must be done.
Are you sure? This action cannot be undone.
-
Ok The Next One: 3. A Simple Program to Find the Factorial of a Number
But the Criteria is You can not use any sort of Loops: while,for, do while. And Recursion is also not permitted.
Are you sure? This action cannot be undone.
-
My code for Odd-Even numbers.
#include<iostream>
//simplycoder
using namespace std;
int main(int argc,char**argv)
{
int num;
scanf("%d",&num);
num&1?printf("ODD\n"):printf("EVEN\n");
system("PAUSE");
return 0;
}
Are you sure? This action cannot be undone.
-
@simplycoder, This is what I was Expecting. Professionally Done and you are "My Programmer of The day" . Real cool.
Are you sure? This action cannot be undone.
-
Nothing much to think of rather than Gamma-Functions and so on.
I coded many series,out of which this series gave me the best and most accurate solution out of all.
This approximation is called as Nemes approximation.
#include<iostream>
#include<math.h>
//simplycoder
using namespace std;
int main(int argc,char**argv)
{
int x;
scanf("%d",&x);
int z=x+1;
double pi=3.1415926535897932;
double e=2.71828182845904523;
double term1=sqrt(2.0*pi/z);
double term2=1.0/e;
double term3=(12*z-1/(10*z));
double term4=z+1.0/term3;
term4=term4*term2;
term4=pow(term4,z);
term4=round(term4*term1);
cout<<"n "<<x<<" fact : "<<term4<<endl;
cout<<endl<<endl;
system("PAUSE");
return 0;
}
Are you sure? This action cannot be undone.
-
Again Spot On. But I would Prefer Some more approaches. Lemme know how many of Engineers here can Think Out of the Box!
Are you sure? This action cannot be undone.
-
H.Ali
Member •
Oct 15, 2011
simplycoder
My code for Odd-Even numbers.
num&1?printf("ODD\n"):printf("EVEN\n");
}
@simply coder please explain how this woks...
Are you sure? This action cannot be undone.
-
Its binary maths.
Refer to my previous tutorial which involves bit-operators for more explanation.
In short,all odd numbers have their last bit set to be 1 and all even number have last bit set to 0.
So the above code works,
condition?cout<<...:cout<<..
is similar to if-else.
Are you sure? This action cannot be undone.
-
Next Assignment: 3) Find the Factorial of 65 in turbo C
This is simplest isn't it? Find the factorial of 65 in Turbo C. If you are using boroland C++ or .Net, find the factorial of 1024 😀
Are you sure? This action cannot be undone.
-
Next Assignment: Declare an Array in Turbo C which can store 100x100 double values. Store and retrieve the values. As Simple as that. 😀
Are you sure? This action cannot be undone.
-
I would not want to spoil this for other members but here is the slightest hint.This is not to be considered as normal and easy problem for findinf factorial, largest data-type is not sufficient to fit in all the numbers.
Hint: use arrays.
Are you sure? This action cannot be undone.
-
H.Ali
Member •
Oct 19, 2011
Rupam Das
Next Assignment: Declare an Array in Turbo C which can store 100x100 double values. Store and retrieve the values. As Simple as that. 😀
Array size is too large !!! 😐 dont know what to do .... let others try.. 😒
Are you sure? This action cannot be undone.
-
Thats why this thread is created. It needs to be solved.
Are you sure? This action cannot be undone.
-
H.Ali
Member •
Dec 18, 2011
No One to answer this...
Rupam please post the Answer and the Next assignment....
Are you sure? This action cannot be undone.
-
Hello Sir , i am new member here , i am in 3rd year of computer science i have to submit proposal for final year project but dont have any idea
my areas of intrest are:
Andriod
Gaming
Netwroking
Are you sure? This action cannot be undone.
-
Hi...
Sir i have read all your cs project ideas...no doubt all that projects are outstanding
I personally asking you to suggest me a project which is unique...and not limited...
Am a computer science student and my grip in c#
A project which will be most usable..
Thank you soo much if you help me....
Are you sure? This action cannot be undone.