IT Job Exam - Most Important 100 Program Code - Learn easily



The Most Important C Programs for IT job Examination
(C++, Java and PHP programs are also coming soon)


1. Add the numbers of a Array

#include<stdio.h>

int main()

{

    int c,n,arr[100],sum=0;

    printf("Enter total number to add\n");

    scanf("%d",&n);

    printf("Enter the number\n");

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

    {

        scanf("%d",&arr[c]);

        sum=sum+arr[c];

    }

    printf("The Sum is =%d",sum);

    return 0;

}

2. Add the digits of a Number

#include<stdio.h>
int main()
{
    int n,sum=0,t,remainder;
    printf("Enter number\n");
    scanf("%d",&n);
    t=n;
    while(t!=0)
    {
        remainder=t%10;
        sum=sum+remainder;
        t=t/10;
    }
    printf("The Sum is %d",sum);

    return 0;

}

3. Area and Circumference of a Circle

#include<stdio.h>
float PI=3.1416;
int main()
{
    float r,area,circumference;
    printf("Enter radius\n");
    scanf("%f",&r);

    area=PI*r*r;
    printf("Area of a circle is=%.4f\n",area);

    circumference=2*PI*r;
    printf("circumference of a circle is=%.4f\n",circumference);
    return 0;

}

4. Armstrong Number Checking

#include<stdio.h>
int power(int,int);
int main()
{
    int temp,n,total_digits=0,reaminder,sum=0,power_digit,remainder_base;
    printf("Enter the number\n");
    scanf("%d",&n);
    temp=n;
    while(temp !=0)
    {
        total_digits++;//It will calculate total digit entered//printf("%d",total_digits);
        temp=temp/10;
    }
    power_digit=total_digits;
    temp=n;
    while(temp !=0)
    {
        remainder_base=temp%10;
        sum=sum+power(remainder_base,power_digit);
        temp=temp/10;
    }
    if(n==sum)
    {
        printf("%d is a armstrong number\n",n);
    }
    else
    {
        printf("%d is not a armstrong number\n",n);
    }
}

int power(int remainder_base,int power_digit)
{
    int power=1,i;
    for(i=1; i<=power_digit; i++)
    {
        power=power*remainder_base;
    }
    return power;
}

5. ASCII value

#include<stdio.h>
int main()
{
    char c;
    printf("Enter Character\n");
    scanf("%c",&c);

    printf("Ascii vaue of %c is =%d",c,c);
    return 0;
}

6. Binary Searching

#include <stdio.h>

int main()
{
    int c, first, last, middle, n, search, array[100];

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

    printf("Enter %d integers\n", n);

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

    printf("Enter value to find\n");
    scanf("%d", &search);

    first = 0;
    last = n - 1;
    middle = (first+last)/2;

    while (first <= last)
    {
        if (array[middle] < search)
        {
            first = middle + 1;
        }
        else if (array[middle] == search)
        {
            printf("%d found at location %d.\n", search, middle+1);
            break;
        }
        else
        {
            last = middle - 1;
            middle = (first + last)/2;
        }
    }
    if(first > last)
    {
        printf("Not found! %d is not present in the list.\n", search);
    }

    return 0;
}

7. Binary to Decimal

#include<stdio.h>
int convertBinaryToDecimel(long long n);
int main()
{
    long long n,convert;
    printf("Enter binary number\n");
    scanf("%lld",&n);
    convert=convertBinaryToDecimel(n);

    printf("After conversion %lld to decimel:%d\n",n,convert);

    return 0;

}
int convertBinaryToDecimel(long long n)
{
    int remainder,decimel=0,i=0;
    while(n!=0)
    {
        remainder=n%10;
        n=n/10;
        decimel=decimel+ remainder*pow(2,i);
        i++;
    }
    return decimel;

}

7. Bubble Sorting

#include<stdio.h>
int main()
{
    int i,c,d,array[100],n,swap;
    printf("Enter array element\n");
    scanf("%d",&n);

    printf("Enter %d Elements\n",n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    for(c=0; c<n-1; c++)
    {
        for(d=0; d<n-c-1; d++)
        {
            if(array[d]<array[d+1])
            {
                swap=array[d];
                array[d]=array[d+1];
                array[d+1]=swap;
            }
        }
    }
    printf("sorted list in decending order\n");
    for(c=0; c<n; c++)
    {
        printf("%d\n",array[c]);
    }

}

8. Checking a vowel


#include<stdio.h>

int main()

{

    char ch;

    printf("Enter the character\n");

    scanf("%c",&ch);

    if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')

    {

        printf("The %c character is a vowel\n",ch);

    }

    else

    {

        printf("The %c character is not vowel\n",ch);

    }

    return 0;

}

9. Decimal to Binary

#include<stdio.h>
int convertDecimelToBinary(long long n);
int main()
{
    long long n,convert;
    printf("Enter decimel number\n");
    scanf("%lld",&n);
    convert=convertDecimelToBinary(n);

    printf("After conversion %lld to binary:%d\n",n,convert);

    return 0;

}
int convertDecimelToBinary(long long n)
{
    int remainder,binary=0,i=1;
    while(n!=0)
    {
        remainder=n%2;
        n=n/2;
        binary=binary+ remainder*i;
        i*=10;
    }
    return binary;

}


10. Delete Vowel from a String

#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int chk_vowel(char);
int main()
{
    char s[100],d[100],ch;
    int i,j=0;
    printf("Enter String\n");
    gets(s);

    for(i=0; s[i] !='\0'; i++)
    {
        if(chk_vowel(s[i])==0)
        {
            d[j]=s[i];
            j++;
        }
    }
    d[j]='\0';
    strcpy(s,d);
    printf("After deletion vowel we get: %s\n ",s);

    return 0;

}

int chk_vowel(char ch)
{
    if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

11. Delete Number from an Array

#include<stdio.h>

int main()
{
    int arr[100],i,n,element,location;

    printf("Enter number of array element\n");
    scanf("%d",&n);

    printf("Enter array element\n");
    for(i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
    }


    printf("Enter the location where number will be deleted\n");
    scanf("%d",&location);

    while (location < n)
    {
        arr[location - 1] = arr[location];
        location++;
    }

    n--;
    printf("After insertion\n");
    for(i=0; i<n; i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}

12. Checking Even and ODD

#include<stdio.h>
int main()
{
    int n;
    printf("Enter the number\n");
    scanf("%d",&n);
    if(n%2==0)
    {
        printf("The %d number is even.",n);
    }
    else
    {
        printf("The %d number is odd.",n);
    }
    return 0;
}

13. Factorial Using for loop

#include<stdio.h>
int main()
{
    int n,fact=1,i;
    printf("Enter the number to calculate factorial\n");
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        fact=fact*i;
    }

    printf("The factorial of %d is= %d",n,fact);
    return 0;
}

14. Factorial using Functions

#include<stdio.h>
long factorial(int);
int main()
{
    int n;
    printf("Enter the number to calculate factorial\n");
    scanf("%d",&n);
    if(n<0)
    {
        printf("Negative integer are not allowed\n");
    }
    else
    {
        printf("The factorial of %d is= %d",n,factorial(n));
    }
    return 0;
}
long factorial(int n)
{
    long fact=1;
    int i;
    if(n==0)
    {
        return 1;
    }
    else
    {
        for(i=1; i<=n; i++)
        {
            fact=fact*i;
        }
        return fact;
    }
}

15. Factorial using Recursive Functions 

#include<stdio.h>
long factorial(int);
int main()
{
    int n;
    long f;
    printf("Enter the number to calculate factorial\n");
    scanf("%d",&n);
    if(n<0)
    {
        printf("Negative integer are not allowed\n");
    }
    else
    {
        f=factorial(n);
        printf("The factorial of %d is= %d",n,f);
    }
    return 0;
}
long factorial(int n)
{
    if(n==0)
    {
        return 1;
    }
    else
    {
        return (n*factorial(n-1));
    }
}

16. Factors of a Positive Integer

#include<stdio.h>
int main()
{
    int number,i;
    printf("Enter positive Integer\n");
    scanf("%d",&number);
    printf("The factors of positive integer are\n");
    for(i=1; i<=number; i++)
    {
        if(number%i==0)
        {
            printf("%d ",i);
        }
    }
    return 0;
}

17. Generate Armstrong Number

#include<stdio.h>
int power(int,int);
int main()
{
    int a,b,i;
    printf("Enter two number\n");
    scanf("%d %d",&a,&b);
    printf("The armstrong number between %d and %d are\n",a,b);
    for(i=a; i<=b; i++)
    {
        if(check_armstrong(i)==1)
        {
            printf("%d\n",i);
        }
    }
}
int check_armstrong(int n)
{
    int temp,total_digits=0,reaminder,sum=0,power_digit,remainder_base;
    temp=n;
    while(temp !=0)
    {
        total_digits++;//It will calculate total digit entered//printf("%d",total_digits);
        temp=temp/10;
    }
    power_digit=total_digits;
    temp=n;
    while(temp !=0)
    {
        remainder_base=temp%10;
        sum=sum+power(remainder_base,power_digit);
        temp=temp/10;
    }
    if(n==sum)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int power(int remainder_base,int power_digit)
{
    int power=1,i;
    for(i=1; i<=power_digit; i++)
    {
        power=power*remainder_base;
    }
    return power;
}

18. HCF, LCM with function

#include<stdio.h>
long gcd(long,long);
int main()
{
    long a,b,gcf,lcm;
    printf("Enter two Number\n");
    scanf("%d%d",&a,&b);
    gcf=gcd(a,b);
    lcm=(a*b)/gcf;
    printf("The GCD of %d and %d is=%d",a,b,gcf);
    printf("The LCM of %d and %d is=%d",a,b,lcm);

}
long gcd(long a,long b)
{
    if(b==0)
    {
        return a;
    }
    else
    {
        return gcd(b,a%b);
    }
}

19. Insert Element in an Array

#include<stdio.h>

int main()
{
    int arr[100],i,n,element,location;

    printf("Enter number of array element\n");
    scanf("%d",&n);

    printf("Enter array element\n");
    for(i=0; i<n; i++)
    {
        scanf("%d",&arr[i]);
    }

    printf("Enter the element to be inserted\n");
    scanf("%d",&element);

    printf("Enter the location where number will be inserted\n");
    scanf("%d",&location);
    for(i=n; i>=location; i--)
    {
        arr[i]=arr[i-1];
    }

    n++;
    arr[location-1]=element;

    printf("After insertion\n");
    for(i=0; i<n; i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}

20. Largest Element of an Array

#include<stdio.h>
int main()
{
    int c,n,size,max,array[100],i,location=0;

    printf("Enter the size of an array\n");
    scanf("%d",&n);

    printf("Enter the numbers\n");

    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    max=array[0];

    for(i=1; i<n; i++)
    {
        if(array[i]>max)
        {
            max=array[i];
            location=i+1;
        }
    }
    printf("The maximum value %d found at location %d",max,location);

    return 0;

}

21. Largest Number among Three Numbers

#include<stdio.h>
int main()
{
    double n1,n2,n3;

    printf("Enter three number\n");
    scanf("%lf %lf %lf",&n1,&n2,&n3);

    if(n1>=n2 && n1>=n3)
    {
        printf("%.2f is the largest number\n",n1);
    }
    else if(n2>=n1 && n2>=n3)
    {
        printf("%.2f is the largest number\n",n2);
    }
    else
    {
        printf("%.2f is the largest number\n",n3);
    }
    return 0;
}

22. Leap Year

#include<stdio.h>
int main()
{
    int year;
    printf("Enter year\n");
    scanf("%d",&year);
    if((year%4==0 || year%400==0) && (year%100 !=0))
    {
        printf("%d year is a leap year",year);
    }
    else
    {
        printf("%d year is not a leap year",year);
    }
    return 0;

}

23. Linear Searching 

#include<stdio.h>
int main()
{
    int array[100],search,n,i,loc,force;
    printf("Enter number of elements\n");
    scanf("%d",&n);

    printf("Enter %d element\n",n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    printf("Enter the element to search\n");
    scanf("%d",&search);

    for(i=0; i<n; i++)
    {
        if(array[i]==search)
        {
            printf("%d found at location %d",search,i+1);
            break;
        }
    }
    if(i==n)
    {
        printf("%d not found\n",search);
    }

    return 0;

}

24. Matrix Addition

#include<stdio.h>
int main()
{
    int c,d,p,q,a[10][10],b[10][10],row,col,sum[10][10];
    printf("Enter row and column\n");
    scanf("%d %d",&row,&col);

    printf("Enter 1st matrix elements\n");
    for(c=0; c<row; c++)
    {
        for(d=0; d<col; d++)
        {
            scanf("%d",&a[c][d]);
        }
    }
    printf("Enter 2nd matrix elements\n");
    for(c=0; c<row; c++)
    {
        for(d=0; d<col; d++)
        {
            scanf("%d",&b[c][d]);
        }
    }
    printf("Sum of entered matrices:-\n");
    for(c=0; c<row; c++)
    {
        for(d=0; d<col; d++)
        {
            sum[c][d]=a[c][d]+b[c][d];
            printf("%d\t",sum[c][d]);
        }
        printf("\n");
    }
    return 0;
}


25. Matrix Subtraction

#include<stdio.h>
int main(){
    int c,d,p,q,a[10][10],b[10][10],row,col,diff[10][10];
    printf("Enter row and column\n");
    scanf("%d %d",&row,&col);

    printf("Enter 1st matrix elements\n");
    for(c=0;c<row;c++){
       for(d=0;d<col;d++){
            scanf("%d",&a[c][d]);
       }
    }
    printf("Enter 2nd matrix elements\n");
    for(c=0;c<row;c++){
       for(d=0;d<col;d++){
            scanf("%d",&b[c][d]);
       }
    }
    printf("Sum of entered matrices:-\n");
    for(c=0;c<row;c++){
       for(d=0;d<col;d++){
           diff[c][d]=a[c][d]-b[c][d];
           printf("%d\t",diff[c][d]);
       }
       printf("\n");
    }
    return 0;
}

26. Merge Two arrays

#include<stdio.h>
int main(){
    int arr1[50],arr2[50],n1,n2,i,j,k,rest[100];

    printf("Enter 1st number of array element\n");
    scanf("%d",&n1);
    printf("Enter 1st array element\n");
    for(i=0;i<n1;i++){
        scanf("%d",&arr1[i]);
    }
    printf("Enter 2nd number of array element\n");
    scanf("%d",&n2);
    printf("Enter 2nd array element\n");


    for(j=0;j<n2;j++){
        scanf("%d",&arr2[j]);
    }
    i=0;j=0;k=0;
    while(i<n1 && j<n2){
        if(arr1[i]<=arr2[j]){
            rest[k]=arr1[i];
            i++;
            k++;
        }
        else{
            rest[k]=arr2[j];
            k++;
            j++;
        }
    }

while (i < n1) {
rest[k] = arr1[i];
i++;
k++;
}

while (j < n2) {
rest[k] = arr2[j];
k++;
j++;
}

    printf("After merging\n");

    for(i=0;i<n1+n2;i++){
        printf("%d ",rest[i]);
    }

    return 0;

}

27. Minimum value in an Array

#include<stdio.h>
int main(){
     int c,n,size,min,array[100],i,location=0;

     printf("Enter the size of an array\n");
     scanf("%d",&n);

     printf("Enter the numbers\n");

     for(i=0;i<n;i++){
        scanf("%d",&array[i]);
     }
     min=array[0];

     for(i=1;i<n;i++){
        if(array[i]<min){
            min=array[i];
            location=i+1;
        }
     }
     printf("The minimum value %d found at location %d",min,location);

     return 0;

}

28. Palinedrone Number

#include<stdio.h>
int main(){
    int temp,n,reverse=0;
    printf("Enter the number to check palindrome\n");
    scanf("%d",&n);
    temp=n;

    while(temp!=0){
       reverse=reverse*10;
       reverse=reverse+(temp%10);
       temp=temp/10;
    }
    if(n==reverse){
        printf("The number is palindrome\n");
    }
    else{
        printf("The number is not palindrome\n");
    }
    return 0;
}

29. Perfect Number Check

#include<stdio.h>
int main(){
    int number,i=1,sum=0;
    printf("Enter positive integer\n");
    scanf("%d",&number);

    while(i<number){
        if(number%i==0){
            sum+=i;
        }
        i++;
    }
    if(number==sum){
        printf("%d is a perfect number\n",number);
    }
    else{
        printf("%d is not a perfect number\n",number);
    }
}

30. Power of a Number by While Loop

#include<stdio.h>
int main(){
    int base,exponent,result=1;
    printf("Enter base\n");
    scanf("%d",&base);
    printf("Enter power\n");
    scanf("%d",&exponent);

    while(exponent !=0){
        result=result*base;
        exponent--;
    }
    printf("Result is :%d",result);
    return 0;
}

31. Prime Number Checking

#include<stdio.h>
int prime(int);
int main(){
    int n,result;
    printf("Enter number\n");
    scanf("%d",&n);
    result=prime(n);
    if(result==1){
        printf("%d is a prime\n",n);
    }
    else{
        printf("%d is not a prime\n",n);
    }
    return 0;
}
int prime(int n){
    int c;
    for(c=2;c<=n-1;c++){
        if(n%c==0){
            return 0;
        }
    }
    if(n==c){
        return 1;
    }
}

32. Print Character A to Z

#include<stdio.h>
int main(){
    char c,i;

    for(i='A';i<='Z';i++){
        printf("%c ",i);
    }
    return 0;

}

33. Random Number Generate

#include<stdio.h>
int main(){
    int n,i,min,max,number,random,length=0,mod;
    printf("Enter min value and max value\n");
    scanf("%d %d",&min,&max);
    printf("Enter total number you want to be random\n");
    scanf("%d",&number);
    printf("Random number between %d and %d is\n",min,max);

    while(max !=0){
        length++;
        max=max/10;
    }
    printf("%d",length);
    if(length==2){mod=10;}else if(length==3){mod=100;}else{mod=1000;}

    for(i=min;i<=number;i++){
       random=rand()%mod+1;
       //printf("%d\n",random);
    }
    return 0;
}

34. Reverse a Number

#include<stdio.h>
long reverse(long);
int main(){
    int c,n,rev;
    printf("Enter number to be reversed\n");
    scanf("%ld",&n);
    rev=reverse(n);
    printf("The reversed number is=%d",rev);
    return 0;
}
long reverse(long n){
   static long r = 0;

   if (n == 0)
    return 0;
   r=r*10;
   r=r+n%10;
   reverse(n/10);
   return r;
}

35. Reverse a Sentence

#include<stdio.h>
void reverse(char);
int main(){
    char ch;
    printf("Enter a sentence\n");
    gets(ch);
    reverse(ch);

    return 0;
}
reverse(char ch){
    while(ch !='\n'){
        Reverse();
        printf("%c",ch);
    }
}

36. Reverse Array Elements

#include<stdio.h>

int main(){
    int array[50],i,j,n,temp;
    printf("Enter array element\n");
    scanf("%d",&n);
    printf("Enter %d element\n",n);
    for(i=0;i<n;i++){
        scanf("%d",&array[i]);
    }
    j=i-1;
    for(i=0;i<j;i++){
        temp=array[i];
        array[i]=array[j];
        array[j]=temp;
        j--;
    }
    printf("After reversal\n");
    for(i=0;i<n;i++){
        printf("%d ",array[i]);
    }

    return 0;

}

37. String Uppercase

#include<stdio.h>
#include<string.h>
void strtoupper(char[]);
int main(){
    char input[100],output[100];

    printf("Input String\n");
    gets(input);

    strtoupper(input);
    printf("The upper string is :%s",input);
    return 0;

}

void strtoupper(char s[]){
    int i=0;

    while(s[i] !='\0'){
        if(s[i]>='a' && s[i]<='z'){
            s[i]-=32;
        }
        i++;
    }
}

38. String Comparing without strcmp function

#include<stdio.h>
int main()
{
    char a[100],b[100],match;
    printf("Enter 1st String\n");
    gets(a);
    printf("Enter 2nd String\n");
    gets(b);
    match=string_match(a,b);
    if(match==0)
    {
        printf("Strings are equal\n");
    }
    else
    {
        printf("Strings are not equal\n");
    }
    return 0;
}
string_match(char a[],char b[])
{
    int c=0;
    while (a[c] == b[c])
    {
        if (a[c] == '\0' || b[c] == '\0')
        {
            break;
        }
        c++;
    }
    if(a[c]=='\0' && b[c]=='\0')  // '\0' means terminator character'
    {
        return 0;
    }
    else
    {
        return -1;
    }
}

39. String Concatenation without strcat function 

#include<stdio.h>
int main(){
    char a[100],b[100],join;
    printf("Enter string one\n");
    gets(a);
    printf("Enter string two\n");
    gets(b);

    join=concatenate(a,b);
    printf("After concate the string is=%s",a);

}
concatenate(char a[],char b[]){
    int m=0,n=0;
    while(a[m]!='\0'){
        m++;
    }

    while(b[n]!='\0'){
        a[m]=b[n];
        n++;
        m++;
    }
    a[m]='\0';

}

40. String Copy without strcpy function

#include<stdio.h>
#include<string.h>
int main(){
    char source[100],destination[100];
    printf("Enter source String\n");
    gets(source);
    copy_string(destination,source);
    printf("Source String %s\n",source);
    printf("Destination String %s\n",destination);
    return 0;
}
copy_string(char destination[],char source[]) {
    int c=0;
    while (source[c] !='\0') {
      destination[c]=source[c];
      c++;
    }
    destination[c] = '\0';
}


41. String Length without strlen function

#include<stdio.h>
int main(){
    char s[100],len;
    printf("Enter a String\n");
    gets(s);

    len=str_length(s);
    printf("Length of %s string is =%d",s,len);
}

int str_length(char s[]){
    int c=0;
    while(s[c] !='\0'){
        c++;
    }
    return c;
}

42. String Palindrome

#include<stdio.h>
int main(){
 char a[100],b[100];
 printf("input string\n");
 gets(a);
 strcpy(b,a);
 strrev(b);
 if(strcmp(a,b)==0){
    printf("String is a palindrome\n");
 }
 else{
    printf("String is not a palindrome\n");
 }
    return 0;
}

43. String Reverse without strrev function

#include<stdio.h>
int main(){
    char s[100],reverse[100],n,c,d;
    printf("Enter string\n");
    gets(s);

    n=strlen(s);

    for(c=0,c=n-1;c>=0;c--,d++){
       reverse[d]= s[c];
    }
    reverse[d]='\0';
    printf("reverse string is \n %s",reverse);

    return 0;
}

44. String Swapping

#include<stdio.h>
#include<string.h>
#include <malloc.h>
int main(){
    char first[100],second[100],*temp;

    printf("Enter first string:\n");
    gets(first);
    printf("Enter second  string:\n");
    gets(second);

    temp=(char*)malloc(100);

    strcpy(temp,first);
    strcpy(first,second);
    strcpy(second,temp);

    printf("After swapping :\n");
    printf("Value of first string:%s\n",first);
    printf("Value of second string:%s\n",second);

    return 0;

}

45. Substring finding in a long String

#include<stdio.h>
int main(){
    char string[100],sub[100];
    int position,length,i=0;

    printf("Input string\n");
    gets(string);

    printf("Enter position and length of string\n");
    scanf("%d %d",&position,&length);

    while(i<length){
        sub[i]=string[position+i-1];
        i++;
    }
    sub[i]='\0';
    printf("After substring: %s\n",sub);

    return 0;
}

46. Swapping with Third Variable

#include<stdio.h>
int main(){
    int a,b,temp;
    printf("Enter value of a= ");
    scanf("%d",&a);
    printf("Enter value of b= ");
    scanf("%d",&b);
    temp=a;
    a=b;
    b=temp;
    printf("After Swapping the value of a is=%d and b is=%d",a,b);

    return 0;

}


47. Swapping without Third Variable

#include<stdio.h>
int main(){
    int a,b;
    printf("Enter value of a\n");
    scanf("%d",&a);
    printf("Enter value of b\n");
    scanf("%d",&b);

    a=a+b;
    b=a-b;
    a=a-b;

    printf("After Swapping\n a is=%d\n b is=%d",a,b);
    return 0;
}

48. Vowel, Consonant, Number, Whitespace counting in a long String

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

int main(){
 int v=0,c=0,n=0,w=0;
 int i;
 char string[150];

 printf("Input String\n");
 gets(string);

 for(i=0;string[i] !=0;i++){
        if(string[i]=='a' ||string[i]=='A'||string[i]=='e'||string[i]=='E'||string[i]=='i'||string[i]=='I'||string[i]=='o'||string[i]=='O'||string[i]=='u'||string[i]=='U'){
            v++;
        }
        else if((string[i]>='a' && string[i] <='z')|| (string[i]>='A' && string[i] <='Z')){
            c++;
        }
        else if(string[i]>='0' && string[i]<='9'){
            n++;
        }
        else if(string[i]==' '){
            w++;
        }
        else{}

 }
    printf("Vowels: %d",v);
    printf("\nConsonants: %d",c);
    printf("\nDigits: %d",n);
    printf("\nWhite spaces: %d",w);

    return 0;
}


49. C program to Identify Class of an IP address

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main()
{
    char ip[50];
    gets(ip);
    int i;
    for(i=0; i<=strlen(ip); i++)
    {
        if(ip[i] == '.')
            ip[i] = '\0';
    }
    int ip_part = atoi(ip);
    if(ip_part<=127 && ip_part>=0)
        printf("Class A");
    else if(ip_part<=191 && ip_part>=128)
        printf("Class B");
    else if(ip_part<=223 && ip_part>=192)
        printf("Class C");
    else if(ip_part<=239 && ip_part>=224)
        printf("Class D");
    return 0;
}


50. Find Day Name from a Date (Format:      02-05-2000)

#include<bits/stdc++.h>
int main()
{
    char str[100];
    char day[10];
    char month[10];
    char year[10];
    scanf("%s", str);
    for(int i=0; i<strlen(str); i++)
    {
        if(str[i] != '-')
        {
            day[i] = str[i];
        }
        else
        {
            day[i] = '\0';
        }
    }
    int daylength = strlen(day);

    for(int i= daylength + 1; i<strlen(str); i++)
    {
        if(str[i] != '-')
        {
            month[i - daylength - 1] = str[i];
        }
        else
        {
            month[i - daylength - 1] = '\0';
        }
    }

    int next = strlen(day) + 1 + strlen(month) + 1;

    for(int i = next; i < strlen(str); i++)
    {

        if(str[i] != NULL)
        {
            year[i-next] = str[i];
        }
        else
        {
            year[i-next] =  '\0';
        }

    }

    int day2 = atoi(day);
    int month2 = atoi(month);
    int year2 = atoi(year);

    int mcode;
    if(month2 == 1)
        mcode = 0;
    if(month2 == 2)
        mcode = 3;
    if(month2 == 3)
        mcode = 3;
    if(month2 == 4)
        mcode = 6;
    if(month2 == 5)
        mcode = 1;
    if(month2 == 6)
        mcode = 4;
    if(month2 == 7)
        mcode = 6;
    if(month2 == 8)
        mcode = 2;
    if(month2 == 9)
        mcode = 5;
    if(month2 == 10)
        mcode = 0;
    if(month2 == 11)
        mcode = 3;
    if(month2 == 12)
        mcode = 5;

    int dcode;
    char dname[100];
    dcode = (day2 + mcode + (year2 - 2000) + ((year2 - 2000)/4)) % 7;
    if(dcode == 0)
        strcpy(dname, "SUNDAY");
    if(dcode == 1)
        strcpy(dname, "MONDAY");
    if(dcode == 2)
        strcpy(dname, "TUESDAY");
    if(dcode == 3)
        strcpy(dname, "WEDNESDAY");
    if(dcode == 4)
        strcpy(dname, "THURSDAY");
    if(dcode == 5)
        strcpy(dname, "FRIDAY");
    if(dcode == 6)
        strcpy(dname, "SATURDAY");
    printf("%s", dname);


    return 0;
}

51. Sum the digits and if the sum is greater than 10, then print those number

#include<bits/stdc++.h>
int main()
{
    char num[10];
    int sum = 0;
    scanf("%s", num);
    printf("%s", num);
    int length = strlen(num);
    printf("\nCharacters = %d", length);
    for(int i=0; i<length; i++)
    {
        sum = sum + (num[i] - '0'); ///char to int
        if(sum>=10)
        {
            num[i] = '\0';
        }
    }
    printf("\nNumber = %s", num);
}

52. Calculate 1s and 2s Complement of a Binary Number 

#include <stdio.h>
int main()
{
    int n;  // variable declaration
    printf("Enter the number of bits do you want to enter :");
    scanf("%d",&n);
    char binary[n+1];  // binary array declaration;
    char onescomplement[n+1]; // onescomplement array declaration
    char twoscomplement[n+1]; // twoscomplement array declaration
    int carry=1; // variable initialization
    printf("\nEnter the binary number : ");
    scanf("%s", binary);
    printf("%s", binary);
    printf("\nThe ones complement of the binary number is :");

    // Finding onescomplement in C
    for(int i=0; i<n; i++)
    {
        if(binary[i]=='0')
            onescomplement[i]='1';
        else if(binary[i]=='1')
            onescomplement[i]='0';
    }
    onescomplement[n]='\0';
    printf("%s",onescomplement);


    printf("\nThe twos complement of a binary number is : ");

// Finding twoscomplement in C
    for(int i=n-1; i>=0; i--)
    {
        if(onescomplement[i] == '1' && carry == 1)
        {
            twoscomplement[i] = '0';
        }
        else if(onescomplement[i] == '0' && carry == 1)
        {
            twoscomplement[i] = '1';
            carry = 0;
        }
        else
        {
            twoscomplement[i] = onescomplement[i];
        }
    }
    twoscomplement[n]='\0';
    printf("%s",twoscomplement);
    return 0;
}
 

53. C program - Write in a text file and read from and Calculate Highest Salary

#include<stdio.h>
#include<string.h>
int main()
{
    struct records
    {
        int id;
        char *name;
        float salary;
    };
    struct records employee[20] =
    {
        {1, "Adam", 28000},
        {2, "Bony", 19000},
        {3, "Choko", 38000},
        {4, "Dabu", 53000},
        {5, "Elman", 47000},
        {6, "Fua", 37000},
        {7, "Gyai", 42000},
        {8, "Himel", 23000},
        {9, "Iva", 48000},
        {10, "John", 45000},
        {11, "Koli", 93000},
        {12, "Lopa", 76000},
        {13, "Mimi", 35000},
        {14, "Nitu", 100000},
        {15, "Oishi", 80000},
        {16, "Porshi", 70000},
        {17, "Qakoli", 40000},
        {18, "Rasheda", 30000},
        {19, "Shanta", 87000},
        {20, "Tuli", 90000},
    };

///Wrting in the file
    int i = 0;
    FILE *p;
    p = fopen("employee.txt", "w");

    while(i<20)
    {
        fprintf(p, "%d %s %f\n", employee[i].id, employee[i].name, employee[i].salary);
        i++;
    }
    fclose(p);


///Reading and calculating from file
    struct readrecords
    {
        int id;
        char name[200];
        float salary;
    };
    struct readrecords reademployee;
    int id, newid;
    float salary, highest = 0;
    char name[200], newname[200];
    FILE *q;
    q = fopen("employee.txt", "r");
    do
    {
        fscanf(q, "%d %s %f\n", &reademployee.id, reademployee.name, &reademployee.salary);
        printf("%d %s %0.2f\n", reademployee.id, reademployee.name, reademployee.salary);
        if(highest < reademployee.salary)
        {
            newid = reademployee.id;
            strncpy(newname, reademployee.name, 200); ///strncpy Copy characters from string.
            highest = reademployee.salary;
        }
    }
    while(!feof(q));
    printf("\n%d. %s gets the highest salary = %0.2f \n.",newid, newname, highest);
    fclose(q);

}
 

54. Bubble Sort

#include<iostream>
using namespace std;
int main()
{
    int n,arr[30],i,j,temp;
    cout<<"Enter Num of Elements: "<<endl;
    cin>>n;
    cout<<"Enter Elements: "<<endl;
    for(i=0; i<n; i++)
        cin>>arr[i];
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(arr[i]>arr[j])
            {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }
    for(i=0; i<n; i++)
    {

        cout<<arr[i]<<endl;
    }
    return 0;
}

55. Insertion Sort

#include<iostream>
using namespace std;
int main()
{
    int n,arr[20],i,d,temp;
    cout<<"Enter n\n";
    cin>>n;
    cout<<"Enter Elements:\n";
    for(i=0; i<n; i++)
        cin>>arr[i];
    for(i=1; i<n; i++)
    {
        d=i;
        while(d>0&&arr[d]<arr[d-1])
        {
            temp=arr[d];
            arr[d]=arr[d-1];
            arr[d-1]=temp;
            d--;
        }
    }
    for(i=0; i<n; i++)
    {
        cout<<arr[i]<<endl;
    }

    return 0;
}

56. Merge Sort

#include<iostream>
using namespace std;
void merge_sort(int [],int ,int );
void merge(int [],int,int ,int );

int main()
{
    int n;
    cout<<"Enter the size of the array"<<endl;
    cin>>n;
    int a[n];
    cout<<"Enter the elements in the array"<<endl;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    cout<<"sorting using merge sort"<<endl;
    int p=1,r=n;

    merge_sort(a,p,r);

   cout<<"sorted form"<<endl;
   for(int i=1;i<=n;i++)
   {
       cout<<"a["<<i<<"]="<<a[i]<<endl;
   }
     return 0;
}

void merge_sort(int a[],int p,int r)
    {
        int q;
        if(p<r)
        {
         q=(p+r)/2;
         merge_sort(a,p,q);
         merge_sort(a,q+1,r);
         merge(a,p,q,r);
        }
    }

 void merge(int a[],int p,int q,int r)
    {
        cout<<"Entered merge"<<endl;
        int n1=q-p+1;
        int n2=r-q;
        int L[n1+1];
        int R[n2+1];
        for(int i=1;i<=n1;i++)
        {
            L[i]=a[p+i-1];
        }
        for(int j=1;j<=n2;j++)
        {
            R[j]=a[q+j];
        }
        L[n1+1]=999;
        R[n2+1]=999;
        int i=1, j=1;
        for(int k=p;k<=r;k++)
    {
            if(L[i]<=R[j])
            {
                a[k]=L[i];
                i=i+1;
            }
            else
            {
                a[k]=R[j];
                j=j+1;
            }
        }
    }

57. Quick Sort

#include<iostream>
using namespace std;

void quicksort(int[],int,int);
int part(int[],int,int);

int main()
{
    int n,a[60];
    cout<<"Size of the array"<<endl;
    cin>>n;
    cout<<"Enter the elements in the array"<<endl;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
    }
    cout<<"sorting using quick sort"<<endl;
    int p=1,r=n,i;
    quicksort(a,p,r);
    cout<<"sorted form"<<endl;
    for(i=1; i<=n; i++)
    {
        cout<<"a["<<i<<"]="<<a[i]<<endl;
    }
    return 0;
}

void quicksort(int a[],int p,int r)
{
    int q;
    if(p<r)
    {
        q=part(a,p,r);
        quicksort(a,p,q-1);
        quicksort(a,q+1,r);
    }
}

int part(int a[],int p,int r)
{
    int temp,temp1;
    int x=a[r];
    int i=p-1;
    for(int j=p; j<=r-1; j++)
    {
        if(a[j]<=x)
        {
            i++;
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }
    temp1=a[i+1];
    a[i+1]=a[r];
    a[r]=temp1;
    return i+1;
}

58. Heap Sort


#include <iostream>
using namespace std;

void max_heapify(int *a, int i, int n)
{
    int j, temp;
    temp = a[i];
    j = 2*i;
    while (j <= n)
    {
        if (j < n && a[j+1] > a[j])
            j = j+1;
        if (temp > a[j])
            break;
        else if (temp <= a[j])
        {
            a[j/2] = a[j];
            j = 2*j;
        }
    }
    a[j/2] = temp;
    return;
}
void heapsort(int *a, int n)
{
    int i, temp;
    for (i = n; i >= 2; i--)
    {
        temp = a[i];
        a[i] = a[1];
        a[1] = temp;
        max_heapify(a, 1, i - 1);
    }
}
void build_maxheap(int *a, int n)
{
    int i;
    for(i = n/2; i >= 1; i--)
    {
        max_heapify(a, i, n);
    }
}
int main()
{
    int n, i, x;
    cout<<"Enter number of elements of array\n";
    cin>>n;
    int a[20];
    for (i = 1; i <= n; i++)
    {
        cout<<"Enter element"<<(i)<<endl;
        cin>>a[i];
    }
    build_maxheap(a,n);
    heapsort(a, n);
    cout<<"\n\nSorted Arrayn";
    for (i = 1; i <= n; i++)
    {
        cout<<a[i]<<endl;
    }
    return 0;
}


59. Find pattern in a String

#include<bits/stdc++.h>
using namespace std;

void search (string text, string pat)
{
    int n=text.length();
    int m=pat.length();
    for(int i=0; i<=n-m; i++)            // Iteration over n-m+1 values of shift
    {
        int j;
        for(j=0; j<m; j++)
        {
            if(pat[j]!=text[i+j])
                break;
        }
        if(j==m)
            cout<<"Pattern found at : "<<i<<endl;
    }
}
int main()
{
    string a,b;
    cout<<"Enter the string"<<endl;
    cin>>a;
    cout<<"Enter the sub-string you wish to search for"<<endl;
    cin>>b;
    search(a,b);
    return 0;
}


60. Check Balanced Parenthesis in an Expression 

#include<bits/stdc++.h>
using namespace std;
bool isBalancedExp(string exp) {
     stack<char>stk;
     char x;
     for (int i=0; i<exp.length(); i++) {
       if (exp[i]=='(' || exp[i]=='[' || exp[i]=='{') {
         stk.push (exp[i]);
         continue;
       }
      if (stk.empty())
         return false;
       switch (exp[i]) {
       case ')':
          x = stk.top();
          stk.pop();
          if (x=='{' || x=='[')
              return false;
          break;
        case '}':
           x = stk.top();
           stk.pop();
           if (x=='(' || x=='[')
               return false;
           break;
          case ']':
             x = stk.top();
            stk.pop();
          if (x == '(' || x == '{')
            return false;
           break;
       }
    }
      return (stk.empty());
  }
int main() {
    char expression[100];
    gets(expression);

     if(isBalancedExp(expression))
        cout << "This is Balanced Expression";
     else
       cout << "This is Not Balanced Expression";
   }

Note: To Learn program with Stack Come here

61. C program to determine Even ODD a very very big Integer Number 

#include<bits/stdc++.h>
int main()
{
    int i;
    char n[100];
    scanf("%s", n);
    int l = strlen(n);
    if((n[l-1] - '0') % 2 == 0)  // Converting last character into int
    {
        printf("Even");
    }
    else
    {
        printf("Odd");
    }
    return 0;
}

62. See the following input and output and write a C Code 

2
12
case 1: 1 2 3 4 6 12
25
case 2: 1 5 25

Solution: 

#include<bits/stdc++.h>
int main()
{
    int T, i, N, j;
    scanf("%d", &T);

    for(i = 1; i <= T; i++)
    {
        scanf("%d", &N);
        printf("case %d:", i);
        for(j = 1; j <= N; j++)
        {
            if(N%j == 0)
            {
                printf(" %d", j);
            }
        }
        printf("\n");
    }
    return 0;
}


63: See the output and write C Code

2
3
***
***
***

4
****
****
****
****

Solution: 

#include<bits/stdc++.h>
int main()
{
    int T, i, N, j, k;
    scanf("%d", &T);

    for(i = 1; i <= T; i++)
    {
        scanf("%d", &N);
        for(j = 1; j <= N; j++)
        {
            for(k = 1; k <= N; k++)
            {
                printf("*");
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

64. C code of Pascal Triangle

Input and Output

5

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Solution approach:
 
row 1 is fixed 1 (odd row)

row 2 is also fixed 1 and 1, assume it even[] array, 

so, even[0] = 1 and even[1] = 1;  (even row)

now, lets start with row 3, assume it odd[] array, so odd[0] = 1;  

odd[middle_numbers or j] = even[j-1] + even[j]; and odd[j] = 1; 

continued in this way....

we will check even and odd row by dividing 2 

C code of pascal triangle:

#include<bits/stdc++.h>
int main()
{
    int i, j, N;
    int odd[100], even[100];
    scanf("%d", &N);
    even[0] = 1;
    even[1] = 1;
    printf("1\n");
    printf("%d %d\n", even[0], even[1]);
    for(i = 3; i <= N; i++)
    {
        if(i%2 !=0 )
        {
            odd[0] = 1;
            printf("%d ", odd[0]);
            for(j=1; j<i-1; j++)
            {
                odd[j] = even[j-1] + even[j];
                printf("%d ", odd[j]);
            }
            odd[j] = 1;
            printf("%d\n", odd[j]);

        }
        else
        {
            even[0] = 1;
            printf("%d ", even[0]);
            for(j=1; j<i-1; j++)
            {
                even[j] = odd[j-1] + odd[j];
                printf("%d ", even[j]);
            }
            even[j] = 1;
            printf("%d\n", even[j]);
        }
    }
    return 0;
}

65. Matrix Multiplication C Code

#include<stdio.h>

int main(void)
{
    int c, d, p, q, m, n, k, tot = 0;
    int fst[10][10], sec[10][10], mul[10][10];

    printf("Write Rows and Columns for first matrix: \n");
    scanf("%d%d", &m, &n);

    ///Example: m = 2; n = 2;

    printf("Write your 1st matrix elements: \n");
    for (c = 0; c < m; c++)
        for (d = 0; d < n; d++)
            scanf("%d", &fst[c][d]);


    /*
    example matrix:
        1 3
        3 4

    here, c controls rows and d controls columns
    1 = fst[0][0]
    2 = fst[0][1]

    3 = fst[1][0]
    2 = fst[1][1]
    */

    printf("Write Rows and Columns for second matrix\n");
    scanf(" %d %d", &p, &q);

    if (n != p)
        printf("Your given matrices cannot be multiplied with each other. \n");
    else
    {
        printf("Write your 2nd matrix elements\n");

        for (c = 0; c < p; c++)
            for (d = 0; d < q; d++)
                scanf("%d", &sec[c][d] );

        /*
    example matrix:
        5 6
        7 8

    here, c controls rows and d controls columns
    5 = sec[0][0]
    6 = sec[0][1]

    7 = sec[1][0]
    8 = sec[1][1]
        */

        /*
    Rules of Matrix Multiplication:

    We know that first matrix column number
    needed to be same of second matrix row number
    so first matrix's number of every row will be multiplied with
    second matrix's number of every column and added with.

    first matrix row number will final matrix row number
    second matrix column number with final matrix column number

    1 2   5 6
          x
    3 4   7 8


        1*5 + 2*7     1*6 + 2*8
    =
        3*5 + 4*7     3*6 + 4*8
    
        */


       ///Multiplication

        for (c = 0; c < m; c++) //as m 1st matrix row, c controls row
        {
            for (d = 0; d < q; d++) //as q 2nd matrix column, d controls column
            {
                for (k = 0; k < p; k++)
                {
                    tot = tot + fst[c][k] * sec[k][d];
                    ///most important line
                }
                mul[c][d] = tot; ///important line
                tot = 0;
            }
        }

        printf("The result of matrix multiplication: \n");
        for (c = 0; c < m; c++)
        {
            for (d = 0; d < q; d++)
                printf("%d ", mul[c][d] );
            printf("\n");
        }
    }
    return 0;
}


Previous Article Next Article