Found 33676 Articles for Programming

C++ Program to Sum the digits of a given number

Nishu Kumari
Updated on 03-Mar-2025 13:17:07

13K+ Views

The task is to write C++ programs that calculates the sum of the digits of a given number. For example, if the number is 453, the sum of its digits would be 4 + 5 + 3 = 12. To solve this, we will extract each digit from the number and add them together. We will use basic programming concepts such as loops and arithmetic operations to achieve this. In this article, we will show you how to write a C++ program that takes a number as input and sums the digits of that number. Approaches for ... Read More

Implement your own sizeof operator using C++

Tapas Kumar Ghosh
Updated on 21-Apr-2025 18:00:20

964 Views

In C++, the sizeof() operator is a unary operator which is used to calculate the size of any data type. Implementing sizeof() OperatorWe can use the #define directive to implement a macro that copies the behavior of the sizeof() operator for objects, though it will not be the same as the sizeof() operator. Syntax Following is the syntax of C++ macro to implement the sizeof(): #define any_name(object) (char *)(&object+1) - (char *)(&object) Here, any_name : The name you want to give to your own sizeof() operator. Note: The main benefit of using MACROS is defining by once ... Read More

What all is inherited from parent class in C++?

Tapas Kumar Ghosh
Updated on 04-Jun-2025 14:22:42

4K+ Views

In C++, when a class inherits from another class, it inherits all members from parent class, but their accessibility are based on access specifiers. Key Points on What Inherits from Parent Class Following are some points on derived class inherits from its parent: The derived class can inherit data members, member functions of base class. If the data members are public, they can be accessed by derived class, same class and outside the class. If data members are protected, they can be accessed by derived and same ... Read More

How will you print numbers from 1 to 100 without using loop in C?

Tapas Kumar Ghosh
Updated on 21-Apr-2025 18:12:19

4K+ Views

You can print the numbers from 1 to 100 without a loop by using the various methods like recursive function and goto statement that print the list of integers.The following are the approaches to print the numbers from 1 to 100: Using Recursion As we know, recursion is the process of calling the function itself. Here, we use the recursive function to accept an integer and set the iteration of plus 1 without the logic of loop and get the expected outcome. Example In this example, we use an if statement to check whether the given integer is less than ... Read More

isgraph() C library function

karthikeya Boyini
Updated on 26-Jun-2020 08:17:52

150 Views

The function isgraph() is used to check that the passed character has a graphical representation or not. It is declared in “ctype.h” header file.Here is the syntax of isgraph() in C language, int isgraph(int char);Here is an example of isgraph() in C language, Example Live Demo#include #include int main() {    int a = '';    int b = '8';    int c = 's';    if(isgraph(a))    printf("The character has graphical representation");    else    printf("The character isn’t having graphical representation");    if(isgraph(b))    printf("The character has graphical representation");    else    printf("The character isn’t having graphical representation");    if(isgraph(c)) ... Read More

Difference between strlen() and sizeof() for string in C

karthikeya Boyini
Updated on 26-Jun-2020 08:07:54

948 Views

strlen()The function strlen() is a predefined function in C language. This is declared in “string.h” header file. It is used to get the length of array or string.Here is the syntax of strlen() in C language, size_t strlen(const char *string);Here, string − The string whose length is to be calculated.Here is an example of strlen() in C language, Example Live Demo#include #include int main () {    char s1[10] = "Hello";    int len ;    len = strlen(s1);    printf("Length of string s1 : %d", len );    return 0; }OutputLength of string s1 : 10In the above ... Read More

isupper() function in C Language

Samual Sam
Updated on 26-Jun-2020 08:10:38

2K+ Views

The function isupper() is used to check that the character is uppercase or not. It returns non-zero value if successful otherwise, return zero. It is declared in “ctype.h” header file.Here is the syntax of isupper() in C language, int isupper(int character);Here, character − The character which is to be checked.Here is an example of isupper() in C language, Example Live Demo#include #include int main() {    char val1 = 's';    char val2 = 'S';    if(isupper(val1))    printf("The character is uppercase");    else    printf("The character is not uppercase");    if(isupper(val2))    printf("The character is uppercase");    else    printf("The ... Read More

isalnum() function in C Language

karthikeya Boyini
Updated on 26-Jun-2020 08:11:19

9K+ Views

The function isalnum() is used to check that the character is alphanumeric or not. It returns non-zero value, if the character is alphanumeric means letter or number otherwise, returns zero. It is declared in “ctype.h” header file.Here is the syntax of isalnum() in C language, int isalnum(int character);Here, character − The character which is to be checked.Here is an example of isalnum() in C language, Example Live Demo#include #include int main() {    char val1 = 's';    char val2 = '8';    char val3 = '$';    if(isalnum(val1))    printf("The character is alphanumeric");    else    printf("The character is not ... Read More

Print contents of a file in C

Samual Sam
Updated on 26-Jun-2020 07:56:43

7K+ Views

Here is an example to print contents of a file in C language, Let’s say we have “new.txt” file with the following content.0, hell!o 1, hello! 2, gfdtrhtrhrt 3, demoNow, let us see the example.Example#include #include void main() {    FILE *f;    char s;    clrscr();    f=fopen("new.txt", "r");    while((s=fgetc(f))!=EOF) {       printf("%c", s);    }    fclose(f);    getch(); }Output0, hell!o 1, hello! 2, gfdtrhtrhrt 3, demoIn the above program, we have a text file “new.txt”. A file pointer is used to open and read the file. It is displaying the content of file.FILE *f; ... Read More

fopen() for an existing file in write mode in C

karthikeya Boyini
Updated on 26-Jun-2020 07:57:29

721 Views

The function fopen() opens the file pointed by pointer and read or write the file. In the write mode, “w” is used and in the read mode, “r” is used.When a file exists in the directory, it treats as a new empty file and override the content of file by new data.Here is the syntax of fopen() in C langauge, FILE *fopen(const char *filename, const char *access_mode)Here, filename − The name of file which is to be opened.acess_mode − The mode to access the file like read or write mode.Here is an example of fopen() in C language, Let’s say ... Read More

Advertisements