Inside a "for" loop, how can I increment another variable every time the indexer counts "x"units? - c

Title says it all,
i think there is no need for the code as the problem is finding the algorithm itself.

int x=10; //Just suppose
int b=0;
for(int i=0;i<10000;i++){
if(i%x == 0){
b++;
}
// Rest of loop code
}
That should about do it, I think.

Try,
for(i=0;i<MAX;i++)
{
if(i%x==0)
{
counter++;
}
/* Loop Body */
}

For an integer variable x, the expression x % y == 0 will be 1 if x is evenly divisible by y and 0 otherwise.
for (i=0; i < N; i++) {
counter += (i % interval == 0);
}

This one maybe?
#include <stdio.h>
int main()
{
int i;
int multiple = 40;
int j = 0;
for (i = 0; i < 1200; i++)
{
if (i % multiple == 0)
j++;
}
printf("%d, %d, %d\n", i, multiple, j);
return 0;
}
After compiling it and running it I get the following:
$ gcc test.c
$ ./a.out
1200, 40, 30
$

i=0;
while(1)
{
i=(i+1)%x ;
if(i==0)
counter++;
}

for( int i=0; i < 1200 ;i++)
{
j += i/40;
}

Related

How not to print - (minus symbol) after the last output?

I'm trying to solve a problem, The objective is to print the last step can be achieved, the inputs are N as testcases, and A as the integers of the testcases. For example :
N = 10
A = 1 2 1 1 2 3 4 1 2 3 <- 10 integers
So, this is what I have done.
#include <stdio.h>
int main()
{
int N,i,j;
scanf("%d",&N);
int A[N+1];
for(int i=0;i<N;i++)
{
scanf("%d",&A[i]);
}
A[N] = 1;
for(int i=0;i<N;i++)
{
if(A[i] > A[i+1])
{
printf("%d-",A[i]);
}
else if(A[i] == A[i+1])
{
printf("%d-",A[i]);
}
}
printf("\n");
return 0;
}
The expected output is :
2-1-4-3
but the output I get is :
2-1-4-3-
How do I exclude the minus symbol after the last output? Thanks in advance.
Personally, my preference is to print the separator character before. The simple reason is that generally this happens in loops that begin at zero, so I can do a zero-test as follows:
for(int i = 0; i < N; i++)
{
if (i > 0)
putc('-', stdout);
printf("%d", A[i]);
}
BUT, in your case, your loop won't always print a value. So you actually need to be smarter. There are many ways to achieve this, but for simplicity, why not just use a flag:
int has_output = 0;
for(int i = 0; i < N; i++)
{
if(A[i] >= A[i+1])
{
if (has_output)
putc('-', stdout);
else
has_output = 1;
printf("%d", A[i]);
}
}
Notice that the preference is still to print the separator just in time. In other words, only when you determine that you need to print something.
Going a bit more crazy:
const char* fmt[2] = { "%d", "-%d" };
int has_output = 0;
for(int i = 0; i < N; i++)
{
if(A[i] >= A[i+1])
{
printf(fmt[has_output], A[i]);
has_output = 1;
}
}
#include<stdio.h>
int main()
{
int N,i,j;
scanf("%d",&N);
int A[N+1];
for(int i=0;i<N;i++)
{
scanf("%d",&A[i]);
}
A[N] = 1;
int flag = 0; //added a new variable here
for(int i=0;i<N;i++)
{
if(A[i] >= A[i+1])
{
if(flag == 0){ //check if its printing for the first time
printf("%d",A[i]); // then print without dash
flag = 1; //change the flag so that this block never executes again
}else{
printf("-%d",A[i] ); //else print a dash and then the number
}
}
}
printf("\n");
return 0;
}
As "-" is a separator only used after the first printed value, consider changing the separator.
const char *separator = "";
for(int i=0;i<N;i++) {
if(A[i] >= A[i+1]) {
printf("%s%d", separator, A[i]);
separator = "-";
}
}
printf("\n");

I want to print out the following to the console

I want to print out the following to the console:
+++++
++++*
+++**
++***
+****
*****
I am a new learner of programming, so encountering some difficulties. Can anyone help me, please? I have tried this, but is incorrect. What do I need to change?
#include<stdio.h>
int main(){
int i, j, k;
for(i=0; i<5; i++){
for(j=i; j<5; j++){
for(k=0; k<j; k++){
printf("*");
}
printf("+");
}
printf("\n");
}
return 0;
}
You have the right idea: Use three for loops.
#include <stdio.h>
int main() {
for (int i = 0; i < 6; i++) {
for (int k = i; k < 5; k++) {
printf("+");
}
for (int j = 0; j < i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Test
+++++
++++*
+++**
++***
+****
*****
Online demo
First, generalise it and wrap it in a function. You want a square with a diagonal. It has to be an even number of characters to look right. But + and * could be any character, and the size could be 6 or all the way up to screen maximum width.
so
/* print a square with a diagonal
N - the size of the sides of the square
cha - character a (eg '+')
chb - character b (eg '*')
*/
void printdiagsquare(int N, char cha, char chb);
That's our prototype, and that's half the battle.
Now we need to check N is even and positive, then write the loops.
Let's get the test away first.
if(N < 2 || (N % 2) == 1)
printf(N must be even\n");
Now the main loop for each line
for(i=0;i<N;i++)
{
//printline code here
printf("\n");
}
Now test it. Is it printing N blank lines?
main(void)
{
printdiagsquare(6, '+', '*');
}
Now to get the lines printed.
to print N-1 '+'s is easy. We need j as the counter since i is the outer
for(j=0;j<N-1;j++)
printf("%c", cha);
But we need to generalise, we need to print 6,, 5, 4, 3 and so on as i increases.
So
for(j=0;j<N-i-1;j++)
printf("%c", cha);
I'll leave the last little bit for you to do. No point just typing ina function blindly.
You could try more optimized code for m-rows and n-columns
in 2 for loop only :-
#include <stdio.h>
int main(void) {
int m = 6; // Rows
int n = 5; // Cols
int i,j,k;
for (i = 0; i < m; i++) {
k = i;
for (j = n; j >= 0; j--) {
if(k>=j)
printf("*");
else
printf("+");
}
printf("\n");
}
return 0;
}

average of prime numbers program

I want to calculate the average of the prime numbers between 1 to 10 and I have written a program which is as follows:
#include <stdio.h>
int main()
{
int i, j, sum = 0, count = 0;
loop1:
for(i = 2; i <= 10; i++)
{
for(j = i - 1; j > 1; j--)
{
if(i % j == 0)
{
goto loop1;
}
}
sum = sum + i;
count++;
}
printf("The avg:%d", (sum / count));
return 0;
}
Please help me whether the program is correct.
Simple answer for you:
#include <stdio.h>
int main() {
float sum = 0, count = 0, average;
for(int i=2; i<11; i++){
for(int j=2; j<=i; j++){
if(j==i){
sum+=i;
count++;
}else if(i%j==0){
break;
}
}
}
average=sum/count;
printf("Average = %.2f", average);
return 0;
}
Please ensure you do not make a habit of usinggoto: statements. Your program is incorrect. Here, when the statement
if(i % j == 0)
returns true, thegoto: statement takes control to the beginning of the parent for loop and the loop will run from start again. This way, you will never get your desired output. As per your question, your solution is wrong. A modified approach is
#include <stdio.h>
int main()
{
int i, j, sum = 0, count = 0,flag;
for(i = 2; i <= 10; i++)
{
flag=0;
for(j = i - 1; j > 1; j--)
{
if(i % j == 0)
{
flag=1;
break;
}
}
if(flag==0)
{
sum = sum + i;
count++;
}
}
printf("The avg:%d", (sum / count));
return 0;
}
What do you mean by "Correct" ?
If correct to you means that it works: well that's easy to verify for yourself.
If correct means that you are hitting the best practices, well then, nope, you missed them I'm afraid.
goto: about the most easy of all "do not use that" signs.
A "better" approach would be to write a function that tests if a number is a prime or not.

how to generate number pattern in triangular form [duplicate]

I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}
Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.
Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.
Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}
For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}
#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}

How do I generate number pattern in triangular form

I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}
Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.
Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.
Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}
For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}
#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}

Resources