Monday, 14 March 2016

Print 1


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
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 (a<=b)
  {
    printf("%d\n",a);
    a++;
  }
  return 0;
}
  

Multiplication Table


Write a C program to print the multiplication table of an integer n upto m rows using a for 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 n, m,i;
  printf("Enter n\n");
  scanf("%d",&n);
  printf("Enter m\n");
  scanf("%d",&m);
  printf("The multiplication table of %d is\n",n);
  for (i=1;i<=m;i++)
  {
    printf("%d*%d=%d\n",i,n,(i*n));
  }
  
  
  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 for 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,j[20],count=0,count1=0;
  printf("Enter the value of n\n");
  scanf("%d",&n);
  for (i=0;i<n;i++)
  {
    printf("Enter the number\n");
    scanf("%d",&j[i]);
    if(j[i]>=0)
     count++;
    else if(j[i]<0)
      count1++;
  }
  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 non-negative numbers entered and the sum of all positive numbers entered using a for 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,i;
  int a, count=0,sum=0;
  printf("Enter the value of n\n");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    printf("Enter the number\n");
    scanf("%d",&a);
    if(a>=0)
    {
      count++;
      sum=sum+a;
    }
  }
  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 for 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 a[10],n,sum=0,i;
  printf("Enter the value of n\n");
  scanf("%d",&n);
  for (i=0;i<n;i++)
  {
    printf("Enter the number\n");
    scanf("%d",&a[i]);
    sum=sum+a[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 for 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

Print 2


Write a C program to print all numbers between a and b (a and b inclusive) using a for 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,i;
  printf("Enter the value of a\n");
  scanf("%d",&a);
  printf("Enter the value of b\n");
  scanf("%d",&b);
  if(a>=b)
  {
    for (i=a;i>=b;i--)
    {
      printf("%d\n",i);
    }
  }
  return 0;
}