I'm newbie to programming. So as an exercise, I'm trying to print a number pattern like below.
0
10
210
3210
43210
I tried the code below.
#include<stdio.h>
void main()
{
int i ,j,n=5;
for(i=0;i<=n;i++)
{
for(j=0;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}
}
The output am getting after running the code above is:-
10
10
10
10
10
Am just stuck. Not able to solve this question. Can anyone help me please?
There are two loops.
The first loop control the line number with range as [0,N).
The second loop control the character sequences per line. Each line print out [Line_Number + 1] digital numbers. The sequence has a pattern as [Line_Number - Column_Number].
The example code:
#include <stdio.h>
void main() {
int i, j, n = 5;
for (i = 0; i < n; i++) {
for (j = 0; j < (i + 1); j++) {
printf("%d", (i - j));
}
printf("\n");
}
}
Build and run:
gcc test.c
./a.out
The output:
0
10
210
3210
43210
You have done a simple mistake for the inner loop-j. Make sure that your outer loop i-loop refers to the number of lines and your printing as printf("%d",i); defines how many lines you want.
#include<stdio.h>
void main()
{
int i ,j,n=5;
for(i=0;i<n;i++)
{
for(j=i;j>=0;j--)
{
printf("%d",j);
}
printf("\n");
}
}
Then the output will be:
0
10
210
3210
43210
#include<stdio.h>
void main()
{
int i ,j,n=5,t[n];
for(i=0;i<n;i++)
{
t[i]=i;
for(j=i;j>=0;j--)
{
printf("%d",t[j]);
}
printf("\n");
}
try this...
Related
i am new to C and i was trying to write a code that removes all the duplicate from and array of integers and print the result. But no matter what input i give it returns an output that dosent makes any sense. For example if i give the input 1,1,2,2,3 it gives me the output 1,48 instead of 1,2,3. The output dosen't make any sense. Can anybody please tell me what am i doing wrong. It might be very silly mistake i am not seeing. So irrespective of the input the first duplicate gets removed but the the code prints 48 and exits.
#include <stdio.h>
int main()
{
int arr[5],i,j;
printf("Enter the values of the array\n");
for(i=0;i<5;i++)
{
scanf("%d\n", &arr[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(arr[i]==arr[j])
{
arr[j] = 0;
}
}
}
for(i=0;i<5;i++)
{
if(arr[i] > 0)
{
printf("%d ", arr[i]);
}
}
return 0;
}
hey there is nothing wrong in your code.
i have verified in a online compiler and got the expected output.
make sure you give only 5 inputs.
here is what ive checked
programme:
#include <stdio.h>
int main()
{
int arr[5],i,j;
printf("Enter the values of the array\n");
for(i=0;i<5;i++)
{
scanf("%d\n", &arr[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(arr[i]==arr[j])
{
arr[j] = 0;
}
}
}
for(i=0;i<5;i++)
{
if(arr[i] > 0)
{
printf("%d ", arr[i]);
}
}
return 0;
}
output:
Enter the values of the array
1 1 2 2 3 5
1 2 3
I'm a beginner C programmer and a college freshman.
I need a little help here with a test i'm working on here.
I want to make a nested loop that shows a sorted number. Sorta like this:
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
21 20 19 18 17 16
22 23 24 25 26 27 28
... ... ... and so on, depending the limit of rows you input
I already tried to make a crude trial-and-error test code:
int i;
int j;
int limit;
int number1 = 1;
int number2 = 3;
int spesial = 0;
printf("Input limit : ");
scanf("%d", &limit);
for (i=1;i<=limit;i++)
{
for(j=1;j<=i;j++)
{
if (i%2==0)
{
printf("%d ", number2);
number2--;
}
else
{
printf("%d ", number1);
}
number1++;
}
if (i%2==0)
{
number2=(i*6)-i+(spesial*1);
spesial+=1;
}
printf("\n");
}
I managed to make it sorted to the 7th rows, but the rest are not..
help please...
I want to know if we could actually control the position of the output without sorta crude our way like this.
Also, sorry for my English... I'm not really from an English speaking country and this is my first time posting/question in this site.
Thank you for reading this lengthy question and I hope you have a good day and good night.
https://ideone.com/yCxpHo:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int rows;
int i, j;
int n = 0;
printf ("How many rows do you want? ");
if (scanf("%d", & rows) != 1 || rows < 1) return EXIT_FAILURE;
printf ("\n");
for (i = 1; i <= rows; ++ i) {
for (j = 0; j < i; ++ j) {
printf ("%4d", n + (i % 2 == 0 ? i - j : j + 1));
}
printf ("\n");
n = n + i;
}
return EXIT_SUCCESS;
}
It can be more convenient to create another function that will calculate the biggest number of a row (I called it lineMax).
int lineMax(int num){
int cnt=0;
for (int i=1;i<=num;i++)
cnt+=i;
return cnt;
}
void main(){
int i,j,limit;
printf("Input limit : ");
scanf("%d", &limit);
for(i=1;i<=limit;i++){
if(i%2==0){ //right to left
for(j=lineMax(i);j>=lineMax(i-1)+1;j--)
printf("%d ",j);
}
else{ //left to right
for(j=lineMax(i-1)+1;j<=lineMax(i);j++)
printf("%d ",j);
}
printf("\n");
}
}
You are making a lot of special cases with number1, number2 and special. This will not work for bigger numbers.
One way is to calculate count which will give you the value to start from in each loop of j. count += i and then every time print count -j
count = 0;
for (i=1;i<=limit;i++)
{
count += i;
for(j=0;j< i;j++)
{
printf ("%d ",count-j);
}
printf("\n");
}
Okay so I am trying to make my program ask for user integers (all working fine) and then call the function, which adds 5 to each array element, which i think is fine? then i need to print in the main program: the original integers with 5 added to them. anyone see what is wrong? my program crashes
#include <stdio.h>
void FUN(int ARR2[]);
int main()
{
int i=0;
int ARR1[20];
printf("Please enter 20 integers.\n");
for(i=0; i<20; i++) // switch back to 20
{
scanf("%i", &ARR1[i]);
}
FUN(&ARR1[i]);
printf("The new numbers are: %i", ARR1[i]);
return 0;
}
void FUN(int ARR2[])
{
int i=0;
ARR2[20];
for(i=0;i<20;i++)
ARR2[i]+=5;
}
When you reach this line
FUN(&ARR1[i]);
the value of your i variable is 20 so you are accessing the 21st element in ARR which is probably giving you an access violation. If you replace this line with
FUN(ARR1);
and remove the line
ARR2[20];
from your function FUN you might get the behaviour you are expecting
After your first for loop, i get's the value 20 but ARR1 is indexed 0 to 19. So, you might get a Segmentation Fault when you call
FUN(&ARR1[i]);
You should change that to
FUN(ARR1);
Also, in your function void FUN(int ARR2[]) , you have
ARR2[20];
Which also causes this error. Remove it.
You are also giving output the wrong way. You are doing
printf("The new numbers are: %i", ARR1[i]);
which also tries to access ARR1[20] , again causing problems.
You should change it to
printf("The new numbers are: \n");
for ( i=0 ; i < 20 ; i++)
printf("%d\n", ARR1[i]);
You should change your code to
#include <stdio.h>
void FUN(int ARR2[]);
int main()
{
int i=0;
int ARR1[20];
printf("Please enter 20 integers.\n");
for(i=0; i<20; i++) // switch back to 20
{
scanf("%i", &ARR1[i]);
}
FUN(ARR1);
printf("The new numbers are: \n");
for ( i=0 ; i < 20 ; i++)
printf("%d\n", ARR1[i]);
return 0;
}
void FUN(int ARR2[])
{
int i=0;
for(i=0;i<20;i++)
ARR2[i]+=5;
}
Input:
The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
Here's the link to the problem : http://www.spoj.com/problems/PRIME1/
Here is my program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n,m,t,i=0,j,k=0;
int tm[10],tn[10];
scanf("%d",&t); //test cases
while(i<t)
{
scanf("%d %d", &tm[i],&tn[i]);
i++;
}
int * a = malloc((n+1)*sizeof(int));
while(k<t)
{
n=tn[k];
m=tm[k];
a[0]=a[1]=0;
for(i=2;i<=n;i++)
a[i]=i;
for(i=2;i<=sqrt(n);i++)
{
if(a[i])
{
for(j=(i*i);j<=n;j+=i)
{
a[j]=0;
}
}
}
for(i=m;i<=n;i++)
{
if(a[i])
printf("%d \n", a[i]);
}
k++;
printf("\n");
}
return 0;
}
Declaring an array of size 10^9 is not permitted on many coding sites.That might be causing SIGSEGV.
Any way you need to change logic of your code since even if you resolve this error , your logic will possibly give you TLE
I am trying to code a project in c, that displays a fractal called Sierpinski fractal, (where the nodes are represented by '#'). So a 1-sierpinski triangle looks like :
##
#
a 2-sierpinski triangle
####
# #
##
#
and so on... Here's a link to find what it looks like : http://fr.wikipedia.org/wiki/Triangle_de_SierpiĆski
I was told it could be done without any loop, just by recursive method. So I tried something like :
//extracting the power of two's index
int puiss_2(int N){
int i=0,j=1;
for(i=0;i<N;i++){
j=j*2;
i++;
}
return j;
}
//the recursive method
void fractal(int N)
{
int M;
M= puiss_2(N);
if(M==0){
printf("##\n");
printf("# ");
}
else{
fractal(N-1);
fractal(N-1);
printf("\n");
fractal(N-1);
printf(" ");
}
}
int main()
{
int N;
scanf("%d",&N);
fractal(N);
}
Of course it didn't work because, when I jump to a line, I can't reverse it. So when I call it two times :
fractal(N-1);
fractal(N-1);
two contiguous motives are not gathered one aside the other... Does anyone has an idea on how to
make that ? Or perhaps I went completely wrong in my algo's design?
Here's some code that is perhaps complicated but recursive !
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sierpinsky(int N, char c[1000]){
int i=0,j,k,l,born;
for(i=0;i<N;i++){printf("%c",c[i]);}
printf("\n");
if(N==1){}
else{
if((c[0]=='#')&&(c[1]=='#')&&(c[2]=='#')){
for (j=0;2*j<N;j++){
if(c[2*j]=='#'){
c[2*j]='#';c[2*j+1]=' ';
}
else{
c[2*j]=' ';c[2*j+1]=' ';
}
}
}
else if ((c[0]=='#')&&(c[1]!='#')&&(c[2]=='#')){
for (j=0;4*j<N;j++){
if(c[4*j]=='#'){
c[4*j]='#';c[4*j+1]='#';c[4*j+2]=' ';c[4*j+3]=' ';
}
else{
c[4*j]=' ';c[4*j+1]=' ';c[4*j+2]=' ';c[4*j+3]=' ';
}
}
}
else if ((c[0]=='#')&&(c[1]!='#')&&(c[2] !='#')){
k=0;
while(c[k+1] !='#'){k++;}
born = k+1;
j=0;
while(j<N){
if((c[j]=='#')&&(c[j+born]=='#')){
for(l=0;l<born;l++){
c[j+l]='#';
}
j=j+born+1;
}
else if ((c[j]!='#')&&(c[j-1+born]=='#')&&(c[j-1+2*born] !='#'))
{
c[j-1]='#';
for(l=0;l<born;l++){
c[j+l]='#';
}
j=j+born+1;
}
else{
c[j-1]= ' ';
c[j]=' ';
j++;
}
}
}
else if ((c[0] =='#')&&(c[1] =='#')&&(c[2] !='#')){
for (j=0;4*j<N;j++){
if(c[4*j]=='#'){
c[4*j]='#';c[4*j+1]=' ';c[4*j+2]=' ';c[4*j+3]=' ';
}
else{
c[4*j]=' ';c[4*j+1]=' ';c[4*j+2]=' ';c[4*j+3]=' ';
}
}
}
else{}
sierpinsky(N-1, c);
}
}
int main()
{ int i,size;
scanf("%d",&size);
char c[1000];
for(i=0;i<size;i++){c[i]='#';}
for(i=size;i<1000;i++){c[i]='a';}
sierpinsky(size, c);
}
I think you dont need recursion for this. Let a triplet of # be 1 set. So The value of n = No. of levels of the set you're supposed to print one below the other. In the first line, print the set n times. In the next line, n-1 times, and so on. Try it iteratively.
Edit : If you were looking for a recursive solution, kindly ignore my answer.
you can probably code this using the pascal triangle.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
If you can print out this triangle as a whole with a loop, then , perhaps you can skip over even numbers.
to print the plain triangle just count the number of spaces in relation to the number of lines you want and code using a for loop(or a couple of them).Check which (pascal) number corresponds to which printing and skip over the even ones.