100 c programming examples with output for practice

100 c programming examples with output for practice

  1. Write the simplest program of printing hello world.
    Ans.

    #include <stdio.h>
    int main(void) {
    // your code goes here
    printf("hello world!");
    return 0;
    }
    Output: hello world!
  2. How will you print “Hello World” without using semicolon even once in your code?
    Ans.

    int main(void)
    {
        if(printf(“Hello World”))
        {
        }
    }

    printf() function will print “Hello World” to the console. It will return a non-zero value to if statement and there is no other code to execute.

  3. Write a program to add two numbers.
    Ans.

    #include <stdio.h>
    int main() {    
    int number1, number2, sum;
    scanf("%d %d", &number1, &number2);
    sum = number1 + number2;  
    printf(" %d",sum);
    return 0;
    }
    Input: 2 3
    Output: 5
  4. Check whether a number is positive or negative.
    Ans.

    #include <stdio.h>
    int main() {
    double num;
    scanf("%lf", &num);
    if (num <= 0.0) {
           printf("negative number");
    } else
        printf("positive number.");
    return 0;
    }
    Input: -3
    Output: negative number
  5. Write to program to print the ASCII value of character.
    Ans.

    #include <stdio.h>
    int main() {  
    char c;
    scanf("%c", &c);  
    printf(" %d", c);
    return 0;
    }
    Input: d
    Output: 100
  6. Check whether the input character is a vowel or not.
    Ans.

    #include<stdio.h>
    int main()
    {
    char ch;
    scanf("%c", &ch);
    
    switch(ch)
    {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            printf("\nvowel\n");
            break;
        default:
            printf("not a vowel\n");
    }   
    return 0;
    }
    Input: a
    Output: vowel
  7. Write a program to find the remainder of number when divided by another number.
    Ans.

    #include <stdio.h>
    int main(){
    int a = 5;
    int b = 13;
    int remainder = b % a;
    printf("Remainder = %d", remainder);
    }
    Output: 3
  8. Find the area of a circle when radius is given by input.
    Ans.

    #include <stdio.h>
    int main(void)
    {
    float radius, area, pi = 3.14;
    scanf("%f", &radius);
    area = pi * radius * radius;
    printf(" %f", area);
    return 0;
    }
    Input: 5
    Output: 78.500000
  9. Find the area of a square when one side is given.
    Ans.

    #include <stdio.h>
    int main(void)
    {
    float side, area;
    scanf("%f", &side);
    area = side * side;
    printf("%f\n", area);
    return 0;
    }
    Input: 5
    Output: 25.000000
  10. You can easily print numbers from 1 to 100 using a loop, can you do the same without using any type of loop?
    Ans.

    #include<stdio.h>
    void printNumbers(int);
    int main()
    {
        int n=1;
        printNumbers(n);
        return 0;
    }
    void printNumbers(int n)
    {
        if(n<=100)
        {
            printf("%d     ", n);
            printNumbers(n+1);
        }
    }
  11. Convert the first character of a string to upper case.
    Ans.

    #include <stdio.h>
    #include <ctype.h>
    int main()
    {
    char str[20];
    scanf("%[^\n]s", str);
    str[0] = toupper(str[0]);
    puts(str);
    }
    Input: prepbytes
    Output: Prepbytes
  12. Find the maximum & minimum of two numbers in a single line without using any condition & loop.
    Ans.

    #include <stdio.h>
    #include<stdlib.h>
    int main()
    {
        int a=5,b=3;
        printf ("max = %d, min = %d", ((a+b) + abs(a-b))/2, ((a+b) - abs(a-b)) /2);
        return 0;
    }
    Output: 5 3
  13. How will you find the total number of elements in the array if the size of the array is not provided?
    Ans. We will use “sizeof” operator to calculate number of elements in the array.

    int main()
    {
        int arr[]={1,2,3,4,5,6,7,8,9,10};
        printf(“Number of elements: %d”,sizeof(arr)/sizeof(arr[0]); // sizeof(arr) = 40 , sizeof(arr[0])=4 for integer size=4
        return 0;
    }
    Output: 10
  14. Reverse the case of a character i.e., from upper case to lower and vice versa.
    Ans.

    #include
    #include
    int main()
    {
    char alphabet;
    alphabet=getchar();
    if(islower(alphabet))
        putchar(toupper(alphabet));
    else 
        printf("%c",tolower(alphabet)) ;
    return 0;
    }
    Input: a
    Output: A
  15. Can you write a C program to find whether a number is odd or even?
    Ans. Even numbers do not have any remainder when divided by 2, for odd number the remainder is 1.

    #include
    int main(void) {
    // your code goes here
    int a;
    scanf("%d",&a);
    if (a % 2 == 0)
        printf("even");
    else
        printf("odd");
    return 0;
    }
    Input: 4
    Output: even
  16. Explain the use of global and local variables using a program.
    Ans.

    #include
    int a, b = 23;
    void func_1()
    {
    printf("Global a = %d\n", a);
    printf("Global b = %d\n", b);
    }
    void func_2()
    {
    int a = 5;
    printf(" a = %d\n", a);
    }
    int main()
    {
    func_1();
    func_2();
    return 0;
    }
    Output: Global a = 0
    Global b = 23
    a = 12
  17. Write a program to swap two numbers in C.
    Ans. We use a third variable here to swap the numbers.

    #include
    main()
    {
     int a=5, b=21;
     clrscr();
     printf("%d %d",a,b);
     int temp = a;
     a = b;
     b = temp;
     printf("%d %d",a,b);
    }
    Output: 5 21
    21 5
  18. Write a program to swap two numbers without a third variable.
    Ans.

    #include
    int main()
    {
    int a=3, b=21;
    a=a+b;
    b=a-b;
    a=a-b;
    printf("%d %d",a,b);
    return 0;
    }
    Output: 21
  19. Using an example show the use of pointer to function.
    Ans.

    #include
    int func(int a, int b)
    {
    printf("%d ", a);
    printf("%d ", b);
    }
    int main()
    {
    int(*fptr)(int , int);
    fptr = func;
    func(5,12);
    fptr(5,12);
    return 0;
    }
    Output: 5 12 5 12
  20. Find the largest and smallest number using local and global variable declarations.
    Ans.

    #include
    int a,b;
    int main(){
    scanf("%d%d", &a, &b);
    if(a == b)
        printf("equal\n");
    else if(a < b)
    {
        printf("\n\n largest %03d\n";, b);
        printf("\n smallest  %03d\n";, a);
    }
    else
    {
        printf(" largest  %03d\n";, a);
        printf("smallest  %03d\n";, b);
    }
    return 0;
    }
    Input: 5 10
    Output: largest 10
    Smallest 5
  21. Write a program in C to print the current time.
    Ans.

    #include 
    #include   
    #include
    int main()
    {
    time_t cur_time;
    char* cur_t_string;
    cur_time = time(NULL);
    cur_t_string = ctime(&cur_time); 
    (void) printf(" %s ", cur_t_string);
    exit(EXIT_SUCCESS);
    }
    Output: Wed Mar 17 08:16:44 2021
  22. Write a program to explain the use of call by reference.
    Ans.

    #include
    void swap(int *x, int *y)
    {
        int temp;
    temp=*x;  // store the value present at address of ‘x’
    *x=*y;    // copy the value present at the address of ‘y’ to address of ‘x
    *y=temp;  // copy the value present in temp to address of ‘y’
    }
    int main(void)
    {
    int i=10, j=20;
    swap(&i, &j);
    printf("%d %d",i,j);
    return 0;
    }
    Output:20 10
  23. Write a program to show the use of static variables.
    Ans.

    #include   
    int product()
    {
    static int var = 1;
    var=var*5; 
    if(var>1000)
        return;
    printf("%d, ", var);
    product();
    }
    int main()
    {
    product();
    return 0;
    }
    Output: 5, 25, 125, 625,
  24. Write a program to find the size of an array.
    Ans.

    #include   
    int main()
    {
        int arr[]={1,2,3,4,5,6,7};
        printf(“%d”,sizeof(arr)/sizeof(arr[0]);
        return 0;
    }
    Output: 7
  25. Write a program to print all names in a given file directory.
    Ans.

    #include  
    int main(void)
    {
    DIR *d;
    struct dirent *temp;
    d = opendir(".");   //directory address
    if (d)
    {
        while ((temp = readdir(d)) != NULL)
        {
            printf("%s\n", temp->d_name);
        }
        closedir(d);
    }
    return(0);
    }
    Output: system dependent
  26. Write a simple program to find the simple interest.
    Ans.

    #include  
    void main()
    {
    float principal_amt, rate, simple_interest;
    int time;
    scanf("%f%f%d", &principal_amt, &rate, &time);
    simple_interest = (principal_amt*rate*time)/100.0;
    printf("%f ", principal_amt);
    
    printf("%f ", rate);
    printf("%f ", time);
    printf("%f ", simple_interest);
    return 0;
    }
    Input: 3000 2 3
    Output: 3000.000000 2.000000 2.000000 180.000000
  27. Write a function to find all roots of a quadratic equation.
    Ans.

    #include
    int main()
    {
    float a, b, c, determinant, r1, r2, real, imag;
    scanf("%f%f%f", &a, &b, &c);
    determinant == b*b - 4*a*c; 
    
    if(determinant != 0)   
    {
        r1 = (-b + sqrt(determinant))/2*a; 
        r2 = (-b - sqrt(determinant))/2*a;
        printf("%f %f ", r1, r2);
    }
    else if(determinant == 0)   
    {
        r1 = r2 = -b/(2*a); 
        printf("%f %f ", r1, r2);
    }
    else
    {
        real = -b/(2*a);
        imag = sqrt(-determinant)/(2*a);
        printf("%f + i%f %f - i%f ", real, imag, real, imag);
    }
    return 0;
    }
    Input: 1 1 1
    Output: -0.500000 -0.500000
  28. Write a program to print source code as program output.
    Ans.

    #include   
    int main()
    {
    FILE *fp;
    char c; 
    fp = fopen(__FILE__, "r"); 
    do
    {
        c=fgetc(fp);
        putchar(c);
    }
    while(c!=EOF); 
    fclose(fp); 
    return 0;
    }
    Output: Computer dependent
  29. Print a star pattern shown below.

    *
    *
    *

    *

    *

    Ans.

    #include 
    int main() {
    int i, j, rows;
    scanf("%d", &rows);
    for (i = 1; i <= rows; ++i) {
      for (j = 1; j <= i; ++j) {
         printf("*");
      }
      printf("\n");
    }
    return 0;
    }
  30. Write a program to convert Celsius to Fahrenheit.
    Ans.

    #include
    int main()
    {
    float celsius, fahrenheit;
    scanf("%f", &celsius);
    fahrenheit = (1.8*celsius) + 32;
    printf("%f ", fahrenheit);
    return 0;
    }
    Input: 35
    Output: 95
  31. Write a program to delete an element from the array.
    Ans.

    #include
    int main()
    {
    
    int array[100], position, c, n;
    scanf("%d", &n);
    
    for(c = 0; c < n; c++)
        scanf("%d",array[c]);
    
    scanf("%d",position);
    
    if(position <= n+1)
        for(c = position-1; c < n-1; c++)
        array[c] = array[c+1];
    
    for(c = 0; c < n-1; c++) 
        printf("%d", array[c]);
    return 0;
    }
    Input: 5
    1 2 3 4 5
    3
    Output: 1  2  4  5
  32. Write a program to swap adjacent elements in an array.
    Ans.

    #include 
    #define MAX 100
    int main()
    {
    int arr[MAX],n,i;
    int temp;
    scanf("%d",&n);
    if(n%2 !=0)
    {
        return 1;
    }  
    for(i=0;i < n;i++)
    {
        scanf("%d",arr[i]);
    }
    for(i=0;i < n;i+=2)
    {
        temp    = arr[i];
        arr[i]  = arr[i+1];
        arr[i+1]= temp;
    }
    for(i=0;i < n;i++)
    {
        printf("%d\n",arr[i]);
    }
    return 0;
    }
    Input: 5 1 2 3 4 5
    Output: 2 1 4 3 5
  33. Write a program to write factorial of a number.
    Ans.

    #include
    #include
    void main()
    {
    int fact, i, n;
    fact = 1;
    scanf("%d" , &n);
    for(i = 1; i <= n; i++)
    {
       fact = fact*i;
    }
    printf("%d",fact);
    getch();
    }
    Input: 5
    Output: 120
  34. Find the size of a union.
    Ans.

    #include 
    void main()
    {
    union sample
    {
        int   m;
        float n;
        char  ch;
    };    
    union sample u;    
    printf("The size of union =%d\n", sizeof(u));    
    u.m = 25;
    printf("%d %f %c\n", u.m, u.n,u.ch);    
    u.n = 0.2;
    printf("%d %f %c\n", u.m, u.n,u.ch);    
    u.ch = 'p';
    printf("%d %f %c\n", u.m, u.n,u.ch);    
    }    
    Output 4
    3 0.000000 
    1069547520 1.500000 
    1069547632 1.500013 p
  35. Check if a given number is a perfect square or not.
    Ans.

    #include
    int main()
    {
    int i, number;
    scanf("%d", &number);
    int flag = 0;
    for(i = 0; i <= number; i++)
    {
        if(number == i*i)
        {
            flag = 1;
        }
    }
    if(flag)
    printf("%d perfect square", number);
    return 0;
    }
    Input: 25
    Output: perfect square
  36. Write a program to find nPr.
    Ans.

    #include
    long factorial(int);
    long find_npr(int, int);
    int main()
    {
    int n, r;
    long npr;
    scanf("%d%d", &n, &r);
    npr = find_npr(n, r);
    printf(" %ld",npr);
    return 0;
    }
    long find_npr(int a, int b)
    {
    return (factorial(a)/factorial(a-b));
    }
    long factorial(int c)
    {
    if(c == 1 || c == 0)
        return 1;
    else
        return c*factorial(c-1);
    }
    Input: 3 1
    Output: 3
  37. Write a program to find nCr.
    Ans.

    #include
    long factorial(int);
    long find_ncr(int, int);
    int main()
    {
    int n, r;
    long ncr;
    scanf("%d%d", &n, &r);
    ncr = find_ncr(n, r);
    printf("%ld",ncr);
    return 0;
    }
    long find_ncr(int a, int b)
    {
    return (factorial(a)/(factorial(b)*factorial(a-b)));
    }
    long factorial(int c)
    {
    if(c == 1 || c == 0)
        return 1;
    else
        return c*factorial(c-1);
    }
    Input: 3 1
    Output: 3
  38. Write a program to shift all zeroes present in the array to left side.
    Ans.

    #include 
    #include 
    int main(void){ 
    int arr_size;
    int a1[] = {3, 4, 0, 1,0, 5};
    arr_size = sizeof(a1)/sizeof(a1[0]);  
    int pos = 0;
    for (int i = 0; i < arr_size; i++)
            {
                if (a1[i] == 0)
                {
                    a1[i] = a1[pos];
                    a1[pos++] = 0;
                }
            }
    
    arr_size = sizeof(a1)/sizeof(a1[0]); 
    int i;      
    for( i=0; i<arr_size-1; i++)  
    {  
        printf("%d, ", a1[i]);  
    } 
    return 0;
    }      
    Output: 0, 0, 3, 1, 4
  39. Write a program to shift an array by 1 unit in the left.
    Ans.

    #include 
    #include 
    print_array(int parray[], int size)
    {
    int i;      
    for( i=0; i<size-1; i++)  
    {  
        printf("%d, ", parray[i]);  
    } 
    printf("%d ", parray[i]);  
    printf("\n");   
    }
    int main(void){ 
    int arr_size;
    int a1[] = {2, 3, 5, 4};
    arr_size = sizeof(a1)/sizeof(a1[0]);
    int result[arr_size];
    for (int i = 0; i < arr_size; i++)
         {
           result[i] = a1[(i + 1) % arr_size];
         }
    print_array(result, arr_size);
    }         
    Output: 3 5 4 2
  40. Write a program to check whether 3 or 5 is present in the array.
    Ans.

    #include
    #include
    int solve(int nums[], int arr_size)
         {
          for (int i = 0; i < arr_size; i++)
            {
                if (nums[i] == 3 || nums[i] == 5) return 1;
            }
            return 0;
         }
    int main(void){
    int arr_size;
    int array1[] = {2, 0, 1, 2, 4};
    arr_size = sizeof(array1)/sizeof(array1[0]);
    printf("%d",solve(array1, arr_size));
    }
    Output: 0
  41. Write a program to check if given array contains either 2 even or 2 odd values all next to each other.
    Ans.

    #include 
    int solve(int numbers[], int arr_size)
         {
           int tot_odd = 0, tot_even = 0;
            for (int i = 0; i < arr_size; i++)
            {
                if (tot_odd < 2 && tot_even < 2)
                {
                    if (numbers[i] % 2 == 0)
                    {
                        tot_even++;
                        tot_odd = 0;
                    }
                    else
                    {
                        tot_odd++;
                        tot_even = 0;
                    }
                }
            }
               return tot_odd == 2 || tot_even == 2;
      }
    int main(void){
    int arr_size;
    int array1[] = {1, 2, 3, 4 };
    arr_size = sizeof(array1)/sizeof(array1[0]);
    printf("%d",solve(array1, arr_size));
      return 0;
    }
    Output: 0
  42. Write a program to concatenate two strings.
    Ans.

    #include 
    int main() {
    char s1[100] = "This is string1 ", s2[] = "This is string 2";
    int length, j;
    length = strlen(s1);
    for (j = 0; s2[j] != '\0'; ++j, ++length) {
    s1[length] = s2[j];
    }
    //or simply
    //strcat(s1,s2);
    s1[length] = '\0';
    puts(s1);
    return 0;
    }
    Output: This is string1 This is string2
  43. Write a program to compare two strings.
    Ans.

    #include 
    int main()
    {
    char str1[] = "prep";
    char str2[] = "bytes";
    printf("%d", strcmp(str1, str2));
    }
    Output: 14
    Find the length of the given string.
    Ans. #include 
    int stringLength(char * word) {
    int count = 0;
    while (word[count] != '\0') {
      count++;
    }
    return (count);
    }
    int main() {
    int   index = 0;
    char  word[100];
    scanf("%s", word);
    printf("%d", word, stringLength(word));
    return (0);
    }
    Input: programming
    Output: 11
  44. Sort a string alphabetically.
    Ans.

    #include 
    int main () {
    char string[] = "thisisstring";
    char temp;
    int i, j;
    int n = strlen(string);
    for (i = 0; i < n-1; i++) {
      for (j = i+1; j  string[j]) {
            temp = string[i];
            string[i] = string[j];
            string[j] = temp;
         }
      }
    }
    printf(" %s ", string);
    return 0;
    }
    Output: ghiiinrssstt
  45. Show the use of pointers and their outputs using a program.
    Ans.

    #include 
    const int MAX = 3; 
    int main()
    {
    int var[] = {100, 200, 300};
    int i, *ptr;
    
    ptr = var;
    
    for(i = 0; i < MAX; i++)
    {
        printf("%d",ptr);
        printf("%d", *ptr);
        ptr++;
    }
    return 0;
    }
    Output: -1090230052100-1090230048200-1090230044300
  46. Write a program to explain the use of goto statement.
    Ans.

    #include 
    int main() {
    int n;
    scanf("%d", & n);
    if (n % 2 == 0 && n > 0)
      goto a;
    else if (n % 2 != 0 && n  0)
      goto d;
    else
      goto e;
    a:
      printf("number is even and positive");
    goto stop;
    b:
      printf("number is odd and negative");
    goto stop;
    c:
      printf("number is even and negative");
    goto stop;
    d:
      printf("number is odd and positive");
    goto stop;
    e:
      printf("special number");
    goto stop;
    stop:
      return 0;
    }
    Input: 25
    Output: number is odd and positive
  47. Write a C program to divide an integer by 2 without using ‘/’ operator.
    Ans.

    #include
    int main()
    {
        int n=30;
        printf("%d", n>>1);
    }
    Output: 15
  48. Write a C program to find the sum of two integers without using '+' operator.
    Ans.

    #include
    int main()
    {
        int x=10;
        int y=20;
        int sum = -(-x-y); // -(-x-y)= x+y, simple mathematics trick
        printf("%d%d",sum);
        return 0;
    }
    Output: 30
  49. Write a program to print the Fibonacci series.
    Ans

    #include
    void fibonacci(int num)
    {
    int a, b, c, i = 3;
    a = 0;
    b = 1;
    if(num == 1)
    printf("%d",a);
    if(num >= 2)
    printf("%d\t%d",a,b);
    while(i <= num)
    {
      c = a+b;
      printf("\t %d ", c);
      a = b;
      b = c;
      i++;
    }
    }
    int main()
    {
    int num = 0;
    scanf("%d", &num);
    fibonacci(num);
    return 0;
    }
    Input: 6
    Output: 0 1 1 2 3 5
  50. Write a C program to convert an integer into a string without using library function.
    Ans.

    #include
    int main()
    {
        int num,result,rem,len=0,n,i;
        char str[10];
        printf("Enter a number: ");
        scanf("%d",&num);
        n=num;
        while(n!=0)
        {
            len++;
            n=n/10;
        }
        for(i=0;i<len;i++)
        {
            rem=num%10;
            num=num/10;
            str[len-(i+1)]=rem+'0';
        }
        str[len]='';
        printf("%s",str);
        return 0;
    }
    Input: 1231
    Output: 1231
  51. Write a program to find whether a number is palindrome or not.
    Ans.

    #include
    int main()
    {
    int a, b, c, s = 0;
    scanf("%d", &a);
    c = a;
    while(a > 0)
    {
      b = a%10;
      s = (s*10)+b;
      a = a/10;
    }
    if(s == c)
    {
      printf("palindrome");
    }
    else
    {
      printf(" not a palindrome");
    }
    return 0;
    }
  52. Write a program to find the sum of the digits of a number.
    Ans.

    #include
    int main()
    {
    int n, sum = 0, c, remainder;
        scanf("%d", &n);
    
    while(n != 0)
        {
            remainder = n%10;
            sum += remainder;
            n = n/10;
        }
        printf("%d", sum);      
        return 0;
    }
  53. Reverse a inputing present in a character array.
    Ans.

    #include 
    int main()
    {
    char input[1000], rev[1000];
    int i, j, count = 0;
    scanf("%s", input);
    while (input[count] != '\0')
    {
    count++;
    }
    j = count - 1;
    for (i = 0; i < count; i++)
    {
    rev[i] = input[j];
    j--;
    }
    printf("%s", rev);
    }
    Input: abcde
    Output: edcba
  54. How to write a C program without using stdio.h header file.
    Ans.

    int printf(const char*format, ...);
    int main() { 
    printf("without stdio.h"); 
    return 0; 
    }
    Output: without stdio.h
  55. Write a program to generate random numbers.
    Ans.

    #include
    #include
    int main()
    {
     int a,b;
     for(a=1;a<=5;a++)
     {
           b=rand();
           printf("%d\n",b);
     }
     return 0;
    }
    Output:
    1804289383
    846930886
    1681692777
    1714636915
    1957747793
  56. Write a program to link two polynomials.
    Ans.

    #include
    #include
    struct Node { 
    int coeff;  
    int exp;     
    struct Node * next;  
    }* poly = NULL;    
    void create() {     
    struct Node * t, * last = NULL; 
    int num, i;
    scanf("%d", & num);
    for (i = 0; i coeff, &t->exp);                
    t-> next = NULL;           
    if (poly == NULL) {      
      poly = last = t;
    } else {
      last -> next = t;
      last = t;
    }
    }
    }
    void Display(struct Node * p) {
    while (p) {
    printf("%dx%d +", p -> coeff, p -> exp); 
    p = p -> next;    
    }
    printf("\n");
    }
    long Eval(struct Node * p, int x) {   
    long val = 0;
    while (p) {                       
    val += p -> coeff * pow(x, p -> exp);
    p = p -> next;
    }
    return val;
    }
    int main() {
    create();
    Display(poly);
    printf("%ld\n", Eval(poly, 1));
    return 0;
    }
    Input: 2 4 3 2 1
    Output: 4x3 +2x1 +
    6
  57. Write a program to find the average of a series of numbers.
    Ans.

    #include
    int main()
    {
    int n, i;
    float sum = 0, x;
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%f", &x);
        sum += x;
    }
    printf("%f";, (sum/n));   
    return 0;
    }
    Input: 5 1 2 3 4 5 
    Output: 3.000000
  58. Write a program to check if a number is Armstrong number or not.
    Ans.

    #include
    int main()
    {
    int n,sum,t,a;
    scanf("%d",&n);    
        t = n;
        sum = 0;
        while(t != 0)
        {
            a = t%10;
            sum += a*a*a;
            t = t/10;
        }
        if(sum == n)
        printf("Armstrong ");
        else
        printf("Not Armstrong ");    
    return 0;
    }
    Input: 371
    Output: Armstrong
  59. Find the factors of a number.
    Ans.

    #include
    int main()
    {    
    int  num, i;
    scanf("%d",&num);   
    for(i = 1; i <= num/2; i++)
    {
        if(num%i == 0)
            printf("\t%d", i);
    }
    return 0;
    }
    Input: 26
    Output: 1 2 13
  60. Find the sum of n numbers given.
    Ans.

    #include
    int main()
    {
    int n,sum=0,c,value;
    scanf("%d", &n);
    for(c = 1; c <= n; c++)
    {
        scanf("%d", &value);
        sum += value;
    }
    printf("%d", sum);
    return 0;
    }
    Input: 5 1 2 3 4 5
    Output: 15
  61. Write a program to print the first n prime numbers.
    Ans.

    #include
    int main()
    {    
    int n,i = 3, count, c;
    scanf("%d", &n);
    if(n >= 1)
    {
        printf("2 ");
    }
    for(count = 2; count <= n; i++)  
    {        
        for(c = 2; c < i; c++)
        {
            if(i%c == 0)
                break;
        }
        if(c == i)  
        {
            printf("%d ", i);
            count++;    
        }
    }    
    return 0;
    }
    Input: 6
    Output: 2 3 5 7 11 13
  62. Implement a Binary Search Algorithm.
    Ans.

    #include 
    int binarySearch(int arr[], int number, int start, int end) {
    if(start>= end) {
        return arr[start] == number ? 0 : 1;
    }
    int tmp = (int) end / 2;
    if(number == arr[tmp]) {
        return 0;
    } else if(number > arr[tmp]) {
        return binarySearch(arr, number, start, tmp);
    } else {
        return binarySearch(arr, number, tmp, end);
    }
    }
    int main() {
    int arr[] = {1, 2, 3, 5, 6};
    int size_of_arr = sizeof(arr) / sizeof(int);
    printf("%d\n", binarySearch(arr, 2, 0, size_of_arr-1));
    return 0;
    }
    Output: 1
  63. Write a program to find the largest number among n input numbers.
    Ans.

    #include
    int main()
    {
    int n,i;
    float c,big;
    scanf("%d", &n);   
    scanf("%f", &big);
    for(i = 2; i <= n; i++)
    {       
        scanf("%f", &c);      
        if(big < c)  
            big = c;    
    }
    printf("%f", big);
    return 0;
    }
    Input: 3 5 7 11 13 
    Output: 13
  64. Write a power function of a number.
    Ans.

    #include
    int main()
    {
    int n, exp, exp1;
    long long int value = 1;
    scanf("%d%d",&n, &exp);
    exp1 = exp;   
    while(exp-- > 0)
    {
        value *= n; 
    }
    printf(" %lld",value);   
    return 0;
    }
    Input: 3 3
    Output: 27 
  65. Write a program to find whether a number is int or float.
    Ans.

    #include
    #include
    int main()
    {    
    char number[10];
    int flag = 0;
    int length, i = 0;
    scanf("%s", number);
    length = inputlen(number);
    while(number[i++] != '\0')   
    {
        if(number[i] == '.')  
        {
            flag = 1;
            break;
        }
    }
    if(flag)
        printf("\nFloat\n");
    else
        printf("\n integer\n");
    return 0;
    }
    Input: 2.000
    Output: Float
  66. Try to print the multiplication table of any given number.
    Ans.

    #include
    int main()
    {
    int n,i;
    scanf("%d", &n);
    for(i = 1; i <= 10; i++)
    {
        printf("%d", n*i);
    }
    return 0;
    }
    Input: 5
    Output: 5 10 15 20 25 30 35 40 45 50
  67. Write a program to reverse an array.
    Ans.

    #include
    int main()
    {
    int c, d, n, a[100], b[100];
    scanf("%d", &n);
    for(c = 0; c = 0; c--, d++)
        b[d] = a[c];
    for(c = 0; c < n; c++)
        a[c] = b[c];
    for(c = 0; c < n; c++)
        printf("%d", a[c]);
    return 0;
    }
    Input: 5 1 2 3 4 5
    Output: 5 4 3 2 1
  68. Find the largest and smallest element in an array.
    Ans.

    #include
    int main()
    {
    int a[50], size, i, big, small;
    scanf("%d", &size);
    for(i = 0; i< size; i++)
    scanf("%d", &a[i]);
    big = a[0]; 
    for(i = 1; i < size; i++)
    {
        if(big<a[i]) 
        {
            big = a[i]; 
        }
    }
    printf("%d", big);
    small = a[0]; 
    for(i = 1; i a[i])   
        {
            small = a[i];  
        }
    }
    printf("%d",small);
    return 0;
    }
    Input: 5 1 2 3 4 5
    Output: 5 1
  69. Find the sum of array elements in the array.
    Ans.

    #include
    int main()
    {
    int a[50], size, i,sum =0;
    scanf("%d", &size);
    for(i = 0; i < size; i++)
    scanf("%d", &a[i]);
    sum = a[0]; 
    for(i = 1; i < size; i++)
    {
       sum += a[i];
    }
    printf("%d\n", sum);
    return 0;
    }
    Input: 5 1 2 3 4 5 
    Output: 15
  70. Sort the given array. (Bubble Sort)
    Ans.

    #include
    void sorting(int *x, int y)
    {
    int i, j, temp;
    for(i = 1; i <= y-1; i++)
    {
        for(j = 0; j  *(x+j+1))
            {
                temp = *(x+j);
                *(x+j) = *(x+j+1);
                *(x+j+1) = temp;
            }
        }
    }
    }
    int main()
    {
    int a[20], i, c, n;
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    scanf("%d", &a[i]);
    sorting(a, n);
    for(i = 0; i <n; i++)
    printf("%d\t", a[i]);
    return 0;
    }
    Input: 5 1 4 3 2 5
    Output: 1 2 3 4 5 
  71. Write a program to find an element in linear time.
    Ans.

    #include
    int main(){
    int arr[] = {100, 5, 201, 13, 5, -8,12,-8,3,-8}; 
    int i,key,position = -1;
    int arr_size = sizeof(arr)/sizeof(int); 
    scanf("%d",&key);
    for(i=0;i<arr_size;i++){
        if(key == arr[i])
        {
            position = i;
            break;
        }
    }
    if(position!=-1)
        printf(" %d \n",position);
    else
        printf(" not found \n",key);
    }
    Input: 201
    Output: 2
  72. Find the second largest element in the array.
    Ans.

    #include
    int main(){
    int arr[] = {20,21,20,34,5,32};
    int largest,second_largest,i;
    int arr_size = sizeof(arr)/sizeof(int); 
    largest = second_largest = INT_MIN;
    for(i=0;i< arr_size;i++){
        if(largest < arr[i]){
            second_largest = largest;
            largest = arr[i];
        }else if( arr[i]  second_largest)
        {
            second_largest = arr[i];
        }
    }
        printf("%d\n",second_largest);
    }
    Output: 32
  73. Write a program to remove duplicates from an array.
    Ans.

    #include
    int main(){
    int arr[] = {1,2,2,5,4,3,3}; 
    int i,j,k,key,exists;
    int arr_size = sizeof(arr)/sizeof(int); 
    int arr_2[100]={0};
    for(i=0,j=0;i<arr_size;i++){
        key = arr[i];exists=0;
        for(k=0;k<i;k++){
            if(arr_2[k]==key)
            {
                exists = 1;
            }
        }
        if(!exists){
            arr_2[j] = arr[i];
            j++;
        }
    }
    for(i=0;i<j;i++){
        printf("%d\t",arr_2[i]);
    }
    }
    Output: 1 2 3 4 5
  74. Check if a year is a leap year or not.
    Ans.

    #include
    int main(){
    int number;
    scanf("%d",&number);
    if( ((number % 4 == 0) && (number % 100 != 0))
            || (number % 400 ==0) )
    {
        printf("Leap year",number);
    }else
        printf("not Leap year",number);
    }
    Input: 2020
    Output: Leap Year
  75. Convert the given number to binary.
    Ans.

    #include
    int Decimal2Binary(int number){
    int binary=0, i=1,remainder;
    while( number != 0){
        remainder = number % 2;
        number = number / 2;
        binary = binary + remainder*i;
        i = i * 10;
    }
    return binary;
    }
    int main(){
    int number;
    scanf("%d",&number);
    printf("%d",Decimal2Binary(number));      
    }
  76. Convert a binary number to decimal.
    Ans.

    #include
    int Binary2Decimal(int number){
    int decimal=0, i=0,ptrDigit;
    while( number != 0){
        ptrDigit = number % 10;
        number = number / 10;
        decimal = decimal + ptrDigit * pow(2,i);
        i++;
    }
    return decimal;
    }
    int main(){
    
    int number;
    scanf("%d",&number);
    printf("%d",Binary2Decimal(number));
    }
    Input: 11001
    Output: 25
  77. Find the LCM of two numbers.
    Ans.

    #include
    int main(){
    
    int  a,b,multiple,lcm;
    scanf("%d %d",&a,&b);
    
    multiple = (a < b) ? a : b;
    while(1){
        if(multiple % a == 0 && multiple % b == 0){
            lcm = multiple;
            break;
        }
        multiple++;
    }
    printf("%d",lcm);
    }
    Input: 2 5
    Output: 10
  78. Find GCD of two numbers.
    Ans.

    #include
    int main(){
    int  a,b,i,gcd;
    scanf("%d %d",&a,&b);
    for(i=1;i<=a && i<=b;i++){
        if( a % i == 0 &&  b % i == 0){
            gcd = i;
        }
    }
    printf("%d",gcd);
    }
    Input: 2 5
    Output: 1
  79. Check whether a number is prime or not.
    Ans.

    #include
    int main(){
    int number,i,flag=0;
    scanf("%d",&number);
    for(i=2; i <= number/2;i++){
            if(number % i==0)
            {
                flag =1;
            }
        }
    if(number==1)
        printf("Neither Prime nor Composite \n");
    else if(flag ==1 )
        printf("Composite \n");
    else
        printf("Prime \n");
    }
    Input: 5
    Output: Prime
  80. Check whether a number is palindrome or not.
    Ans.

    #include
    int main(){
    int number,ptrDigit,originalNumber,reverseNumber=0;
    scanf("%d",&number);
    originalNumber = number;
    while(number!=0){
        ptrDigit = number % 10;
        reverseNumber = reverseNumber*10 + ptrDigit;
        number = number/10;
    }
    if(originalNumber == reverseNumber)
        printf("Palindrome\n");
    else
        printf("not a Palindrome \n");
    }
    Input: 1331
    Output: Palindrome
  81. Explain the use of enum using a program.
    Ans.

    #include 
    #include 
    int main(void)
    {
    typedef enum { SLOW, NORMAL, FAST, FASTEST } speed;
    speed s = SLOW;
    s=s+1;
    if(s == NORMAL)
        printf("NORMAL\n");
    typedef enum {
        TALL = 1,
        CLEVER = 2,
        BIG  = 4,
    } flags;    
    flags f = TALL|CLEVER;
    if(f & TALL)
        printf(" TALL bit set\n");
    return 0;
    }
    Output: NORMAL
    TALL bit set
  82. Check if a number is perfect number or not.
    Ans.

    #include
    int main(){
    int number,i,sum=0;
    scanf("%d",&number);
    for(i=1; i < number;i++){
            if(number%i==0)
            {
                sum = sum + i;
            }
    }
    if(sum == number)
        printf("Perfect number\n");
    else
        printf(" not a Perfect number \n");
    }
  83. Convert a given binary number to octal.
    Ans.

    #include
    int Binary2Decimal(int number){
    int decimal=0, i=0,ptrDigit;
    
    while( number != 0){
        ptrDigit = number % 10;
        number = number / 10;
        decimal = decimal + ptrDigit * pow(2,i);
        i++;
    }
    return decimal;
    }
    int Binary2Octal(int number){
    int octal=0,value, i=1,ptrDigit;
    while( number != 0){
        ptrDigit = number % 1000;
        value = Binary2Decimal(ptrDigit);
        octal = octal + value * i;
        i = i * 10;
        number = number / 1000;
    }
    return octal;
    }
    int main(){
    int number,octal;
    scanf("%d",&number);
    octal = Binary2Octal(number);
    printf("%d",octal);   
    }
    Input: 11101
    Output 35
  84. Write a function htoi(s), which converts a input of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F.
    Ans.

    #include 
    #include 
    #define MAXLEN 1000
    int htoi(char []);
    int htoi(char s[])
    {
    int i, hexDigit, intValue;
    i = 0;
    if (s[0] == '0' && tolower(s[1]) == 'x' && s[2] != '\0')
        i = 2;
    hexDigit = intValue = 0;
    for ( ; s[i] != '\0'; ++i) {
        if (!isdigit(s[i]) && (tolower(s[i])  'f'))
            return -1; 
        if (isdigit(s[i]))
            hexDigit = s[i] - '0'; 
        else
            hexDigit = tolower(s[i]) - 'a' + 10; 
        intValue = 16 * intValue + hexDigit; 
    }
    return intValue;
    }
    int main(void)
    {
    int result;
    char s[MAXLEN];
    scanf("%s", s);
    if ((result = htoi(s)) < 0)
        return -1; 
    printf("%i\n", result);
    return 0;
    }
    Input: 0x11101
    Output: 69889
  85. Check if the given matrix is sparse matrix or not.
    Ans.

    #include
    int main()
    {
    int n, m, c, d, matrix[10][10];
    int counter = 0;
    scanf("%d%d",&m,&n);
    for(c = 0; c < m; c++)  
    {
        for(d = 0; d  (m*n)/2)
        printf("sparse matrix\n");
    else
        printf(" not a sparse matrix\n");
    return 0;
    }
    Input: 3 3 
    1 2 0
    0 0 0
    0 1 0
    Output: sparse matrix
  86. Check whether a matrix is square matrix or not.
    Ans.

    #include
    int main()
    {
    int c, d, a[10][10], b[10][10], n, temp;
    scanf("%d", &n);
    for(c = 0; c < n; c++) 
        for(d = 0; d < n; d++)
            scanf("%d", &a[c][d]);
    
    for(c = 0; c < n; c++)
        for(d = 0; d < n; d++) 
            b[d][c] = a[c][d];
    for(c = 0; c < n; c++)
    {
        for(d = 0; d < n; d++) 
        {
            printf("%d\t", b[c][d]);
        }
        printf("\n");
    }
    
    for(c = 0; c < n; c++)   
    {
        for(d = 0; d < n; d++)   
        {
            if(a[c][d] != b[c][d]) 
            {
                printf("not Square\n");
                exit(0);    
            }
        }
    }
    
    printf("Square\n");
    return 0;
    }
    Input: 2 1 2 2 1
    Output: Square
  87. Find the determinant of a 2x2 matrix.
    Ans.

    #include
    int main()
    {
    int a[2][2], i, j;
    long determinant;
    for(i = 0; i < 2; i++)
    for(j = 0; j < 2; j++)
    scanf("%d", &a[i][j]);
    determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1];
    printf("%d", determinant);
    return 0;
    }
    Input: 1 2 2 1
    Output: -3
  88. Find the trace of a matrix.
    Ans.

    #include
    #include 
    int main()
    {
    int i, j, n, aj[10][10], sum = 0, sum1 = 0, a = 0, normal;
    scanf("%d", &n);
    
    for(i = 0; i < n; i++)  
    {
        for(j = 0; j < n; j++)   
        {
            scanf("%d", &aj[i][j]);
            a = aj[i][j]*aj[i][j];  
            sum1 += a;  
        }
    }
    normal = sqrt((double)sum1);    
    for(i = 0; i < n; i++)
    {
        sum = sum + aj[i][i];  
    }
    printf(" %d", sum);
    return 0;
    }
    Input: 2 1 2 2 1
    Output: 2
  89. Given two matrices find the sum of the matrices.
    Ans.

    #include
    int main()
    {
    int n, m, c, d, first[10][10], second[10][10], sum[10][10];
    scanf("%d%d", &m, &n);
    for(c = 0; c < m; c++)  
        for(d = 0; d < n; d++)   
            scanf("%d", &first[c][d]);
    for(c = 0; c < m; c++) 
        for(d = 0; d < n; d++)   
            scanf("%d", &second[c][d]);
    for(c = 0; c < m; c++)
        for(d = 0; d < n; d++)
            sum[c][d] = first[c][d] + second[c][d];
    for(c = 0; c < m; c++)
    {
        for(d = 0; d < n; d++)
        {
            printf("%d\t", sum[c][d]);
        }
        printf("\n");
    }
    return 0;
    }
    Input: 2 2 1 2 2 1 3 3 4 4 
    Output: 4 5 6 5
  90. Given two matrices find the difference matrix.
    Ans.

    #include
    int main()
    {
    int n, m, c, d, first[10][10], second[10][10], diff[10][10];
    scanf("%d%d", &m, &n);
    for(c = 0; c < m; c++)  
        for(d = 0; d < n; d++)   
            scanf("%d", &first[c][d]);
    for(c = 0; c < m; c++) 
        for(d = 0; d < n; d++)   
            scanf("%d", &second[c][d]);
     for(c = 0; c < m; c++)
        for(d = 0; d < n; d++)
            diff[c][d] = first[c][d] - second[c][d];
    for(c = 0; c < m; c++)
    {
        for(d = 0; d < n; d++)
        {
            printf("%d\t", diff[c][d]);
        }
        printf("\n");
    }
    return 0;
    }
    Input: 2 2 1 2 2 1 3 3 4 4
    Output: -2 -1 -2 -3
  91. Find the matrix multiplication of two matrices.
    Ans.

    #include
    int main()
    {
    int n, m, c, d, p, q, k, first[10][10], second[10][10], pro[10][10],sum = 0;
    scanf("%d%d", &m, &n);
    for(c = 0; c < m; c++)   
        for(d = 0; d < n; d++)  
            scanf("%d", &first[c][d]);
    scanf("%d%d", &p, &q);
    if(n != p)
        printf("Incompatible\n");
    else  
    {
        for(c = 0; c < p; c++)  
            for(d = 0; d < q; d++)  
                scanf("%d", &second[c][d]);
        for(c = 0; c < m; c++)  
        {
            for(d = 0; d < q; d++)   
            {
                for(k = 0; k < p; k++)
                {
                    sum = sum + first[c][k]*second[k][d];
                }
            pro[c][d] = sum;    
            sum = 0;   
            }
        }
        for(c = 0; c < m; c++)   
        {
            for(d = 0; d < q; d++)   
            {
                printf("%d\t", pro[c][d]);
            }
            printf("\n"); 
        }
    }
    return 0;
    }
    Input: 2 2 1 2 2 1 2 2 3 3 4 4
    Output: 11 11 10 10
  92. Write a program to produce the following output from the input.
    Input: a1b3c2
    Output: abbbcc
    Ans.

    #include
    #include
    int main(void)
    {
    char *input,ptr;
    int i,n=0;
    input=malloc(100);
    printf("Enter the inputing  : ");
    scanf("%s",input);
    for(i=0;input[i];i++)
    { 
    if(input[i]57)
    {
     printf("%c",input[i]);                           
     continue;
    }
    for(ptr=i;(input[i]>=48&&input[i]<=57)&&input[i];i++)  
     n=n*10+(input[i]-48);
    i--;
    if(ptr)                                          
    for(n-=1;n;n--)                                   
    printf("%c",input[ptr-1]);            
    else n=0;
    }
    return 0;
    }
    Input: a1b3c2
    Output: abbbcc
  93. Implement quicksort algorithm.
    Ans.

    #include 
    int main()
    {
    int n, arr[1000], c, d, t;
    scanf("%d", &n);
    for (c = 0; c < n; c++)
    scanf("%d", &arr[c]);
    for (c = 1 ; c  0 && arr[d-1] > arr[d]) {
      t          = arr[d];
      arr[d]   = arr[d-1];
      arr[d-1] = t;
      d--;
    }
    }
    for (c = 0; c <= n - 1; c++) {
    printf("%d\n", arr[c]);
    }
    return 0;
    }
    Input: 5 4 5 3 2 1
    1 2 3 4 5
  94. Implement a stack using a LinkedList.
    Ans.

    #include
    typedef struct Node {
    int value;    
    struct Node* next;
    } Node;
    Node* top;
    void push(int value){
    Node* temp = (Node*)malloc(sizeof(Node));
    temp->value = value;
    temp->next = top;
    top = temp;
    printStack();
    }
    void pop(){
    Node* temp = top;
    if(top == NULL) return;
    top = temp->next;
    free(temp);
    printStack();
    }
    int peek(){
    return top->value;
    }
    char* isEmpty(){
    Node* temp = top;
    int i = 0;
    while(temp != NULL){
        temp = temp->next;
        i++;
    }
    return i == 0 ? "Empty" : "Not Empty";
    }
    void printStack(){
    Node* temp = top;
    while(temp != NULL){
        printf("%d", temp->value);
        temp = temp->next;
    }
    printf("\n");
    }
    int main(void){
    printf("%s\n", isEmpty());
    push(3);
    push(4);
    push(5);
    pop();
    printf("%s\n", isEmpty());
    pop();
    pop();
    printf("%s\n", isEmpty());
    return 0;
    }
    Output: 0 1
  95. Count the number of words in a string.
    Ans.

    #include 
    #include 
    #include 
    void main()
    {
    char words[200];
    int i, count = 0;
    fgets(words, sizeof(words),stdin); 
    if(isalpha(words[0]))
      count = 1;    
    for (i = 0;words[i] != '\0';i++)
    {
            if(words[i]== ' '){            
                while(isspace(words[i])){i++;}            
                if(words[i] != '\0')
                    count++;
            }   
    }
    printf("%d", count);
    }
    Input: this is a string
    Output: 4
  96. Given the lines removes the blanks from the lines.
    Ans.

    #include
    #include
    int main()
    {
    char aj[1000], mj[1000];
    int i = 0, j = 0, len;
    gets(aj);
    len = strlen(aj);   
    while(aj[i] != '\0')  
    {
        if(aj[i] != ' ')  
        {
            mj[j++] = aj[i];
        }
        i++;
    }
    mj[j] = '\0';
    printf(" %s", mj);
    return 0;
    }
    Input: this is string
    Output: thisisstring
  97. Convert the case of characters without using built in functions.
    Ans.

    #include 
    char myStr[30];
    void convertToLower()
    {
    for (size_t i = 0; myStr[i] != '\0'; i++)
    {
        if (myStr[i] >= 65 && myStr[i] = 97 && myStr[i] <= 122)
        {
            myStr[i] = myStr[i] - 32;
        }
    }
    }
    int main()
    {
    scanf("%[^\n]s", myStr);
    getchar();
    char option;
    option = getchar();
    
    switch (option)
    {
    case 'L':
        convertToLower();
        break;
    case 'U':
        convertToUpper();
        break;
    }
    puts(myStr);
    }
    Input: prepbytes U
    Output: PREPBYTES
  98. Write a C program to divide an integer by 2 without using ‘/’ operator.
    Ans.

    #include
    int main()
    {
        int n=30;
        printf("%d", n>>1);
    }
    Output: 15
  99. Write a function to find the running rum of an array.
    Input: [1 2 3 4]
    Output: 1 3 6 10
    Ans.

    int* runningSum(int* nums, int numsSize, int* returnSize){
    int* buffer = (int*)malloc(numsSize*sizeof(int));
    int sum = 0;
    for (int i = 0; i < numsSize; i++){
        sum += nums[i];
        buffer[i] = sum; 
    }
    *returnSize = numsSize;
    return buffer;
    }
  100. Write a C program to compute the sum of the numbers in a given array except those numbers starting with 2 followed by atleast one 3. Return 0 if the given array has no integer.
    Ans.

    #include 
    #include 
    int test(int nums[], int arr_size)
         {
            int sum = 0;
            int inSection = 0;
            for (int i = 0; i < arr_size; i++)
            {
                if (nums[i] == 2) inSection = 1;
                else if (inSection && nums[i] == 3) inSection = 0;
                else if (!inSection) sum += nums[i];
            }
            return sum;
         }
    int main(void){
    int array1[] = {2, 7, 3, 5, 17, 1};
    int arr_size = sizeof(array1)/sizeof(array1[0]);
    printf("%d",test(array1, arr_size));
    return 0;
    }    
    Output: 23
Previous post What is difference between core java and advanced java?
Next post 20 most common interview questions and answers for freshers

Leave a Reply

Your email address will not be published. Required fields are marked *