Posts

Showing posts with the label main(void)

First C Program

Image
Your First C Program Here, is a Hello World program in C #include<stdio.h> //Pre-processor directive void main() //main function declaration { printf("Hello World"); //to output the string on a display getch (); //terminating function } Here is the code explanation: Pre-processor directive #include is a pre-processor directive in 'C.' #include <stdio.h> , stdio is the library where the function printf is defined . printf is used for generating output. Before using this function, we have to first include the required file, also known as a header file (.h). You can also create your own functions, group them in header files and declare them at the top of the program to use them. To include a file in a program, use pre-processor directive #include <file-name>.h File-name is the name of a file in which the functions are stored. Pre-processor directives are always placed at the beginning of the program. The main function The

C Programming Questions and Answers

Image
// a) If a 5 digit number is input through the keyboard, write a program to print the sum. #include <stdio.h> int main(){    int number, sum = 0, remainder = 0, counter = 0;    printf("Enter an integer\n");    scanf("%d", &number);    while (number != 0){        remainder = number%10;        counter += 1;        sum += remainder;        number = number/10;    }    if(counter == 5){        printf("Sum of digits of %d.\n", sum);    }    else{        printf("\nIt was not 5 digit number.\n");    }    return 0; } // b) If a four digit number is input through the keyboard, write a program to find the sum of first and last digit. #include <stdio.h> int main(){    int number, sum = 0, remainder = 0, counter = 0;    printf("Enter an integer\n");    scanf("%d", &number);    while (number != 0){        remainder = number%10;        counter += 1;        if(counter == 1 || counter == 4