0% found this document useful (0 votes)
230 views56 pages

100 Codes

The document contains C code snippets for various basic programs including: 1) Checking if a number is positive, negative, or zero. 2) Checking if a number is even or odd. 3) Calculating the sum of the first n natural numbers. 4) Calculating the sum of n natural numbers input by the user. 5) Finding the greatest of two numbers and three numbers. 6) Checking if a year is a leap year. 7) Checking if a number is prime. 8) Calculating the factorial of a number. 9) Finding the sum of the digits of a number. 10) Reversing a number. 11) Checking if a number

Uploaded by

Wajiha Tanveer
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)
230 views56 pages

100 Codes

The document contains C code snippets for various basic programs including: 1) Checking if a number is positive, negative, or zero. 2) Checking if a number is even or odd. 3) Calculating the sum of the first n natural numbers. 4) Calculating the sum of n natural numbers input by the user. 5) Finding the greatest of two numbers and three numbers. 6) Checking if a year is a leap year. 7) Checking if a number is prime. 8) Calculating the factorial of a number. 9) Finding the sum of the digits of a number. 10) Reversing a number. 11) Checking if a number

Uploaded by

Wajiha Tanveer
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
You are on page 1/ 56

Positive or negative

#include<stdio.h>
int main()
{
int num;
printf(“Insert a number: “);
scanf(“%d”, &num);
//Condition to check if the number is negative or positive
if (num <= 0)
{
if (num == 0)
printf(“The number is 0.”);
else
printf(“The number is negative”);
}
else
printf(“The number is positive”);
return 0;
}

Even or Odd
#include<stdio.h>
int main()
{
int number;
printf(“Insert a number \n“);
scanf(“%d”,&number);

//Checking if the number is divisible by 2


if (number%2 == 0)
printf(“The number is even\n“);

else
printf(“The number is odd\n“);
return 0;
}

Sum of 1st n natural num

#include<stdio.h>
int main()
{
int sum = 0, n;
printf(“Enter the first N Natural Number\n“);
scanf(“%d”,&n);
sum=(n*(n+1))/2;
printf(“sum is %d”,sum);
return 0;
}

Sum of N natural numbers

#include<stdio.h>
int main()
{
//for initialize variable
int Number, i, Sum = 0;
//to take user input
printf (“\n Kindly Insert an Integer Variable\n“);
scanf (“%d”, &Number);
//use for loop for these condition
for(i = 1; i <= Number; i++)
{
Sum = Sum + i;
}

//display
printf (“Sum of Natural Numbers = %d”, Sum);

return 0;
}
Sum of numbers in a given range

#include <stdio.h>
int main()
{
//for initialization of variable
int firstrange,lastrange, i=0, total= 0;

//to use user input first range & last range


printf(“Enter the value first range and last range\n“);
scanf(“%d\n%d”,&firstrange, &lastrange);

//use for loop for total no.inside the range


for(i = firstrange; i <= lastrange; i++){

//total+=i;
total = total + i;
}

//print the sum of number


printf(“Sum of number firstrang %d to lastrange %d is: %d”,firstrange, last
range, total);
}
Greatest of two numbers

#include<stdio.h>
int main()
{
int no1, no2;
printf(“Insert two numbers:”);
scanf(“%d %d”,&no1, &no2);

//Condition to check which of the two number is greater


//it will compare of number where number 1 is greater
if(no1 > no2)
printf(“%d is greatest”,no1);

//where number 2 is greater


else if(no2 > no1)
printf(“%d is greatest”,no2);

//for both are equal


else
printf(“%d and %d are equal”, no1, no2);

return 0;
}
Greatest of three numbers

#include<stdio.h>
int main()
{
int no1,no2,no3;

//Prompt user to insert any three integer variables


printf(“\nInsert value of no1, no2 and no3:”);
scanf(“%d %d %d”, &no1, &no2, &no3);

//for check of number 1 is greater


if((no1 > no2) && (no1 > no3))
printf(“\n Number1 is greatest”);

//weather number 2 is grater


else if((no2 > no3) && (no2 > no1))
printf(“\n Number2 is greatest”);

//other conditions are false than number 3 is greater


else
printf(“\n Number3 is greatest”);
return 0;
}
Leap year or not

#include<stdio.h>
int main()
{
//initialization of Year
int year;

//to take user input


printf(“Enter Year for find leap year or not : “);
scanf(“%d”,&year);

//we use this statement for check leap year


if(((year%4==0)&&(year%100!=0)) || (year%400==0))
printf(“%d is a Leap Year”,year);

//not leap year


else
printf(“%d is not a Leap Year”,year);
return 0;

}
Prime number

#include <stdio.h>
int main()
{
//initialize of variable
int i, number, fact = 1;

//to take user input.


printf("Enter a number to calculate its factorial\n");
scanf("%d", &number);

//use this loop of following statement


for (i = 1; i<= number;i++)
fact = fact * i;

//display of factorial of a given number


printf("Factorial of a number %d is = %d\n", number, fact);
return 0;
}
Prime number with a given range

#include <stdio.h>
int main()
{
//initialize of variable
int i, number, fact = 1;

//to take user input.


printf("Enter a number to calculate its factorial\n");
scanf("%d", &number);

//use this loop of following statement


for (i = 1; i<= number;i++)
fact = fact * i;

//display of factorial of a given number


printf("Factorial of a number %d is = %d\n", number, fact);

return 0;
}
Factorial of a number
#include <stdio.h>
int main()
{
//initialize of variable
int i, number, fact = 1;

//to take user input.


printf("Enter a number to calculate its factorial\n");
scanf("%d", &number);

//use this loop of following statement


for (i = 1; i<= number;i++)
fact = fact * i;

//display of factorial of a given number


printf("Factorial of a number %d is = %d\n", number, fact);

return 0;
}

/* C program to take a number & calculate the sum of its numbers */


#include<stdio.h>
int main()
{

int no, temp, digit, sum = 0;

printf ("Insert a number \n");


scanf ("%d", &no);

temp = no;
while (no > 0)
{
digit = no % 10;
sum = sum + digit;
no /= 10;
}
printf("Given number = %d\n", temp);
printf("Sum of the numbers %d = %d\n", temp, sum);
return 0;
}

Reverse of number
#include<stdio.h>
int main()
{
//Initialization of variables where rev='reverse=0'
int number, rev = 0,store, left;

//input a numbers for user


printf("Enter the number\n");
scanf("%d", &number);

store= number;

//use this loop for check true condition


while (number > 0)
{
//left is for remider are left
left= number%10;

//for reverse of no.


rev = rev * 10 + left;

//number /= 10;
number=number/10;

}
//To show the user value
printf("Given number = %d\n",store);

//after reverse show numbers


printf("Its reverse is = %d\n", rev);

return 0;
}

Palindrome

 A number is 123321 .If you read number “123321” from reverse order, it is same as
“123321”. In that number is a palindrome.

#include<stdio.h>
int main()
{
//Initialization of variables where rev='reverse=0'
int number, rev = 0,store, n1,left;

//input a numbers for user


printf("Enter the number\n");
scanf("%d", &number);

//for duplicacy of number


n1=number;

store= number;
//use this loop for check true condition
while (number > 0)
{
//left is for remider are left
left= number%10;

//for reverse of no.


rev = rev * 10 + left;

//number /= 10;
number=number/10;
}
//To check reverse no is a Palindrome
if(n1==rev)
printf("Number %d is Palindrome number",n1);
else
printf("it is not a Palindrome number");
return 0;
}

Armstrong num

Ex:- Enter any number 153.

1**3 + 5**3 + 3**3 = 153

#include<stdio.h>
int main()
{
int num ,n,n1,c=0,mul=1,sum=0,r,f,i;
printf("enter any num: \n");
scanf("%d",&num);
n=num;
n1=num;
while(n!=0)
{
r=n%10;
c++;
n=n/10;
}
while (num!=0)
{
f=num%10;
mul=1;
for(i=1;i<=c;i++)
{
mul=mul*f;
}

sum=sum+mul;
num=num/10;
}
if(n1==sum)
printf("Armstrong Number");
else
printf("Not an Armstrong Number");
return 0;
}

Armstrong in a given range

#include<stdio.h>
int main()
{
//For initializing variables
int start, end, i, temp1, temp2, rem, n = 0, result = 0;
//user give start and end point of a number
printf("Insert the start value and end value :");
scanf("%d %d", &start, &end);

//to display pint of range


printf("\n Armstrong nums between %d an %d are: ", start, end);

//for use this loop to store all number in given range


for(i = start + 1; i < end; ++i)
{
//store a duplicity value of given range
temp2 = i;
temp1 = i;

while (temp1 != 0)
{
//temp1 /= 10;
temp1=temp1/10;
++n;
}
while (temp2 != 0)
{
rem = temp2 % 10;
//result += pow(rem, n);
result=result+pow(rem,n);
//temp2 /= 10;
temp2=temp2/10;
}
//check true condition if result is equal to i
if (result == i)
{
//display
printf("%d ", i);
}
n = 0;
result = 0;
}
printf("\n");
return 0;
}

Fibonacci series
#include<stdio.h>
int main()
{
//To initialize variables
int n1=0,n2=1,n3,limit,i;

//To take user input


printf("enter a limit of series \n");
scanf( "%d",&limit);

printf("Fibonacci series %d %d ",n1,n2);

//To use this loop for given length


for(i=2;i<limit;i++)
{
//n1 and n2 sum store in new variable n3
n3=n1+n2;
n1=n2;
n2=n3;
//display serious
printf("%d ",n3);
}
return 0;
}

Power of a number
#include<stdio.h>
int main()
{
//To initialize variables
int number, expo,temp = 1;

//To take user input


printf("Enter a base number: ");
scanf("%d", &number);
//To display Exponent
printf("Enter an exponent: ");
scanf("%d", &expo);
//use while loop when power is not equal to zero
while (expo != 0)
{
//temp*=number
temp = temp * number;
--expo;
}
printf("power of a %d is %d",number, temp);
return 0;
}

Factorial of number
#include <stdio.h>
int main()
{
//initialize of variable
int i, number, fact = 1;

//to take user input.


printf("Enter a number to calculate its factorial\n");
scanf("%d", &number);

//use this loop of following statement


for (i = 1; i<= number;i++)
fact = fact * i;

//display of factorial of a given number


printf("Factorial of a number %d is = %d\n", number, fact);

return 0;
}

Factor of number
Ex- no is 16,5.
16= 2 x 2 x 2 x 2
5= 1 x 5

#include<stdio.h>
int main()
{
//To initialize variable
int number, u;
//to take user input
printf("Enter an any number: ");
scanf("%d",&number);

printf("Factors of a number %d are: ", number);

//Use for loop this condition


for(u=1; u<= number; u++)
{
//now we check for true condition of this
if (number%u == 0)
//display factor
printf("%d ",u);
}
return 0;
}
Op:
Enter an any number: 12
Factors of a number 12 are: 1 2 3 4 6 12
Strong number
Ex:- number is 145
1! + 4! + 5!=1 + 24 +120=145
#include<stdio.h>

//find factorial of a number.


int factorial(int number)
{
//to initialize of factorial
int i,fact=1;
//use for loop with this condition
for(i=1;i<=number;i++)
{
//fact*=1;
fact=fact*i;
}
return fact;
}
//to main function
int main()
{
//to initialize variables
int number,digit,sum=0,temp;

//To take user input


printf("Enter a number:");
scanf("%d",&number);
//To store a duplicity value of a given number
temp=number;

//use this whenever number is not equal to 0


while(temp!=0)
{
//for last digit
digit=temp%10;
//now we call of factorial function
digit = factorial(digit);
//to improve of a sum on digit
sum=sum+digit;
temp=temp/10;
}
//we check sum is equal to number its true
if(sum==number)
{
//display
printf("It is a Strong Number");
}
//false condition
else
{
//display
printf("It is not Strong Number");
}
return 0;
}

Perfect number

#include<stdio.h>
int main()
{
// Initialization of variables
int number,i=1,total=0;

// To take user input


printf("Enter a number: ");
scanf("%d",&number);

while(i<number)
{
if(number%i==0)
{
total=total+i;
i++;
}
}
//to condition is true
if(total==number)
//display
printf("%d is a perfect number",number);
//to condition is false
else
//display
printf("%d is not a perfect number",number);

return 0;
}

Automorphic

 5=(5)2=25
 6=(6)2=36

 25=(25)2=625
 76=(76)2=5776
 376=(376)2=141376

#include<stdio.h>

int checkAutomorphic(int num)


{
int square = num * num;

while (num > 0)


{
if (num % 10 != square % 10)
return 0;

// Reduce N and square


num = num / 10;
square = square / 10;
}
return 1;
}

int main()
{
//enter value
int num;
scanf("%d",&num);

//checking condition
if(checkAutomorphic(num))
printf("Automorphic");
else
printf("Not Automorphic");
return 0;
}

Harshad number
Ex– Number is 21.it is divisible by own sum (1+2) of its digit(2,1).So it is harshad number

#include<stdio.h>
int main()
{
//To initialize of variable
int number,temp,sum = 0, digit, res;

//To take user input


printf("enter any number : ");
scanf("%d",&number);

//store in temporary variable


temp = number;
//use while loop with this condition
while(temp!=0)
{
//to find last digit
digit=temp % 10;
//sum+=digit
sum = sum + digit;
//temp/=10
temp = temp / 10;
}
res = number % sum;
//check result is equal is to 0
if(res == 0)
//display
printf("%d is Harshad Number",number);
else
//display
printf("%d is not Harshad Number",number);
return 0;
}

Abundant number
Ex:- Abundant number 12 having a proper divisor is 1,2,3,4,6 the sum of these factor is 16 it is
greater than 12 so it is a Abundant number.
#include<stdio.h>
int main()
{
//initialization variables
int number,sum=0,c;
//input from user
printf("Enter a number : ");
scanf("%d",&number);
//declare a variable to store sum of factors of the number
for(c = 1 ; c < number ; c++)
{
if(number % c == 0)
//sum+=c;
sum = sum + c;
}
if(sum > number)
//display the result
printf("Abundant Number");
else
//display
printf("Not an Abundant Number");

return 0;
}

Friendly pair
For instance, for numbers 6 and 28,.Divisors of 6 are- 1, 2, 3, and 6. Divisors of 28 are- 1, 2, 4, 7,
14, and 28. Sum of the divisors of 6 and 28 are 12 and 56 respectively. Also, the abundant
index of 6 and 28 is 2. Therefore, 6 and 28 is a friendly pair.

#include<stdio.h>
int main()
{
//1 Create two variables to use in first and second numbers
int i;
int f_Num,s_Num;
//2 two more variables created to store the sum of the divisors
int f_DivisorSum = 0;
int s_DivisorSum = 0;

//3 Asking user to enter the two numbers


printf("Enter two numbers to check if Amicable or not : ");
scanf("%d %d",&f_Num,&s_Num);

//4 Using one variable for loop and second to check for each number
for(int i=1;i<f_Num;i++)
{
//5 Condition check
if(f_Num % i == 0)
f_DivisorSum = f_DivisorSum + i;
}
//6 Calculating the sum of all divisors
for(int i=1;i<s_Num;i++)
{
if(s_Num % i == 0)
s_DivisorSum = s_DivisorSum + i;
}
//7 Check condition for friendly numbers
if((f_Num == s_DivisorSum) && (s_Num == f_DivisorSum))

else
{
printf("%d and %d are not Amicable numbers\n",f_Num,s_Num);
}
return 0;
}
Hcf of two numbers
#include <stdio.h>
int main()
{
//for initialize variables
int a, b, i, hcf;
a = 12;
b = 16;
//find hcf of number
for(i = 1; i <= a || i <= b; i++)
{
if( a%i == 0 && b%i == 0 )
hcf = i;
}
//display hcf
printf("HCF = %d", hcf);
return 0;
}

/** C program to calculate the LCM of two numbers */

#include<stdio.h>

void lcm_two_no(int,int);
int main()
{
int n1,n2;

//to take user input n1,n2


printf("Enter two numbers: ");
scanf("%d %d",&n1,&n2);

//call of user define function


lcm_two_no(n1,n2);
return 0;
}

//function to calculate l.c.m


void lcm_two_no(int n1,int n2)
{
int check1,check2;
//to use of duplicity value
check1=n1;
check2=n2;

//to find lcm of number


while(check1!=check2)
{
//for condition true
if(check1< check2
check1=check1+n1;

//for condition false


else
check2=check2+n2;
}
printf("\nL.C.M of %d and %d is: %d",n1,n2,check1);

// C program to calculate GCD of two numbers


#include<stdio.h>
// The code used a recursive function to return gcd of p and q
int gcd(int p, int q)
{

// checking divisibility by 0
if (p == 0)
return q;

if (q == 0)
return p;

// base case
if (p == q)
return p;

// p is greater
if (p > q)
return gcd(p-q, q);

else
return gcd(p, q-p);
}

// Driver program to test above function


int main()
{
int p = 98, q = 56;
printf("GCD of %d and %d is %d ", p, q, gcd(p, q));
return 0;
}
the H.C.F or G.C.D of 12 and 14 is 2. The H.C.F or G.C.D of 16 and 12 is 4

/** C program to convert the given binary number into decimal**/

#include<stdio.h>

int main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;

printf("Insert a binary num (1s and 0s) \n");


scanf("%d", &num); /* maximum five digits */

binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
//num/=10;
num = num / 10 ;
//base*=2;
base = base * 2;
}
//display binary number
printf("The Binary num is = %d \n", binary_val);
//display decimal number
printf("Its decimal equivalent is = %d \n", decimal_val);
return 0;
}
/** C Program to Convert Binary to Octal*/
#include<stdio.h>

int main()

{
//For initialize variables
long int binary_num, octal_num = 0, j = 1, rem;

//Inserting the binary number from the user

printf("Enter a binary number: ");


scanf("%ld", &binary_num);

// while loop for number conversion

while(binary_num != 0)
{

rem = binary_num % 10;


octal_num = octal_num + rem * j;
//j*=2
j = j * 2;
//binary_num/10;
binary_num = binary_num / 10;

printf("Equivalent octal value: %ld", octal_num);

return 0;
}
* C program to accept a decimal number and convert it to binary
* and count the number of 1's in the binary number
*/

#include<stdio.h>
int main()
{
//for initialize a variables
long number, dec_num, rem, base = 1, bin = 0, count = 0;
//To insert a number
printf("Insert a decimal num \n");
scanf("%ld", &number);
dec_num = number;
while(number > 0)
{
rem = number % 2;
/* To count no.of 1s */
if (rem == 1)
{
count++;
}

bin = bin + rem * base;


//number/=2;
number = number / 2;
base = base * 10;
}
//display
printf("Input num is = %d\n", dec_num);
printf("Its binary equivalent is = %ld\n", bin);
printf("Num of 1's in the binary num is = %d\n", count);
return 0;
}
//Program to convert Decimal number into octal number
#include<stdio.h>

int main()
{
//Variable initialization
long dec_num, rem, quotient;
int i, j, octalno[100];
//Taking input from user
printf("Enter a number for conversion: ");
//Storing the value in dec_num variable
scanf("%ld",&dec_num);
quotient = dec_num;
i=1;
//Storing the octal value in octalno[] array
while (quotient!=0)
{
octalno[i++]=quotient%8;
quotient=quotient/8;
}
//Printing the octalno [] in reverse order
printf("\nThe Octal of %ld is:\n\n",dec_num);
for (j=i-1;j>0;j--)
//display it
printf ("%d", octalno[j]);
return 0;
}
* C Program to Convert Octal to Binary number
*/
#include<stdio.h>
#include<conio.h>
#define MAX 1000

int main()
{
char octalnum[MAX];
//For initialize
long i = 0;
//taking user input of octalnum
printf("Insert an octal number: ");
scanf("%s", octalnum);
printf("Equivalent binary number: ");
while (octalnum[i])
{
//use switch case for multiple condition
switch (octalnum[i])
{
case '0':
printf("000"); break;
case '1':
printf("001"); break;
case '2':
printf("010"); break;
case '3':
printf("011"); break;
case '4':
printf("100"); break;
case '5':
printf("101"); break;
case '6':
printf("110"); break;
case '7':
printf("111"); break;
//for invalid case
default:
printf("\n Invalid octal digit %c ", octalnum[i]);
return 0;
}
i++;
}
return 0;
}

Octal to decimal
#include<stdio.h>
#include<math.h>

int main()
{
//Variable Initialization
long int oct, dec = 0;
int i = 0;
//Taking input from user
printf("Enter an octal number: ");
scanf("%ld", &oct);
//Conversion of octal to decimal
while(oct != 0)
{
dec = dec +(oct % 10)* pow(8, i++);
//oct/=10;
oct = oct / 10;
}
//display
printf("Equivalent decimal number: %ld",dec);
return 0;
}
Quadrant in which co ordinates lie

#include<stdio.h>
int main()
{
//for initialization of coordinates
int x, y; //user input
printf("Insert the value for variable X and Y\n");
scanf("%d %d", &x, &y);
//find true condition of first quadrant
if (x > 0 && y > 0)
printf("point (%d, %d) lies in the First quadrant\n",x,y);
//find second quadrant
else if (x < 0 && y > 0)
printf("point (%d, %d) lies in the Second quadrant\n",x,y);
//To find third quadrant
else if (x < 0 && y < 0)
printf("point (%d, %d) lies in the Third quadrant\n",x,y);
//To find Fourth quadrant
else if (x > 0 && y < 0)
printf("point (%d, %d) lies in the Fourth quandrant\n",x,y);
//To find dose not lie on origin
else if (x == 0 && y == 0)
printf("point (%d, %d) lies at the origin\n",x,y);
return 0;
}
Permutation

#include<stdio.h>

// Program to find the factorial of the number


int factorial (long int x)
{
long int fact=1,i;
for(i=1;i<=x;i++)
{
fact=fact*i;
}
return fact;
}
int main()
{
long int n,r,permutation,temp;
long int numerator, denominator;

// Insert the num of people

printf("\nEnter the number of persons : ");

scanf("%ld",&n);

// Insert the num of seats

printf("\nEnter the number of seats available : ");

scanf("%ld",&r);
// Base condition
// Swap n and r when n is less than r

if(n < r)
{
temp=n;
n=r;
r=temp;
}
numerator=fact(n);
denominator=fact(n-r);
permutation=numerator/denominator;
printf("\nNum of ways people can be seated : ");
printf("%ld\n",permutation);
}

// C program to find the maximum number of handshakesM


#include<stdio.h>
int main()
{
//fill the code
int num;
scanf("%d",&num);
int total = num * (num-1) / 2; // Combination nC2
printf("%d",total);
return 0;
}
Handshake Find nC2, and calculate as n * (n – 1) / 2.
To find the sum of two fractions we will be using the concept of LCM and GCD.

For example: we have to find the sum of 6/2 and 16/3

Firstly the LCM of 2 and 3 will be found. Using the LCM we will convert the numerators
i.e. 6 and 16 into digits that can be added and sum of those digits is found, lastly
normalization is done using the GCD of sum and LCM.

#include <stdio.h>
int main()
{
//for initialize variables
int numerator1, denominator1,numerator2,denominator2,x,y,c,gcd_no;
//To take user input of numerators and denominators
printf("\nEnter the numerator for 1st number : ");
scanf("%d",&numerator1);
printf("\nEnter the denominator for 1st number : ");
scanf("%d",&denominator1);
printf("\nEnter the numerator for 2nd number : ");
scanf("%d",&numerator2);
printf("\nEnter the denominator for 2nd number : ");
scanf("%d",&denominator2);
//numerator
x=(numerator1*denominator2)+(denominator1*numerator2);
//denominator
y=denominator1*denominator2;
// Trick part. Reduce it to the simplest form by using gcd.
for(c=1; c <= x && c <= y; ++c)
{
if(x%c==0 && y%c==0)
gcd_no = c;
}
//To display fraction of givien numerators and denominators
printf("\nThe added fraction is %d/%d ",x/gcd_no,y/gcd_no);
printf("\n");
return 0;
}
Output:-
// C program to replace all 0’s with 1 in a given integer
#include
int replace (long int num)
{
// Base case for recursion termination
if (num == 0)
return 0;
// Extract the last digit and change it if needed
int digit = num % 10;
if (digit == 0)
digit = 1;
// Convert remaining digits and append the last digit

return replace(num/10) * 10 + digit;


}
int Convert(long int num)
{
if (num == 0)
return 1;
else
return replace(num);
}
int main()
{
long int num;
//To take user input
printf("\nInsert the num : ");
scanf("%d", &number);
//display final result
printf("\n Num after replacement : %d\n", Convert (num));
return 0;
}
Output
Insert the num: 80150

Num after replacement = 81151

// C program to check whether a number can be expressed as a sum of two prime


numbers

#include<stdio.h>
int sum_of_two_primes(int n);
int main()
{
int n, i;

printf(“Insert the num: “);


scanf(“%d”, &n);
int flag = 0;
for(i = 2; i <= n/2; ++i)
{
// Condition for i to be prime
if(sum_of_two_primes(i) == 1)
{
if(sum_of_two_primes(n-i) == 1)
{
printf(“\n%d can be expressed as the sum of %d and %d\n\n”, n,
i, n – i);
flag = 1;
}

}
}

if(flag == 0)
printf(“%d cannot be expressed as the sum of two primes\n”, n)

return 0;
}

int sum_of_two_primes(int n)
{
int i, isPrime = 1;
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
{
isPrime = 0;
break;
}
}
return isPrime;
}

C program to check whether a character is a vowel or consonant:-


#include <stdio.h>
int main()
{
char c;
int isLowerVowel, isUpperVowel;
printf("Enter an alphabet: ");
scanf("%c",&c);

//To find the corrector is lowercase vowel


isLowerVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
//To find the character is Upper case vowel
isUpperVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// compare to charector is Lowercase Vowel or Upper case Vowel

if (isLowerVowel || isUpperVowel)
printf("%c is a vowel", c);
//to check character is alphabet or not

elseif((c >= 'a' && c= 'A' && c <= 'Z'))


prinf("\n not a alphabet\n");

else
printf("%c is a consonant", c);

return 0;
}

//C Program to find character is alphabet or not


#include<stdio.h>
#include<conio.h>
int main()
{
char a;

//Requesting user to insert the character


printf("Insert any character: ");

//keeping the inserted character into the variable a


scanf("%c",&a);

if( (ch>='a' && ch<='z') || (ch>='A' && ch<=Z'))


printf("The inserted character %c is an Alphabet”, a);

else
printf("The entered character %c is not an Alphabet”, a);
return 0;
}

Area of circle
#include<stdio.h>
int main()
{

//for initialization of radius and area in a float datatype


float radius,area,pi=3.14;

// for use user input


printf("Enter the Radius of a Circle : ");
scanf("%f",&radius);

//formula of area of circle

area = pi*radius*radius;

printf("Area of Circle is: %f",area);


return 0;
}

/* C Program to identify ASCII Value of a Character */


#include<stdio.h>
#include<conio.h>
int main()
{
char a;

printf("\n Kindly insert any character \n");


scanf("%c",&a);

printf("\n The ASCII value of inserted character = %d",a);


return 0;
}

//C Code to print prime number 1 to 100


#include <stdio.h>

//Main function
int main()
{
int i,j,count=0;

//print values between 1 to 100


printf("The value between 1 to 100\n");

for(i=2;i<=100;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%d",i);
count=0;
}
}

Number of digits in integer


#include <stdio.h>

int main()
{
//to initialize of variable
int count=0;
int n,c;

//to take user input


printf("enter the number: ");
scanf("%d",&n);
c=n;

//when number is zero


if(n==0)
printf("digit is 1");
//false condition

else
{
while(n!=0)
{
//find last digit of number
n=n/10;
//count of a number
++count;
}
printf("the total digit in giving number %d is : %d",c,count);
}
return 0;
}

//C++ program
//convert number to text
#include<iostream>
#include<string.h>
using namespace std;
//main Program
void numToWords(string num)
{
int length_of_string = strlen(num);
if (length_of_string == 0){
cout<<“String is Empty”;
return;
}

if (length_of_string > 4){


cout<<“Please enter the string with supported length”;
return;
}
string ones_digits = {“zero”, “one”, “two”, “three”, “four”, “five”, “si
x”, “seven”, “eight”, “nine”};
string tens_digits = {“”, “ten”, “eleven”, “twelve”, “thirteen”, “fourte
en”, “fifteen”, “sixteen”, “seventeen”, “eighteen”,“nineteen”};
string multiple_of_ten = {“”, “”, “twenty”, “thirty”, “forty”, “fifty”,
“sixty”, “seventy”, “eighty”, “ninety”};
string power_of_ten = {“hundred”, “thousand”};
cout<<num<<“:\n“;
if (length_of_string == 1){
cout<<ones_digits[num[0] – ‘0’];
return;
}
int x=0;
while (x < strlen(num)){
if(length_of_string >= 3){
if (num[x] – 48 != 0){
cout<<ones_digits[num[x] – 48]<<“\n“;
cout<<power_of_ten[length_of_string – 3]<<“\n“;
length_of_string–;
}
}
else{
if (num[x] – 48 == 1){
sum = (num[x] – 48 + num[x] – 48);
cout<<tens_digits[sum]);
return;
}
else if(num[x] – 48 == 2 and num[x + 1] – 48 == 0){
cout<<“twenty”;
return;
}
else{
int i = num[x] – 48;
if(i > 0){
print(multiple_of_ten[i], end = ” “);
}
else{
print(“”, end = “”);
}
x += 1;
if(num[x] – 48 != 0){
cout<<ones_digits[num[x] – 48];
}
}
}
x++;
}
}
int main()
{
numToWords(“1121”);
return 0;
}

//C++ program
//to display number of days in a month
#include<iostream>
using namespace std;
//main Program
int main()
{
//take user inputs for Month and year in integer
int Month,Year;
cout<<“\nEnter the Month :”;
cin>>Month;
cout<<“\nEnter the Year :”;
cin>>Year;
//Check condition for Month and leap year
if(Month == 2 && (Year%4 == 0) || ((Year%100 == 0) && (Year%400 == 0)))
cout<<“Number of days is 29”;
else if(Month == 2)
cout<<“Number of days is 28”;
else if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month ==
8 || Month == 10 || Month == 12)
cout<<“Number of days is 31”;
else
cout<<“Number of days is 30”;
}
Number of times the digit x occurs in the given number
Enter a number : 897982
Enter the digit : 9

Output : 2
#include <bits/stdc++.h>

using namespace std;

int main()
{
//Declare variables

int n; //given integer


int d; //given digit
int count=0; //declare counter variable

cin >> n >> d; // take input from user

while(n)
{

int k = n%10; // to store the digits of the given input

n=n/10;

if(k==d) // compare the given digit with digit of input


{
count++; // increment counter variable
}

cout << count; // display count of digits

return 0;
}

Roots of quadratic equation


#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, d, root1, root2, r, i;
printf(“Enter value of a, b and c: “);
scanf(“%lf %lf %lf”, &a, &b, &c);

d = b * b – 4 * a * c;

// condition for real and different roots


if (d > 0) {
printf(“Two Real Roots\n“);
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b – sqrt(d)) / (2 * a);
printf(“root1 = %.2lf \nroot2 = %.2lf”, root1, root2);
}

// condition for real and equal roots


else if (d == 0) {
printf(“Equal Roots\n“);
root1 = root2 = –b / (2 * a);
printf(“root1 = root2 = %.2lf;”, root1);
}

// if roots are not real


else {
r = –b / (2 * a);
i = sqrt(-d) / (2 * a);
printf(“No Real Roots\n“);
printf(“root1 = %.2lf+%.2lfi \nroot2 = %.2f-%.2fi”, r, i, r, i);
}

return 0;
}

C Program to find lcm of 3 numbers


[code language=”cpp”]
#include

int lcm(int,int);
int main(){
int a,b,c,l,k;
printf(“Enter any three positive integers “);
scanf(“%d%d%d”,&a,&b,&c);
if(a<b) l = lcm(a,b); else l = lcm(b,a); if(l>c)

k= lcm(l,c);

else

k= lcm(c,l);

printf(“LCM of two integers is %d”,k);

return 0;

int lcm(int a,int b){

int temp = a;

while(1){

if(temp % b == 0 && temp % a == 0)

break;

temp++;

return temp;
}

1. // GCD of 3 numbers
2.
3. #include <stdio.h>
4.
5. // to determine where to start gcd counting from
6. int greatest(int a, int b, int c){
7. if(a>=b && a>=c){
8. return a;
9. }
10.
11. else if(b>=a && b>=c){
12. return b;
13. }
14.
15. else if(c>=a && c>=b){
16. return c;
17. }
18. }
19.
20. void main(){
21.
22. int x,y,z;
23. printf("Enter 3 numbers: ");
24. scanf("%d %d %d",&x,&y,&z);
25. int gcd;
26.
27. for(gcd=greatest(x,y,z); gcd>=1; gcd--){
28. if(x%gcd==0 && y%gcd==0 && z%gcd==0){
29. break;
30. }
31. }
32. printf("GCD is : %d\n",gcd);
33.
34. }
ARRAYS

//C Code to find smallest element in an array


#include<stdio.h>
int main()
{
int a[50], p, num, smallest;
printf("Enter the size of array :"); // Take size of array
scanf("%d", &num);
printf("Enter the elements of array : \n");
for (p = 0; p < num; p++) //Read elements of an array
scanf("%d", &a[p]);
smallest = a[0]; //Let first element as smallest
for (p = 0; p < num; p++)
{
if (a[p] < smallest)
{
smallest = a[p];
}
}
printf("Smallest Element of an array : %d", smallest); // Print
smallest element
return (0);
}

//C++ program
//program for finding the second smallest element in an array
#include<iostream>
using namespace std;
int prep(int a[], int total)
{
int temp;
for (int i = 0; i < total–1; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i=1;i<total;i++)
{
if(a[i]!=a[0])
return a[i];
}
return –1 ; //all elements are equal
}
int main()
{
int a[]={7,8,9,5,2};
cout<<“Second smallest: “<<prep(a,5));
return 0;
}

Largest element of array


#include <stdio.h>
int main()
{
int size, i, largest_ele;
printf("\n Enter the size of the array: "); //taking size of array
scanf("%d", &size);

int array[size];
printf("\n Enter %d elements of the array: \n", size); //print size
and take input from user
for (i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
largest_ele = array[0];
for (i = 1; i < size; i++) //loop for checking largest element
{
if (largest_ele < array[i])
largest_ele = array[i];
}
printf("\n largest element in array is : %d", largest_ele); //print
largest element in array
return 0;
}

//C Program to Find Largest and Smallest Element in an Array


#include <stdio.h>
int main()
{
int a[50], size, i, large, small;

printf("Enter the size of the array: ");


scanf("%d", &size);

printf("Enter the %d elements of the array:\n", size);


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

large = a[0];
for(i = 1; i < size; i++)
{
if(large < a[i]) // if larger value is encountered
{
large = a[i]; // update the value of large
}
}
printf("The largest element is: %d",large);

small = a[0];
for(i = 1; i < size; i++) { if(small>a[i]) // if smaller value is
encountered
{
small = a[i]; // update the value of small
}
}
printf("\nThe smallest element is: %d", small);
return 0;
}

//C Program to find Sum of Elements in an Array


#include
#define ARR_SIZE 100
int main()
{
int arr[ARR_SIZE];
int i,m,size,sum=0;
printf("Enter size of the array: ");// Input size of the array
scanf("%d", &m);

printf("Enter %d elements in the array: ",m);// Input elements in array


for(i=0; i<m; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<m; i++) //Add each elements to sum array
{
sum = sum + arr[i];
}
printf("Sum of elements in an array = %d", sum);
return 0;
}

// C code to Reverse of an array


#include
int main()
{
int n, p, q, a[100], b[100];
printf("Enter the number of elements in an array:\n"); //input size
of array
scanf("%d", &n);

printf("Enter the elements of an array:\n"); //print elements of


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

for (p = n - 1, p = 0; p >= 0; p--, q++)


b[q] = a[p];

for (p = 0; p < n; p++) //Reverse of digit


a[p] = b[p];
printf("Reverse of an array is:\n");

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


printf("%d\n", a[p]); //print Reverse of an array

return 0;
}

 In this problem, we are required to display the first half of the array in ascending order and
the remaining half in descending order.

#include <stdio.h>

int main()
{
int a[100];
int n, i, j, temp;

printf("Enter the size of list : ");


scanf("%d",&n);

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


{
scanf("%d",&a[i]);
}

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


{
for(j=0; j<n/2; j++)
{
if(a[i]<a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

for(i=n/2; i<n; i++)


{
for(j=n/2; j<n; j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

printf("First Half in Ascending order : ");


for(i=0; i<n/2; i++)
{
printf("%d ",a[i]);
}
printf("\nSecond Half in Descending order : ");
for(i=n/2; i<n; i++)
{
printf("%d ",a[i]);
}

return 0;
}

//C Program to sort an array in ascending or descending order


#include <stdio.h>
void main()
{
int i,j,n,a[100],temp,p,q,temp1;
printf("Enter the size of array : \n") ; //Taking size of array
scanf("%d",&n) ;
printf("Enter the elements : \n") ; //Taking input from user
for(i=0;i<n;i++)
{
scanf("%d",&a[i]) ;
}
for(i=0;i<n;i++) // loop for sorting array in ascending order
{
for(j=i+1;j<n;j++) { if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Ascending order of an array : \n"); //print ascending order
for(i=0;i<n;i++)
{
printf("%d ",a[i]) ;
}

for(p=0;p<n;p++) // loop for sorting array in descending order


{
for(q=p+1;q<n;q++)
{
if(a[p]<a[q])
{
temp1=a[p];
a[p]=a[q];
a[q]=temp1;
}
}
}
printf("\n Descending order of an array : \n"); // print descending order
for(p=0;p<n;p++)
{
printf("%d ",a[p]) ;
}
}

Distinct elements of array


import java.util.Scanner;
public class Main
{
public static int DCount(int a[],int n)
{
int res=0;

for(int i=0;i<n;i++)
{
int j;

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

if (a[i] == a[j])
break;

if (i == j)
{
res++;
}
}
System.out.println();
return res;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int[] a=new int[50];
System.out.println( "enter size of an array");
int size=sc.nextInt();
System.out.println( "enter elements of an array");

for(int i=0;i<size;i++)
{
a[i]=sc.nextInt();
}

System.out.println( "total distinct number of element


"+DCount(a,size));
}
}

Pyramid Star Pattern

#include<stdio.h>
int main()
{
int i, j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}

Output:
*
* *
* * *
* * * *
* * * * *

2. Write a program to print the following pattern


*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include<stdio.h>
int main()
{
int i, j, k=1;
for(i=1; i<=5; i++)
{
for(j=1; j<=k; j++)
{
printf("* ");
}
printf("\n");
k = k + 2;
}
return 0;
}

Write a program to print the following pattern


*
* *
* * *
* * * *
* * * * *

#include <stdio.h>
int main()
{
int n,m=1;
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i-1;j++)
{
printf(" ");
}
for(int k=1;k<=m;k++)
{
printf("*");
}
printf("\n");
m++;
}
return 0;
}

4. Write the code to print the following patter


*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

#include <stdio.h>
int main()
{
int i,j,k=16,s=1;
for(i=0;i<5;i++)
{
for(j=0;j<k;j++)
{
printf(" ");
}
k=k-4;
for(j=1;j<=s;j++)
{
printf("* ");
}
s=s+2;
printf("\n");
}
return 0;
}

5. Write the code to print the following pattern.


*
***
*****
*******
*********

#include <stdio.h>
int main()
{
int i,j,k=0,row=5;
//this loop work on the row
for (i=1;i<=row;i++)
{
//this loop work for both the space and print *
for(j=1;j<=row-i;j++)
printf(" ");
//until value of the k is not equal to 2*i-i, it print star
while(k!=(2*i-1))
{
printf("*");
k++;
}
k=0;
printf("\n");
}
return 0;
}

1. Write the Program to print the following pattern


Enter the row 5

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include <stdio.h>
int main()
{
int i, j, n;
printf("Enter number of rows : ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
2. Write a program to print the following pattern
Enter number 5

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

#include <stdio.h>
int main()
{
int i, j, n;
printf("Enter number of rows : ");
scanf("%d",&n);
for(i=n; i>=1; i--)
{
for(j=1; j<=i; j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
3. Write a program to print the following pattern
Enter the row 5

1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

#include <stdio.h>
int main()
{
int n, i, j, k=0, s, count=0, count1=0;
printf("Enter number of rows : ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
for(s=1; s<=n-i; s++)
{
printf(" ");
count++;
}
while(k!=2*i-1)
{
if(count<=n-1)
{
printf("%d ",k+i);
count++;
}
else
{
count1++;
printf("%d ",i+k-2*count1);
}
k++;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}

1. Write a program to print the following pattern.


Enter the row 5

1
121
12321
1234321
123454321

#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter number of rows : ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
printf(" ");
}
for(j=1; j<=i; j++)
{
printf("%d",j);
}
for(j=i-1; j>=1; j--)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
2. Write a program to print the following pattern
Enter the row
5

ABA

ABCBA

ABCDCBA

ABCDEDCBA
#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter number of rows : ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
printf(" ");
}
for(j=1; j<=i; j++)
{
printf("%c",j+64);
}
for(j=i-1; j>=1; j--)
{
printf("%c",j+64);
}
printf("\n");
}
return 0;
}

Write a program to print the following pattern


Enter the number of row 5

*
***
*****
*******
*********
*******
*****
***
*

#include <stdio.h>
int main()
{
int n, i, j;
printf(“Enter number of rows : “);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
for(j=1;j<=n–i;j++)
{
printf(” “);
}
for(j=1;j<=i*2–1;j++)
{
printf(“*”);
}
printf(“\n“);
}
for(i=n–1;i>0;i–)
{
for(j=1;j<=n–i;j++)
{
printf(” “);
}
for(j=1;j<=i*2–1;j++)
{
printf(“*”);
}
printf(“\n“);
}
return 0;
}

1. Write a program to print the following pattern


Enter the number of row 5

1
123
12345
1234567
123456789
1234567
12345
123
1

#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter number of rows : ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i*2-1;j++)
{
printf("%d",j);
}
printf("\n");
}
for(i=n-1;i>0;i--)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i*2-1;j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
2. Write a program to print the following pattern
Enter the number of row 5
1
222
33333
4444444
555555555
4444444
33333
222
1

#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter number of rows : ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i*2-1;j++)
{
printf("%d",i);
}
printf("\n");
}
for(i=n-1;i>0;i--)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i*2-1;j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}

1. Write a program to print the following pattern


Enter number of row 5

2 3

4 5 6

7 8 9 10

11 12 13 14 15

#include <stdio.h>
int main()
{
int i,j,n,k=1;
printf("enter number of rows : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",k);
k++;
}
printf("\n");
}
return 0;
}

1. Write a program to print the following pattern


Enter number of row 5

2 3

4 5 6

7 8 9 10

11 12 13 14 15

#include <stdio.h>
int main()
{
int i,j,n,k=1;
printf("enter number of rows : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",k);
k++;
}
printf("\n");
}
return 0;
}

Write a program to print the following pattern


Enter the number of row 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

#include <stdio.h>
int main()
{
int row,c=1,k,i,j;
printf("Enter the number of row ");
scanf("%d",&row);
for(i=0;i<row;i++)
{
for(k=1;k<=row-i;k++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("%d ",c);
}
printf("\n");
}
return 0;
}

Array similarity

#include <stdio.h>
int main()
{
int n1, n2, i , j, count = 0;
printf("enter size of array 1 : ");
scanf("%d",&n1);
int arr1[n1];
printf("enter elements of array 1 : ");
for(i=0; i<n1; i++)
{
scanf("%d",&arr1[i]);
}
printf("enter size of array 2 : ");
scanf("%d",&n2);
int arr2[n2];
printf("enter elements of array 2 : ");
for(i=0; i<n2; i++)
{
scanf("%d",&arr2[i]);
}
for(i=0; i<n1; i++)
{
for(j=0; j<n2; j++)
{
if(arr1[i]==arr2[j])
{
count++;
break;
}
}
}
if((count==n1)&&(count==n2))
{
printf("Arrays are same");
}
else
{
printf("Arrays are not same");
}
return 0;
}

enter size of array 1

enter element

13

enter size of array2

enter element

13

Both array is same

Sum of positive square elements of the array


Here we will learn about Sum of positive square elements of array. For this program, we need to
find the square of every element and add all these square elements.

Example
Input: 12345

Square: 1 4 9 16 25

sum: 1+4+9+16+25=55

#include <stdio.h>
int main()
{
int n, i , j, sum = 0;
printf("enter size : ");
scanf("%d",&n);
int arr[n];
printf("enter elements : ");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
for(i=0; i<n; i++)
{
sum = sum + (arr[i]*arr[i]);
}
printf("Sum is : %d",sum);
return 0;
}

#include <stdio.h>
int main()
{
int n, i , j, temp, rem, num;
printf("enter size of both array : ");
scanf("%d",&n);
int arr1[n];
printf("enter elements of array 1 : ");
for(i=0; i<n; i++)
{
scanf("%d",&arr1[i]);
}
//sorting of array in ascending order
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[i]>arr1[j])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
for(i=n; i>=0; i--)
{
num = 0;
temp = arr1[i];
while(arr1[i]!=0)
{
rem = arr1[i] % 10;
num = num*10 + rem;
arr1[i] = arr1[i]/10;
}
if(num==temp)
{
printf("%d",num);
break;
}
}
return 0;
}
Output
enter size
5
enter element
12
3
11
212
345
Longest palindrome number in the array 212
STRINGS

Length of string

#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 programming code to toggle each character in a string.


#include <stdio.h>
#include <string.h>

int main()
{
//Initializing variable.
char str[100];
int i;

//Accepting input.
printf("\n Please Enter any String: ");
gets(str);

//Initializing for loop.


for (i = 0; str[i]!='\0'; i++)
{
//Toggling characters.
if(str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32;
}
else if(str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}

printf("\n Toggoled string: %s", str);//Print toggled string.

return 0;
}
Number of vowels in a string

#include <stdio.h>
#include <string.h>

int main()
{
//Initializing variable.
char str[100];
int i,vowels=0;

//Accepting input.
printf(" Enter the string : ");
gets(str);

//Initializing for loop.


for(i=0;str[i];i++)
{
//Counting the vowels.
if(str[i]=='a'||
str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='
E'||str[i]=='I'||str[i]=='O' ||str[i]=='U')
{
vowels++;
}
}

//Printing the count of vowels.


printf(" Total number of vowels in the string = %d\n",vowels);

return 0;
}

Remove the vowels of string

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

//Accepting input.
printf("Enter a string : ");
gets(str);
len=strlen(str);

//Accepting input.
for(i=0; i<len; i++)
{
//Checking vowels.

if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'|
|str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
{

//Deleting vowels.
for(j=i; j<len; j++)
{
//Storing string without vowels.
str[j]=str[j+1];
}
len--;
}
}
printf("After deleting the vowels, the string will be : %s",str);

return 0;
}

Check if string is palindrome or not

#include <stdio.h>
#include <string.h>

int main()
{
//Initializing variable.
char str[100];
int i,length=0,flag=0;

//Accepting input.
printf("Enter the string : ");
gets(str);
length=strlen(str);

//Initializing for loop.


for(i=0;i<length/2;i++)
{
//Checking if string is palindrome or not.
if(str[i]==str[length-i-1])
flag++;

}
//Printing result.
if(flag==i)
printf("String entered is palindrome");
else
printf("String entered is not palindrome");

return 0;
}

Print given string in reverse order

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

//Accepting input.
printf(" Enter a string: ");
gets(str);
//Calculating length.
len = strlen(str);

//Reversing and printing the string using for loop.


printf("The reverse string is: ");
for(i = len - 1; i >= 0; i--)
{
printf("%c", str[i]);
}

return 0;
}

Remove all characters except alphabets

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

//Accepting input.
printf(" Enter a string : ");
gets(str);

//Iterating each character and removing non alphabetical characters.


for(i = 0; str[i] != '\0'; ++i)
{
while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i]
<= 'Z') || str[i] == '\0') )
{
for(j = i; str[j] != '\0'; ++j)
{
str[j] = str[j+1];
}
str[j] = '\0';
}
}
//Printing output.
printf(" After removing non alphabetical characters the string is :");
puts(str);
return 0;
}
Output:
Enter a string : *1prep_insta*
After removing non alphabetical characters the string is :prepinsta

Remove spaces from string

#include <stdio.h>
int main()
{

//Initializing variables.
char str[100], str_no_spc[100];
int i=0, j=0 ;

//Accepting inputs.
printf("Enter the string:\n");
gets(str);

//Iterating each character of string.


while(str[i] != '\0')
{
if(str[i] != ' ')
{

str_no_spc[j++] = str[i];
}

i++;
}
str_no_spc[j] = '\0';
//Printing result.
printf("The string after removing all the spaces is:\n%s", str_no_spc);

return 0;
}
Output:
Enter the string
PREP INSTA
The string after removing all the spaces is:
PREPINSTA

Remove brackets from algebraic equation

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

//Accepting inputs.
printf("Enter the string:\n");
gets(str);

//Iterating each character of string.


while(str[i] != '\0')
{
if(str[i] != '(' && str[i] != ')')//Excluding brackets.
{
str_no_spc[j++] = str[i];
}
i++;
}
str_no_spc[j] = '\0';

//Printing result.
printf("The string after removing all the spaces is:\n%s", str_no_spc);
return 0;
}

Sum of numbers in a string

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

//Accepting inputs.
printf("Enter the string:\n");
gets(str);

//Iterating each character through for loop.


for (i= 0; str[i] != '\0'; i++)
{
if ((str[i] >= '0') && (str[i] <= '9')) //Checking for numeric
characters.
{

sum += (str[i] - '0'); //Adding numeric characters.

}
}
//Printing result.
printf("Sum of all digits:\n%d", sum);
return 0;
}
Output:
Enter the string:
4PREP2INSTA6
Sum of all digits:
12

Capitalize first and last character of each word

#include <stdio.h>
#include <string.h>

int main()
{

//Initializing variables.
char str[100];
int length = 0;

//Accepting inputs.
printf("Enter a string:\n");
gets(str);

//Calculating length.
length = strlen(str);

for(int i=0;i<length;i++)
{
if(i==0||i==(length-1)) //Conerting character at first and last index to
uppercase.
{
str[i]=toupper(str[i]);
}
else if(str[i]==' ')//Conerting characters present before and after space
to uppercase.
{
str[i-1]=toupper(str[i-1]);
str[i+1]=toupper(str[i+1]);

}
}

//Printing result.
printf("String after capitalizing first and last letter of each word:\n%s",
str);

return 0;
}

Frequency of string

#include <stdio.h>

int main()
{
//Initializing variables.
char str[100];
int i;
int freq[256] = {0};

//Accepting inputs.
printf("Enter the string: ");
gets(str);

//Calculating frequency of each character.


for(i = 0; str[i] != '\0'; i++)
{
freq[str[i]]++;
}

//Printing frequency of each character.


for(i = 0; i < 256; i++)
{
if(freq[i] != 0)
{
printf("The frequency of %c is %d\n", i, freq[i]);
}
}
return 0;
}
Output:
Enter the string: prepinsta
The frequency of a is 1
The frequency of e is 1
The frequency of i is 1
The frequency of n is 1
The frequency of p is 2
The frequency of r is 1
The frequency of s is 1
The frequency of t is 1

Find non repeating characters in string

#include <stdio.h>

int main()
{
//Initializing variables.
char str[100];
int i;
int freq[256] = {0};

//Accepting inputs.
printf("Enter the string: ");
gets(str);

//Calculating frequency of each character.


for(i = 0; str[i] != '\0'; i++)
{
freq[str[i]]++;
}

printf("The non repeating characters are: ");


for(i = 0; i < 256; i++)
{
if(freq[i] == 1)//Finding uniques charcters and printing them.
{
printf(" %c ", i);
}
}
return 0;
}

C Code for replacing a substring in a string


#include <stdio.h>
#include <string.h>
int main() {
char str[256], substr[128], replace[128], output[256];
int i = 0, j = 0, flag = 0, start = 0;
//Accepts all the inputs
printf("Enter your input string:\n");
gets(str);
printf("Enter the string to be removed:\n");
gets(substr);

printf("Enter the string to replace:\n");


gets(replace);
str[strlen(str) - 1] = '\0';
substr[strlen(substr) - 1] = '\0';
replace[strlen(replace) - 1] = '\0';

// check whether the substring to be replaced is present


while (str[i] != '\0')
{
if (str[i] == substr[j])
{
if (!flag)
start = i;
j++;
if (substr[j] == '\0')
break;
flag = 1;
}
else
{
flag = start = j = 0;
}
i++;
}
if (substr[j] == '\0' && flag)
{
for (i = 0; i < start; i++)
output[i] = str[i];
// replace substring with another string
for (j = 0; j < strlen(replace); j++)
{
output[i] = replace[j];
i++;
}

// copy remaining portion of the input string "str"


for (j = start + strlen(substr); j < strlen(str); j++)
{
output[i] = str[j];
i++;
}

// print the final string


output[i] = '\0';
printf("Output: %s\n", output);
} else {
printf("%s is not a substring of %s\n", substr, str);
}
return 0;
}
Output
Enter your input string:
prepinsta
Enter the string to be removed:
insta
Enter the string to replace:
ster
Output:
prepster

C programming code to count common subsequence in two strings


Subsequence are the part of sequence (‘a’, ‘b’, ‘ab’ are the subsequence of ‘ab’). So we will
be taking two string as an input here and then we will find out the subsequence of
each string and then we will count the common subsequence and will print this count
as an output.

#include <stdio.h>
#include <string.h>

//Function to count the number of subsequences in the string.


int countsequences(char str[], char str2[])
{
int n1 = strlen(str);
int n2 = strlen(str2);
int cnt[n1+1][n2+1];
for (int i = 0; i <= n1; i++)
{
for (int j = 0; j <= n2; j++)
{
cnt[i][j] = 0;
}
}
//Taking each character from first string.
for (int i = 1; i <= n1; i++)
{
//Taking each character from second string.
for (int j = 1; j <= n2; j++)
{
//If characters are same in both the string.

if(str[i-1] == str2[j-1])
{
cnt[i][j] = 1 + cnt[i][j-1] + cnt[i-1][j];
}
else
{
cnt[i][j] = cnt[i][j-1] + cnt[i-1][j] - cnt[i-1][j-1];
}
}
}
return cnt[n1][n2];
}
//Main function for printing the result.
int main()
{
char str[100] ,str2[100];
printf("Enter the first string: ");
gets(str);
printf("Enter string second: ");
gets(str2);
printf("Number of common subsequence is: %d ",countsequences(str, str2));
return 0;
}
Output:
Enter the first string: abc
Enter string second: bc
Number of common subsequence is: 3

C program to check if two strings are same, if one string contains wild characters
#include <stdio.h>
#include <stdbool.h>
bool check(char *str1, char * str2) ;// declaration of the check() function
int main()
{
char str1[100],str2[100];
printf("Enter first string with wild characters : ");
gets(str1);
printf("Enter second string without wild characters : ");
gets(str2);
test(str1,str2);
return 0;
}

bool check(char *str1, char * str2)


{
// checking end of both the strings
if (*str1 == '\0' && *str2 == '\0')
return true;

// comparing the characters of both the strings and wild characters(*)


if (*str1 == '*' && *(str1+1) != '\0' && *str2 == '\0')
return false;

// checking wild characters(?)


if (*str1 == '?' || *str1 == *str2)
return check(str1+1, str2+1);
if (*str1 == '*')
return check(str1+1, str2) || check(str1, str2+1);
return false;
}

// test() function for running test cases


void test(char *str1, char *str2)
{
check(str1, str2)? puts(" Yes "): puts(" No ");

}
Output
Enter the first string with wild characters : prep***ta
Enter second string without wild characters : prepinsta
yes

You might also like