Online C Programming Test

Test Description

Subject = C Programming
No of Questions = 20
Time = 30 mins
Total Marks = 20

Correct Answer = 1 mark
Wrong Answer = - 0.25 mark (negative)

C Program to find Armstrong Number

Definition of Armstrong Number:
A number is Armstrong if the sum of cubes of individual digits of a number is equal to the number itself.

Example:
0, 1, 153, 370, 371, 407 etc.

Explanation:
Let us take 153.
So, the sum of cubes of individual digits
= 13 + 53 + 33
= 1 + 125 + 27
= 153

Programming Approach (Algorithm):
Step 1: number = <Entered Number>
Step 2: Right Most Digit = number % 10
Step 3: number = number / 10
Step 4: Sum = Sum + Cube of Right Most Digit
Step 5: Repeat Step 2, 3 & 4 until number = 0
Step 6: if Sum = <Entered Number> then return 1
                                 // <Entered Number> is Armstrong number
        else return 0 // <Entered Number> is not an Armstrong number

C Program:
/*
 * PROGRAM NAME: Armstrong Number
 * SOURCE: http://CProgrammingOnline.blogspot.com
 */

/* HEADER FILE DECLARATION SECTION */
#include<stdio.h>

/* FUNCTION PROTOTYPE DECLARATION SECTION */
_Bool armstrong(int );

/* MAIN PROGRAM SECTION */
    int main(char* argv[], int argc){
    int number;    //Used to store the given number

    printf("\nProgram to Test Whether a Number is Armstrong Number or Not\n");
    printf("Enter an Integer Value (Max Size = %d bytes): ",sizeof(int));
    scanf("%d",&number);

    if(armstrong(number)==1){
        printf("\n%d is an armstrong number.",number);
    } else {
        printf("\n%d is not an armstrong number.",number);
    }
}

/*
 * FUNCTION SIGNATURE: _Bool armstrong(int );
 * INPUT: int number
 * OUTPUT: 1 if number(INPUT) is an armstrong number
 *         0 if number(INPUT) is not an armstrong number
 */
_Bool armstrong(int number){
    int stored_number = number;  //Used to Store the given number
    int sum = 0;                       //Used to Store the sum of digits
    char digit;                       //Used to Store the digits

    while(stored_number > 0){
        digit = stored_number % 10;
        sum += digit * digit * digit;
        stored_number /= 10;
    }

    if(sum == number){
        return 1;
    } else {
        return 0;
    }
}

Confused, Have any questions?
Post your comments, questions. State your confusion to us. We're here to help you.

Are you facing problems in writing C programs?
Hire us now!
Submit Your Problem Now

Subscribe via Email


Copy Text File using C Program


#include<stdio.h>

int main(int argc, char* argv[]){
    FILE *sfp;                   // Source File Pointer
    FILE *tfp;                   // Target File Pointer
    char SourceFilename[255];    // Stores the source file name
    char TargetFilename[255];    // stores the target file name
    char ch;                     // used to read charecter
                                 //       from source file name

    /* Get sourcefile name and targetfile name from the user */
    printf("\nEnter Soucefile Name: ");
    gets(SourceFilename);
    fflush(stdin);
    printf("Enter Targetfile Name (NOTE : If Targetfile already exists, its content will be overwritten): ");
    gets(TargetFilename);

    /* Open source file in read mode and target file in write mode */
    sfp=fopen(SourceFilename,"r");
    if(sfp==NULL){
        printf("\nERROR: Failed to open %s in read mode. Closing the program...",SourceFilename);
        return;
    }
    tfp=fopen(TargetFilename,"w");
    if(tfp==NULL){
        printf("\nERROR: Failed to open %s in write mode. Closing the program...",TargetFilename);
        return;
    }

    /* read charecter by charecter from sourcefile
       and write those charecters accordingly in targetfile */
    while(1){
        ch=fgetc(sfp);
        if(ch==EOF){
            break;
        }else{
            fputc(ch,tfp);
        }
    }

    /* close file pointers */
    fclose(sfp);
    fclose(tfp);
}