 |
|
02-09-2007, 04:34 PM
|
#1 (permalink)
|
|
Dreamweaver
Join Date: Aug 2006
Location: Bangalore
Posts: 3,904
|
Post your C/C++ Programs Here
Hi guys n gals,
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
Code:
#include <stdio.h>
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.
● A program to findcalc area, surface area, volume, total surface area etc. of numerous 2D and 3D figures using functions.
● A Program to perform basic calculations.
● Program To Reverse a Number
● What is this?
● A Program that Binary, Decimal, Octal Inter-Conversions
● A bulls and cows game
● A program that selects a number which is of 4 digits and all are unique numbers.
● Write a C Program to sort numbers of an Array using Bubble Sort Method.
● Reusable java class to send email through pop3
● Simple Switch-Case Example.
● Swapping of two numbers without a Temporary variable in 1 line.
● A program that creates a vB list out of a file given in a particular format.
● Digit Forum URL Bulleted List Generator
● Write a C program to find an element from an Array using Binary Search Method.
● TOWER OF HANOI Simulator
● A Simple C Program to read and print a one-dimensional array
● C Program to find the largest element in an array and the position of its occurrence.
● A C program to find the maximum and minimum elements in an array having N elements
● Program which compares two strings, concatenate them and then gives the length of string.
● Program to kill a software /demo version expire in C
● Program To arrange numbers in Pyramid Pattern
● Program: a simple shop using string, integers, float, & algebraic operators
● Character Eater
● Program to insert a node in a single linked list.
● A C Program to convert decimal number to its Binary Number Equivalent
● Introduction to C++ - Stanford Video Tutorials and Other Lectures (Not a program)
● A Program to remind that u r using other account
● Typedef Example Program
● A C Program to check whether a given word is Palindrome or not.
● A Simple C Program to Display the number and its square from 1 to 10 using register variable.
● A C Program to find the sum of two matrix using two dimensional array.
● TURBO CALC 15000
● A Command Line Calculator.
● Function 2 input a matrix
● Read an int & an int array & search for that int in the int array & return its position.
● Python version of the above program.
● Program to perform operations on a binary search tree.
● Program to calculate the computer sales man salary. ( Correction)
● Basic Inventory tracker software program code.
● Sudoku
● Simple DOS Text-based alien shooter game
● A program for unit conversions.
===========================================
Last Update: 12 August 2008
--------------------------------------------------------
Some good free C/C++ Compilers
● Relo
● Bloodshed Dev C++
● GCC
● Borland C++ 5.5
● MinGW
● Open Watcom
__________________
Today's noobs are tomorrow's geeks. Don't make fun of them.. encourage them. - Gigacore
Follow me on twitter.com/gigacore
Last edited by ico; 15-10-2010 at 01:07 PM.
Reason: Listed programs
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
02-09-2007, 04:47 PM
|
#2 (permalink)
|
|
NP : Crysis
Join Date: Mar 2007
Location: City 17
Posts: 1,434
|
Re: Post ur C/C++ Programs Here
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
Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
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<<"\n\n1. 3-D Figures"
<<"\n\n2. Plane Figures"
<<"\n\n3. Exit"
<<"\n\nEnter your choice -->\t";
cin>>o1;
switch(o1)
{
case 1: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\t3-D Figures\n\n";
line();
cout<<"\n\n1. Cuboid"
<<"\n\n2. Cube"
<<"\n\n3. Cylinder"
<<"\n\n4. Cone"
<<"\n\n5. Sphere"
<<"\n\n6. Hemisphere"
<<"\n\n7. Main Menu"
<<"\n\nEnter your choice -->\t";
cin>>o2;
switch(o2)
{
case 1: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCuboid\n\n";
line();
cuboid();
getch();
break;
case 2: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCube\n\n";
line();
cube();
getch();
break;
case 3: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCylinder\n\n";
line();
cylinder();
getch();
break;
case 4: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCone\n\n";
line();
cone();
getch();
break;
case 5: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tSphere\n\n";
line();
sphere();
getch();
break;
case 6: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tHemisphere\n\n";
line();
hemisphere();
getch();
break;
case 7: goto menu;
default: cout<<"\n\nInvalid Input\a\a\a";
goto menu;
}
getch();
break;
case 2: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tPlane Figures\n\n";
line();
cout<<"\n\n1. Traingle"
<<"\n\n2. Rectangle"
<<"\n\n3. Sqaure"
<<"\n\n4. Circle"
<<"\n\n5. Parallelogram"
<<"\n\n6. Main Menu"
<<"\n\nEnter your choice -->\t";
cin>>o2;
switch(o2)
{
case 1: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tTraingle\n\n";
line();
triangle();
getch();
break;
case 2: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tRectangle\n\n";
line();
rectangle();
getch();
break;
case 3: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tSquare\n\n";
line();
square();
getch();
break;
case 4: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCircle\n\n";
line();
circle();
getch();
break;
case 5: clrscr();
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tParallelogram\n\n";
line();
parallelogram();
getch();
break;
case 6: goto menu;
default: cout<<"\n\nInvalid Input\a\a\a";
goto menu;
}
getch();
break;
case 3: cout<<"\n\nPress any key to exit...\a";
getch();
break;
default: cout<<"\n\nInvalid Input\a\a\a\a";
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<<"\n\nEnter length of cuboid -->\t";
cin>>l;
cout<<"\n\nEnter breadth of cuboid -->\t";
cin>>b;
cout<<"\n\nEnter height of cuboid -->\t";
cin>>h;
v=l*b*h;
a=2*(l*b+b*h+l*h);
cout<<"\n\nVolume of cuboid is "<<v;
cout<<"\n\nSurface area of cuboid is "<<a;
}
void cube()
{
float l,a,v;
cout<<"\n\nEnter length of side of cube -->\t";
cin>>l;
v=l*l*l;
a=6*l*l;
cout<<"\n\nVolume of cube is "<<v;
cout<<"\n\nSurface area of cube is "<<a;
}
void cylinder()
{
float r,h,v,c,t;
cout<<"\n\nEnter radius of cylinder -->\t";
cin>>r;
cout<<"\n\nEnter 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<<"\n\nVolume of cylinder is "<<v;
cout<<"\n\nCurved Surface Area of cylinder is "<<c;
cout<<"\n\nTotal Surface Area of cylinder is "<<t;
}
void cone()
{
float r,h,l,v,c,t;
cout<<"\n\nEnter radius of cone -->\t";
cin>>r;
cout<<"\n\nEnter 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<<"\n\nSlant height of cone is "<<l;
cout<<"\n\nVolume of cone is "<<v;
cout<<"\n\nCurved Surface Area of cone is "<<c;
cout<<"\n\nTotal Surface Area of cone is "<<t;
}
void sphere()
{
float r,v,a;
cout<<"\n\nEnter radius of sphere -->\t";
cin>>r;
v=(4*3.14*r*r*r)/3;
a=4+3.14*r*r;
cout<<"\n\nVolume of sphere is "<<v;
cout<<"\n\nSurface Area of sphere is "<<a;
}
void hemisphere()
{
float r,v,c,t;
cout<<"\n\nEnter 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<<"\n\nVolume of hemisphere is "<<v;
cout<<"\n\nCurved Surface Area of hemisphere is "<<c;
cout<<"\n\nTotal Surface Area of hemisphere is "<<t;
}
void triangle()
{
float b,h,a;
cout<<"\n\nEnter base of triangle -->\t";
cin>>b;
cout<<"\n\nEnter height of triangle -->\t";
cin>>h;
a=(b*h)/2;
cout<<"\n\nArea of the triangle is "<<a;
}
void rectangle()
{
float l,b,a,p;
cout<<"\n\nEnter length of rectangle -->\t";
cin>>l;
cout<<"\n\nEnter breadth of rectangle -->\t";
cin>>b;
a=l*b;
p=2*(l+b);
cout<<"\n\nArea of rectangle is "<<a;
cout<<"\n\nPerimeter of rectangle is "<<p;
}
void square()
{
float s,a,p;
cout<<"\n\nEnter length of side of square -->\t";
cin>>s;
a=s*s;
p=4*s;
cout<<"\n\nArea of square is "<<a;
cout<<"\n\nPerimeter of square is "<<p;
}
void circle()
{
float r,a,c;
cout<<"\n\nEnter radius of circle -->\t";
cin>>r;
a=3.14*r*r;
c=2*3.14*r;
cout<<"\n\nArea of circle is "<<a;
cout<<"\n\nCircumference of circle is "<<c;
}
void parallelogram()
{
float b,h,a,p;
cout<<"\n\nEnter base of parallelogram -->\t";
cin>>b;
cout<<"\n\nEnter height of parallelogram -->\t";
cin>>h;
a=b*h;
p=2*(b+h);
cout<<"\n\nArea of parallelogram is "<<a;
cout<<"\n\nPerimeter of parallelogram is "<<p;
}
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...
__________________
Gaming: Phenom II x4 965BE @ 3.6Ghz/Corsair H50 Liquid Cooling/MSI 790FX-GD70/2x2GB GSkill Ripjaws 7-7-7-24/3 x GTX 470 w/Zalman VF3000F/Corsair 850W PSU/Antec 1200/2xSpinpoint F3 500Gb RAID 0
|
|
|
02-09-2007, 04:49 PM
|
#3 (permalink)
|
|
IM AS MAD AS HELL!!
Join Date: Oct 2006
Location: localhost
Posts: 1,618
|
Re: Post ur C/C++ Programs Here
My Program to calculate Addition
Code:
#include <stdlib.h>
main()
{
float a,b,c,total,multiply,add,input;
printf("Welcome to Demon Calculator \n\n");
printf("Enter the values-\n\n");
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.\a\a\a\a\a\a\a\a\a\n\n");
else
printf("The answer is %f\n\n\a\a",total);
system("pause");
}
__________________
When someone dies in the grip of a powerful rage... a curse is born.
Kayako Saeki: Croakkkkkkkkkkkkkkkkkkkkk!
|
|
|
02-09-2007, 04:52 PM
|
#4 (permalink)
|
|
||uLtiMaTE WinNER||
Join Date: Nov 2006
Location: Kathmandu,Nepal
Posts: 698
|
Re: Post ur C/C++ Programs Here
scanf("%d%d%d",&a,&b,&c);
New to c++.......Whta those this line means
__________________
||uLtiMaTE WinNER||
|
|
|
02-09-2007, 04:58 PM
|
#5 (permalink)
|
|
The Lord of Death
Join Date: May 2005
Location: यमलोक
Posts: 253
|
Re: Post ur C/C++ Programs Here
Quote:
|
Originally Posted by Intel_Gigacore
Code:
#include <stdio.h>
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.
Quote:
|
Originally Posted by xbonez
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.
Quote:
|
Originally Posted by Ron
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++.
Last edited by Yamaraj; 02-09-2007 at 04:58 PM.
Reason: Automerged Doublepost
|
|
|
02-09-2007, 04:59 PM
|
#6 (permalink)
|
|
Pawned!... Beyond GODLIKE
Join Date: May 2006
Location: World Of Warcraft -DOTA
Posts: 1,051
|
Re: Post ur C/C++ Programs Here
+no the program is correct dude
__________________
If God has indeed created Himself in His own image, then I submit to you that God is a cockroach :mrgreen:!!!!!!!!
|
|
|
02-09-2007, 05:07 PM
|
#7 (permalink)
|
|
Dreamweaver
Join Date: Aug 2006
Location: Bangalore
Posts: 3,904
|
Re: Post ur C/C++ Programs Here
i added the return statement, is it rite now?
__________________
Today's noobs are tomorrow's geeks. Don't make fun of them.. encourage them. - Gigacore
Follow me on twitter.com/gigacore
|
|
|
02-09-2007, 05:11 PM
|
#8 (permalink)
|
|
Google Bot
Join Date: Aug 2005
Posts: 9,772
|
Re: Post ur C/C++ Programs Here
Quote:
|
Originally Posted by Ron
scanf("%d%d%d",&a,&b,&c);
New to c++.......Whta those this line means
|
y r u using scanf in c++???
|
|
|
02-09-2007, 05:30 PM
|
#9 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Post ur C/C++ Programs Here
Quote:
|
Originally Posted by Yamaraj
Your code doesn't conform to either ISO C or C++ standards.
|
Don't be so hard on him  He doesn't know TC++'s evils
Anyway, here's xbonez's program in proper code
Code:
#include<iostream>
#include<math.h>
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<<"\n\n1. 3-D Figures"
<<"\n\n2. Plane Figures"
<<"\n\n3. Exit"
<<"\n\nEnter your choice -->\t";
cin>>o1;
switch(o1)
{
case 1: cout<<"\n\n";
line();
cout<<"\n\t\t\t\t3-D Figures\n\n";
line();
cout<<"\n\n1. Cuboid"
<<"\n\n2. Cube"
<<"\n\n3. Cylinder"
<<"\n\n4. Cone"
<<"\n\n5. Sphere"
<<"\n\n6. Hemisphere"
<<"\n\n7. Main Menu"
<<"\n\nEnter your choice -->\t";
cin>>o2;
switch(o2)
{
case 1: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCuboid\n\n";
line();
cuboid();
break;
case 2: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCube\n\n";
line();
cube();
break;
case 3: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCylinder\n\n";
line();
cylinder();
break;
case 4: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCone\n\n";
line();
cone();
break;
case 5: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tSphere\n\n";
line();
sphere();
break;
case 6: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tHemisphere\n\n";
line();
hemisphere();
break;
case 7: break;
default: cout<<"\n\nInvalid Input\a\a\a";
}
break;
case 2:
cout<<"\n\n";
line();
cout<<"\n\t\t\t\tPlane Figures\n\n";
line();
cout<<"\n\n1. Traingle"
<<"\n\n2. Rectangle"
<<"\n\n3. Sqaure"
<<"\n\n4. Circle"
<<"\n\n5. Parallelogram"
<<"\n\n6. Main Menu"
<<"\n\nEnter your choice -->\t";
cin>>o2;
switch(o2)
{
case 1: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tTraingle\n\n";
line();
triangle();
break;
case 2: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tRectangle\n\n";
line();
rectangle();
break;
case 3: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tSquare\n\n";
line();
square();
break;
case 4: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tCircle\n\n";
line();
circle();
break;
case 5: cout<<"\n\n";
line();
cout<<"\n\t\t\t\tParallelogram\n\n";
line();
parallelogram();
break;
case 6: break;
default: cout<<"\n\nInvalid Input\a\a\a";
}
break;
case 3: cout<<"\n\nPress any key to exit...\a";
break;
default: cout<<"\n\nInvalid Input\a\a\a\a";
}
}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<<"\n\nEnter length of cuboid -->\t";
cin>>l;
cout<<"\n\nEnter breadth of cuboid -->\t";
cin>>b;
cout<<"\n\nEnter height of cuboid -->\t";
cin>>h;
v=l*b*h;
a=2*(l*b+b*h+l*h);
cout<<"\n\nVolume of cuboid is "<<v;
cout<<"\n\nSurface area of cuboid is "<<a;
}
void cube()
{
float l,a,v;
cout<<"\n\nEnter length of side of cube -->\t";
cin>>l;
v=l*l*l;
a=6*l*l;
cout<<"\n\nVolume of cube is "<<v;
cout<<"\n\nSurface area of cube is "<<a;
}
void cylinder()
{
float r,h,v,c,t;
cout<<"\n\nEnter radius of cylinder -->\t";
cin>>r;
cout<<"\n\nEnter 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<<"\n\nVolume of cylinder is "<<v;
cout<<"\n\nCurved Surface Area of cylinder is "<<c;
cout<<"\n\nTotal Surface Area of cylinder is "<<t;
}
void cone()
{
float r,h,l,v,c,t;
cout<<"\n\nEnter radius of cone -->\t";
cin>>r;
cout<<"\n\nEnter 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<<"\n\nSlant height of cone is "<<l;
cout<<"\n\nVolume of cone is "<<v;
cout<<"\n\nCurved Surface Area of cone is "<<c;
cout<<"\n\nTotal Surface Area of cone is "<<t;
}
void sphere()
{
float r,v,a;
cout<<"\n\nEnter radius of sphere -->\t";
cin>>r;
v=(4*3.14*r*r*r)/3;
a=4+3.14*r*r;
cout<<"\n\nVolume of sphere is "<<v;
cout<<"\n\nSurface Area of sphere is "<<a;
}
void hemisphere()
{
float r,v,c,t;
cout<<"\n\nEnter 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<<"\n\nVolume of hemisphere is "<<v;
cout<<"\n\nCurved Surface Area of hemisphere is "<<c;
cout<<"\n\nTotal Surface Area of hemisphere is "<<t;
}
void triangle()
{
float b,h,a;
cout<<"\n\nEnter base of triangle -->\t";
cin>>b;
cout<<"\n\nEnter height of triangle -->\t";
cin>>h;
a=(b*h)/2;
cout<<"\n\nArea of the triangle is "<<a;
}
void rectangle()
{
float l,b,a,p;
cout<<"\n\nEnter length of rectangle -->\t";
cin>>l;
cout<<"\n\nEnter breadth of rectangle -->\t";
cin>>b;
a=l*b;
p=2*(l+b);
cout<<"\n\nArea of rectangle is "<<a;
cout<<"\n\nPerimeter of rectangle is "<<p;
}
void square()
{
float s,a,p;
cout<<"\n\nEnter length of side of square -->\t";
cin>>s;
a=s*s;
p=4*s;
cout<<"\n\nArea of square is "<<a;
cout<<"\n\nPerimeter of square is "<<p;
}
void circle()
{
float r,a,c;
cout<<"\n\nEnter radius of circle -->\t";
cin>>r;
a=3.14*r*r;
c=2*3.14*r;
cout<<"\n\nArea of circle is "<<a;
cout<<"\n\nCircumference of circle is "<<c;
}
void parallelogram()
{
float b,h,a,p;
cout<<"\n\nEnter base of parallelogram -->\t";
cin>>b;
cout<<"\n\nEnter height of parallelogram -->\t";
cin>>h;
a=b*h;
p=2*(b+h);
cout<<"\n\nArea of parallelogram is "<<a;
cout<<"\n\nPerimeter of parallelogram is "<<p;
}
__________________
Harsh J
www.harshj.com
|
|
|
02-09-2007, 05:46 PM
|
#10 (permalink)
|
|
Dreamweaver
Join Date: Aug 2006
Location: Bangalore
Posts: 3,904
|
Re: Post ur C/C++ Programs Here
Guys... is my program rite now?
__________________
Today's noobs are tomorrow's geeks. Don't make fun of them.. encourage them. - Gigacore
Follow me on twitter.com/gigacore
|
|
|
02-09-2007, 07:35 PM
|
#11 (permalink)
|
|
The Lord of Death
Join Date: May 2005
Location: यमलोक
Posts: 253
|
Re: Post ur C/C++ Programs Here
Quote:
|
Originally Posted by Intel_Gigacore
Guys... is my program rite now?
|
Try this:
Code:
#include <stdio.h>
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;
}
|
|
|
02-09-2007, 07:42 PM
|
#12 (permalink)
|
|
Dreamweaver
Join Date: Aug 2006
Location: Bangalore
Posts: 3,904
|
Re: Post ur C/C++ Programs Here
^ thanks buddy......
__________________
Today's noobs are tomorrow's geeks. Don't make fun of them.. encourage them. - Gigacore
Follow me on twitter.com/gigacore
|
|
|
02-09-2007, 07:43 PM
|
#13 (permalink)
|
|
damn busy...
Join Date: Sep 2006
Location: Jhansi/Meerut
Posts: 1,991
|
Re: Post ur C/C++ Programs Here
this is a program in which u will hav 2 enter any 5 nos between 1 to 49 and if the number matches with the numbers chosen by the computer, u will win the lottery
#include <cstdlib>
#include <iostream>
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<s; 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<x; 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;
}
}
}
__________________
MSI GX660 with ATI 5870 :grin: ultimate gaming lappy :grin:
Dell Studio 15(1555)
1TB+1.5TB external|N86|ZTE Blade|5230|E63|EP-630|Soundmagic PL50|Sennheiser CXL 400|Meelec M11P+
www.techjunkiez.com
|
|
|
02-09-2007, 07:47 PM
|
#14 (permalink)
|
|
The Sexy Beast
Join Date: Feb 2006
Location: Mumbai
Posts: 618
|
Re: Post ur C/C++ Programs Here
^^this is totally wrong what if b>c>gr8test there's no condition to test that
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
|
|
|
02-09-2007, 07:49 PM
|
#15 (permalink)
|
|
Dreamweaver
Join Date: Aug 2006
Location: Bangalore
Posts: 3,904
|
Re: Post ur C/C++ Programs Here
@ utsav... did it execute buddy?
__________________
Today's noobs are tomorrow's geeks. Don't make fun of them.. encourage them. - Gigacore
Follow me on twitter.com/gigacore
|
|
|
02-09-2007, 07:52 PM
|
#16 (permalink)
|
|
The Lord of Death
Join Date: May 2005
Location: यमलोक
Posts: 253
|
Re: Post ur C/C++ Programs Here
Quote:
|
Originally Posted by ratedrsuperstar
^^this is totally wrong what if b>c>gr8test there's no condition to test that
|
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.
|
|
|
02-09-2007, 07:56 PM
|
#17 (permalink)
|
|
NP : Crysis
Join Date: Mar 2007
Location: City 17
Posts: 1,434
|
Re: Post ur C/C++ Programs Here
@yamraj : what was wrong with my prog??  it seemed to run perfectly fine
__________________
Gaming: Phenom II x4 965BE @ 3.6Ghz/Corsair H50 Liquid Cooling/MSI 790FX-GD70/2x2GB GSkill Ripjaws 7-7-7-24/3 x GTX 470 w/Zalman VF3000F/Corsair 850W PSU/Antec 1200/2xSpinpoint F3 500Gb RAID 0
|
|
|
02-09-2007, 08:00 PM
|
#18 (permalink)
|
|
The Lord of Death
Join Date: May 2005
Location: यमलोक
Posts: 253
|
Re: Post ur C/C++ Programs Here
Quote:
|
Originally Posted by xbonez
@yamraj : what was wrong with my prog??  it seemed to run perfectly fine
|
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.
|
|
|
02-09-2007, 08:32 PM
|
#19 (permalink)
|
|
damn busy...
Join Date: Sep 2006
Location: Jhansi/Meerut
Posts: 1,991
|
Re: Post ur C/C++ Programs Here
@ gigacore it executed nicely on devc++. tell me the problm with my program
__________________
MSI GX660 with ATI 5870 :grin: ultimate gaming lappy :grin:
Dell Studio 15(1555)
1TB+1.5TB external|N86|ZTE Blade|5230|E63|EP-630|Soundmagic PL50|Sennheiser CXL 400|Meelec M11P+
www.techjunkiez.com
|
|
|
02-09-2007, 08:35 PM
|
#20 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
Re: Post ur C/C++ Programs Here
And for heaven's sake indent the code like the first post. It's unreadable otherwise.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
|
|
|
02-09-2007, 08:40 PM
|
#21 (permalink)
|
|
Dreamweaver
Join Date: Aug 2006
Location: Bangalore
Posts: 3,904
|
Re: Post ur C/C++ Programs Here
Program To Reverse a Number
Quote:
#include <stdio.h>
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);
}
|
__________________
Today's noobs are tomorrow's geeks. Don't make fun of them.. encourage them. - Gigacore
Follow me on twitter.com/gigacore
Last edited by Gigacore; 02-09-2007 at 08:59 PM.
|
|
|
02-09-2007, 08:49 PM
|
#22 (permalink)
|
|
18 Till I Die............
Join Date: Jul 2004
Location: India, Mumbai, Marine Lines
Posts: 5,792
|
Re: Post ur C/C++ Programs Here
And how about using comments in the code so people like me could understand it faster and better.
|
|
|
02-09-2007, 08:52 PM
|
#23 (permalink)
|
|
damn busy...
Join Date: Sep 2006
Location: Jhansi/Meerut
Posts: 1,991
|
Re: Post ur C/C++ Programs Here
^^ i hav used comments in my program
__________________
MSI GX660 with ATI 5870 :grin: ultimate gaming lappy :grin:
Dell Studio 15(1555)
1TB+1.5TB external|N86|ZTE Blade|5230|E63|EP-630|Soundmagic PL50|Sennheiser CXL 400|Meelec M11P+
www.techjunkiez.com
|
|
|
02-09-2007, 08:52 PM
|
#24 (permalink)
|
|
Dreamweaver
Join Date: Aug 2006
Location: Bangalore
Posts: 3,904
|
Re: Post ur C/C++ Programs Here
use /* before the coment
__________________
Today's noobs are tomorrow's geeks. Don't make fun of them.. encourage them. - Gigacore
Follow me on twitter.com/gigacore
|
|
|
02-09-2007, 08:58 PM
|
#25 (permalink)
|
|
Pee into the Wind...
Join Date: May 2007
Location: Mumbai
Posts: 782
|
Re: Post ur C/C++ Programs Here
Quote:
|
Originally Posted by Intel_Gigacore
use /* before the coment
|
....and */ after the comment.
can someone tell me what's wrong in this program.Using Dev C++
Quote:
#include <iostream>
#include <conio.h>
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 :"<<sum<<"";
}
calsum(float a,float b)
{ float sum;
sum=a+b;
return (sum);
}
|
__________________
Life is as complicated as you say it is.
|
|
|
02-09-2007, 09:03 PM
|
#26 (permalink)
|
|
Beware of the innocent
Join Date: Dec 2005
Posts: 1,024
|
Re: Post ur C/C++ Programs Here
lol... why doesn't thinkdigit have a programming section?
__________________
Life is too short. Have fun.
|
|
|
02-09-2007, 09:04 PM
|
#27 (permalink)
|
|
NP : Crysis
Join Date: Mar 2007
Location: City 17
Posts: 1,434
|
Re: Post ur C/C++ Programs Here
Quote:
|
Originally Posted by Yamaraj
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.
|
hmm, u use Oxford and Sumita Arora's textbook for C++. they're our course books in XI and XII
__________________
Gaming: Phenom II x4 965BE @ 3.6Ghz/Corsair H50 Liquid Cooling/MSI 790FX-GD70/2x2GB GSkill Ripjaws 7-7-7-24/3 x GTX 470 w/Zalman VF3000F/Corsair 850W PSU/Antec 1200/2xSpinpoint F3 500Gb RAID 0
|
|
|
02-09-2007, 09:10 PM
|
#28 (permalink)
|
|
Pee into the Wind...
Join Date: May 2007
Location: Mumbai
Posts: 782
|
Re: Post ur C/C++ Programs Here
Quote:
|
Originally Posted by xbonez
hmm, u use Oxford and Sumita Arora's textbook for C++. they're our course books in XI and XII
|
I find books by Yashavant Kanetkar the best ones for C/C++.
__________________
Life is as complicated as you say it is.
|
|
|
02-09-2007, 09:11 PM
|
#29 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
Re: Post ur C/C++ Programs Here
@shady_inc :
conio.h is non-standard.
Your calsum() doesn't have a forward declaration. Put main() after calsum() or put the line :
Code:
int calsum(float, float);
before main().
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
|
|
|
02-09-2007, 09:27 PM
|
#30 (permalink)
|
|
Dreamweaver
Join Date: Aug 2006
Location: Bangalore
Posts: 3,904
|
Re: Post ur C/C++ Programs Here
does any know how to write this one?
Write a program using functions to find whether the given number is a prime number?
__________________
Today's noobs are tomorrow's geeks. Don't make fun of them.. encourage them. - Gigacore
Follow me on twitter.com/gigacore
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|