Friday, 30 January 2015

Find out the sum of digit in c language.

Sum of the digit


For example if the input is 98, sum(variable) is 0 initially
98%10 = 8 (% is modulus operator which gives us remainder when 98 is divided by 10).
sum = sum + remainder
so sum = 8 now.
98/10 = 9 because in c whenever we divide integer by another integer we get an integer.
9%10 = 9
sum = 8(previous value) + 9
sum = 17
9/10 = 0.
So finally n = 0, loop ends we get the required sum.

C programming code

#include<stdio.h>
#include<conio.h>

void main()
{
   int n, t, sum = 0, remainder;
   clrscr();

   printf("Enter an integer\n");
   scanf("%d", &n);

   t = n;

   while (t != 0)
   {
      remainder = t % 10;
      sum = sum + remainder;
      t  = t / 10;
   }

   printf("Sum of digits of %d = %d\n", n, sum);

   getch();
}
 
 

Output of program:


 

Make money from your Website or Blog with BidVertiser

Friday, 23 January 2015

Check a number is amstrong number or not.

Armstrong number c program

A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 153, 370,407.

Solve the problem using C programming language.

#include <stdio.h>

#include<conio.h>



void main()

{

   int number, sum = 0, temp, remainder;

   clrscr();

   printf("Enter an integer\n");

   scanf("%d",&number);



   temp = number;



   while( temp != 0 )

   {

      remainder = temp%10;

      sum = sum + remainder*remainder*remainder;

      temp = temp/10;

   }



   if ( number == sum )

      printf("%d is an armstrong number.\n",temp);

   else

      printf("%d is not an armstrong number.\n",temp);


  

}

Thursday, 22 January 2015

How to print prime number between a range(1 to 10).


What is logic?

Program logic is a set of instructions to solve a particular problem. Program logic is important for every program as every program have some logic to solve the problem.
Logic is a special set of instructions given to the computer. The computer executes the given instructions and solves the problem by using those instructions.

How to think logically?
Every programmer should think logically to solve the problem. But how to think logically?

 So in order to think logically we should learn how logic will work and how to develop logic for a program.

How to develop logic?

To develop logic for a program we should be capable of thinking logically. They must have good concept in mathematics. Only a logical thinker can be capable of developing logic for a program. But most of the programmers and beginners struggle a lot in order to improve their logical thinking skills.

Points to be remember while developing logic:-

Yes You can – faith on yourself:

Computer has no brain. They cannot understand anything unless you give some instruction. You will write logic for a computer to solve a particular problem. So without your logic a computer cannot solve any problem. So a computer needs your logic in order to solve a particular problem.

Remember all Programming concepts you have learnt:

When developing logic first think how to solve this problem mathematically. Then build that logic by using the syntax in a particular language (C, C++). So it is necessary for a programmer to learn all the programming concepts and recall them while developing logic for a particular problem.

Real Time application of Logic:-

Suppose that you have to print all the even numbers between 1 to 10.

To solve that problem first think that how to calculate even numbers? A number which is divisible by 2 that number is even number. But how computer can understand that a number is divided by 2 or not. Here is logic – the number is divisible by 2 means the remainder is 0. So your logic must be number mod by 2 is equal to 0 then the number is even number, otherwise not.

  If you use C language then for checking the condition you have to use if case.

Syntax is --     if (condition)

                        {

                                    Body of the if block ....

                        }

                        else

{

            Body of the else block ....

                        }

So if you use if then the code is like that

if (( number % 2) == 0)

{

printf(“%d is even number”, number);

}

Next covering range 1 to 10 you have to use a iteration statement. In C language there are 3 types of iteration statement. Choose any one from that. Suppose you choose for loop.

Syntax of for loop is— for (initialization; condition check; step value)

                                    {

                                                Body of for loop

                                    }

So covering 1 to 10 the code is like that

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

{

........

}

Finally we put the logic of even number into the for loop.

So the final code is like that

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

{

if (( i % 2) == 0)

{

printf(“%d is even number”, number);

}

}

Here i starts with 1 and ends in 10. During this iteration every i value check through if condition if the value is divisible by 2 and the remainder is equal to 0 then the i value is even number.

Here the output is – 2, 4, 6, 8, 10.

Final conclusion:-

So to solve the problem first think logically then build the logic using any programming language. Test the code that it produces proper output or not.