How to produce the following output? - c

Since i'm new in the programming world ,i'm facing little problem while writting program for this pattern .I tried many times but the result is not what i wanted ?
The pattern is :
1
23
456
78910
What i have written is :-
#include<stdio.h>
#include<conio.h>
void main()
{
int num = 1 , j = 1 , x = 1 , i = 1 ;
while( j <= 4 ) {
while( i <= num ) {
printf( "%d", x ) ;
x++ ;
i++ ;
}
num++ ;
i = ( i + 1 ) - num ;
j++ ;
}
getch() ;
}

#include <stdio.h>
int main()
{
printf("1\n23\n456\n78910\n");
return 0;
}
produces the output you desire

You need to print a newline after the inner loop:
#include<stdio.h>
#include<conio.h>
int main()
{
int num = 1 , j = 1 , x = 1 , i = 1 ;
while( j <= 4 ) {
while( i <= num ) {
printf( "%d", x ) ;
x++ ;
i++ ;
}
printf("\n");
num++ ;
i = ( i + 1 ) - num ;
j++ ;
}
getch();
return(0);
}

There is another example:
int main()
{
int i, j, num = 1, line = 4;
for(i = 1; i <= line ; i++)
{
for(j = 0; j < i; j++)
{
printf("%d", num);
num++;
}
printf("\n");
}
return 0;
}

With single loop:
# include<stdio.h>
# define LIMIT 100
int main(){
int i, prev=0, next=0, diff=1;
for(i=1;i<LIMIT;i++){
printf("%d", i);next++;
if(diff == next-prev){
printf("\n");
diff++;prev = next = 0;
}
}
}

Related

Pyramid patterns using C

I need to do the following pattern using do-while, while or for.
I tried the following code but it prints the pattern only 1-5
I also tried to alter the n being 10 but then the spacing goes nuts.
#include <stdio.h>
int main(void)
{
int n = 5, i, j, num = 1, gap;
gap = n - 1;
for ( j = 1 ; j <= n ; j++ )
{
num = j;
for ( i = 1 ; i <= gap ; i++ )
printf(" ");
gap --;
for ( i = 1 ; i <= j ; i++ )
{
printf("%d", num);
num++;
}
num--;
num--;
for ( i = 1 ; i < j ; i++)
{
printf("%d", num);
num--;
}
printf("\n");
}
return 0;
}
Try replacing both occurences of:
printf("%d", num);
with
printf("%d", num % 10);
Now only the last digit will be shown.
After the change, for n set to 10 the program produces:
1
232
34543
4567654
567898765
67890109876
7890123210987
890123454321098
90123456765432109
0123456789876543210

How to skip certain numbers in a for-loop?

I started practicing a for loop in C and so far I understand the main principle behind it. But I can't figure out how to get the following output:
1 2 3 5 6 7 9 10 11 ...
I managed to print 1 to 12 with the following for loop but how can I skip 4 and 8 or how to skip any number in general?
for(int i = 1; i < 12; i++)
{
printf("%d", i);
}
Either you can use an if statement inside the body of the loop like
for ( int i = 1; i < 12; i++ )
{
if ( i % 4 != 0 )
{
printf( "%d ", i );
}
}
Or you can avoid the numbers that divisible by 4 in the third expression of the for loop like
for ( int i = 1; i < 12; i += ( i + 1 ) % 4 == 0 ? 2 : 1 )
{
printf( "%d ", i );
}
If you need to output the space character instead of a number divisible by 4 you can use an if-else statement inside the body of the loop. For example
for ( int i = 1; i < 12; i++ )
{
if ( i % 4 != 0 )
{
printf( "%d ", i );
}
else
{
putchar( ' ' );
}
}
the simplest solution would be to check with an if statement for any values that you don't want.
if you have a rule like not printing all numbers that are divisible by 4 you can make your if statement like this
if(i % 4 == 0)
{
//print
}
there is no way to do it specifically with the for loop expression.
if you want to do specific numbers you just add more conditions to the if statement:
for(int i = 1; i < 12; i++)
{
if(i != 11 && i != 5 && i%3 != 0)
{
printf("%d", i);
}
}
The best way to do this, especially for a large amount of irregular exceptions, is the following way.
#include <stdio.h>
int main(int argc, char *argv[])
{
int exceptionsv[] = {4, 8};
int exceptionsc = sizeof(exceptionsv)/sizeof(exceptionsv[0]);
for(int i = 1; i < 12; i++)
{
for(int j = 0; j < exceptionsc; j++)
{
if(exceptionsv[j] == i) i++;
}
printf("%d ", i);
}
}
for (char i=0; i<12; i++) { if (i%4 != 0) printf("%d", i); }
Or you can isolate the pattern and duplicate it using a nested loop :
char patternCount = 3;
for (char i=0; i<patternCount; i++) {
for (char j=1; j<4; j++) printf("%d", i*4 + j);
}

Cicle FOR to generate two sequences of numbers

I use this code to create a number sequence, but what i want is increment a second sequence of numbers(each 1000 times 'i' increment, 'j' the second increment of 1)
for (i=0;i<5000;i++)
{
printf("/sample/%d/%d\n",j,i+1);
}
what i want is this:
/sample/0005/0005000
Please try this
#include <usual.h>
int main( )
{
int i = 0;
int j = 0;
int ticker = 1000;
for ( i = 0; i < 5001; i++ )
{
if ( i && i % ticker == 0 )
{
j++;
ticker += 1000;
printf( "\n sample i== %d j== %d ", i, j );
}
}
}

What is wrong with this insertion sort in C?

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
void insertionSort(int ar_size, int* ar) {
int i, j;
int temp = 0;
for (i = 1; i < ar_size; i++) {
j = i;
while (j > 0 && ar[i - 1] > ar[i]) {
temp = ar[i - 1];
ar[i - 1] = ar[i];
ar[i] = temp;
j--;
}
}
for (j = 0; j < ar_size; j++) {
printf("%d", ar[j]);
printf(" ");
}
}
int main(void) {
int _ar_size;
scanf("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for (_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
scanf("%d", &_ar[_ar_i]);
}
insertionSort(_ar_size, _ar);
return 0;
}
I have been trying to look for the error. I cannot see any. What is wrong with this code?
For input as 6 and 4 1 3 5 6 2 , it gives output as 1 3 4 5 2 6. There is one less iteration of the loop but I cannot see why? Please help. Thanks.
You are using index i instead of index j in the internal loop of the function.
while(j>0 && ar[i-1]>ar[i])
{
temp = ar[i-1];
ar[i-1] = ar[i];
ar[i] = temp;
j--;
}
Here everywhere index j has to be used.
Also it is a bad idea that the function also outputs the sorted array. It should do only sorting.
The other bad idea is to use identifiers that start with undescores.
It is better when the first parameter is an array and the second parameter is the size of the array.
The code could look the following way
#include <stdio.h>
void InsertionSort( int *a, int n )
{
int i;
for ( i = 1; i < n; i++ )
{
int j = i;
while ( j > 0 && a[j-1] > a[j] )
{
int tmp = a[j-1];
a[j-1] = a[j];
a[j] = tmp;
--j;
}
}
}
int main(void)
{
int size;
scanf( "%d", &size );
int a[size];
int i;
for ( i = 0; i < size; i++ ) scanf( "%d", &a[i] );
for ( i = 0; i < size; i++ )
{
printf( "%d ", a[i] );
}
puts( "" );
InsertionSort( a, size );
for ( i = 0; i < size; i++ )
{
printf( "%d ", a[i] );
}
puts( "" );
return 0;
}
If the input is
10
2 7 5 4 9 1 4 8 3 5
then output is
2 7 5 4 9 1 4 8 3 5
1 2 3 4 4 5 5 7 8 9
You seem to be using the wrong iterator when pushing the value to the front.
while(j>0 && ar[j-1]>ar[j]) {
temp = ar[j-1];
ar[j-1] = ar[j];
ar[j] = temp;
j--;
}
Try performing a dry run on a piece of paper, it's easy to spot the problem.
for (i = 1; i < ar_size; i++) {
j = i;
while (j > 0 && ar[i-1] > ar[i]) { // problem begins here
temp = ar[i-1];
ar[i-1] = ar[i];
ar[i] = temp;
j--;
}
}
i does not change in the inner loop.
At some point your program swaps 6 with 2 and your sample input list becomes 1 3 4 5 2 6, a[i-1] is 2 and a[i] is 6 and because of ar[i-1] > ar[i] condition the program's flow does not go into the inner loop.
Try this fix:
for (i = 1; i < ar_size; i++) {
j = i;
while (j > 0 && ar[j-1] > ar[j]) {
temp = ar[j-1];
ar[j-1] = ar[j];
ar[j] = temp;
j--;
}
}
The problem with your insertion-sort is you have your j indexes replaced with i indexes in the following code:
while(j>0 && ar[j-1]>ar[j])
{
temp = ar[j];
ar[j] = ar[j-1];
ar[j-1] = temp;
j--;
}
Just a note, when requesting input, it is good practice to print a brief statement describing the input expected. (yes, quick a dirty testing is an exception) It makes if much easier to avoid mistakes with something like:
printf ("enter array size: ");
scanf ("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for (_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
printf ("enter array element[%d] : ", _ar_i);
scanf ("%d", &_ar[_ar_i]);
}

How to find array[x] = array[x] + 1

This is my code to find facotrial of a number and to find number of occurunces of all digits in the factorial.
#include <stdio.h>
#include <stdbool.h>
int iFactorial(int iCount)
{
int iProduct = 1;
int iNumber = 1;
while (iNumber <= iCount)
{
iProduct *= iNumber;
iNumber++;
}
return iProduct;
}
int main(void)
{
int iFac[10] = {0};
int iCount = 0;
printf("Please input a Integer: ");
scanf("%d",&iCount);
iFac[iCount] = iFactorial(iCount);
printf("\nThe value of the factorial of %d is %d\n",iCount, iFac[iCount]);
int i;
int dig[10] = {0};
while (iFac <=0)
{
int n;
n= ((iFac % 10) + 1);
dig[n] = dig[n] +1;
iFac = iFac / 10;
}
for (i = 0; i > 9; i++)
{
if (dig[i+1] >0)
{
printf ("%d %d\n", i, dig[i+1]);
}
}
}
I need to find the proper method for writing array[x] = array[x] + 1
I think following code will clear how you wanted to count the digits.
#include<stdio.h>
int factorial (int n)
{
if ( n == 1 ) return 1 ;
return n * factorial( n-1 ) ;
}
int main()
{
//Input number
int num ;
scanf( "%d", &num ) ;
//Calculate Factorial
int fact = factorial ( num ) ;
cout<< "\nFactorial of Number is " << fact ;
//Count the frequency of Digits
int dig[10] = {0} ;
while( fact )
{
int i = fact % 10 ;
dig[i]++ ;
fact /= 10 ;
}
for ( int i = 0 ; i < 10 ; i++ )
printf("\n The digit %d is present %d times " , i , dig[i] );
return 0 ;
}
Make sure you dont input large values for calculating the factorial for a number.

Resources