Post your C/C++ Programs Here

Status
Not open for further replies.
OP
Gigacore

Gigacore

Dreamweaver
Re: Post ur C/C++ Programs Here

Program To Reverse a Number

#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);
}
 
Last edited:

mehulved

18 Till I Die............
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.
 

shady_inc

Pee into the Wind...
Re: Post ur C/C++ Programs Here

Intel_Gigacore said:
use /* before the coment

....and */ after the comment.

can someone tell me what's wrong in this program.Using Dev C++

#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);
}
 

ilugd

Beware of the innocent
Re: Post ur C/C++ Programs Here

lol... why doesn't thinkdigit have a programming section?
 

xbonez

NP : Crysis
Re: Post ur C/C++ Programs Here

Yamaraj said:
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
 

shady_inc

Pee into the Wind...
Re: Post ur C/C++ Programs Here

xbonez said:
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++.
 

Sykora

I see right through you.
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().
 
OP
Gigacore

Gigacore

Dreamweaver
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?
 

ilugd

Beware of the innocent
Re: Post ur C/C++ Programs Here

do an iteration from 2 to int(n/2) and if it isn't divisible then it is prime.
 

Yamaraj

The Lord of Death
Re: Post ur C/C++ Programs Here

xbonez said:
hmm, u use Oxford and Sumita Arora's textbook for C++.
Who told you that? I recommend what I use - Lafore's, Prata's, Lippman's and Stroustrup's.
 

xbonez

NP : Crysis
Re: Post ur C/C++ Programs Here

sorry, i meant to say I use those books...typo *83.149.99.14/forums/style_emoticons/DarksideRG/blush2.gif
 

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

shady_inc said:
....and */ after the comment.

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:

Code:
#include <iostream>
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<<"";
        
    return 0;
}

[B] float[/B] calsum(float a,float b)
{ 
    float sum;
    
        sum=a+b;
        
    return (sum);
}
 

Nav11aug

In the zone
Re: Post ur C/C++ Programs Here

ilugd said:
do an iteration from 2 to int(n/2) and if it isn't divisible then it is prime.

bad math funda :D .. iterate from 2 to (int)sqrt(n) ..if it isnt divisible then itz prime

Yamaraj said:
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

shady_inc said:
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
 
Last edited:

ilugd

Beware of the innocent
Re: Post ur C/C++ Programs Here

i got 33% in my math exams during my bachelor's degree. Don't know how i lost it. Got 98% in +2 though. Come to think of it, I was socializing, eating and drinking the whole 3 years in BCA (if you know what I mean). :-D
 

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

Here's a simple Stack program I wrote and documented a bit in C long time ago. Surprisingly found it in my programs directory ..

Code:
/* Stack Implementation using Linked Lists */

#include<stdio.h>
#include<stdlib.h>

/* 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: %d\n",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("\nStacks\n-------\n");
                    printf("1. Create\n2. Push\n3. Pop\n4. Display\n5. Exit");
                    printf("\nEnter your choice: ");
                    scanf("%d",&choice);
                    
                    switch(choice)
                        {
                            case 1:
                            
                                temp=(stack *)malloc(sizeof(stack));
                                
                                create(temp);
                                
                                printf("\nNULL Stack created\n");
                                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");
                                
                        }
                }
        }
 
Status
Not open for further replies.
Top Bottom