Reverse string C program without using strrev
First we calculate length of the string without using strlen
function and then copy its characters in reverse order (from end
to beginning) to a new string using a for loop.
#include <stdio.h>
int main()
{
char s[1000], r[1000];
int begin, end, count = 0;
printf("Input a string\n");
gets(s);
// Calculating string length
while (s[count] != '\0')
count++;
end = count - 1;
for (begin = 0; begin < count; begin++)
{
r[begin] = s[end];
end--;
}
r[begin] = '\0';
printf("%s\n", r);
return 0;
C program to reverse a string using pointers
Now we will invert a string using pointers or without using the
library function strrev.
#include<stdio.h>
int string_length(char*);
void reverse(char*);
main()
{
char s[100];
printf("Enter a string\n");
gets(s);
reverse(s);
printf("Reverse of the string is \"%s\".\n", s);
return 0;
}
void reverse(char *s)
{
int length, c;
char *begin, *end, temp;
length = string_length(s);
begin = s;
end = s;
for (c = 0; c < length - 1; c++)
end++;
for (c = 0; c < length/2; c++)
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
int string_length(char *pointer)
{
int c = 0;
while( *(pointer + c) != '\0' )
c++;
return c;
}
Factorial program in C using for loop
Here we will find factorial using for loop.
#include <stdio.h>
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
Factorial program in C using recursion
#include<stdio.h>
long factorial(int);
int main()
{
int n;
long f;
printf("Enter an integer to find its factorial\n");
scanf("%d", &n);
if (n < 0)
printf("Factorial of negative integers isn't defined.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
Recursion is a technique in which a function calls itself, for
example, in the above code factorial function is calling itself. To
solve a problem using recursion you must first express its
solution in recursive form.
Even or odd
C program to find odd or even using bitwise operator
#include <stdio.h>
int main()
{
int n;
printf("Input an integer\n");
scanf("%d", &n);
if (n & 1 == 1)
printf("Odd\n");
else
printf("Even\n");
return 0;
}
C program to check even or odd using conditional operator
#include <stdio.h>
int main()
{
int n;
printf("Input an integer\n");
scanf("%d", &n);
n%2 == 0 ? printf("Even\n") : printf("Odd\n");
return 0;
}
C program to check odd or even without using bitwise or
modulus operator
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if ((n/2)*2 == n)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
C program to check odd or even without using bitwise or
modulus operator
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if ((n/2)*2 == n)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
In C programming language when we divide two integers we get
an integer result, for example, 7/3 = 2. So we can use it to find
whether the number is odd or even. Consider an integer 'n' we
can first divide it by two and then multiply it by two if the result
is same as the original number then the number is even
otherwise it is odd. For example, 11/2 = 5, 5*2 = 10 (which isn't
equal to eleven), now consider 12/2 = 6 and 6*2 = 12 (same as
original number). These are some logic which you may use to
determine if a number is odd or even.
Sum of digits in C
C program to calculate the sum of digits of a number: we will
use modulus operator (%) to extract individual digits of a
number and keep on adding them.
C programming code
#include <stdio.h>
int main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}
If you wish you can modify the input variable (n) and without
using an additional variable (t) but it isn't recommended.
Output of program:
For example, if the input is 98, the variable sum is 0 initially
98%10 = 8 (% is modulus operator which gives us remainder
when 98 is divided by 10).
sum = sum + remainder
so sum = 8 now.
98/10 = 9 because in C language whenever we divide an integer
by an another integer we get an integer.
9%10 = 9
sum = 8 (previous value) + 9
sum = 17
9/10 = 0.
So finally n = 0, the loop ends we get the required sum.
Calculate sum of digits in C without modulus operator
C program to find the sum of digit(s) of an integer which does
not use modulus operator. Our program uses a character array
(string) for storing an integer. We convert every character of the
string into an integer and add all these integers.
#include <stdio.h>
int main()
{
int c, sum, t;
char n[1000];
printf("Input an integer\n");
scanf("%s", n);
sum = c = 0;
while (n[c] != '\0') {
t = n[c] - '0'; // Converting character to integer
sum = sum + t;
c++;
}
printf("Sum of digits of %s = %d\n", n, sum);
return 0;
}
An advantage of this method is that input integer can be very
large which can't be stored in an int or a long long variable, see
an example below.
Output of program:
Input an integer
123456789123456789123456789
Sum of digits of 123456789123456789123456789 = 135
Sum of n numbers in C
Sum of n numbers in C: This program adds n numbers which
will be entered by a user. The user will enter a number
indicating how many numbers he/she wishes to add and then the
user will enter n numbers. To perform the arithmetic operation
of addition of n numbers we can use an array or do it without
using an array.
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("How many numbers you want to add?\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 1; c <= n; c++)
{
scanf("%d", &value);
sum = sum + value;
}
printf("Sum of the integers = %d\n", sum);
return 0;
}
Swapping of two numbers without third variable
You can also swap two numbers without using temp or
temporary or third variable. In that case C program will be as
shown :-
#include <stdio.h>
int main()
{
int a, b;
printf("Input two integers (a & b) to swap\n");
scanf("%d%d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %d\nb = %d\n",a,b);
return 0;
}
To understand the logic choose the variables 'a' and 'b' as '7' and
'9' respectively and then do what is being done by the program.
You can choose any other combination of numbers as well.
Sometimes it's an excellent way to understand a program
C program to reverse a number
C program to reverse a number: This program reverses the
number entered by the user and then print it on the screen. For
example, if a user will enter 123 as input then 321 will be
printed as output. In our program, we use modulus (%) operator
to obtain the digits of a number. To invert number look at it and
write it from opposite direction or the output of the program is a
number obtained by writing the original number from right to
left. To reverse or invert large numbers use long data type or
long long data type if your compiler supports it, if you still have
large numbers then use a string or other data structure.
C program to find reverse of a number
#include <stdio.h>
int main()
{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d", &n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;
}
Check palindrome number algorithm
To test if a number is palindrome or not, do the following steps:
1. Get the number from a user.
2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are the same then print palindrome number else print
not a palindrome number.
C program for palindrome number
#include <stdio.h>
int main()
{
int n, reverse = 0, t;
printf("Enter a number to check if it is a palindrome or
not\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
reverse = reverse * 10;
reverse = reverse + t%10;
t = t/10;
}
if (n == reverse)
printf("%d is a palindrome number.\n", n);
else
printf("%d isn't a palindrome number.\n", n);
return 0;
}
Prime numbers
Prime number program in C: C program for prime number, this
code prints prime numbers using C programming language. C
program to check whether a number is prime or not. Prime
number logic: a number is prime if it is divisible only by one
and itself. Remember two is the only even and the smallest
prime number. First few prime numbers are 2, 3, 5, 7, 11, 13, 17,
.... Prime numbers have many applications in computer science
and mathematics. A number greater than one can be factorized
into prime numbers, for example, 540 = 22*33*51.
Prime number program in C language
#include<stdio.h>
int main()
{
int n, i = 3, count, c;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n", i);
count++;
}
i++;
}
return 0;
}
C program for prime number or not
#include<stdio.h>
main()
{
int n, c = 2;
printf("Enter a number to check if it is prime\n");
scanf("%d",&n);
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d isn't prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);
return 0;
}
Fibonacci series C program using for loop
Fibonacci series C program
Fibonacci series in C programming: C program for Fibonacci
series using a loop and recursion. Using the code below you can
print as many terms of the series as required. Numbers of this
sequence are known as Fibonacci numbers. The first few
numbers of the series are 0, 1, 1, 2, 3, 5, 8, ...,. Except for the
first two terms of the sequence, every other term is the sum of
the previous two terms, for example, 8 = 3 + 5 (addition of 3 and
5). This sequence has many applications in Mathematics and
Computer Science.
/* Fibonacci series program in C language */
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d", &n);
printf("First %d terms of Fibonacci series are:\n", n);
for (c = 0; c < n; c++)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n", next);
}
return 0;
}
C string length program
C string length program: This program prints length of a string,
for example, length of the string "C programming" is 13 (space
character is counted). The null character isn't counted when
calculating string length. To find length of a string, we can use
strlen function of "string.h." C program to find length of a string
without using strlen function, recursion.
String length C program
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
printf("Enter a string to calculate it's length\n");
gets(a);
length = strlen(a);
printf("Length of the string = %d\n",length);
return 0;
}
String length in C without strlen
You can also find string length without strlen function. We
create our function to find it. We scan all the characters in the
string if the character isn't a null character increment the counter
by one. Once the null character is found the counter equals
length of the string.
#include <stdio.h>
int string_length(char []);
int main()
{
char s[1000];
int length;
printf("Input a string\n");
gets(s);
length = string_length(s);
printf("Length of %s = %d\n", s, length);
return 0;
}
int string_length(char s[]) {
int c = 0;
while (s[c] != '\0')
c++;
return c;
}
C program to find next prime palindrome
C program to find next prime palindrome: a user will enter a
number, and we have to find the least number greater than it
which is a palindrome as well as prime. For example, if the input
is 7 then the output will be 11 as it is a prime as well as a
palindrome, if the input is 21 then the output will be 101. In the
program, we check if a number is a palindrome and then check
if it is prime as it takes less time as primes occur more
frequently than palindromes.
C programming code
#include <stdio.h>
#include <math.h>
int main()
{
long n, t, r = 0, c, d;
printf("Enter an integer\n");
scanf("%ld", &n);
while (1)
{
n++;
t = n;
// Calculating reverse of the number
while(t)
{
r = r*10;
r = r + t%10;
t = t/10;
}
// If reverse equals original then it is a palindrome
if (r == n)
{
d = (int)sqrt(n);
/* Checking prime */
for (c = 2; c <= d; c++)
{
if (n%c == 0)
break;
}
if (c == d+1)
break;
}
r = 0;
}
printf("%ld\n",n);
return 0;
}