as

Wednesday, 12 November 2014

for Loop in C



Program to display Addition of 1 to 50 numbers .
#include<stdio.h>

void main()
{
  int i=1,sum=0;
  for(i=1;i<=50;i++)
  {
    sum=sum+i;
  }
  printf("\n sum of 1 to 50 numbers is : %d",sum);
     
  getchar();
}

Program to accept 20 numbers from the user & count the number of positive values,negative values & zeros.
#include<stdio.h>

void main()
{
  int i=1,no,z=0,p=0,n=0;
  for(i=1;i<=20;i++)
  {
      printf("\n Enter values:");
      scanf("%d",&no);
      if(no==0)
      {
        z++;
      }
      if(no>0)
      {
        p++;
      }
      if(no<0)
      {
       n++;
      }
 
  }
  printf("\n Zeros no is :%d",z);
  printf("\n Positive no is :%d",p);
  printf("\n Negative no is :%d",n);
 
  getchar();
}

Program to display series of 1 to 15 numbers in reverse order and display the sum of 1 to 15 numbers .
#include<stdio.h>

void main()
{
  int i=0,sum=0;
  int fno=15,sno=1;
  for(i=fno;i>=sno;i--)
  {
      printf("%d \t",i);
      sum=sum+i;
  }
  printf("\n Sum is :%d",sum);
 
  getchar();
}

Sponsored Links

Popular Posts

Comments

ShareThis

Google+ Followers

Google+ Badge