Functions and structured programming

Discussion in 'C' started by zachry00, Sep 22, 2011.

  1. zachry00

    zachry00 New Member

    Joined:
    Jul 20, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    can you help me with this one?..

    write a program that accepts a long num. in the main prog. and produces several outputs through the functions described below. supply appropriate parameters.

    1.Display_With_Spaces(...)
    given the value of num, this function should display num with spaces in between. this function should not return any result to the main program.
    2.Reverse_Num(...)
    Given the value of num, this function should return the reversed form of num to the main program. The reversed form must be displayed in the main program.
    3.Sum_Odd_Position_digits(...)
    Given the value of num, this function should return the sum of odd positioned digits to the main program. the sum of the odd-positioned digits must be displayed in the main program.

    Sample Run:

    Enter Number: 12345
    Display with spaces :1 2 3 4 5
    Reversed number : 54321
    Sum of odd-positioned digits : 9

    your answers are greatly appriciated. thanks.
     
  2. DRK

    DRK New Member

    Joined:
    Apr 13, 2012
    Messages:
    44
    Likes Received:
    3
    Trophy Points:
    0
    Code:
    void Display_With_Spaces(long num)
    {
    	long weight = 1;
    
    	while (num / weight > 9)
    	{
    		weight *= 10;
    	}
    	while (weight > 0)
    	{
    		printf("%d ", num / weight);
    		num %= weight;
    		weight /= 10;
    	}
    }
    
    long Reverse_Num(long num)
    {
    	long result = 0;
    
    	while (num > 0)
    	{
    		result *= 10;
    		result += num % 10;
    		num /= 10;
    	}
    
    	return result;
    }
    
    int Sum_Odd_Position_digits(long num)
    {
    	int result = 0;
    	long weight = 1;
    	int position = 1;
    
    	while (num / weight > 9)
    	{
    		weight *= 10;
    	}
    	while (weight > 0)
    	{
    		if (position % 2 != 0)
    		{
    			result += num / weight;
    		}
    		num %= weight;
    		weight /= 10;
    		position++;
    	}
    
    	return result;
    }
     
  3. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void display(long int,long int);
    void reverse(long int);
    void oddposition(long int,long int);
    void main()
    {
    long int n,p=0,c;
    clrscr();
    printf("Enter the desired number");
    scanf("%ld",&n);
    c=n;
    while(c>0)
    {
    c=c/10;
    p++;
    }
    display(n,p);
    reverse(n);
    oddposition(n,p);
    getch();
    }
    void display(long int n,long int p)
    {
     long int m;
    while(p>0)
    {
    m=n/pow(10,(p-1));
    printf("%ld ",m);
    n=n-(m*pow(10,(p-1)));
    p--;
     }
     }
    void reverse(long int n)
    {
          long int k,rev=0;
    for(;n>0;n=n/10)
    {
    k=n%10;
    rev=rev*10+k;
    }
    printf("\n Reverse of the number is");
    printf("\n %ld",rev);
    }
    void oddposition(long int n,long int p)
    {
    long int s=0,r,t;
    while(p>0)
    {
    r=n/pow(10,(p-1));
    s+=r;
    n=n-(r*pow(10,(p-1)));
    p--;
    t=n/pow(10,(p-1));
    n=n-(t*pow(10,(p-1)));
    p--;
     }
     printf("\n Sum at odd positions");
     printf("\n %ld",s);
    }
     
  4. zachry00

    zachry00 New Member

    Joined:
    Jul 20, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    :pleased::pleased:thank you so much now i know why my friends love this forum.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice