Monday, November 17, 2014

For Loop,While Loop,do while loop C Pro



Using For Loop

#include
#include 
int main()
 {
   int i = 1;
    for (i = 1; i <= 10; i++)
   {
      printf("%d", i);
   }
   return (0);
}

Using While Loop

#include
#include
int main()
{
   int i = 1;
   while (i <= 10)
   {
      printf("%d", i);
      i++;
   }
    return (0);
}

Using do while loop

#include
#include
int main()
{
   int i = 1;
   do
   {
      printf("%d", i);
      i++;
   }
   while (i <= 10);
   return (0);
}

Digits of the Number using Recursive Function in C Programming


#include
#include
int calsum(int num)
{
   int rem, sum;
   if (num != 0)
   {
      rem = num % 10;
      sum = sum + rem;
      calsum(num / 10);
   }
   return sum;
}
int main()
 {
   int num, val;
   printf("\nEnter a number: ");
   scanf("%d", &num);
   val = calsum(num);
   printf("\nSum of the digits of %d is : %d", num, val);
   return 0;
}


Output



Enter a number: 123
Sum of the digits of 123 is : 6

No comments:

Post a Comment