Tuesday 9 December 2014

C Program to examine various loops

Program to examine various loops
#include<stdio.h>
#include<stdlib.h>
loops()
{
printf("program to define loops\n");
printf("**************************\n");
forloop();
whileloop();
dowhile();
exit(0);
}
forloop()
{
 int i,n,sum=0;
printf("program to print sum of first N integer values\t");
printf("enter value for N\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
sum=sum+i;
}
printf("sum ofn integers is %d\n",sum);
}
whileloop()
{
 int i=1,n,mul=1;
printf("program to multiply first N integers\t");
printf("***************************************\n");
printf("enter value for n\n");
scanf("%d",&n);
while(i<n)
{
 mul=mul*i;
i++;
}
printf("multiplication=%d\n",mul);
}
dowhile()
{
int i=0,n;
printf("program to show first N integers\n");
printf("**********************************\n");
printf("enter value for N\n");
scanf("%d",&n);
do
{
  printf("the numbers are %d\n",i);
i++;
}while(i<n);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi operations.c
madan@madan-Lenovo-G570:~/madan$ vi operations.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles operations.c
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400440
madan@madan-Lenovo-G570:~/madan$ ./a.out
program to define loops
**************************
program to print sum of first N integer values enter value for N
5
sum ofn integers is 10
program to multiply first N integers
enter value for n
5
multiplication=120
program to show first N integers
enter value for N
5
the numbers are 0
the numbers are 1
the numbers are 2
the numbers are 3
the numbers are 4