Programming Fundamentals:Looping 1 (Session 5)

Programming Fundamentals:Looping 1 (Session 5)


Program 1

#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);

  for(i=a;i<=b;i++)

    printf("%d\n",i);

  return 0;

}

Program 2

#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);

  for(i=a;i>=b;i--)

    printf("%d\n",i);

  return 0;

}

Program 3

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

Program 4

#include<stdio.h>

int main()

{

  int a,i,sum=0,b;

  printf("Enter the value of n\n");

  scanf("%d",&a);

  for(i=1;i<=a;i++)

  {

    printf("Enter the number\n");

    scanf("%d",&b);

    sum=sum+b;

  }

  printf("The sum is %d",sum);

  return 0;

}

Program 5

#include<stdio.h>

int main()

{

  int a,i,sum=0,b,c=0;

  printf("Enter the value of n\n");

  scanf("%d",&a);

  for(i=1;i<=a;i++)

  {

    printf("Enter the number\n");

    scanf("%d",&b);

    if(b>=0)

    {  c++;

      sum=sum+b;

    }

  }

  printf("Number of positive numbers entered is %d and the sum is %d",c,sum);

  return 0;

}

Program 6

#include<stdio.h>

int main()

{

  int a,i,sum=0,b,c=0;

  printf("Enter the value of n\n");

  scanf("%d",&a);

  for(i=1;i<=a;i++)

  {

    printf("Enter the number\n");

    scanf("%d",&b);

    if(b>=0)

    {  c++;

    }

    else

      sum++;

  }

  printf("Number of positive numbers entered is %d and the number of negative numbers entered is %d",sum,c);

  return 0;

}

Program 7

#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;

}

Program 8

#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);

  i=a;

  while(i<=b)

  {

    printf("%d\n",i);

    i++;

  }

  return 0;

}

Program 9

#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);

  i=a;

  while(i>=b)

  {

    printf("%d\n",i);

    i--;

  }

  return 0;

}

Program 10

#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);

  i=a;

  if(a>b)

  {

    while(i>=b)

  {

    printf("%d\n",i);

    i--;

  }

  }

  else

  {

    while(i<=b)

  {

    printf("%d\n",i);

    i++;

  }

  }

  return 0;

}

Program 11

#include<stdio.h>

int main()

{

  int a,b,sum=0,i=0;

  printf("Enter the value of n\n");

  scanf("%d",&a);

  while(i<a)

  {

    printf("Enter the number\n");

    scanf("%d",&b);

    sum=sum+b;

    i++;

  }

  printf("The sum is %d",sum);

  return 0;

}

Program 12

#include<stdio.h>

int main()

{

  int a,b,sum=0,i=0,c=0;

  printf("Enter the value of n\n");

  scanf("%d",&a);

  while(i<a)

  {

    printf("Enter the number\n");

    scanf("%d",&b);

    if(b>=0)

    {sum=sum+b;

     c++;

    }

    i++;

  }

  printf("Number of positive numbers entered is %d and the sum is %d",c,sum);

  return 0;

}

Program 13

#include<stdio.h>

int main()

{

  int a,b,sum=0,i=0,c=0;

  printf("Enter the value of n\n");

  scanf("%d",&a);

  while(i<a)

  {

    printf("Enter the number\n");

    scanf("%d",&b);

    if(b>=0)

    {c++;

    }

    else

      sum++;

    i++;

  }

  printf("Number of positive numbers entered is %d and the number of negative numbers entered is %d",sum,c);

  return 0;

}

Program 14

#include<stdio.h>

int main()

{

  int n,m,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;

}

Program 15

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

Post a Comment

Post a Comment (0)

Previous Post Next Post