What's new

Need Help for C programming :)

warfareknow

BANNED
Joined
Apr 4, 2015
Messages
463
Reaction score
-6
Country
Iran, Islamic Republic Of
Location
Israel
Hi guys, I am right now a cs stud and I am behind everybody else since I have literally 0 experience with coding.

I have a homework I have to finish till 23:59 o'clock today and send it to my tutor at the uni.

One of the tasks was that I should program something that calculates the sum and sqaure sum of 1-n
I was able to do it this way:

Code:
//ADIP Aufgabe 2: Die Summe und Quadratsumme von 1 bis n.

#include <stdio.h>

int main()
{
    int n;

    printf("\nIch berechne Dir die Summe und Quadratsumme von 1 bis n :)\n");

    printf("\nBitte eine positive, ganze Zahl eingeben:\n");
    scanf("%d", &n);

    if (n>0){
            printf("\nKorrekte Eingabe.\n");
    }else if(n<1){printf("\nFalsche Eingabe. Bitte neustarten :(.\b");
    return 0;
       }


    printf("\nDie Summe lautet: %d\n", ((n+1)*n)/2);
    printf("\nDie Quadratsumme lautet %d\n", (n*(n+1)*(2*n+1))/6);

    return 0;
}


Now the next task is to write a program that will tell you if your positive number (n) is a prime number or not.
I know i have to use somehow "for" but i failed several times and i dont know how to use it and whats its purpose, other studs say we have to use modulo rest? . Oh also my program should give an error if number that is entered is negative, i did master this part.

this is my unfinished code:

Code:
#include <stdio.h>
int main()
{
    int input, i,;
    printf("\n\=== Aufgabe 3 ===\n"); //Task 3


    printf("\nBitte eine positive, ganze Zahl eingeben :"); //Please enter a positive and integer number
    scanf("%i", &input);
    if(input<0){
        printf("\nAchtung: Die eingegebene Zahl '%i' war negativ!\n", input); //Warning: Number entered was negative.
        return 0;
    
        
        }
}

If you could help me I would be very grateful. :)
 
Last edited:
Der richtige Platz für so eine Frage wäre stackoverflow.com

Hi guys, I am right now a cs stud and I am behind everybody else since I have literally 0 experience with coding.

I have a homework I have to finish till 23:59 o'clock today and send it to my tutor at the uni.

One of the tasks was that I should program something that calculates the sum and sqaure sum of 1-n
I was able to do it this way:

Code:
//ADIP Aufgabe 2: Die Summe und Quadratsumme von 1 bis n.

#include <stdio.h>

int main()
{
    int n;

    printf("\nIch berechne Dir die Summe und Quadratsumme von 1 bis n :)\n");

    printf("\nBitte eine positive, ganze Zahl eingeben:\n");
    scanf("%d", &n);

    if (n>0){
            printf("\nKorrekte Eingabe.\n");
    }else if(n<1){printf("\nFalsche Eingabe. Bitte neustarten :(.\b");
    return 0;
       }


    printf("\nDie Summe lautet: %d\n", ((n+1)*n)/2);
    printf("\nDie Quadratsumme lautet %d\n", (n*(n+1)*(2*n+1))/6);

    return 0;
}


Now the next task is to write a program that will tell you if your positive number (n) is a prime number or not.
I know i have to use somehow "for" but i failed several times. Oh also my program should give an error if number that is entered is negative, i did master this part.

this is my unfinished code:

Code:
#include <stdio.h>
int main()
{
    int input, i,;
    printf("\n\=== Aufgabe 3 ===\n"); //Task 3


    printf("\nBitte eine positive, ganze Zahl eingeben :"); //Please enter a positive and integer number
    scanf("%i", &input);
    if(input<0){
        printf("\nAchtung: Die eingegebene Zahl '%i' war negativ!\n", input); //Warning: Number entered was negative.
        return 0;
     
         
        }
}

If you could help me I would be very greatful. :)
 
Use the modulo operator % with 2. If result is zero. This would eliminate the even numbers.
Next run a loop up to the half of the number of you have entered and by using the modulo operator see if any of it results in a zero. If yes then is not a prime number otherwise it is.

--------

You live in Germany and use the British crown as your DP?
 
Use the modulo operator % with 2. If result is zero. This would eliminate the even numbers.
Next run a loop up to the half of the number of you have entered and by using the modulo operator see if any of it results in a zero. If yes then is not a prime number otherwise it is.

--------

You live in Germany and use the British crown as your DP?

Thanks for your answer bro
could you please explain it with a code line? what are loops how do i even use them :3 im noob i did start yesterday
with coding for the first time in my life bro.


I find the picture nice :)
 
#include <stdio.h>
int main()
{
int n, i, flag = 0;

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

for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}

if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);

return 0;
}



output will be something like

enter a positive integer 13
13 is a prime number
 
ofcourse translate the message part to german and combine the non negative part in this hope this works

Thanks for your answer bro
could you please explain it with a code line? what are loops how do i even use them :3 im noob i did start yesterday
with coding for the first time in my life bro.


I find the picture nice :)
are soheil and you the same person?
 
#include <stdio.h>
int main()
{
int n, i, flag = 0;

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

for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}

if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);

return 0;
}



output will be something like

enter a positive integer 13
13 is a prime number

Wow thank you very much for the help man, would you please explain what break; is ?

ofcourse translate the message part to german and combine the non negative part in this hope this works


are soheil and you the same person?

no but he is helping me to get help :DD
 
Wow thank you very much for the help man, would you please explain what break; is ?



no but he is helping me to get help :DD
break is for ending the loop in case any exception is thrown like a decimal number or something indivisible first run this program as it is and see if its error free i dont have a C compiler with me so can't promise a error free run first do that and post the result.


also my name is hannah:mad::mad::mad::mad::sick:
 
break is for ending the loop in case any exception is thrown like a decimal number or something indivisible first run this program as it is and see if its error free i dont have a C compiler with me so can't promise a error free run first do that and post the result.


also my name is hannah:mad::mad::mad::mad::sick:


Sry Hannah didn't want to be impolite, was just hungry for understanding your code, and the code works correctly i compiled it once and im trying to understand the part i didn't manage to write. Thanks a thousand times you helped me a lot :)
 
So in future I can get help from Pakistan defence forum hopefully regarding c++ as it is going to suffee me soon:-)
 

Latest posts

Pakistan Affairs Latest Posts

Back
Top Bottom