0% found this document useful (0 votes)
14 views6 pages

C Programs: String Length & Strong Number

The document contains C code snippets for various string and number operations: 1) Finding the length of a string without built-in functions. 2) Checking if a number is a "strong number" by calculating the factorial sum of its digits. 3) Converting a decimal number to binary. 4) Performing a linear search on an array. 5) Checking if a number exists in an array and inserting if not found. 6) Finding the sum of the first and last digits of a number. 7) Reversing a string without built-in functions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

C Programs: String Length & Strong Number

The document contains C code snippets for various string and number operations: 1) Finding the length of a string without built-in functions. 2) Checking if a number is a "strong number" by calculating the factorial sum of its digits. 3) Converting a decimal number to binary. 4) Performing a linear search on an array. 5) Checking if a number exists in an array and inserting if not found. 6) Finding the sum of the first and last digits of a number. 7) Reversing a string without built-in functions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C program to find length of a string without using string handling functions.

#include <stdio.h>
int main()
{
//Initializing variable.
char str[100];
int i,length=0;

//Accepting input.
printf("Enter a string: \n");
scanf("%s",str);

//Initializing for loop.


for(i=0; str[i]!='\0'; i++)
{
length++; //Counting the length.
}

printf("\nLength of input string: %d",length);

return 0;
}

C Program to check if a given number is a strong number or not.


A strong number is a number in which the sum of the factorial of the digits is
equal to the number itself.
Eg:- 145=1!+4!+5!=1+24+120=145

#include <stdio.h>
int main()
{
int n;
int sum=0;
printf("Enter a number");
scanf("%d",&n);
int k=n;
int r;
while(k!=0)
{
r=k%10;
int mul=1;
for(int i=1;i<=r;i++)
{
mul=mul*i;
}
k=k/10;
sum=sum+f;
}
if(sum==n)
{
printf("\nNumber is a strong");
}
else
{
printf("\nNumber is not a strong");
}
return 0;
}

C program to convert the given decimal number into binary number.

#include <stdio.h>
int main()
{
int a[10], number, i, j;
printf("\n Please Enter the Number You want to Convert : ");
scanf("%d", &number);
for(i = 0; number > 0; i++)
{
a[i] = number % 2;
number = number / 2;
}

printf("\n Binary Number of a Given Number = ");


for(j = i - 1; j >= 0; j--) {
printf(" %d ", a[j]);
}
printf("\n");
return 0;
}

C program to perform linear search on an array of numbers

#include <stdio.h>
int main()
{
int a[10], i, item,n;
printf("\nEnter number of elements of an array:\n");
scanf("%d",&n);
printf("\nEnter elements: \n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
for (i=0; i<=9; i++)
if (item == a[i])
{
printf("\nItem found at location %d", i+1);
break;
}
if (i > 9)
printf("\nItem does not exist.");
return 0;
}

C program to check if a number is present in a given list of numbers. If present,


give location of the number otherwise insert the number in the list at the end.

#include<stdio.h>
#include<conio.h>

void main()
{
int i,n,m,flag=0; int a[10],pos;
clrscr();

printf("How many elements you want to enter n");


scanf("%d",&n);

printf("Enter element in the array n");


for (i=0; i<n; i++)
scanf("%d", &a[i]);

printf("Enter the element you want to search n");


scanf("%d", &m);

for (i=0; i<n; i++)


{
if(a[i]==m)
{
flag=1;
pos=i;
break;
}
}
if(flag==0)
printf("Not present");
else
printf("Present at location %d",pos);
getch();
}

C program to find the sum of first and last digit of a number.


#include <stdio.h>
int main()
{
int n, sum=0, firstDigit, lastDigit;
printf("Enter number to find sum of first and last digit = ");
scanf("%d", &n);
// Find last digit of a number
lastDigit = n % 10;
//Find the first digit by dividing n by 10 until n greater then 10
while(n >= 10)
{
n = n / 10;
}
firstDigit = n;
//Calculate sum of first and last digit
sum = firstDigit + lastDigit;
printf("Sum of first and last digit = %d", sum);
return 0;
}
C program to reverse a string without using string handling functions.

#include <stdio.h>
#include <string.h>
int main()
{
char Str[100], RevStr[100];
int i, j, len;
printf("\n Please Enter any String : ");
gets(Str);
j = 0;
len = strlen(Str);
for (i = len - 1; i >= 0; i--)
{
RevStr[j++] = Str[i];
}
RevStr[i] = '\0';
printf("\n String after Reversing = %s", RevStr);
return 0;
}

You might also like