100 Codes
100 Codes
#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);
else
printf(“The number is odd\n“);
return 0;
}
#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;
}
#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;
//total+=i;
total = total + i;
}
#include<stdio.h>
int main()
{
int no1, no2;
printf(“Insert two numbers:”);
scanf(“%d %d”,&no1, &no2);
return 0;
}
Greatest of three numbers
#include<stdio.h>
int main()
{
int no1,no2,no3;
#include<stdio.h>
int main()
{
//initialization of Year
int year;
}
Prime number
#include <stdio.h>
int main()
{
//initialize of variable
int i, number, fact = 1;
#include <stdio.h>
int main()
{
//initialize of variable
int i, number, fact = 1;
return 0;
}
Factorial of a number
#include <stdio.h>
int main()
{
//initialize of variable
int i, number, fact = 1;
return 0;
}
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;
store= number;
//number /= 10;
number=number/10;
}
//To show the user value
printf("Given number = %d\n",store);
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;
store= number;
//use this loop for check true condition
while (number > 0)
{
//left is for remider are left
left= number%10;
//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
#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;
}
#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);
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;
Power of a number
#include<stdio.h>
int main()
{
//To initialize variables
int number, expo,temp = 1;
Factorial of number
#include <stdio.h>
int main()
{
//initialize of variable
int i, number, fact = 1;
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);
Perfect number
#include<stdio.h>
int main()
{
// Initialization of variables
int number,i=1,total=0;
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 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;
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;
//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;
}
#include<stdio.h>
void lcm_two_no(int,int);
int main()
{
int n1,n2;
// 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);
}
#include<stdio.h>
int main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
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;
while(binary_num != 0)
{
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++;
}
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>
scanf("%ld",&n);
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);
}
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
#include<stdio.h>
int sum_of_two_primes(int n);
int main()
{
int n, i;
}
}
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;
}
if (isLowerVowel || isUpperVowel)
printf("%c is a vowel", c);
//to check character is alphabet or not
else
printf("%c is a consonant", c);
return 0;
}
else
printf("The entered character %c is not an Alphabet”, a);
return 0;
}
Area of circle
#include<stdio.h>
int main()
{
area = pi*radius*radius;
//Main function
int main()
{
int i,j,count=0;
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;
}
}
int main()
{
//to initialize of variable
int count=0;
int n,c;
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;
}
//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>
int main()
{
//Declare variables
while(n)
{
n=n/10;
return 0;
}
d = b * b – 4 * a * c;
return 0;
}
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);
return 0;
int temp = a;
while(1){
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++ 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;
}
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;
}
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;
}
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;
return 0;
}
for(int i=0;i<n;i++)
{
int 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();
}
#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:
*
* *
* * *
* * * *
* * * * *
#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;
}
#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;
}
#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
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
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;
}
*
***
*****
*******
*********
*******
*****
***
*
#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
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;
}
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;
}
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;
}
#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 element
13
enter element
13
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);
return 0;
}
int main()
{
//Initializing variable.
char str[100];
int i;
//Accepting input.
printf("\n Please Enter any String: ");
gets(str);
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);
return 0;
}
#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;
}
#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);
}
//Printing result.
if(flag==i)
printf("String entered is palindrome");
else
printf("String entered is not palindrome");
return 0;
}
#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);
return 0;
}
#include <stdio.h>
int main()
{
//Initializing variable.
char str[100];
int i, j;
//Accepting input.
printf(" Enter a string : ");
gets(str);
#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);
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
#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);
//Printing result.
printf("The string after removing all the spaces is:\n%s", str_no_spc);
return 0;
}
#include <stdio.h>
int main()
{
//Initializing variables.
char str[100];
int i,sum = 0;
//Accepting inputs.
printf("Enter the string:\n");
gets(str);
}
}
//Printing result.
printf("Sum of all digits:\n%d", sum);
return 0;
}
Output:
Enter the string:
4PREP2INSTA6
Sum of all digits:
12
#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);
#include <stdio.h>
int main()
{
//Initializing variables.
char str[100];
int i;
int freq[256] = {0};
//Accepting inputs.
printf("Enter the string: ");
gets(str);
#include <stdio.h>
#include <string.h>
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;
}
}
Output
Enter the first string with wild characters : prep***ta
Enter second string without wild characters : prepinsta
yes