Monday, 14 March 2016

Valid


A number is said to be valid iff it is divisible by 8.
Write a C program that allows the user to keep entering numbers as long as the input is valid and also displays a count of the valid numbers entered using a while loop.

Input Format: 
Input consists of integers.

Output Format: 
Refer Sample Input and Output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output: 
Enter the number
8
Enter the number
16
Enter the number
96
Enter the number
6
The number of valid numbers entered is 3

Code:
  #include<stdio.h>
int main(){
  int n,count=0;;
  while(1){
  printf("Enter the number\n");
  scanf("%d",&n);
    if(n%8==0)
    {      count++;
    }else
    {
      printf("The number of valid numbers entered is %d",count);
      break;
    }
  }
  return 0;
}
    
  

Multiplication Table


Write a program to print the multiplication table of an integer n upto m rows using a while loop. 

Input Format: 
Input consists of 2 integers. The first integer corresponds to n. The second integer corresponds to m.

Output Format: 
Refer Sample Input and Output for formatting specifications. 

[All text in bold corresponds to input and the rest corresponds to output] 
Sample Input and Output: 
Enter n 
5 
Enter m 
4 
The multiplication table of 5 is 
1*5=5 
2*5=10 
3*5=15 
4*5=20

Code:
  #include<stdio.h>
int main(){
  int m,n,i=1;
  printf("Enter n\n");
  scanf("%d",&n);
  printf("Enter m\n");
  scanf("%d",&m);
  printf("The multiplication table of %d is\n",n);
  while(i<=m)
  {
    
    printf("%d*%d=%d\n",i,n,(i*n));
    
    i++;
  }
  return 0;
}

    
  
  

Count positive and negative


Write a C program to that allows the user to enter 'n' numbers and finds the number of positive numbers entered and the number of negative numbers entered using a while loop.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to n. The next n integers correspond to the numbers to be added. Consider 0 to be a positive number.

Output Format: 
Refer Sample Input and Output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]
Sample Input and Output: 
Enter the value of n
4
Enter the number
5
Enter the number
-2
Enter the number
-1
Enter the number 6
Number of positive numbers entered is 2 and the number of negative numbers entered is 2

Code:
  #include<stdio.h>
int main(){
  int n,i=1,b,count=0,count1=0;
  printf("Enter the value of n\n");
  scanf("%d",&n);
  while(i<=n)
  {
    printf("Enter the number\n");
    scanf("%d",&b);
    if(b>=0)
      count++;
    else
      count1++;
    i++;
  }
  printf("Number of positive numbers entered is %d and the number of negative numbers entered is %d",count,count1);
  return 0;
}

    
  
  

Sum of 'n' positive numbers


Write a C program to that allows the user to enter 'n' numbers and finds the number of positive numbers entered and the sum of all positive numbers entered using a while loop.

Input Format:
Input consists of n+1 integers. The first integer corresponds to n. The next n integers correspond to the numbers to be added. Consider 0 to be a positive number.

Output Format: 
Refer Sample Input and Output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output: 
Enter the value of n
4
Enter the number
5
Enter the number
-2
Enter the number
-1
Enter the number
6
Number of positive numbers entered is 2 and the sum is 11

Code:
  #include<stdio.h>
int main(){
  int n,sum=0,count=0,i=1,b;
  printf("Enter the value of n\n");
  scanf("%d",&n);
  while(i<=n)
  {
    printf("Enter the number\n");
    scanf("%d",&b);
    i++;
    if(b>=0)
    {
      count++;
      sum=sum+b;
    }
  }
  printf("Number of positive numbers entered is %d and the sum is %d",count,sum);
  return 0;
}

Sum of 'n' numbers


Write a C program to find the sum of 'n' numbers using a while loop.

Input Format: 
Input consists of n+1 integers. The first integer corresponds to n. The next n integers correspond to the numbers to be added.

Output Format: 
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output: 
Enter the value of n
4
Enter the number
5
Enter the number
2
Enter the number
1
Enter the number
6
The sum is 14

Code:
  #include<stdio.h>
int main(){
  int n,i=1,sum=0,nsum=0;
  printf("Enter the value of n\n");
  scanf("%d",&n);
  while(i<=n)
  {
    printf("Enter the number\n");
    scanf("%d",&nsum);
    sum=sum+nsum;
    i++;
  }
  printf("The sum is %d",sum);
  return 0;
}
  

Print 3


Write a C program to print all numbers between a and b (a and b inclusive) using while loops.

Input Format: 
Input consists of 2 integers. The first integer corresponds to a and the second integer corresponds to b.

Output Format: 
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output 1: 
Enter the value of a
10
Enter the value of b
4
10
9
8
7
6
5
4

Sample Input and Output 2: 
Enter the value of a
4
Enter the value of b
10
4
5
6
7
8
9
10

Code:
  #include<stdio.h>
int main()
{
  int a,b;
  printf("Enter the value of a\n");
  scanf("%d",&a);
  printf("Enter the value of b\n");
  scanf("%d",&b);
  while(1)
  {
    if(a>b){
      printf("%d\n",a);
      --a;}
    else if(a<b){
      printf("%d\n",a);
      ++a;}
    else if(a==b){
      printf("%d\n",a);
    break;
    }
      
    
  }
  return 0;
}

Print 2


Write a C program to print all numbers between a and b (a and b inclusive) using a while loop.

Input Format: 
Input consists of 2 integers. The first integer corresponds to a and the second integer corresponds to b. Assume a>=b.

Output Format: 
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output: 
Enter the value of a
10
Enter the value of b
4
10
9
8
7
6
5
4

Code:
  #include<stdio.h>
int main(){
    int a,b;
    printf("Enter the value of a\n");
    scanf("%d",&a);
    printf("Enter the value of b\n");
    scanf("%d",&b);
    while(b<=a)
    {
      printf("%d\n",a);
      a--;
    }
    return 0;
  }