Simplifiying proram which generates prime number from 2 to n - c

I have found the solution to the programs, but i got running time error. I need to simplify the sourse code, so it could be compiled in 0.3 second. Please help me simplify the program.
#include <stdio.h>
#include <math.h>
int main()
{
int i, j, c=0, num;
scanf("%d", &num);
for(i=2; i<=num; i++)
{
for(j=1;j<=sqrt(i);j++)
{ if(i%j==0)
c++;
}
if(c==1)
{ printf(" %d", i);
}
c=0;
}
printf("\n");
return 0;
}

Related

Why is my program breaking when I add a void function to the main? It works without it

I have 2 functions + my main.
One of them is a void function that "prints out instructions"
The other is the one that actually does what I want it to do.
For some reason when the play function is by itself in the main it works just fine, but as soon as I add the print instructions function, it breaks and I cannot figure out why it's doing that.
Functions:
int playGame();
void printInstructions();
`
int playGame()
{
int dice[100];
int diceAmount, j, sum = 0;
printf("How many dice would you like to roll? ");
scanf("%d",&diceAmount);
for( int i = 0; i < diceAmount; i++)
{
dice[j] = rand() % 6 + 1;
sum += dice[j];
printf("Dice %d: %d\n",i+1,dice[j]);
}
printf("---------\nSum: %d", sum);
}
`
`
void printInstructions()
{
printf("--------------\n");
printf("- HOW TO WIN -\n");
printf("--------------\n");
printf("Your dice roll must equal 7 or 11 or else you lose.\n");
printf("Want to test your luck?\n\n");
}
`
Whole thing:
`
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int playGame();
void printInstructions();
int playGame()
{
int dice[100];
int diceAmount, j, sum = 0;
printf("How many dice would you like to roll? ");
scanf("%d",&diceAmount);
for( int i = 0; i < diceAmount; i++)
{
dice[j] = rand() % 6 + 1;
sum += dice[j];
printf("Dice %d: %d\n",i+1,dice[j]);
}
printf("---------\nSum: %d", sum);
}
int main()
{
printInstructions();
playGame();
}
void printInstructions()
{
printf("--------------\n");
printf("- HOW TO WIN -\n");
printf("--------------\n");
printf("Your dice roll must equal 7 or 11 or else you lose.\n");
printf("Want to test your luck?\n\n");
}
`
Without the printInstructions();
With the printIUnstruction();
Why is it breaking?
With the suggestions from UnholySheep & Martin James, I was able to get my code to work.
Here is the working code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int playGame();
void printInstructions()
{
printf("--------------\n");
printf("- HOW TO WIN -\n");
printf("--------------\n");
printf("Your dice roll must equal 7 or 11 or else you lose.\n");
printf("Want to test your luck?\n\n");
}
int playGame()
{
int dice;
int diceAmount, sum = 0;
printf("How many dice would you like to roll? ");
scanf("%d",&diceAmount);
for( int i = 0; i < diceAmount; i++)
{
dice = rand() % 6 + 1;
sum += dice;
printf("Dice %d: %d\n",i+1,dice);
}
printf("---------\nSum: %d\n",sum);
}
int main()
{
printInstructions();
playGame();
}
Result:

I need to create a number pattern program in C for my school assignment. Pattern- 1 141 14941

The pattern is
___1
__141
_14941
I have tried to some extent but looks like i have created a odd number pattern program
#include <stdio.h>
int main()
{
int i, j, N=3;
for(i=1; i<=N; i++)
{
// Prints the first part of pattern
for(j=1; j<=(i*2)-1; j+=2)
{
printf("%d", j);
}
// Prints the second part of pattern
for(j=(i-1)*2-1; j>=1; j-=2)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
I can see that the pattern has numbers that are off by odd numbers such as 3 and 5 respectively.
But i cant seem to grasp exactly how to do it.
Any help is much appreciated.
#include <stdio.h>
int main()
{
int i, j, N=3;
for(i=0; i<N; i++)
{
//For printing Spaces
for(int sp=0;sp<N-i;sp++){
printf(" ");
}
/*For printing the numbers
from left till the middle column*/
for(int j=1;j<=i+1;j++){
printf("%d",j*j);
}
/*For printing the numbers
from the strict right to the middle column
and till the end*/
for(int j=1;j<i+1;j++){
printf("%d",j*j);
}
printf("\n");
}
return 0;
}

Do we have to create an even and an odd array?

How can we modify the following code (which initially asks the user for 10 numbers to be entered, get stored in an array, and printed on the screen) so that the even numbers are printed on the first line, and the odd on the second:
#include <stdlib.h>
#include <stdio.h>
int i,j;
int array_1[10];
int main() {
for(i=0;i<10;i++) {
printf("Enter a number: ");
scanf("%d", &array_1[i]);
}
printf("The elements of the array are: ");
for (j=0;j<10;j++) {
printf("%d ", array_1[j]);
}
printf("\n");
return 0;
}
O(n) Solution:
you have to add odd numbers at the back of the array and add the even numbers at the front of the array and keep track of the indexes.
int array_1[10];
int main() {
int even = 0, odd = 10;
for (int i = 0; i < 10; i++) {
printf("Enter a number: ");
int inp;
scanf("%d", &inp);
if (inp % 2 == 0) {
array_1[even++] = inp;
} else {
array_1[--odd] = inp;
}
}
// even numbers
for (int i = 0; i < even; i++) {
printf("%i ", array_1[i]);
}
printf("\n");
// odd numbers
for (int i = 9; i >= odd; i--) {
printf("%i ", array_1[i]);
}
printf("\n");
return 0;
}
Since you asked, here is how I would do it. I suspect this may leave you with more questions than answers through.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define NUMBERS_SIZE 10
typedef bool (*number_validator)(int num);
bool isEven(int num)
{
return (num & 1) == 0;
}
bool isOdd(int num)
{
return (num & 1) != 0;
}
void print(const char *title, int *array, int array_size, number_validator isValid)
{
printf("%s", title);
bool first = true;
for (int i = 0; i < array_size; ++i)
{
if (isValid(array[i]))
{
if (!first)
{
printf(", ");
}
printf("%d", array[i]);
first = false;
}
}
printf("\n");
}
int main()
{
int numbers[NUMBERS_SIZE] = { 0 };
for (int i = 0; i < NUMBERS_SIZE; i++)
{
printf("Enter a number: ");
scanf("%d", &numbers[i]);
}
printf("\n");
print("Even: ", numbers, NUMBERS_SIZE, isEven);
print(" Odd: ", numbers, NUMBERS_SIZE, isOdd);
return 0;
}
Demo on ideone
you can try this way.I have used binary AND(&) instead of MOD(%) as it is faster:
#include <stdlib.h>
#include <stdio.h>
int i,j;
int array_1[10];
int main()
{
for(i=0; i<10; i++)
{
printf("Enter a number: ");
scanf("%d", &array_1[i]);
}
printf("The Even elements of the array are: ");
for (j=0; j<10; j++)
{
if((array_1[j]&1) == 0)
printf("%d ", array_1[j]);
}
printf("\nThe Odd elements of the array are: ");
for (j=0; j<10; j++)
{
if((array_1[j]&1) != 0)
printf("%d ", array_1[j]);
}
printf("\n");
return 0;
}
No need to create a new array. You can just go through it first checking for even numbers, and then again for odd numbers. Also, there's no need to declare i and j before using them. You can just declare them and initialize them in the for loop:
#include <stdlib.h>
#include <stdio.h>
int array_1[10];
int main() {
for(int i=0;i<10;i++) {
printf("Enter a number: ");
scanf("%d", &array_1[i]);
}
printf("The elements of the array are: ");
// Print even numbers
for (int j=0;j<10;j++) {
if(array_1[j] % 2 == 0)
printf("%d ", array_1[j]);
}
printf("\n");
// Print odd numbers
for (int j=0;j<10;j++) {
if(array_1[j] % 2 != 0)
printf("%d ", array_1[j]);
}
printf("\n");
return 0;
}
Edit: As tadman suggested in the comment below, there's a better and cleaner way to do this kind of task. As you can see in the above example, I'm repeating 4 lines of code where only one character changes. This task could be abstracted into a function to reduce code repetition:
void printIfMod(int* arr, size_t array_size, int mod){
for (int j=0;j<array_size;j++) {
if(arr[j] % 2 != mod)
continue;
printf("%d ", arr[j]);
}
printf("\n");
Remember to add a function prototype before main if you place the function after main:
void printIfMod(int* arr, size_t array_size, int mod);
int main(){...}
Now, to print the numbers, call the method with modulo 0 to get even numbers, and 1 to get odd numbers:
// Print even numbers
void printIfMod(&array_1, 10, 0);
// Print odd numbers
void printIfMod(&array_1, 10, 1);
One last note, hard-coding array_size is not wise, and that goes for all arrays. I recommend using sizeof() to dynamically calculate the size of your array:
size_t size = sizeof(array_1) / sizeof(int);
// Print even numbers
void printIfMod(&array_1, size, 0);
// Print odd numbers
void printIfMod(&array_1, size, 1);

reversing elements of an integer array

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20],int n);
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20],int n)
{
for(int i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
return 0;
}
here if I input n=4 then during runtime i have to take 5 elements and then it reverses.For eg if i take n=4 and then for no of elements i have to take 1,2,3,4,5 and then only output is coming as 4 3 2 1.Why? is my logic wrong? also in this code I am unable to take the number of elements of arrays in a straight line, like 1 2 3 4.When I am entering the number each number is entering in new line .I am a novice programmer in C and thus having these doubts.Please anyone explain...
The problem with your code is the extra space after %d in your scanf line where you accept array elements i.e.
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]); //should be scanf("%d",&a[i]);
}
Change that and you're good to go.
Here is your entire program refactored to work correctly:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20], int n)
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for (int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20], int n)
{
int mid = n/2;
for (int i=0; i < mid; ++i)
{
int temp = a[n-i-1];
a[n-i-1] = a[i];
a[i] = temp;
}
return 0;
}

first 50 positive numbers divisible by 4

This is the code I reached.
what I don't get is the condition where x should stop. I know what I wrote is wrong.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, i;
for(i=0; i<=50; i++)
{
for (x=0;x<=50 ;x++ )
{
if (x%4==0)
printf ("%d\n", x);
}
}
return 0;
}
The simplest solution, as suggested by Art in a comment above:
for(unsigned int c=1; c<=50; c++)
{
printf ("%d\n", c*4);
}
Original answer: One loop is enough. And increase by 4 (every 4th number is divisible by 4, and none else):
int curNum=4;
for (unsigned int c=1; c<=50; c++, curNum+=4)
{
printf ("%d\n", curNum);
}
So you see we have two variables, one counts the numbers we have printed already (c), and the other contains the current number to be printed (curNum).
With this code you are showing numbers less than 50 that are divisible by 4 & then you are printing it 50 times. Although your question has been answered but if you are hell bent on checking if a number is divisible by 4 & using two for loops, this might work.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=0, i;
for(i=0; i<=50; i++)
{
for(;;)
{
if(x%4==0)
{
printf("\n%d",x);
x++;
break;
}
x++;
}
}
return 0;
}
int main()
{
int x = 0;
int i = 0;
while(i<50)
{
if (x % 4==0)
{
printf ("(result %d) %d is divisible by 4\n", i, x);
i++;
}
x++;
}
return 0;
}
No need to use nested for loops. Try this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0, count = 1;
while(1)
{
if (i%4==0)
printf ("%d\n", i);
i++;
if (count++ == 50)
break;
}
return 0;
}
you just need one for
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned int n = 0;
unsigned int i = 0;
while(n < 50)
{
if (i%4==0){
printf ("%d\n", i);
++n;
}
++i;
}
return 0;
}

Resources