Waste less time on Facebook — follow Brilliant.
×

Programming Problems

Q.1

     Take as input a natural number, say n. Output all subsets of {1,2,...,n}.
     You cannot use functions or recursion.

Q.2

     Write a program to read a sequence of non-zero integers till the number zero is entered and at the end display the following:
   a)      the number of even and odd numbers, 
   b)      sum of all the numbers entered,
   c)      the length and starting index (or position) of a largest subsequence of consecutive non-decreasing integers entered. Assume that the index of the numbers start from 1.

  For example, if the user input is  1 -2 3 17 9 5 -10 -12 0, then the output will be:
 No. of even numbers: 3
 No. of odd numbers: 5
 Sum of the numbers: 11
 Length of largest non-decreasing subsequence: 3
 Starting index: 2
 Note: Do not use arrays.

Note by Rishabh Deep Singh
3 months, 3 weeks ago

No vote yet
1 vote

Comments

Sort by:

Top Newest

i got it @Agnishom Chattopadhyay Rishabh Deep Singh · 3 months, 3 weeks ago

Log in to reply

@Rishabh Deep Singh Nice! Agnishom Chattopadhyay · 3 months, 3 weeks ago

Log in to reply

@Agnishom Chattopadhyay Thanks Rishabh Deep Singh · 3 months, 3 weeks ago

Log in to reply

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>
#include<math.h>
int main()
{
    int n,i,r=1,z,b=0,rem,k=0,j=1;
    printf("Enter the number\n");
    scanf("%d",&n);
    for(i=0;i<pow(2,n);i++)
    {
        b=0;
        r=1;
        z=i;
        while(z!=0)
            {
                rem=z%2;
                b=b+rem*r;
                z=z/2;
                r=r*10;
             }      
        printf("{");
        for(j=1;j<=n;j++)
        {
            k=b%10;
            if(k==1)
            {
                printf("%d,",j);
            }
            b=b/10;
        }
        if(i!=0)
        printf("\b");
        printf("},");   
    }
    printf("\b ");
} 
Rishabh Deep Singh · 3 months, 3 weeks ago

Log in to reply

Thanks @Agnishom Chattopadhyay Can u post a solution to Question no 1. Rishabh Deep Singh · 3 months, 3 weeks ago

Log in to reply

@Rishabh Deep Singh Try answering the hint Agnishom Chattopadhyay · 3 months, 3 weeks ago

Log in to reply

@Agnishom Chattopadhyay i need to submit my assignment now please post the answer Bro. Rishabh Deep Singh · 3 months, 3 weeks ago

Log in to reply

I have increased the scope of your problems from C to all other languages by editing "C" out of the discussion.

Hint about 2: How do functions work? What is the underlying data structure that keeps track of where the execution should return to? Agnishom Chattopadhyay · 3 months, 3 weeks ago

Log in to reply

Answer 2.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<stdio.h>
int main()
{
  int n=1, e=0, o=0, sum=0, p=0, i=0, index=1, c=0, gre=0, first=1;
  printf("Enter a sequnce of numbers terminal no. of sequence being 0");
  while(n!=0)
    {
      i++;
      scanf("%d" ,&n);
      if(n==0)
        break;
      sum+=n;
      if(n%2==0)
        e++;
      else
       o++;
      if(n>=p)
        {
          c++;
        }
      else
       {
          if(c>gre)
       {
gre=c;
first=index;
 index=1;
            }
              c=1;
        }p=n;
    }
  if(c>gre)
    {
gre=c;
      first=index;
    }
  printf("No. of even no. is %d \n" ,e);
  printf("No. of odd numbers is %d \n" ,o);
  printf("Sum of the entered numbers is %d \n" ,sum);
  printf("Length of largest non decreasing subsequence within is %d \n" ,gre);
  printf("starting index is %d \n" ,first);
  return 0;
} 
Rishabh Deep Singh · 3 months, 3 weeks ago

Log in to reply

×

Problem Loading...

Note Loading...

Set Loading...