FTalk

FTalk

Welcome guest! Please Login or Register.

#1  2009-09-12 13:24:11

othe_89
 wrath of music. instumental dread
othe_89's display avatar
» FTalker
FTalk Level: 2
roll ur lyf baby!!
Class abc-123
canada
152
11
Today

C Programming Exercises: Test your Skill (Basic)

Ex. #1
Write a C program using a Function Prototype wherein the program will ask the user to input a number and returns 1 if it is Prime and 0 if it is not. (Challenge: returns the word "prime" itself if it is prime and "not prime" if not).

Output:

Ex. #2
Write a C program where it will ask the user to input the number of students and number of exams they had. For every student, the grade for each examination will be asked and the average score for each student will be given.

Output:

Ex. #3
Write a C program wherein it will ask the user to input a name and password. Then the program will clear the screen and display the typed in name as 'username' and will ask to verify the password. The program will NOT EXIT until the user enters the correct password. And if the user did, the program will display a certain message that the Registration is complete/successful.

Output:

Ex. #4
Write a C program that will ask you to guess a number from 1-10 (the program will randomize numbers from 1 to 10) and the program will display "correct" if you have guessed correctly, and "Incorrect" otherwise. The program will then ask you if you want to try again ("Try Again? Y/N") and will NOT terminate unless you press N.

Output:

Ex. #5
A Fibonacci program. Write a C program that will ask you up to how many Fibonacci digits will be displayed. I.e., if you typed in 9, it will display "1 1 2 3 5 8 13 21 34". The Screenshots below will help you analyze. Using only FOR LOOP.

Output:

Last edited by othe_89 (2009-10-03 07:32:17)

                    

lollipop93[?] likes this topic.

#2  2009-09-29 07:26:32

flint_hydz
 Tired of making layouts..
flint_hydz's display avatar
» FTalkGeek
FTalk Level: zero
Je L'Aime autant de :(
The Insiders
The Vow City
1419
66
Website

Re: C Programming Exercises: Test your Skill (Basic)

I get #2, I'm having a hard time @ #3 It always loop the "Verify Password".
Obviously, I didn't tried #4 and #5 yet.

and #1, I don't know function prototype. XD

Anyway, here's my code in #2. :D

Code:

#include <conio.h>
#include <iostream.h>

void main() {
 clrscr();
 int numStud, numEx;
 int i, n = 0;
 float avg, sol, grade;
 cout<<"Enter the number of Students : ";
 cin >> numStud;
 cout<<"\nEnter the number of Exams : ";
 cin >> numEx;
 for ( i = 1; i <= numStud; i++ ) {
 n = 0;
 sol = 0;
  cout<<"\n\nStudent #"<< i;
    do {
     n++;
     cout<<"\n\nGrade #"<< n <<": ";
     cin >> grade;
     sol += grade;
    } while ( n != numEx );
  avg = sol/numEx;
  cout<<"\n\n\nThe Average is "<< avg;
 }
 getch();
}

#3  2009-09-30 16:32:43

othe_89
 wrath of music. instumental dread
othe_89's display avatar
» FTalker
FTalk Level: 2
roll ur lyf baby!!
Class abc-123
canada
152
11
Today

Re: C Programming Exercises: Test your Skill (Basic)

flint_hydz wrote:

matagal na akong di nka C++. :lol: wew..
*nosebleed*

Wow. very good. didn't expected that you'd answer it by C++ (though it's for C), haha. Well anyway, i'm quite amazed, 'cause i'm not really that familiar with C++ functions, i'm more used to C. But your answer is definitely correct. Though our codes are different, the output was the same.

flint_hydz wrote:

I don't know function prototype

I wanted to give you a sample of this program, but i reformatted my laptop and forgot to back up my C++ sample programs. I haven't saved any in the net so I have to redo all of these again. ;)


Anyway, Hope u could answer #3 soon!! Gudluck. :D

Last edited by othe_89 (2009-09-30 16:39:08)

#4  2009-10-01 05:49:42

dhenzbhee
 dhenzbhee
dhenzbhee's display avatar
» n00b
FTalk Level: zero
23
2
2010-02-24

Re: C Programming Exercises: Test your Skill (Basic)

:arrow: here's the code on the first one... hope this is what you mean..

Code:

#include <stdio.h>
#include <conio.h>

main()
{
  clrscr();
  int num, rem;
  printf("ENTER A NUMBER: ");
  scanf("%d", &num);

  rem = num % 2;

  if (rem == 0)
    printf("NOT PRIME");
  else
    printf("PRIME");

  getch();

}

:arrow: here's the fourth one...

Code:

#include<stdio.h>
#include<conio.h>

main()
{
    char ans;

    do
    {

    clrscr();
    int num;

    printf("Guess a number [1-10]");
    scanf("%d",&num);

    if (num==4)
        printf("CORRECT!");
    if (((num>=1)&(num<4)) || ((num>4)&(num<=10)))
        printf("INCORRECT!");
    if ((num>10) || (num<1))
        printf("OUT OF RANGE!");




    printf("\n\nDo you want to try Another?[Y/N]");
    scanf("%s",&ans);
    } while((ans=='Y')||(ans=='y'));

    getch();
}

:arrow: and here's the fifth one... fibonacci... using for loop

Code:

#include <stdio.h>
#include <conio.h>

main()

{
    char ans;

    do
    {

    clrscr();
    int n, i, x, a, b;

    printf("Enter a number: ");
    scanf("%d", &n);

      a = x = 1;
      printf("\nFibonacci series:");
      for (i=1; i<=n; i++)
      {
    printf(" %d",x);
    b = x+a;
    x = a;
    a = b;
      }
      printf("\n\nDo you want to try another?[Y/N]");
      scanf("%s",&ans);

      } while((ans=='Y')||(ans=='y'));
      getch();
}

:idea: i didn't make other considerations in each problems like, should accept numeric values only, overflow, etc., since the problem was not that specific. this is just the basic part of the problems... by the way, i didn't do the 2nd and 3rd prob. coz i got a little hard time doing the 1st, 4th and 5th probs. :D maybe next time. :D

Last edited by dhenzbhee (2009-10-01 05:58:27)

#5  2009-10-02 13:47:08

othe_89
 wrath of music. instumental dread
othe_89's display avatar
» FTalker
FTalk Level: 2
roll ur lyf baby!!
Class abc-123
canada
152
11
Today

Re: C Programming Exercises: Test your Skill (Basic)

For your #1:

dhenzbhee wrote:

#include <stdio.h>
#include <conio.h>

main()
{
  clrscr();
  int num, rem;
  printf("ENTER A NUMBER: ");
  scanf("%d", &num);

  rem = num % 2;

  if (rem == 0)
    printf("NOT PRIME");
  else
    printf("PRIME");

  getch();

}

This, my friend, is wrong. I tried running your program and it modified 1,15,25 and some other numbers as prime numbers, but they are not. Actually, the list of prime numbers are:

For your #4:

dhenzbhee wrote:

#include<stdio.h>
#include<conio.h>

main()
{
    char ans;

    do
    {

    clrscr();
    int num;

    printf("Guess a number [1-10]");
    scanf("%d",&num);

    if (num==4)
        printf("CORRECT!");
    if (((num>=1)&(num<4)) || ((num>4)&(num<=10)))
        printf("INCORRECT!");
    if ((num>10) || (num<1))
        printf("OUT OF RANGE!");




    printf("\n\nDo you want to try Another?[Y/N]");
    scanf("%s",&ans);
    } while((ans=='Y')||(ans=='y'));

    getch();
}

This, again, is wrong. The program didn't randomize the said numbers but actually, only focused on the "4" number as the Correct one. The program should randomly choose a number between 1 and 10. And the user should type in the number of his guess. So if the user kept on choosing the same number over and over again, he should get either Correct or Incorrect message spontaneously. In your program, number 4 was the only one correct and the rest were not. You only need the code for randomization and you can get this done next time. ;)


For your #5:

dhenzbhee wrote:

#include <stdio.h>
#include <conio.h>

main()

{
    char ans;

    do
    {

    clrscr();
    int n, i, x, a, b;

    printf("Enter a number: ");
    scanf("%d", &n);

      a = x = 1;
      printf("\nFibonacci series:");
      for (i=1; i<=n; i++)
      {
    printf(" %d",x);
    b = x+a;
    x = a;
    a = b;
      }
      printf("\n\nDo you want to try another?[Y/N]");
      scanf("%s",&ans);

      } while((ans=='Y')||(ans=='y'));
      getch();
}

This one is correct. Very clear and detailed. Your code is understandable and more simplier than mine actually =). Congrats. ;)

Over all. You got 1 correct program out of 3.  :thumbsup: Hope you get the rest. =) Keep it up.

#6  2009-10-03 06:24:33

dhenzbhee
 dhenzbhee
dhenzbhee's display avatar
» n00b
FTalk Level: zero
23
2
2010-02-24

Re: C Programming Exercises: Test your Skill (Basic)

but sir othe_89, you did not mention on your problem no. 4 that the correct number should be picked randomize, so i assumed that i will assign my own number. i just did what's on your problem #4 which is to...

anyway, if it is randomize, heres the code..

Code:

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

main()
{
    char ans;

    do
    {

    clrscr();
    int num, i;

    randomize();

    printf("Guess a number [1-10]");
    scanf("%d",&num);

    i = rand() % 11;

    if (num==i)
        printf("CORRECT!");
    if ((num!=i) && ((num>=0 && num<=10)))
        printf("INCORRECT!");
    if (num>10)
        printf("OUT OF RANGE!");


    printf("\n\nDo you want to try Another?[Y/N]");
    scanf("%s",&ans);
    } while((ans=='Y')||(ans=='y'));

    getch();
}

on example #1, my mistake, i thought prime # and not prime # is the same as odd and even. :wallbash: :D

Last edited by dhenzbhee (2009-10-03 06:27:02)

#7  2009-10-03 07:30:38

othe_89
 wrath of music. instumental dread
othe_89's display avatar
» FTalker
FTalk Level: 2
roll ur lyf baby!!
Class abc-123
canada
152
11
Today

Re: C Programming Exercises: Test your Skill (Basic)

dhenzbhee wrote:

but sir othe_89, you did not mention on your problem no. 4 that the correct number should be picked randomize, so i assumed that i will assign my own number. i just did what's on your problem #4 which is to...

Sorry abt that Mr. dhenzbhee, my bad. You simply misunderstood my given exercise. I should have mentioned that the program should be randomized from numbers 1-10. Perhaps i should reedit it.

dhenzbhee wrote:

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

main()
{
    char ans;

    do
    {

    clrscr();
    int num, i;

    randomize();

    printf("Guess a number [1-10]");
    scanf("%d",&num);

    i = rand() % 11;

    if (num==i)
        printf("CORRECT!");
    if ((num!=i) && ((num>=0 && num<=10)))
        printf("INCORRECT!");
    if (num>10)
        printf("OUT OF RANGE!");


    printf("\n\nDo you want to try Another?[Y/N]");
    scanf("%s",&ans);
    } while((ans=='Y')||(ans=='y'));

    getch();
}

Still wrong. :D You included 0 (zero) to be randomized. Your out of range digits should be less than 1 and/or greater than 10. You should reedit your if-else statement to make it right. =):thumbsup:

dhenzbhee wrote:

on example #1, my mistake, i thought prime # and not prime # is the same as odd and even. :wallbash: :D

Haha. i think this one will be quite easy for you to redo now that you have #5 done. ;)




Anyway, tnx for participating Mr. dhenzbhee. =)

#8  2009-10-04 04:11:33

dhenzbhee
 dhenzbhee
dhenzbhee's display avatar
» n00b
FTalk Level: zero
23
2
2010-02-24

Re: C Programming Exercises: Test your Skill (Basic)

hope this one is right now... :lol:

Code:

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

main()
{
    char ans;

    do
    {

    clrscr();
    int num, i;

    randomize();

    printf("Guess a number [1-10]");
    scanf("%d",&num);

    i = rand() % 10 + 1;

    if (num==i)
        printf("CORRECT!");
    if ((num!=i) && ((num>=1 && num<=10)))
        printf("INCORRECT!");
    if ((num>10) || (num<1))
        printf("OUT OF RANGE!");


    printf("\n\nDo you want to try Another?[Y/N]");
    scanf("%s",&ans);
    } while((ans=='Y')||(ans=='y'));

    getch();
}

well, sir its a member's job to participate in any topic in this forum... thanks for appreciation...

#9  2009-10-04 07:48:27

othe_89
 wrath of music. instumental dread
othe_89's display avatar
» FTalker
FTalk Level: 2
roll ur lyf baby!!
Class abc-123
canada
152
11
Today

Re: C Programming Exercises: Test your Skill (Basic)

dhenzbhee wrote:

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

main()
{
    char ans;

    do
    {

    clrscr();
    int num, i;

    randomize();

    printf("Guess a number [1-10]");
    scanf("%d",&num);

    i = rand() % 10 + 1;

    if (num==i)
        printf("CORRECT!");
    if ((num!=i) && ((num>=1 && num<=10)))
        printf("INCORRECT!");
    if ((num>10) || (num<1))
        printf("OUT OF RANGE!");


    printf("\n\nDo you want to try Another?[Y/N]");
    scanf("%s",&ans);
    } while((ans=='Y')||(ans=='y'));

    getch();
}

Now you've got it right. Great. =):thumbsup:
Now i have to make some more exercises. :lol: ;)

Well anyway, how about, making this program without any kind of loop? But can run the program again to the start (without exiting) after selecting 'y'/'Y' in the "Try Again" question.  :thumbsup:

#10  2009-10-04 16:30:04

flint_hydz
 Tired of making layouts..
flint_hydz's display avatar
» FTalkGeek
FTalk Level: zero
Je L'Aime autant de :(
The Insiders
The Vow City
1419
66
Website

Re: C Programming Exercises: Test your Skill (Basic)

othe_89 wrote:

Wow. very good. didn't expected that you'd answer it by C++ (though it's for C), haha. Well anyway, i'm quite amazed, 'cause i'm not really that familiar with C++ functions, i'm more used to C. But your answer is definitely correct. Though our codes are different, the output was the same.

Lol. I don't have any compiler for C, and obviously I don't want to code it on C because I think It will take so much time as I have only learned C++. Nice Exercises, I love it show us more.

othe_89 wrote:

Anyway, Hope u could answer #3 soon!! Gudluck. :D

I will try to solve #3, I'm kind of busy with my Web Application using AJAX and PHP right now. :(
It's almost deadline and I'm racing against time. :penguin:

Anyway, can I post an Exercise? and kindly make it Exercise #6? Hehe.
I can't forget this C++ problem, because this is my favorite.

Make a program that will input a number, use an array to hold the number inputs.
Maximum number of inputs is 50 and it will stop the inputs when entered 0. If the input has stopped,
it will then output the number you've input and separate odd from even.

- Example Output -

Enter a number :
1
2
3
4
5
6
7

The odd numbers are : 1, 3, 5, 7
The even numbers are : 2, 4, 6

If the problem isn't clear, just tell me. :D :thumbsup:
Have fun!

#11  2009-10-05 11:33:51

hadez_rey
 Rey-rey
hadez_rey's display avatar
» FTalker
FTalk Level: zero
its me, thats all
LS : The Next Level
Kaphaloingkz
255
16
2010-03-05
Website

Re: C Programming Exercises: Test your Skill (Basic)

i forgot 'bout that C++, anyway I enjoyed the DELAY SCRIPT and text colors and blinking effects...


#12  2009-10-05 23:43:35

othe_89
 wrath of music. instumental dread
othe_89's display avatar
» FTalker
FTalk Level: 2
roll ur lyf baby!!
Class abc-123
canada
152
11
Today

Re: C Programming Exercises: Test your Skill (Basic)

flint_hydz wrote:

Lol. I don't have any compiler for C, and obviously I don't want to code it on C because I think It will take so much time as I have only learned C++. Nice Exercises, I love it show us more.

Opposite here. =) am more familiar with C than C++, but I use C++ program when writing C, 'cause some programs are quite hard to be read in C program only. It sometimes make an output different from what the user wants it to be. In other term, it's not that developed for me.


flint_hydz wrote:

Anyway, can I post an Exercise? and kindly make it Exercise #6? Hehe.
I can't forget this C++ problem, because this is my favorite.

Post as many as you want. :D:thumbsup: That would seem challenging for me too. =)

flint_hydz wrote:

Anyway, can I post an Exercise? and kindly make it Exercise #6? Hehe.
I can't forget this C++ problem, because this is my favorite.

This one is kinda hard.. hahaha. Perhaps it's new for me. I tried to finish this one asap but that "zero" thing that should stop the INPUTS and that COMMAS (in the outputs) are making me nuts. Haha. but
i was able to analyze and do 'commas' and what is left are the 'inputs'. But i'm gonna finish this thing ryt after i get home (still have classes). So kindly w8. ;)

hadez_rey wrote:

i forgot 'bout that C++, anyway I enjoyed the DELAY SCRIPT and text colors and blinking effects...

i tried that once when i was in High Schoo. Pretty cool. yah.:cool:

Last edited by othe_89 (2009-10-06 04:15:20)

#13  2009-10-06 04:29:26

flint_hydz
 Tired of making layouts..
flint_hydz's display avatar
» FTalkGeek
FTalk Level: zero
Je L'Aime autant de :(
The Insiders
The Vow City
1419
66
Website

Re: C Programming Exercises: Test your Skill (Basic)

^ Lol. Ok, Ignore the commas.. a space will do, i just want to see the even and the odd numbers being separated. :D

#14  2009-10-06 15:20:52

othe_89
 wrath of music. instumental dread
othe_89's display avatar
» FTalker
FTalk Level: 2
roll ur lyf baby!!
Class abc-123
canada
152
11
Today

Re: C Programming Exercises: Test your Skill (Basic)

flint_hydz wrote:

^ Lol. Ok, Ignore the commas.. a space will do, i just want to see the even and the odd numbers being separated. :D

Here it is. I wouldn't have solved this if those commas are included. lol. Well anyway, here it is without them. :)

Code:

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,num[49];


    clrscr();
    printf("Enter number(s): \n");

    for(b=0;b<50;b++)
        {

        scanf("%d",&num[b]);
        if(num[b]==0)
            {

            c=b;
            b=50;
            }

         }




    printf("\n\nEven Numbers are: ");

    for(a=0;a<c;a++)
        {


        if(num[a]%2==0&&num[a]!=0)
            {

            printf("%d ",num[a]);


            }


        }


    printf("\n\nOdd Numbers are: ");
    for(a=0;a<c;a++)
        {

        if(num[a]%2!=0)
            {
            printf("%d ",num[a]);


            }

        }

getch();clrscr();return 0;
}

Output:

#15  2009-10-14 22:17:59

othe_89
 wrath of music. instumental dread
othe_89's display avatar
» FTalker
FTalk Level: 2
roll ur lyf baby!!
Class abc-123
canada
152
11
Today

Re: C Programming Exercises: Test your Skill (Basic)

flint_hydz wrote:

- Example Output -

Enter a number :
1
2
3
4
5
6
7

The odd numbers are : 1, 3, 5, 7
The even numbers are : 2, 4, 6

i think i got the program with a comma in it. =) But it seems more complicated than with spaces only.

Code:

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,d=0,e=0,f=1,num[49];


    clrscr();
    printf("Enter number(s): \n");

    for(b=0;b<50;b++)
        {

        scanf("%d",&num[b]);
        if(num[b]==0)
            {

            c=b;
            b=50;
            }

         }


    for(a=0;a<c;a++)
        {

        if(num[a]%2==0)
        {d=a+1;}
        if(num[a]%2!=0)
        {e=a+1;}

        }d--;;e--;

    printf("\n\nEven Numbers are: ");

    for(a=0;a<c;a++&&f++)
        {


        if(num[a]%2==0&&num[a]!=0)
            {

            printf("%d",num[a]);
            if(c<3&&a<1)
            printf(",");

            }
        if(f<d&&num[a]%2==0)
            {printf(",");}


        }

    f=1;
    printf("\n\nOdd Numbers are: ");
    for(a=0;a<c;a++&&f++)
        {

        if(num[a]%2!=0)
            {
            printf("%d",num[a]);
            if(c<3&&a<1)
            printf(",");
            }
        if(f<e&&num[a]%2!=0)
            {printf(",");}

        }


getch();clrscr();return 0;
}

#16  2010-02-09 06:09:29

nevemindme98
» n00b
FTalk Level: zero
5
0
2010-03-02

Re: C Programming Exercises: Test your Skill (Basic)

pwd java nlng??? hehehehe

#17  2010-02-18 14:01:07

[Vigilante]•Zephyr•
 Fretz Villanueva
[Vigilante]•Zephyr•'s display avatar
» FTalkAddict
FTalk Level: 3
Simple sentence.
Alchemy Explicit AE
Somewhere.
423
6
2010-03-18
Website

Re: C Programming Exercises: Test your Skill (Basic)

Envy you people! Hopefully I would become as good as you do. :D




Must Visits
C M P E  ||  My Tweets

Search Friendstertalk

Board footer

The F Talk dot com
© 2002–2010 PunBB

Current time is 07:43:50 GMT

[ 13 queries - 0.049 second ]
Powered by SyntheticNetwork

FTalk

Welcome to FTalk! You'll need to login in order to fully use all the features and view all the sections of this site.

Please register if you're not yet a member. =)

( hide )