Thread: newbie- need help with triangular #

  1. #1
    DirtElk
    Guest

    newbie- need help with triangular #

    Trying to figure out an exercise out of a text I've got.
    Supposed to use the formula:
    Triangular number = n(n+1)/2

    I now need to create something that will allow a user to enter initial, ending, and step values. i.e. 1 11 2 (this would give me an output of 1, 3, 5, 7, 9, 11)

    How do I go about using scanf, and a for loop to do this? Then I want to display it as a simple little table. i.e. triangular number
    _______________
    1
    3
    5
    7
    9
    11

    Probably something simple I have just overlooked...or too many hours at the office has my mind swimming.
    I appreciate any help pointing me in the right direction.

    Thanks.

  2. #2
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Smile

    Hope this code solves ur problem

    Code:
    #include <stdio.h>
    
    int main()
    {
       int initial_value,end_value,step_value,i;
    
       printf("\nEnter the Initial, End & Step Values\n");
       scanf("%d %d %d",&initial_value,&end_value,&step_value);
    
       for(i=initial_value;i<=end_value;i=i+step_value)
             printf("\n%d",i);
    
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    Much help...

    That helped alot...I was on the right track...just forgot one thing.

    So now a buddy and I are trying to figure out a couple of others.

    1. I'm trying to create a factorial table such as
    5! = 5x4x3x2x1 =120. What I want to do is just create a loop that uses scanf to ask a user to input the number they want the factorial of i.e. 6 and then the program will display 6!. Can anyone show a simple loop to do that?

    2. Want to use only int to take an integer that is entered i.e. 1234, then makes the sum of those digits (10). How do I do this one? Thanks again...

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    One down...

    Ok...I got my factorial program all figured out.... now need to figure out how to strip those digits and add them.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    heres a clue.....

    1234%10=4
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    Factorial of a number:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int num,i,j=1;
    
    	printf("Enter the number whose fatorial has to be found: ");
    	scanf("%d",&num);
    
    	for(i=1;i<=num;i++)
    	{
    		j=j*i;
    	}
    
    	printf("Fatorial is %d",j);
    
    	return 0;
    }

    sum of a 4 digit number

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int num,j,i;
    
    	printf("Enter a 4 digit number: ");
    	scanf("%d",&num);
    
    	for(i=1;i<=4;i++)
    	{
    		j=num%10;
    		num=num/10;
    		printf("%d",j);
    	}
    
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    starting to make sense...

    I understand the 1234%10....
    I'm just trying to figure out how to keep stripping those numbers...
    ie. I've got 1234%10=4, now I need to get 123%10, etc. and all them together...that where my struggle is. I'm sure it is just something simple....I'm probably just realizing how easy it is and making the logic to difficult.

    Silver....isn't the one you posted for sum of 4 digits just one that will reverse digits?

    Thanks for the help and hints everyone...it's coming together now.

  8. #8
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    Sorry m8. Just got a little confused with the question.

    Try this

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int num,j,i,k=0;
    
    	printf("Enter a 4 digit number: ");
    	scanf("%d",&num);
    
    	for(i=1;i<=4;i++)
    	{
    		j=num%10;
    		num=num/10;
    		k=k+j;
    
    	}
    
    	printf("%d",k);
    
    	return 0;
    }

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    Thanks for the help... I had acutally figured that piece out not long ago. I'm going to try some other stuff this weekend. If I get in a bind....you will see me again.

    Thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  3. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  4. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM