Posts

C program to print a string character by character using pointer

In this C program, we are going to learn  how to read and print (character by character) a string using pointer ? Here, we have two variables,  str  is a string variable and  ptr  is a character pointer, that will point to the string variable  str . First of all, we are reading string in  str  and then assigning the base address of  str  to the character pointer  ptr  by using  ptr=str  or it can also be done by using  ptr = &str[0] . And, finally we are printing the string character by character until NULL not found. Characters are printing by the pointer  *ptr . Consider the program /*C program to print a string using pointer.*/ # include < stdio.h > int main ( ) { char str [ 100 ] ; char * ptr ; printf ( " Enter a string: " ) ; gets ( str ) ; //assign address of str to ptr ptr = str ; printf ( " Entered string is: " ) ; while ( * ptr ! = '\0' ) printf ( " %

C Program to Calculate the Power of a Number

Image
Example on how to calculate the power of a number if the exponent is an integer. Also, you will learn to compute the power using pow() function. C Program to Calculate the Power of a Number To understand this example, you should have the knowledge of following C programming topics: C Program to Reverse a Number C Program to Check Whether a Number is Positive or Negative The program below takes two integers from the user (a base number and an exponent) and calculates the power. For example:  In case of 2 3 2 is the base number 3 is the exponent And, the power is equal to 2*2*2 Example #1: Power of a Number Using while Loop #include <stdio.h> int main() {     int base, exponent;     long long result = 1;     printf("Enter a base number: ");     scanf("%d", &base);     printf("Enter an exponent: ");     scanf("%d", &exponent);     while (exponent != 0)     {      

C Program to Reverse a Number

Image
Example to reverse an integer entered by the user. This problem is solved using while loop in this example. C Program to Reverse a Number To understand this example, you should have the knowledge of following C programming topics: C Program to Find Factorial of a Number C Program to Generate Multiplication Table Example: Reverse an Integer #include <stdio.h> int main() {     int n, reversedNumber = 0, remainder;     printf("Enter an integer: ");     scanf("%d", &n);     while(n != 0)     {         remainder = n%10;         reversedNumber = reversedNumber*10 + remainder;         n /= 10;     }     printf("Reversed Number = %d", reversedNumber);     return 0; } Output Enter an integer: 2345 Reversed Number = 5432 This program takes an integer input from the user. Then the while loop is used until  n != 0 is false. In each iteration of while loop, the remainder when  n

C Program to Check Whether a Number is Positive or Negative

In this example, you will learn to check whether a number (entered by the user) is negative or positive. To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if, if...else and Nested if...else Statement This program takes a number from the user and checks whether that number is either positive or negative or zero. Read -  Programming language Example #1: Check if a Number is Positive or Negative Using if...else #include <stdio.h> int main () { double number ; printf ( "Enter a number: " ); scanf ( "%lf" , & number ); if ( number <= 0.0 ) { if ( number == 0.0 ) printf ( "You entered 0." ); else printf ( "You entered a negative number." ); } else printf ( "You entered a positive number." ); return 0 ; } You can als

C Program to Check Whether a Character is an Alphabet or not

In this example, you will learn to check whether a character entered by the user is an alphabet or not. To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if, if...else and Nested if...else Statement Example: Program to Check Alphabet #include <stdio.h> int main () { char c ; printf ( "Enter a character: " ); scanf ( "%c" ,& c ); if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' )) printf ( "%c is an alphabet." , c ); else printf ( "%c is not an alphabet." , c ); return 0 ; } Output Enter a character: * * is not an alphabet In C programming, a character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself. The ASCII value of lowercase alphabets are from

C Program to Calculate the Sum of Natural Numbers

To compute the sum of natural numbers from 1 to n (entered by the user), loops can be used. You will learn how to use for loop and while loop to solve this problem. To understand this example, you should have the knowledge of following C programming topics: C Programming for Loop C Programming while and do...while Loop The positive numbers 1, 2, 3... are known as natural numbers. The programs below takes a positive integer (let say  n ) as an input from the user and calculates the sum up to  n . Example #1: Sum of Natural Numbers Using for Loop #include <stdio.h> int main () { int n , i , sum = 0 ; printf ( "Enter a positive integer: " ); scanf ( "%d" ,& n ); for ( i = 1 ; i <= n ; ++ i ) { sum += i ; // sum = sum+i; } printf ( "Sum = %d" , sum ); return 0 ; } The above program takes the input from the user and stores in variabl

C Program to Find Factorial of a Number

The factorial of a positive integer n is equal to 1*2*3*...n. You will learn to calculate the factorial of a number using for loop in this example. To understand this example, you should have the knowledge of following C programming topics: C Programming Data Types C Programming Operators C if, if...else and Nested if...else Statement C Programming for Loop The factorial of a positive number n is given by: factorial of n (n!) = 1*2*3*4....n The factorial of a negative number doesn't exist. And, the factorial of 0 is 1,  0! = 1 Example: Factorial of a Number #include <stdio.h> int main () { int n , i ; unsigned long long factorial = 1 ; printf ( "Enter an integer: " ); scanf ( "%d" ,& n ); // show error if the user enters a negative integer if ( n < 0 ) printf ( "Error! Factorial of a negative number doesn't exist." ); else { for (