Learn C Online Blog

Program to check if a number is Armstrong or not 4

Program to check if a number is Armstrong or not

Definition (Armstrong Number): An n-digit number equal to the sum of the nth powers of its digits. Example: 153 = 13 + 53 + 33 1634 = 14 + 64 + 34 + 44 #include<stdio.h> #include<math.h> int main(){ long int num =153, count = 0, i, temp1, temp2, temp3 = 0; clrscr(); printf(“Enter the number: “); scanf(“%ld”,&num); if(num <= 0){ printf(“\nEnter the number greater than 0”); return 0; } temp1 = temp2 = num; while(temp1!=0){ temp1=temp1/10; count++; } for(i=0;i < count; i++) { temp3 = temp3 + pow((temp2%10),count); temp2 = temp2/10; } if(num == temp3) { printf(“\n %d is an...

String handling functions in C 22

String handling functions in C

In this article, you will understand different string handling functions in C. By the end of this article, you will learn how to write C programs to perform the following string operations –  find the length of a string, compare 2 strings, copy one string to another, concatenate 2 strings, etc. Following are some of the useful string handling functions in C. strlen() strcpy() strncpy() strcat() strncat() strcmp() strncmp() strcmpi() strncmpi() These string handling functions are defined in string.h header file. Hence you need to include this header file whenever you use these string handling functions in your C program....

Program to Calculate Length of a String (Using strlen in-built function) 5

Program to Calculate Length of a String (Using strlen in-built function)

#include #include #include void main(){ int strlength; char *str; clrscr(); printf(“\nEnter the string: “); gets(str); strlength=strlen(str); printf(“\nThe length of the string is %d.”,strlength); getch(); } Output: Enter the string: Learn C Online The length of the string is 14.

Program to Calculate Length of a String (without using strlen in-built function). 30

Program to Calculate Length of a String (without using strlen in-built function).

#include #include void main(){ char *str; int count = 0,i; clrscr(); printf(“\nEnter the String: “); gets(str); for(i=0;str[i]!=’\0′;i++){ count++; } printf(“\nThe length of the string is %d.”,count); getch(); } Output: Enter the String: Learn C Online The length of the string is 14.

Strings in C Programming Language 19

Strings in C Programming Language

A string in C is a series of characters in a group that occupy contiguous memory. A group of characters (Alphabets, digits and special characters) is called as a string. Example 1: “Thank you for visiting www.learnconline.com” “www.learnconline.com is excellent site for beginners. This is example of strings in C.” A string in C should always be enclosed with in double quotes (“). If single quote is used then it is treated as character. Hence ‘A’ is different from “A”. Every string ends with a null character (‘\0’). Note that \0 is a single character. So when it has to...

Program in C to reverse a given Number 9

Program in C to reverse a given Number

Below is the C program to reverse a given number. This C program will accept a number (in a variable named “num”) and performs an operation to reverse the number. #include <stdio.h> #include <stdlib.h> int main(void) { int num=1234, newNum=0; while(num!=0){ newNum=newNum*10 + num%10; num=num/10; } printf(“\n”); printf(“%d”, newNum); return 1; } Output: 4321 Explanation: The statement while(num!=0) will be executed till value of num !=0 num%10 (read as num mod 10) will return the last digit. So in 1st iteration, it will return 4. So, newNum will become–> 0*10 + 4 –>4 Next line i.e. num/10 will return 123...

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet