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
1 year, 3 months ago

No vote yet
1 vote

  Easy Math Editor

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold

- bulleted
- list

  • bulleted
  • list

1. numbered
2. list

  1. numbered
  2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1

paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
    # 4 spaces, and now they show
    # up as a code block.

    print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in \( ... \) or \[ ... \] to ensure proper formatting.
2 \times 3 \( 2 \times 3 \)
2^{34} \( 2^{34} \)
a_{i-1} \( a_{i-1} \)
\frac{2}{3} \( \frac{2}{3} \)
\sqrt{2} \( \sqrt{2} \)
\sum_{i=1}^3 \( \sum_{i=1}^3 \)
\sin \theta \( \sin \theta \)
\boxed{123} \( \boxed{123} \)

Comments

Sort by:

Top Newest

i got it @Agnishom Chattopadhyay

Rishabh Deep Singh - 1 year, 3 months ago

Log in to reply

Nice!

Agnishom Chattopadhyay - 1 year, 3 months ago

Log in to reply

Thanks

Rishabh Deep Singh - 1 year, 3 months 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 - 1 year, 3 months ago

Log in to reply

Thanks @Agnishom Chattopadhyay Can u post a solution to Question no 1.

Rishabh Deep Singh - 1 year, 3 months ago

Log in to reply

Try answering the hint

Agnishom Chattopadhyay - 1 year, 3 months ago

Log in to reply

i need to submit my assignment now please post the answer Bro.

Rishabh Deep Singh - 1 year, 3 months 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 - 1 year, 3 months 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 - 1 year, 3 months ago

Log in to reply

×

Problem Loading...

Note Loading...

Set Loading...