C Programming Old Question Programming Solution( BSc.CSIT-2070)

C- Programming: Dhiraj Jha
Share

Especially early learners in programming get too daunted and look for how to jump start to solve any problem. Keeping this in mind, the problem solving approach is emphasized so that, you can get good grasp of the problem solving approach. It is not about solving some programming hurdles correctly, but reaching up to the solution in a well directed cognitive process.

/**
////////////////////////////////////////////////////////////
// Problem: 2070/1                                        //
// Write down the program for checking whether the number //
// entered by the user is exactly divisible by 5          //
// but not by  11.                                        //
////////////////////////////////////////////////////////////
//Solution Thought Process:                               //
////////////////////////////////////////////////////////////
Step #1. Read the number from the user, say, number
Step #2. Use the Arithmetic operator % (modulus) to
            check remainder,  ( number % 5 ) == 0.
            Also check that it is not exactly divisible
            by 11,  (number % 11 ) != 0.
            Now, we use nested-if, for evaluating the two
            conditions because one condition is dependent on another.
Step #3. Print appropriate messages where necessary.
Step #4. Stop.
/////////////////////////////////////////////////////////
// Alternate Thought Process:                          //
/////////////////////////////////////////////////////////
Step #1. Read the number from the user, say, number
Step #2. Use the Arithmetic operator % (modulus) to
            check remainder after division by 5.
            Also check that it is not exactly divisible
            by 11.
            Now, we use logical AND, for evaluating the two
            conditions both conditions should match.
Step #3. Print appropriate messages
Step #4. Stop.

*/
//////////////////////////////////////////////////////////////////////
// Main program 
/////////////////////////////////////////////////////////////////////

/* header files */
#include<stdio.h>
#include<conio.h>

/* Main implementation */
int main(){
/* declare a variable to store the number entered by the
user */
int number;
/* prompt appropriate message to the user for interaction
and read the number using standard i/o function */
printf("\n Enter an integer number: \n");
scanf("%d", &amp;number); /* mark the &amp; (ampersand, read as
                                     "Address Of" operator)
                        is necessary for storing values
                        at the address of variable, number
                    */
/* check for the given condition */
if((number % 5) == 0)
{
        /* nested if to check for not divisible by 11 */
        if((number % 11) != 0)
        {
            printf("\n %d is exactly divisible by 5 but not by 11 \n", number);
        }else
        {
            printf("\n %d is exactly divisible by 5 and by 11 as well \n", number);
        }


}/* end of if statement */
else
{
 /* nested if to check for not divisible by 11 */
        if((number % 11) != 0)
        {
            printf("\n %d is not exactly divisible by 5 or 11 \n", number);
        }else
        {
            printf("\n %d is not exactly divisible by 5 but by 11 \n", number);
        }
}

/* just to hold the screen for indefinite period for user
to enter some character from the standard input (keyboard)
*/
getch(); /* defined in conio.h */

}/* main ends */

/**
 Test data set [5, 33, 1980 ] 
Run your program using CodeBlocks to see the output.
*/
/////////////////////////////////////////////////////
// Change the above program to match the Alternate //
// Thought  Process                                //
/////////////////////////////////////////////////////

DOWNLOAD Source Code

About Dhiraj 44 Articles
Blogger on development, securities, and efficiency on web technologies.

Be the first to comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.