SET 2 - 39 C Programmes

Here is a list of 39 C language programmes 100% working and tested with sample outputs mentioned.

CPU- Computer Programming and Utilization

To all GECR'ians, these are the programmes given to us as part of CPU assignment. All programmes upto array are done. you might find 2-3 programmes missing  here from the assignment list given to us. That is because I wasn't able to solve it! Haha!

To view C programmes SET-1, Click Here 

List of C programmes in this post :

1) Write a program to evaluate area of square
2) Write a program to convert fahrenhit temperature into celsius
3) Write a program to find greatest number in given two numbers using conditional operator.
4) Write a program to exchange two variables
    (a) Using third variable
    (b) Without using third variable
5) Write a program to convert given lowercase letter to uppercase letter.
6) Write a program to find largest number using if...else statement
7) Write a program to check given num is odd or even.
8) Write a program to find largest number using nested if...else statement
9) Write a program to check the category of given character: Digit, Uppercase, lowercase or other symbol. Using multiple if…else.
10) Write a program to check given num is Armstrong or not. ( using control structures)
11) Write a program to create a simple calculator for addition, subtraction, multiplication and division
12) Write a program to print number of days in a given month. The program requires month number   as an input and then display the days in that month.
13) Write a program to check the entered character is vowel or not.
14) Write a program to print sum of first n integer numbers using while loop.
15) Write a program to reverse a given integer number. And also check for palindrome.
16) Write a program to check given number is Armstrong number or not. (using while loop)
17) Write a program to print first N Fibonacci numbers.(while loop)
18) Write a program to find the factorial of a given number.(While Loop)
19) Write a program to find sum of all even integers using while loop.
20) Write a program to sum the individual digits of a given positive number using (while loop.)
21) Write a program to compute the sum of following series. 1 – 1/2 + 1/3 - … +1/n.
22) Write a program to find sum of all odd integers
23) Write a program to check given number is  prime number or not. (for loop)
24) Write a program to find factorial of a given number using (for loop)
Patterns
25) Write a program to print following pattern. (FLOYD’s Triangle)
1
2 3
4 5 6
7 8 9 10

26) Following pattern: pyramid.
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *

27) Following Pattern:
*
**
***
****
*****

28) Following Pattern: Pascal triangle
             1
           1   1
         1   2   1
       1   3   3    1
     1  4    6   4   1
   1  5   10   10  5   1

29) Following patern:

         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

30) Following patern:
1
12
123
1234
12345

31) Following Pattern:
A
BB
CCC
DDDD
EEEEE

32) Following Pattern:
*****
****
***
**
*
 
Arrays
33) Write a program to perform linear search using array.
34) Write a program for addition of two matrices using multidimensional arrays.
35) C Program to Calculate Average Using Arrays
36) Write a program to insert a number at a specified array index position.
37) Write a program using an array to find largest and smallest number from given n numbers.
38) Write a program to find number of odd and even numbers from given n numbers.
39) Write a program to read n*n  matrix. Display original matrix as well as its transpose matrix.


 PROGRAMMES ARE GIVEN BELOW


1) Write a program to evaluate area of square
#include<stdio.h>

int main()
{
  
float side, area;

 
printf("\nEnter the Length of Side : ");
  
scanf("%f", &side);

  
area = side * side;
  
printf("\nArea of Square : %f", area);

  
return (0);
}

Output:
Enter the Length of Side : 2.5
Area of Square : 6.250000

2) Write a program to convert fahrenhit temperature into celsius
/*program to convert temperature inFahrenheit into Celsius degree*/
#include<stdio.h>
main()
{
float f,c;
printf("\nEnter temperature in Fahrenheit : ");
scanf("%f",&f);
c=(f-32)*5/9;  //Formula to convert Fahrenheit in Celsius
printf("\nTemprature in Celsius : %f",c);
return 0;
}
Output:
Enter temperature in Fahrenheit : 2.5
Temprature in Celsius : -16.388889

3) Write a program to find greatest number in given two numbers using conditional operator.
#include<stdio.h>
main()
{
int n1,n2,big;
printf("\n Enter Two Numbers");
scanf("%d%d",&n1,&n2);
big = ((n1>n2)?n1:n2);
printf("\n The Greater Number is %d",big);
return 0;
}

Output:
Enter Two Numbers 3 4
The Greater Number is 4

4) Write a program to exchange two variables
(a) Using third variable
(b) Without using third variable

(a)
/*Using third variable*/
#include <stdio.h>

int main()
{
   int x, y, temp;

   printf("Enter the value of x and y:\n");
   scanf("%d%d", &x, &y);

   printf("Before Swapping\nx = %d\ny = %d\n",x,y);

   temp = x;
   x    = y;
   y    = temp;

   printf("After Swapping\nx = %d\ny = %d\n",x,y);

   return 0;
}

Output:
Enter the value of x and y
: 3 4
Before Swapping
x = 3
y = 4
After Swapping
x = 4
y = 3

(b)
/*Without using third variable*/
#include <stdio.h>

int main()
{
   int a, b;

   printf("Enter two integers to swap:\n");
   scanf("%d%d", &a, &b);

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

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

Output:
Enter two integers to swap:3 4
a = 4
b = 3

5) Write a program to convert given lowercase letter to uppercase letter.
#include <stdio.h>;

int main()
{
      char lower, upper;

      printf("Please input a lowercase character: ");
      scanf("%c", &lower);

      if(lower >= 'a' && lower <= 'z'){
            upper = ('A' + lower - 'a');
      }
      else{
            upper = lower;
      }

      printf("\nThe uppercase equivalent is: %c\n", upper);
      return 0;
}

Output:
Please input a lowercase character: z
The uppercase equivalent is: Z

6) Write a program to find largest number using if...else statement
/* C program to find largest number using if...else statement */
#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers:\n ");
      scanf("%f %f %f", &a, &b, &c);
      if (a>=b)
      {
          if(a>=c)
            printf("Largest number = %.2f",a);
          else
            printf("Largest number = %.2f",c);
      }
      else
      {
          if(b>=c)
            printf("Largest number = %.2f",b);
          else
            printf("Largest number = %.2f",c);
      }
      return 0;
}

Output:
Enter three numbers:
1 2 33
 Largest number = 33.00

7) Write a program to check given num is odd or even.
#include <stdio.h>
int main(){
      int num;
      printf("Enter an integer you want to check:\n ");
      scanf("%d",&num);
      if((num%2)==0)      /* Checking whether remainder is 0 or not. */
           printf("%d is even.",num);
      else
           printf("%d is odd.",num);
      return 0;
}

Output:
Enter an integer you want to check:107
 107 is odd.

8) Write a program to find largest number using nested if...else statement
/* C Program to find largest number using nested if...else statement */
#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers:\n ");
      scanf("%f %f %f", &a, &b, &c);
      if(a>=b && a>=c)
         printf("Largest number = %.2f", a);
      else if(b>=a && b>=c)
         printf("Largest number = %.2f", b);
      else
         printf("Largest number = %.2f", c);
      return 0;
}

Output:
Enter three numbers:107.5 5 8.6
 Largest number = 107.50

9) Write a program to check the category of given character: Digit, Uppercase, lowercase or other symbol. Using multiple if…else.
Characters                     ASCII VAlues
A-Z                                65-90
a-z                                  97-122
0-9                                 48-57
Special symbols               0-47, 58-64, 91-96, 123-127
#include<stdio.h>
main()
{
 char ch;
 printf("Enter a character:");
 scanf("%c",&ch);
 if(ch>=65 && ch<=90)
 {
  printf("\n Upper case letter");
 }
 else if(ch>=97 && ch<=122)
 {
  printf("\n Lower case letter");
 }
 else if(ch>=48 && ch<=57)
 {
  printf("\n Digit");
 }
 else if((ch>=0 && ch<=47) || (ch>=58&& ch<=64) || (ch>=91 && ch<=96) || (ch>=123 && ch<=127))
 {
  printf("\n Special symbol");
 }
 return 0;
}

Output:
Enter a character: +
 Special symbol

10) Write a program to check given num is Armstrong or not. ( using control structures)
 #include<stdio.h>
int main(){
    int num,r,sum=0,temp;
    printf("Enter a number:\n ");
    scanf("%d",&num);
    for(temp=num;num!=0;num=num/10){
         r=num%10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
         printf("%d is an Armstrong number",temp);
    else
         printf("%d is not an Armstrong number",temp);
    return 0;
}

Output:
Enter a number:
370
370 is an Armstrong number

11) Write a program to create a simple calculator for addition, subtraction, multiplication and division
/* Source code to create a simple calculator for addition, subtraction, multiplication and division using switch...case statement in C programming. */
# include <stdio.h>
int main()
{
    char o;
    float num1,num2;
    printf("Enter operator either + or - or * or divide :\n ");
    scanf("%c",&o);
    printf("Enter two operands:\n ");
    scanf("%f%f",&num1,&num2);
    switch(o) {
        case '+':
            printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
            break;
        case '-':
            printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
            break;
        case '*':
            printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
            break;
        case '/':
            printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
            break;
        default:
            /* If operator is other than +, -, * or /, error message is shown */
            printf("Error! operator is not correct");
            break;
    }
    return 0;
}

Output:
Enter operator either + or - or * or divide :

-
Enter two operands:

3 5
3.0 - 5.0 = -2.0

12) Write a program to print number of days in a given month. The program requires month number as an input and then display the days in that month.
#include <stdio.h> 
 
int main() 
    int month; 
 
    /*
     * Reads month number from user
     */ 
    printf("Enter month number(1-12):\n "); 
    scanf("%d", &month); 
 
    switch(month) 
    { 
        case 1: 
        case 3: 
        case 5: 
        case 7: 
        case 8: 
        case 10: 
        case 12: printf("31 days"); 
            break; 
        case 4: 
        case 6: 
        case 9: 
        case 11: printf("30 days"); 
            break; 
        case 2: printf("28/29 days"); 
            break; 
        default: printf("Invalid input! Please enter month number between 1-12"); 
 
    } 
    return 0; 
}  

Output:
Enter month number(1-12):
7
 31 days

13) Write a program to check the entered character is vowel or not.
#include <stdio.h>

int main()
{
  char ch;

  printf("Input a character:\n");
  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("%c is a vowel.\n", ch);
      break;
    default:
      printf("%c is not a vowel.\n", ch);
  }             

  return 0;
}

Output:
Input a character:
i
i is a vowel.

14) Write a program to print sum of first n integer numbers using while loop.
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("ENTER THE VALUE OF N:\n");
scanf("%d",&n);
printf("First %d numbers are\n",n);
    while (i<=n)
    {
      printf("%7d ",i); //%7 to print the digit within 7 slots
       sum = sum +i;
       i++;
    }
printf("\nSum = %d\n",sum);
return 0;
}

Output:
ENTER THE VALUE OF N:
5
First 5 numbers are
      1       2       3       4       5
Sum = 15

15) Write a program to reverse a given integer number. And also check for palindrome.

    #include <stdio.h>
   main()
    {
       int num, temp, remainder, reverse = 0;
        printf("Enter an integer: \n");
        scanf("%d", &num);
        /*  original number is stored at temp */
        temp = num;
        while (num > 0)
        {
            remainder = num % 10;
            reverse = reverse * 10 + remainder;
            num /= 10;
        }
        printf("Given number is = %d\n", temp);
        printf("Its reverse is  = %d\n", reverse);
        if (temp == reverse)
            printf("Number is a palindrome \n");
        else
            printf("Number is not a palindrome \n");
    return 0;
    }

Output:
Enter an integer:
55

Given number is = 55
Its reverse is  = 55
Number is a palindrome 

16) Write a program to check given number is Armstrong number or not. (using while loop)
#include <stdio.h>
int main()
{
  int n, n1, rem, num=0;
  printf("Enter a positive  integer:\n");
  scanf("%d", &n);
  n1=n;
  while(n1!=0)
  {
      rem=n1%10;
      num+=rem*rem*rem;
      n1/=10;
  }
  if(num==n)
    printf("%d is an Armstrong number.",n);
  else
    printf("%d is not an Armstrong number.",n);
}

Output:
Enter a positive  integer:
 153
 153 is an Armstrong number.

17) Write a program to print first N Fibonacci numbers.(while loop)
/* Displaying Fibonacci sequence up to nth term where n is entered by user. */
#include <stdio.h>
int main()
{
  int count, n, t1=0, t2=1, display=0;
  printf("Enter number of terms: ");
  scanf("%d",&n);
  printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
  count=2;    /* count=2 because first two terms are already displayed. */
  while (count<n) 
  {
      display=t1+t2;
      t1=t2;
      t2=display;
      ++count;
      printf("%d+",display);
  }
  return 0;
}

Output:
Enter number of terms: 10
Fibonacci Series: 0+1+1+2+3+5+8+13+21+34+

18) Write a program to find the factorial of a given number.(While Loop)
#include<stdio.h>
main()
{
   long int fact =1;
   int i=1,n;
   printf("Enter an integer number : ");
   scanf("%d",&n);
   while(i<=n)
   {
     fact*=i;
     i++;
   }
  
   printf("\nNumber is  = %d \n",n);
   printf("factorial value is = %ld  \n",fact);

   return 0;
}

Output:
Enter an integer number :5
 Number is  = 5
factorial value is = 120  

19) Write a program to find sum of all even integers using while loop.
#include  <stdio.h>
main()
{
 int x=0,y=0,n;
 printf("Enter the value of n : ");
 scanf("%d",&n);
 while(x<=n)
 {
   y+=x;
   x+=2;
 }
 printf("\nsummation of even no up to given no is %d",y);
 return 0;
 }

Output:
Enter the value of n : 5
summation of even no up to given no is 6

20) Write a program to sum the individual digits of a given positive number using while loop.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
 int number = 0, digit = 0, sumOfDigits = 0;
 clrscr();
 printf("Enter any number\n ");
 scanf("%d", &number);
 while (number != 0)
 {
  digit = number % 10;
  sumOfDigits = sumOfDigits + digit;
  number = number / 10;
 }
 printf ("Sum of individual digits of a given number is %d", sumOfDigits);
 getch();
}

Output:
Enter any number
1234
Sum of individual digits of a given number is 10

21) Write a program to compute the sum of following series. 1 – 1/2 + 1/3 - … +1/n.
#include<stdio.h>
#include<conio.h>
    void main()
    {
        double n,sum=0,i;
        clrscr();
        printf("\n Please Give The Value of N:  ");
        scanf("%lf",&n);
        for(i=1;i<=n;i++)
        {
            sum = sum + (1/i);
            if(i==1)
                printf("\n 1 +");
            elseif(i==n)
                printf(" (1/%d)  ",i);
            else
                printf(" (1/%d) + ",i);
        }
        printf("\n\n THE SUM OF THIS SERIES IS %.2lf",sum);
        getch();
    }

Output:
Please Give The Value of N:  5
1 + (1/2) +  (1/3) +  (1/4) +  (1/5)
THE SUM OF THIS SERIES IS 2.28

22) Write a program to find sum of all odd integers
#include <stdio.h> 
 
int main() 
    int i, n, sum=0; 
 
    /*
     * Reads a number from user
     */ 
    printf("Enter any number: "); 
    scanf("%d", &n); 
 
    /*
     * Finds the sum of all odd number 
     */ 
    for( i=1 ; i<=n ; i+=2 ) 
    { 
        sum += i; 
    } 
 
    printf("\nSum of all odd number between 1 to %d = %d", n, sum); 
 
    return 0; 
}  

Output:
Enter any number: 10
Sum of all odd number between 1 to 10 = 25 

23) Write a program to check given number is  prime number or not. (for loop)
 #include<stdio.h>
int main(){
    int num,i,count=0;
    printf("Enter a number: ");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++){
        if(num%i==0){
         count++;
            break;
        }
    }
   if(count==0 && num!= 1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   return 0;
}

Output:
Enter a number: 5
5 is a prime number

24) Write a program to find factorial of a given number using for loop
 #include<stdio.h>
int main(){
  int i,f=1,num;
  printf("Enter a number: ");
  scanf("%d",&num);
  for(i=1;i<=num;i++)
      f=f*i;
  printf("\nFactorial of %d is: %d",num,f);
  return 0;
}

Output:
Enter a number: 3
Factorial of 3 is: 6

25) Write a program to print following pattern. (FLOYD’s Triangle)
1
2 3
4 5 6
7 8 9 10

#include<stdio.h>

int main() {

   int i, j, k = 1;
   int range;

   printf("Enter the range: ");
   scanf("%d", &range);

   printf("\nFLOYD'S TRIANGLE : \n");

   for (i = 1; i <= range; i++) {
      for (j = 1; j <= i; j++, k++) {
         printf("%d", k);
      }
      printf("\n");
   }

   return 0;
}

Output:
Enter the range : 4
FLOYD'S TRIANGLE :
1
2 3
4 5 6
7 8 9 10

26) Following pattern: pyramid.
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *

#include <stdio.h>

int main()
{
   int row, c, n, temp;

   printf("Enter the number of rows in pyramid of stars you wish to see: ");
   scanf("%d",&n);

   temp = n;

   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");

      temp--;

      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");

      printf("\n");
   }

   return 0;
}

Output:
Enter the number of rows in pyramid of stars you wish to see: 5
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *

27) Following Pattern:
*
**
***
****
*****

#include <stdio.h>

int main()
{
    int n, c, k;

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

    for ( c = 1 ; c <= n ; c++ )
    {
        for( k = 1 ; k <= c ; k++ )
            printf("*");

        printf("\n");
    }

    return 0;
}

Output:
Enter number of rows:
5
*
* *
* * *
* * * *
* * * * *

28) Following Pattern: Pascal triangle
             1
           1   1
         1   2   1
       1   3   3    1
     1  4    6   4   1
   1  5   10   10  5   1 

#include <stdio.h>

long factorial(int);

int main()
{
   int i, n, c;

   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);

   for (i = 0; i < n; i++)
   {
      for (c = 0; c <= (n - i - 2); c++)
         printf(" ");

      for (c = 0 ; c <= i; c++)
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));

      printf("\n");
   }

   return 0;
}

long factorial(int n)
{
   int c;
   long result = 1;

   for (c = 1; c <= n; c++)
         result = result*c;

   return result;
}

Output:
Enter the number of rows you wish to see in pascal triangle
6
            1
          1   1
        1   2   1
      1   3   3    1
    1  4    6   4   1
  1  5   10   10  5   1 

29) Following pattern:
         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>

main()
{
      int n, c, d, num = 1, space;

      scanf("%d",&n);

      space = n - 1;

      for ( d = 1 ; d <= n ; d++ )
      {
          num = d;

          for ( c = 1 ; c <= space ; c++ )
              printf(" ");

          space--;

          for ( c = 1 ; c <= d ; c++ )
          {
              printf("%d", num);
              num++;
          }
          num--;
          num--;
          for ( c = 1 ; c < d ; c++)
          {
              printf("%d", num);
              num--;
          }
          printf("\n");

      }

      return 0;
}

30) Following patern:
1
12
123
1234
12345
#include <stdio.h>
int main()
{
    int i, j;
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("%d",j);
        }
        printf("\n");
    }
    return 0;
}

31) Following Pattern:
A
BB
CCC
DDDD
EEEEE
#include <stdio.h>
int main()
{
    int i, j;
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("%c",'A'-1 + i);
        }
        printf("\n");
    }
    return 0;

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

33) Write a program to perform linear search using array.
Linear search in c programming: The following code implements linear search (Searching algorithm) which is used to find whether a given number is present in an array and if it is present then at what location it occurs. It is also known as sequential search. It is very simple and works as follows: We keep on comparing each element with the element to search until the desired element is found or list ends. Linear search in c language for multiple occurrences and using function.

#include <stdio.h>

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

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

   printf("Enter %d integer(s)\n", n);

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

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

   for (c = 0; c < n; c++)
   {
      if (array[c] == search)     /* if required element found */
      {
         printf("%d is present at location %d.\n", search, c+1);
         break;
      }
   }
   if (c == n)
      printf("%d is not present in array.\n", search);

   return 0;
}

Output:
Enter the number of elements in array
5
Enter 5 integer(s)
5
6
4
2
9
Enter the number to search
4
4 is present at location 3.

34) Write a program for addition of two matrices using multidimensional arrays.
 #include<stdio.h>
int main(){
  int a[3][3],b[3][3],c[3][3],i,j;
  printf("Enter the First matrix->");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);
  printf("\nEnter the Second matrix->");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&b[i][j]);
  printf("\nThe First matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }
  printf("\nThe Second matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
      printf("%d\t",b[i][j]);
   }
   for(i=0;i<3;i++)
       for(j=0;j<3;j++)
            c[i][j]=a[i][j]+b[i][j];
   printf("\nThe Addition of two matrix is\n");
   for(i=0;i<3;i++){
       printf("\n");
       for(j=0;j<3;j++)
            printf("%d\t",c[i][j]);
   }
   return 0;
}

Output:
Enter the First matrix-> 1 2 3 4 5 6 7 8 9
Enter the Second matrix->
 10 11 12 13 14 15 16 17 18
The First matrix is
1    2    3   
4    5    6   
7    8    9   
The Second matrix is
10    11    12   
13    14    15   
16    17    18   
The Addition of two matrix is
11    13    15   
17    19    21   
23    25    27   

35) C Program to Calculate Average Using Arrays
#include <stdio.h>
int main(){
    int n, i;
    float num[100], sum=0.0, average;
    printf("Enter the numbers of data: ");
    scanf("%d",&n);
    while (n>100 || n<=0)
    {
        printf("Error! number should in range of (1 to 100).\n");
        printf("Enter the number again: ");
        scanf("%d",&n);
    }
   for(i=0; i<n; ++i)
   {
      printf("%d. Enter number: ",i+1);
      scanf("%f",&num[i]);
      sum+=num[i];
   }
   average=sum/n;
   printf("Average = %.2f",average);
   return 0;
}
Output:
Enter the numbers of data: 6
1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69

36)Write a program to insert a number at a specified array index position.
#include <stdio.h>
int main()
{
    int array[100], position, c, n, value;
   
    printf("Enter number of elements in array\n");
    scanf("%d", &n);
   
    printf("Enter %d elements\n", n);
   
    for (c = 0; c < n; c++)   
        scanf("%d", &array[c]);
   
   
    printf("Enter the location where you wish to insert an element\n");
    scanf("%d", &position);
   
    printf("Enter the value to insert\n");
    scanf("%d", &value);
   
    for (c = n - 1; c >= position - 1; c--)   
        array[c+1] = array[c];
   
   
    array[position-1] = value;
   
    printf("Resultant array is\n");
   
    for (c = 0; c <= n; c++)   
        printf("%d\n", array[c]);   
   
    return 0;
}

Output:
Enter the number of elements in array
5
Enter 5 elements
2
3
1
5
6
Enter the location where you wish to insert an element
3
Enter the value to insert
8
Resultant array is
2
3
8
1
5
6


37) Write a program using an array to find largest and smallest number from given n numbers.
 #include<stdio.h>
int main(){
  int a[50],size,i,big,small;
  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ", 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("Largest element: %d",big);

  small=a[0];
  for(i=1;i<size;i++){
      if(small>a[i])
           small=a[i];
  }
  printf("Smallest element: %d",small);
  return 0;
}

Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1


38) Write a program to find number of odd and even numbers from given n numbers.
/* C program to find all the odd and even numbers in an array */
#include<stdio.h>
   
main()
{
int ar[100],i,n;
printf("Enter the size of the array \n");
scanf("%d",&n);     
printf("Enter the elements of the array \n");
for(i=0; i<n; i++)
{
scanf("%d",&ar[i]);
}
printf("Even numbers in the array are - ");
for(i=0;i<n;i++)
{
  if(ar[i]%2==0)
{
printf("%d \t",ar[i]);
}
}
printf("\n Odd numbers in the array are - ");
for(i=0;i<n;i++)
{
 if(ar[i]%2!=0)
 {
 printf("%d \t",ar[i]);
  }
 }
 return 0;
}

Output:
Enter the size of the array
5
Enter the elements of the array
1 2 3 4 5
Even numbers in the array are - 2     4    

Odd numbers in the array are - 1     3     5 


39) Write a program to read n*n  matrix. Display original matrix as well as its transpose matrix.
#include <stdio.h>

int main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];

   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);

   printf("Enter the elements of matrix\n");

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

   for (c = 0; c < m; c++)
      for( d = 0 ; d < n ; d++ )
         transpose[d][c] = matrix[c][d];

   printf("Transpose of entered matrix :-\n");

   for (c = 0; c < n; c++) {
      for (d = 0; d < m; d++)
         printf("%d\t",transpose[c][d]);
      printf("\n");
   }

   return 0;
}

Output:
Enter the number of rows and columns of matrix
2 2
Enter the elements of matrix
1 2 3 4
Transpose of entered matrix :-
1    3   
2    4


That's enough I guess! More C programmes are coming soon, so stay tunned!


Stay Amazed | Stay Geeky | Jay Akbari | Pixmercy

Unknown

Jay Akbari, also known as Pixmercy on web, is a self taught freelance web and graphic designer, founder of Pixmercy Graphics, Compt Engg Student, blogger, the boy behind GECR Geek and a avid PHOTOSHOPer. He is here to share his inspiration, work and free resources. Drop him any line anytime, whether it's about a project, collaboration, feedback or just business-he'd love to hear from you. Thanks for stopping by and Stay amazed, stay geeky!

No comments:

Post a Comment