Post ur C/C++ Programs Here
Filed Under mercialleasing.com |
If you are a good at C/C++ Programming or if you are a programmer or just know this language then post you Programs here. By this way it helps learners a lot. Members can post their programs and get suggestions if there is anything wrong it......
Use this Pattern:
A Program to find the largest of three numbers using nested if
#include
main()
{
int a,b,c,big;
printf("Enter Three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
big=a;
else
big=c;
else
if(b>c)
big=b;
else
big=c;
printf("Largest of %d,%d and %d = %d",a,b,c,big);
return 0;
}================================================= =
List of all the C Programs posted in this thread..
==================================================
● A Program to find the largest of three numbers using nested if. (http://www.thinkdigit.com/forum/showpost.php?p=592920&postcount=11)
● A program to findcalc area, surface area, volume, total surface area etc. of numerous 2D and 3D figures using functions. (http://www.thinkdigit.com/forum/showpost.php?p=592791&postcount=9)
● A Program to perform basic calculations. (http://www.thinkdigit.com/forum/showpost.php?p=592754&postcount=3)
● Program To Reverse a Number (http://www.thinkdigit.com/forum/showpost.php?p=592989&postcount=21)
● What is this? (http://www.thinkdigit.com/forum/showpost.php?p=593594&postcount=42)
● A Program that Binary, Decimal, Octal Inter-Conversions (http://www.thinkdigit.com/forum/showpost.php?p=596801&postcount=47)
● A bulls and cows game (http://www.thinkdigit.com/forum/showpost.php?p=596844&postcount=48)
● A program that selects a number which is of 4 digits and all are unique numbers. (http://www.thinkdigit.com/forum/showpost.php?p=597483&postcount=57)
● Write a C Program to sort numbers of an Array using Bubble Sort Method. (http://www.thinkdigit.com/forum/showpost.php?p=600004&postcount=70)
● Reusable java class to send email through pop3 (http://www.thinkdigit.com/forum/showpost.php?p=600349&postcount=79)
● Simple Switch-Case Example. (http://www.thinkdigit.com/forum/showpost.php?p=600609&postcount=87)
● Swapping of two numbers without a Temporary variable in 1 line. (http://www.thinkdigit.com/forum/showpost.php?p=600134&postcount=75)
● A program that creates a vB list out of a file given in a particular format. (http://www.thinkdigit.com/forum/showpost.php?p=600702&postcount=92)
● Digit Forum URL Bulleted List Generator (http://www.thinkdigit.com/forum/showpost.php?p=600721&postcount=93)
● Write a C program to find an element from an Array using Binary Search Method. (http://www.thinkdigit.com/forum/showpost.php?p=600793&postcount=100)
● TOWER OF HANOI Simulator (http://www.thinkdigit.com/forum/showpost.php?p=603727&postcount=127)
● A Simple C Program to read and print a one-dimensional array (http://www.thinkdigit.com/forum/showpost.php?p=604496&postcount=136)
● C Program to find the largest element in an array and the position of its occurrence. (http://www.thinkdigit.com/forum/showpost.php?p=605115&postcount=139)
● A C program to find the maximum and minimum elements in an array having N elements (http://www.thinkdigit.com/forum/showpost.php?p=605531&postcount=144)
● Program which compares two strings, concatenate them and then gives the length of string. (http://www.thinkdigit.com/forum/showpost.php?p=607880&postcount=164)
● Program to kill a software /demo version expire in C (http://www.thinkdigit.com/forum/showpost.php?p=608341&postcount=170)
● Program To arrange numbers in Pyramid Pattern (http://www.thinkdigit.com/forum/showpost.php?p=609383&postcount=184)
● Program: a simple shop using string, integers, float, & algebraic operators (http://www.thinkdigit.com/forum/showpost.php?p=610678&postcount=187)
● Character Eater (http://www.thinkdigit.com/forum/showpost.php?p=616479&postcount=220)
● Program to insert a node in a single linked list. (http://www.thinkdigit.com/forum/showpost.php?p=617643&postcount=247)
● A C Program to convert decimal number to its Binary Number Equivalent (http://www.thinkdigit.com/forum/showpost.php?p=618560&postcount=262)
● Introduction to C++ - Stanford Video Tutorials and Other Lectures (http://www.thinkdigit.com/forum/showpost.php?p=622122&postcount=299) (Not a program)
● A Program to remind that u r using other account (http://www.thinkdigit.com/forum/showpost.php?p=623390&postcount=306)
● Typedef Example Program (http://www.thinkdigit.com/forum/showpost.php?p=624837&postcount=312)
● A C Program to check whether a given word is Palindrome or not. (http://www.thinkdigit.com/forum/showpost.php?p=645292&postcount=361)
● A Simple C Program to Display the number and its square from 1 to 10 using register variable. (http://www.thinkdigit.com/forum/showpost.php?p=646765&postcount=367)
● A C Program to find the sum of two matrix using two dimensional array. (http://www.thinkdigit.com/forum/showpost.php?p=652794&postcount=375)
● TURBO CALC 15000 (http://www.thinkdigit.com/forum/showpost.php?p=659431&postcount=381)
● A Command Line Calculator.
(http://www.thinkdigit.com/forum/showpost.php?p=660067&postcount=389)● Function 2 input a matrix (http://www.thinkdigit.com/forum/showpost.php?p=741811&postcount=410)
● Read an int & an int array & search for that int in the int array & return its position (http://www.thinkdigit.com/forum/showpost.php?p=751445&postcount=411).
● Python version of the above program. (http://www.thinkdigit.com/forum/showpost.php?p=751492&postcount=412)
● Program to perform operations on a binary search tree (http://www.thinkdigit.com/forum/showpost.php?p=754343&postcount=415).
● Program to calculate the computer sales man salary. (http://www.thinkdigit.com/forum/showpost.php?p=781627&postcount=422) (Correction (http://www.thinkdigit.com/forum/showpost.php?p=781659&postcount=423))
● Basic Inventory tracker software program code. (http://www.thinkdigit.com/forum/showpost.php?p=782287&postcount=425)
● Sudoku
(http://www.thinkdigit.com/forum/showpost.php?p=788009&postcount=426) ● Simple DOS Text-based alien shooter game
(http://www.thinkdigit.com/forum/showpost.php?p=824806&postcount=430) ● A program for unit conversions. (http://www.thinkdigit.com/forum/showpost.php?p=848269&postcount=436)
===========================================
Last Update: 12 August 2008
--------------------------------------------------------
Some good free C/C++ Compilers
● Relo (http://www.fifsoft.com/relo/)
● Bloodshed Dev C++ (http://www.bloodshed.net/devcpp.html)
● GCC (http://gcc.gnu.org/)
● Borland C++ 5.5 (http://cc.codegear.com/Free.aspx?id=24778)
● MinGW (http://www.mingw.org/)
● Open Watcom (http://www.openwatcom.org/)
....and */ after the comment.
can someone tell me what's wrong in this program.Using Dev C++
#include
#include
using namespace std;
int main()
{ /*Use of Function*/
float a,b,sum;
float calsum(float a,float b);
cout<<"Enter the two nos.:n";
cin>>a>>b;
sum=calsum(a,b);
cout<<"The sum is :"<
calsum(float a,float b)
{ float sum;
sum=a+b;
return (sum);
}
#/usr/bin/env python
"Base-n tools with String support (Base-n not complete yet, sorry)"
"String to Binary [Returns a spaced out Binary value string]"
def strtobin(a):
b,c='',0
for each in a:
c = ord(each)
b+=dectobin(c)+' '
return b[:-1]
"String to Decimal [Returns a spaced out Decimal value string]"
def strtodec(a):
b=''
for each in a:
b+=str(ord(each))+' '
return b[:-1]
"String to Octal [Returns a spaced out Octal value string]"
def strtooct(a):
b=''
for each in a:
b+=dectooct(strtodec(each))+' '
return b[:-1]
"String to Hexadecimal [Returns a spaced out Hexadecimal value string]"
def strtohex(a):
b=''
l=strtodec(a).split(' ')
for each in l:
b += hex(int(each)) + ' '
return b[:-1]
"Hexadecimal to Binary [Returns a Binary value string]"
def hextobin(a):
return dectobin(hextodec(a))
"Hexadecimal to Decimal [Returns a Decimal value]"
def hextodec(a):
return int(a,16)
"Hexadecimal to Octal [Returns an Octal value string]"
def hextooct(a):
return oct(hextodec(a))
"Hexadecimal to String [Returns a string corresponding to each Decimal ASCII value of the Hexadecimal value]"
def hextostr(a):
b=""
l=[a.split(" ")]
for each in l[0]:
b+=chr(hextodec(each))
return b
"Octal to Binary [Returns an Octal value string]"
def octtobin(a):
return dectobin(octtodec(a))
"Octal to Decimal [Returns a Decimal value]"
def octtodec(a):
return str(int(str(a),8))
"Octal to Hexadecimal [Returns a Hexadecimal value string]"
def octtohex(a):
a = int(a,8)
return dectohex(octtodec(a))
"Octal to String [Returns a string based on the Decimal value of the Octal number]"
def octtostr(a):
return dectostr(octtodec(a))
"Decimal to Binary [Returns a Binary valued string]"
def dectobin(a):
a,b,c=int(a),'',0
while(a>0):
c = a%2
b+=str(c)
a=a/2
return b[::-1]
"Decimal to Octal [Returns a Decimal]"
def dectooct(a):
return oct(int(a))
"Decimal to Hexadecimal [Returns a Hexadecimal value string]"
def dectohex(a):
return hex(int(a))
"Decimal to String [Returns a string]"
def dectostr(a):
if int(a)<256:
return chr(int(a))
return None
"Binary to Decimal [Returns a Binary valued string]"
def bintodec(a):
c=i=0
a=a[::-1]
for each in a:
if each!=None:
c+=int(each)*pow(2,i)
i+=1
return c
"Binary to Octal [Returns an Octal value string]"
def bintooct(a):
return dectooct(bintodec(a))
"Binary to Hexadecimal [Returns a Binary valued string]"
def bintohex(a):
return dectohex(bintodec(a))
"Binary to String [Returns a string]"
def bintostr(a):
b=''
for each in a.split(' '):
if dectostr(bintodec(each))!=None:
b+=dectostr(bintodec(each))+' '
return b[:-1]
if __name__=='__main__':
print 'Try calling any function here'
I find books by Yashavant Kanetkar the best ones for C/C++.
Mate i suggest you re-learn C++ using Addison Wesley's Accelerated C++
One of the best Standard C++ books out there that actually teach you how to use STL practically rather than just theory.
btw , i read this in class XI(i'm currently in XII) so ditch your textbook(seriously) and use this .
/* Stack Implementation using Linked Lists */
#include
#include
/* Structure Declaration:
Structure for a Stack (Linked List Structure)
Requires two elements.
One Element member, holding the value that the stack is to be made of
One Self-Referring Pointer member for making it a Linked List - Stack. */
typedef struct stacklink
{
int element;
struct stacklink *next;
} top ;
typedef top stack;
/* Create:
Initializes the stack with a NULL value. */
void create(stack *s)
{
s=NULL;
}
/* Push:
Pushes (Inserts) a node into the Stack, using a new temporary node.
Stack's Push operation is done only on the top of the stack */
stack *push(stack *s)
{
int a=0;
stack *temp;
printf("nEnter the element to push into the stack: ");
scanf("%d",&a);
temp = (stack *)malloc(sizeof(stack));
temp->element = a;
temp->next = s;
return temp;
}
/* Pop:
Pops (Deletes) the top-most element out of the Stack.
Also checks if the stack is empty or not and pops only when there is an element available. */
stack *pop(stack *s)
{
stack *temp;
if(s->next!=NULL)
{
temp = s;
printf("nPopped element is: %dn",temp->element);
s = s->next;
free(temp);
}
else
{
printf("nNothing to Pop from the stack.n");
}
return s;
}
/* Display:
Displays the Stack in a pictorial fashion.
A sample output of the stack via this would look like:
The List is:
4 -> 5 -> 6 -> NULL
Where, NULL represents the END of stack. For an empty stack too, it shows NULL. */
void display(stack *s)
{
int temp=1;
printf("nThe Stack is:");
if(s->next==NULL)
{
printf("b empty");
}
while(s->next!=NULL)
{
if(temp)
{
printf("n");
temp=0;
}
printf("%d --> ",s->element);
s=s->next;
if(s->next==NULL)
{
printf("NULL");
break;
}
}
printf("n");
}
/* Main Menu:
Displays a menu-based access system for various operations on the stack.
The menu looks like:
1. Create
2. Push
3. Pop
4. Display
5. Exit
Each item in the menu calls the appropriate functions of the Stack. */
int main()
{
int choice, key, loc,throw=0;
stack *temp;
while(1)
{
printf("nStacksn-------n");
printf("1. Createn2. Pushn3. Popn4. Displayn5. Exit");
printf("nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
temp=(stack *)malloc(sizeof(stack));
create(temp);
printf("nNULL Stack createdn");
throw=1;
break;
case 2:
if(throw==1)
{
temp=push(temp);
display(temp);
}
else
{
printf("nCreate a stack first ..n");
}
break;
case 3:
if(throw==1)
{
temp=pop(temp);
display(temp);
}
else
{
printf("nCreate a stack first ..n");
}
break;
case 4:
if(throw==1)
display(temp);
else
printf("nCreate the stack first ..n");
break;
case 5:
return 0;
default:
printf("nInvalid choice ...n");
}
}
}
PLz tell me what about this program and what it will do, i know stack but plz explain
I have new idea..!!!!
What about some challenges any one can raised problem, before 2 days my teacher challenge me to do a program to convert given number into binary , octal and hexadecimal. I have found solution but you have to try.
for exercise:
do a program like U have to hide the typed string on screen by" * "
like we do **********
nice one haa..
Try this:
#include
int main(void)
{
int a = 0,
b = 0,
c = 0,
greatest = 0;
printf("Enter three numbers:n");
scanf("%d %d %d", &a, &b, &c);
greatest = a;
if(b > greatest)
greatest = b;
if(c > greatest)
greatest = c;
printf("greatest of %d, %d and %d = %d", a, b, c, greatest);
return 0;
}
http://upload.wikimedia.org/wikipedia/commons/2/29/Data_stack.svg20
30
40
----
To add an element to it, we can only push from top and not insert anywhere we like:
So after pushing 10 into the stack, it looks like:
10 <-- (Pushed on top)
20
30
40
----
Similarly, as its a LIFO system, Popping the stack removes the top most element.
Thus, 10 would be removed on popping:
20 --> 10 (Popped out and deleted)
30
40
----
Stack's best application would be the function call. Recursion takes place in stacks format. (And are thus usually avoided)
Read more (http://en.wikipedia.org/wiki/Stack_%28data_structure%29)
i had made this one for a friend - it was his project
#include
#include
#include
void line(void);
void cuboid(void);
void cube(void);
void cylinder(void);
void cone(void);
void sphere(void);
void hemisphere(void);
void triangle(void);
void rectangle(void);
void square(void);
void circle(void);
void parallelogram(void);
void main()
{
clrscr();
int o1,o2;
menu:
do{
clrscr();
cout<<"nn1. 3-D Figures"
<<"nn2. Plane Figures"
<<"nn3. Exit"
<<"nnEnter your choice -->t";
cin>>o1;
switch(o1)
{
case 1: clrscr();
cout<<"nn";
line();
cout<<"ntttt3-D Figuresnn";
line();
cout<<"nn1. Cuboid"
<<"nn2. Cube"
<<"nn3. Cylinder"
<<"nn4. Cone"
<<"nn5. Sphere"
<<"nn6. Hemisphere"
<<"nn7. Main Menu"
<<"nnEnter your choice -->t";
cin>>o2;
switch(o2)
{
case 1: clrscr();
cout<<"nn";
line();
cout<<"nttttCuboidnn";
line();
cuboid();
getch();
break;
case 2: clrscr();
cout<<"nn";
line();
cout<<"nttttCubenn";
line();
cube();
getch();
break;
case 3: clrscr();
cout<<"nn";
line();
cout<<"nttttCylindernn";
line();
cylinder();
getch();
break;
case 4: clrscr();
cout<<"nn";
line();
cout<<"nttttConenn";
line();
cone();
getch();
break;
case 5: clrscr();
cout<<"nn";
line();
cout<<"nttttSpherenn";
line();
sphere();
getch();
break;
case 6: clrscr();
cout<<"nn";
line();
cout<<"nttttHemispherenn";
line();
hemisphere();
getch();
break;
case 7: goto menu;
default: cout<<"nnInvalid Inputaaa";
goto menu;
}
getch();
break;
case 2: clrscr();
cout<<"nn";
line();
cout<<"nttttPlane Figuresnn";
line();
cout<<"nn1. Traingle"
<<"nn2. Rectangle"
<<"nn3. Sqaure"
<<"nn4. Circle"
<<"nn5. Parallelogram"
<<"nn6. Main Menu"
<<"nnEnter your choice -->t";
cin>>o2;
switch(o2)
{
case 1: clrscr();
cout<<"nn";
line();
cout<<"nttttTrainglenn";
line();
triangle();
getch();
break;
case 2: clrscr();
cout<<"nn";
line();
cout<<"nttttRectanglenn";
line();
rectangle();
getch();
break;
case 3: clrscr();
cout<<"nn";
line();
cout<<"nttttSquarenn";
line();
square();
getch();
break;
case 4: clrscr();
cout<<"nn";
line();
cout<<"nttttCirclenn";
line();
circle();
getch();
break;
case 5: clrscr();
cout<<"nn";
line();
cout<<"nttttParallelogramnn";
line();
parallelogram();
getch();
break;
case 6: goto menu;
default: cout<<"nnInvalid Inputaaa";
goto menu;
}
getch();
break;
case 3: cout<<"nnPress any key to exit...a";
getch();
break;
default: cout<<"nnInvalid Inputaaaa";
goto menu;
}
}while(o1!=3);
}
void line()
{
int i;
for (i=0;i<80;i++)
cout<<"*";
}
void cuboid()
{
float l,b,h,a,v;
cout<<"nnEnter length of cuboid -->t";
cin>>l;
cout<<"nnEnter breadth of cuboid -->t";
cin>>b;
cout<<"nnEnter height of cuboid -->t";
cin>>h;
v=l*b*h;
a=2*(l*b+b*h+l*h);
cout<<"nnVolume of cuboid is "<
void cube()
{
float l,a,v;
cout<<"nnEnter length of side of cube -->t";
cin>>l;
v=l*l*l;
a=6*l*l;
cout<<"nnVolume of cube is "<
void cylinder()
{
float r,h,v,c,t;
cout<<"nnEnter radius of cylinder -->t";
cin>>r;
cout<<"nnEnter height of cylinder -->t";
cin>>h;
v=3.14*r*r*h;
c=2*3.14*r*h;
t=3.14*r*(2*h+r);
cout<<"nnVolume of cylinder is "<
void cone()
{
float r,h,l,v,c,t;
cout<<"nnEnter radius of cone -->t";
cin>>r;
cout<<"nnEnter height of cone -->t";
cin>>h;
l=sqrt(r*r+h*h);
v=(3.14*r*r*h)/3;
c=3.14*r*l;
t=3.14*r*(l+r);
cout<<"nnSlant height of cone is "<
void sphere()
{
float r,v,a;
cout<<"nnEnter radius of sphere -->t";
cin>>r;
v=(4*3.14*r*r*r)/3;
a=4+3.14*r*r;
cout<<"nnVolume of sphere is "<
void hemisphere()
{
float r,v,c,t;
cout<<"nnEnter radius of hemisphere -->t";
cin>>r;
v=(2*3.14*r*r*r)/3;
c=2*3.14*r*r;
t=3*3.14*r*r;
cout<<"nnVolume of hemisphere is "<
void triangle()
{
float b,h,a;
cout<<"nnEnter base of triangle -->t";
cin>>b;
cout<<"nnEnter height of triangle -->t";
cin>>h;
a=(b*h)/2;
cout<<"nnArea of the triangle is "<
}
void rectangle()
{
float l,b,a,p;
cout<<"nnEnter length of rectangle -->t";
cin>>l;
cout<<"nnEnter breadth of rectangle -->t";
cin>>b;
a=l*b;
p=2*(l+b);
cout<<"nnArea of rectangle is "< cout<<"nnPerimeter of rectangle is "< }
void square()
{
float s,a,p;
cout<<"nnEnter length of side of square -->t";
cin>>s;
a=s*s;
p=4*s;
cout<<"nnArea of square is "< cout<<"nnPerimeter of square is "< }
void circle()
{
float r,a,c;
cout<<"nnEnter radius of circle -->t";
cin>>r;
a=3.14*r*r;
c=2*3.14*r;
cout<<"nnArea of circle is "< cout<<"nnCircumference of circle is "<
void parallelogram()
{
float b,h,a,p;
cout<<"nnEnter base of parallelogram -->t";
cin>>b;
cout<<"nnEnter height of parallelogram -->t";
cin>>h;
a=b*h;
p=2*(b+h);
cout<<"nnArea of parallelogram is "< cout<<"nnPerimeter of parallelogram is "< }
i've put up a lot more progs at my forum. classmates really find it useful since none of them is interested in computers. its so sad...
What about some challenges any one can raised problem, before 2 days my teacher challenge me to do a program to convert given number into binary , octal and hexadecimal. I have found solution but you have to try.
That is one helluva program which I made 2-3 months back but my program was not perfect as I used a lot of arrays which took up a lot of memory and more often than not they were never used.I seem to have lost my original program code but will write it and post it in a couple of days or so.
YOu can actually display the given no. in octal,hexadecimal,and binary by just using %o,%x,and(I forgot for binary) instead of %d while displaying.
Rules of programming:
1. Read the fscking code. And mine is pretty readable.
2. Test it before complaining.
3. If you still don't get it, it's time to buy a decent C book.
main()
{
int a,b,c,big;
printf("Enter Three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
big=a;
else
big=c;
else
if(b>c)
big=b;
else
big=c;
printf("Largest of %d,%d and %d = %d",a,b,c,big);
}
Your program lacks proper braces and a return statement.
here's a prog to calc area, surface area, volume, total surface area etc. of numerous 2D and 3D figures using functions
i had made this one for a friend - it was his project
...
i've put up a lot more progs at my forum. classmates really find it useful since none of them is interested in computers. its so sad...
Your code doesn't conform to either ISO C or C++ standards.
scanf("%d%d%d",&a,&b,&c);
New to c++.......Whta those this line means
scanf() is a C input function, which is considered unsafe except for formatted data. I advise learning C first, before opting for a much larger, bloated and complex language as C++.
Write a program using functions to find whether the given number is a prime number?
#include
main()
{
long rev, n, num;
int digit;
printf("n Enter a number");
scanf("%d", &num);
rev=0;
n=num;
while (num>0)
{
digit = num % 10;
num = num / 10
rev = rev * 10 + digit;
}
printf("n The number is %d", n);
printf("n The reverse is %d", rev);
}
New to c++.......Whta those this line means
conio.h is non-standard.
Your calsum() doesn't have a forward declaration. Put main() after calsum() or put the line :
int calsum(float, float);
before main().
Who told you that? I recommend what I use - Lafore's, Prata's, Lippman's and Stroustrup's.
Using implementation dependent libraries and functions like clrscr(), and the nonstandard "void main()" violates the rules of the ISO C++ standard. Your executable may run fine on a system, but your code needs more love.
I recommend a decent and recent C++ book, like C++: How To Program, C++ Primer, 4th/e or C++ Primer Plus.
use this thread to request programs and then pm to the requester rather than posting so much code over here it really looks a mess and these are quite basic too
bad math funda :D .. iterate from 2 to (int)sqrt(n) ..if it isnt divisible then itz prime
Who told you that? I recommend what I use - Lafore's, Prata's, Lippman's and Stroustrup's.
How to Program in C++ by Deitel and Deitel is good too
I find books by Yashavant Kanetkar the best ones for C/C++.
And as far as Kanetkar is concerned ... when I was inmy 11/12th I thought so too. But now, :D u jst mature. Those books feel like being fed with a spoon.
They do contain a LOT of funda though
I recommend a decent and recent C++ book, like C++: How To Program, C++ Primer, 4th/e or C++ Primer Plus.
hmm, u use Oxford and Sumita Arora's textbook for C++. they're our course books in XI and XII
Don't be so hard on him :D He doesn't know TC++'s evils :D
Anyway, here's xbonez's program in proper code :)
#include
#include
using namespace std;
void line(void);
void cuboid(void);
void cube(void);
void cylinder(void);
void cone(void);
void sphere(void);
void hemisphere(void);
void triangle(void);
void rectangle(void);
void square(void);
void circle(void);
void parallelogram(void);
int main()
{
int o1,o2;
do{
cout<<"nn1. 3-D Figures"
<<"nn2. Plane Figures"
<<"nn3. Exit"
<<"nnEnter your choice -->t";
cin>>o1;
switch(o1)
{
case 1: cout<<"nn";
line();
cout<<"ntttt3-D Figuresnn";
line();
cout<<"nn1. Cuboid"
<<"nn2. Cube"
<<"nn3. Cylinder"
<<"nn4. Cone"
<<"nn5. Sphere"
<<"nn6. Hemisphere"
<<"nn7. Main Menu"
<<"nnEnter your choice -->t";
cin>>o2;
switch(o2)
{
case 1: cout<<"nn";
line();
cout<<"nttttCuboidnn";
line();
cuboid();
break;
case 2: cout<<"nn";
line();
cout<<"nttttCubenn";
line();
cube();
break;
case 3: cout<<"nn";
line();
cout<<"nttttCylindernn";
line();
cylinder();
break;
case 4: cout<<"nn";
line();
cout<<"nttttConenn";
line();
cone();
break;
case 5: cout<<"nn";
line();
cout<<"nttttSpherenn";
line();
sphere();
break;
case 6: cout<<"nn";
line();
cout<<"nttttHemispherenn";
line();
hemisphere();
break;
case 7: break;
default: cout<<"nnInvalid Inputaaa";
}
break;
case 2:
cout<<"nn";
line();
cout<<"nttttPlane Figuresnn";
line();
cout<<"nn1. Traingle"
<<"nn2. Rectangle"
<<"nn3. Sqaure"
<<"nn4. Circle"
<<"nn5. Parallelogram"
<<"nn6. Main Menu"
<<"nnEnter your choice -->t";
cin>>o2;
switch(o2)
{
case 1: cout<<"nn";
line();
cout<<"nttttTrainglenn";
line();
triangle();
break;
case 2: cout<<"nn";
line();
cout<<"nttttRectanglenn";
line();
rectangle();
break;
case 3: cout<<"nn";
line();
cout<<"nttttSquarenn";
line();
square();
break;
case 4: cout<<"nn";
line();
cout<<"nttttCirclenn";
line();
circle();
break;
case 5: cout<<"nn";
line();
cout<<"nttttParallelogramnn";
line();
parallelogram();
break;
case 6: break;
default: cout<<"nnInvalid Inputaaa";
}
break;
case 3: cout<<"nnPress any key to exit...a";
break;
default: cout<<"nnInvalid Inputaaaa";
}
}while(o1!=3);
return 0;
}
void line()
{
int i;
for (i=0;i<80;i++)
cout<<"*";
}
void cuboid()
{
float l,b,h,a,v;
cout<<"nnEnter length of cuboid -->t";
cin>>l;
cout<<"nnEnter breadth of cuboid -->t";
cin>>b;
cout<<"nnEnter height of cuboid -->t";
cin>>h;
v=l*b*h;
a=2*(l*b+b*h+l*h);
cout<<"nnVolume of cuboid is "<
void cube()
{
float l,a,v;
cout<<"nnEnter length of side of cube -->t";
cin>>l;
v=l*l*l;
a=6*l*l;
cout<<"nnVolume of cube is "<
void cylinder()
{
float r,h,v,c,t;
cout<<"nnEnter radius of cylinder -->t";
cin>>r;
cout<<"nnEnter height of cylinder -->t";
cin>>h;
v=3.14*r*r*h;
c=2*3.14*r*h;
t=3.14*r*(2*h+r);
cout<<"nnVolume of cylinder is "<
void cone()
{
float r,h,l,v,c,t;
cout<<"nnEnter radius of cone -->t";
cin>>r;
cout<<"nnEnter height of cone -->t";
cin>>h;
l=sqrt(r*r+h*h);
v=(3.14*r*r*h)/3;
c=3.14*r*l;
t=3.14*r*(l+r);
cout<<"nnSlant height of cone is "<
void sphere()
{
float r,v,a;
cout<<"nnEnter radius of sphere -->t";
cin>>r;
v=(4*3.14*r*r*r)/3;
a=4+3.14*r*r;
cout<<"nnVolume of sphere is "<
void hemisphere()
{
float r,v,c,t;
cout<<"nnEnter radius of hemisphere -->t";
cin>>r;
v=(2*3.14*r*r*r)/3;
c=2*3.14*r*r;
t=3*3.14*r*r;
cout<<"nnVolume of hemisphere is "<
void triangle()
{
float b,h,a;
cout<<"nnEnter base of triangle -->t";
cin>>b;
cout<<"nnEnter height of triangle -->t";
cin>>h;
a=(b*h)/2;
cout<<"nnArea of the triangle is "<
}
void rectangle()
{
float l,b,a,p;
cout<<"nnEnter length of rectangle -->t";
cin>>l;
cout<<"nnEnter breadth of rectangle -->t";
cin>>b;
a=l*b;
p=2*(l+b);
cout<<"nnArea of rectangle is "< cout<<"nnPerimeter of rectangle is "< }
void square()
{
float s,a,p;
cout<<"nnEnter length of side of square -->t";
cin>>s;
a=s*s;
p=4*s;
cout<<"nnArea of square is "< cout<<"nnPerimeter of square is "< }
void circle()
{
float r,a,c;
cout<<"nnEnter radius of circle -->t";
cin>>r;
a=3.14*r*r;
c=2*3.14*r;
cout<<"nnArea of circle is "< cout<<"nnCircumference of circle is "<
void parallelogram()
{
float b,h,a,p;
cout<<"nnEnter base of parallelogram -->t";
cin>>b;
cout<<"nnEnter height of parallelogram -->t";
cin>>h;
a=b*h;
p=2*(b+h);
cout<<"nnArea of parallelogram is "< cout<<"nnPerimeter of parallelogram is "< }
#include
#include
using namespace std;
void lotto();
void calc(int * a, int * b);
void menu();
// Main function. Seeds random function
// and calls menu function.
int main()
{
srand((unsigned)time(0)); // Seed the random function......
menu();
}
// This function prompts the user to pic
// k five numbers between 1 and 49.
// The function makes sure that the user
// picks numbers between 1 and 49,
// and that the user does not choose dup
// licate numbers. Then the computer
// chooses five random numbers. Error co
// ntrol is used to check if the
// computer chooses the same number twic
// e. Then the numbers are displayed
// to the screen. Finally the calc funct
// ion is called to calculate and
// output the results of the game...
void lotto()
{
int i, k, j, b, x, y;// variables for all the for loops....
int s, t, user_error;
int user[5], comp[5];// arrays for user and computers numbers....
int user_error_check, comp_error_check; // to find if numbers are duplicates....
// Prompt user to choose numbers
cout << "Please choose five numbers from 0 to 49" << endl;
for (i=0; i<5; i++)
{
cout << "Enter number " << i + 1 << ":" ;
cin >> user[i];
if (user[i] > 49 user[i] < 0)
{
cout << "You did not enter a number from 0 to 49" << endl;
i--;
}
}
// ERROR CONTROL, to make sure that the
// user does not choose the
// same number twice.................
for(s=0; s<5; s++)
for(t=0; t
{
user_error_check = user[s];
if(user_error_check == user[t])
{
cout << "You have chosen duplicate numbers, please choose another number: " << endl;
cin >> user_error;
user[s] = user_error;
}
}
// Now the computer chooses numbers.....
// ........
for (k=0; k<5; k++)
{
comp[k] = (rand( ) % 49) + 1; // get comp to randomly generate 1 -49
}
// ERROR CONTROL, to make sure that the
// computer does not pick
// the same number twice.......
for(x=0; x<5; x++)
for(y=0; y
{
comp_error_check = comp[x];
if (comp_error_check == comp[y])
{
comp[x] = (rand() % 49) + 1;
}
} // no guarentee that the computer will not choose another number
// already in the array, but it's the be
// st i can do?????
cout << "You chose: " ;// output users numbers...
for (j=0; j<5; j++)
{
cout << user[j] << ", " ;
}
cout << endl;
cout << "The computer chose: "; // output computers numbers...
for (b=0; b<5; b++)
{
cout << comp[b] << ", " ;
}
cout << endl << endl;
calc(user, comp);// Obtain the results of the game......
}
// This function compares the computers
// random numbers to the numbers
// chosen by the user and lets the user
// know how many (if any) they
// have matched..............
void calc(int * a, int * b)
{
int i, k, match =0;
for (i=0; i<5; i++)
for (k=0; k<5; k++)
if ( a[i] == b[k] )
{
match += 1;
cout << "You have matched: " << a[i] << endl;
}
if (match == 0)
{
cout << "Sorry you have not matched any of the computers numbers..."
<< endl << endl;
}
if (match == 5)
{
cout << "CONGRATULATIONS, YOU HAVE WON THE LOTTERY!!!!!!!" << endl << endl;
}
}
// This function informs the user how to
// play the game and then allows
// the user to play the game until they
// enter 'q' or 'Q' at the prompt.
void menu()
{
char choice;
cout << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$" << endl;
cout << "$(C)$" << endl;
cout << "$ $" << endl;
cout << "$ *The computer will randomly choose five numbers $" << endl;
cout << "$ between 1 and 49.$" << endl;
cout << "$ $" << endl;
cout << "$ *You can choose five numbers and try to match$" << endl;
cout << "$ the computers numbers...... $" << endl;
cout << "$ $" << endl;
cout << "$ **Do not pick the same number twice or pick a$" << endl;
cout << "$number less than 1 or greater than 49, o.k.?$" << endl;
cout << "$ $" << endl;
cout << "$ $" << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$" << endl;
cout << endl;
while (choice != 'q' choice != 'Q')
{
cout << "Make a selection from the menu: " << endl;
cout << "[P]- Play the clotto against the computer." << endl;
cout << "[Q]- Exit this program." << endl;
cin >> choice;
switch(choice)
{
case 'p':
case 'P':
lotto();
break;
case 'q':
case 'Q':
cout << "No lotto for you!" << endl;
exit(0);
break;
}
}
}
New to c++.......Whta those this line means
y r u using scanf in c++???
/* Stack Implementation using Linked Lists */
#include
#include
/* Structure Declaration:
Structure for a Stack (Linked List Structure)
Requires two elements.
One Element member, holding the value that the stack is to be made of
One Self-Referring Pointer member for making it a Linked List - Stack. */
typedef struct stacklink
{
int element;
struct stacklink *next;
} top ;
typedef top stack;
/* Create:
Initializes the stack with a NULL value. */
void create(stack *s)
{
s=NULL;
}
/* Push:
Pushes (Inserts) a node into the Stack, using a new temporary node.
Stack's Push operation is done only on the top of the stack */
stack *push(stack *s)
{
int a=0;
stack *temp;
printf("nEnter the element to push into the stack: ");
scanf("%d",&a);
temp = (stack *)malloc(sizeof(stack));
temp->element = a;
temp->next = s;
return temp;
}
/* Pop:
Pops (Deletes) the top-most element out of the Stack.
Also checks if the stack is empty or not and pops only when there is an element available. */
stack *pop(stack *s)
{
stack *temp;
if(s->next!=NULL)
{
temp = s;
printf("nPopped element is: %dn",temp->element);
s = s->next;
free(temp);
}
else
{
printf("nNothing to Pop from the stack.n");
}
return s;
}
/* Display:
Displays the Stack in a pictorial fashion.
A sample output of the stack via this would look like:
The List is:
4 -> 5 -> 6 -> NULL
Where, NULL represents the END of stack. For an empty stack too, it shows NULL. */
void display(stack *s)
{
int temp=1;
printf("nThe Stack is:");
if(s->next==NULL)
{
printf("b empty");
}
while(s->next!=NULL)
{
if(temp)
{
printf("n");
temp=0;
}
printf("%d --> ",s->element);
s=s->next;
if(s->next==NULL)
{
printf("NULL");
break;
}
}
printf("n");
}
/* Main Menu:
Displays a menu-based access system for various operations on the stack.
The menu looks like:
1. Create
2. Push
3. Pop
4. Display
5. Exit
Each item in the menu calls the appropriate functions of the Stack. */
int main()
{
int choice, key, loc,throw=0;
stack *temp;
while(1)
{
printf("nStacksn-------n");
printf("1. Createn2. Pushn3. Popn4. Displayn5. Exit");
printf("nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
temp=(stack *)malloc(sizeof(stack));
create(temp);
printf("nNULL Stack createdn");
throw=1;
break;
case 2:
if(throw==1)
{
temp=push(temp);
display(temp);
}
else
{
printf("nCreate a stack first ..n");
}
break;
case 3:
if(throw==1)
{
temp=pop(temp);
display(temp);
}
else
{
printf("nCreate a stack first ..n");
}
break;
case 4:
if(throw==1)
display(temp);
else
printf("nCreate the stack first ..n");
break;
case 5:
return 0;
default:
printf("nInvalid choice ...n");
}
}
}
I know the variable names are quite big. But I think that makes it easier to understand the program without comments.
I am new to C++. So, if you can suggest any improvements, then please do so.
One more thing, from my experience, Indian authors tend to provide knowledge based on TurboC which sucks. So, anyone going for a C++ course, please get a book suggested above, and then be sure to read professional C++ by Solter and Kleper (Wrox international). It rocks.
Aditya
/* Project Name :- Bulls and Cows.
Project Version :- 2.1.0.
Project Author :- Aditya Shevade.
Time Started :- 5th of June 2007, 08:07 pm.
Time Finished :- 5th of June 2007, 09:21 pm.
Built Using :- Anjuta 1.2.4a.
*/
#include
#include
#include
using namespace std;
class BullsAndCows
{
private:
int ComputerNumber, UserNumber;
int ComputerReminderUnits, ComputerRem
Like system("shutdown /?"); for example.
Why would you wanna do it though ? I smell a prank :D
can someone tell me what's wrong in this program.Using Dev C++
Either Sykora's solution or simply add a float to your function definition line:
#include
using namespace std;
int main()
{
/*Use of Function*/
float a,b,sum;
float calsum(float a,float b);
cout<<"Enter the two nos.:n";
cin>>a>>b;
sum=calsum(a,b);
cout<<"The sum is :"<
return 0;
}
float calsum(float a,float b)
{
float sum;
sum=a+b;
return (sum);
}
#include
main()
{
float a,b,c,total,multiply,add,input;
printf("Welcome to Demon Calculator nn");
printf("Enter the values-nn");
printf("a:");
scanf("%f",&a);
printf("b:");
scanf("%f",&b);
printf("c:");
scanf("%f",&c);
total=a*b*c;
if (total>80000)
printf("Meri Marzi , tu khud solve kar.aaaaaaaaann");
else
printf("The answer is %fnnaa",total);
system("pause");
}
#If you have any other info about this subject , Please add it free.# |