Pages

Sunday, November 09, 2014

Write a program that pass array to function and function will return their sum

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<stdio.h>
int sum_arr(int num[]){
    int sum = 0, i = 0;
    for(; i<=4; i++)
        sum = sum + num[i];
    return sum;
}
int main(){
    int i=0, num[5];
    printf("Enter 5 integer Numbers: ");
    for(; i <=4; i++){
        scanf("%d", &num[i]);
    }
    printf("Sum = %d", sum_arr(num));
}//main()

Output:

Enter 5 integer Numbers:  2
9
5
6
4
Sum = 26

Write a program that print the 2nd smallest number from 5 numbers by using array

INPUT:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
int main()
{
    int arr[5], i , save = 0 , min = 0, min2 = 0;
    printf("Enter the 5 integer No to find 2nd smallest No.: ");
    for(i=0; i<5; i++)
        scanf("%d",&arr[i]);
    min=arr[0];
    for(i = 1; i < 5; i++) {
        if(min > arr[i]) {
            min = arr[i];
            save = i;
        }
    }// for loop
    min2 = arr[0];
    for(i = 1; i < 5; i++) {
        if(min2 > arr[i] && save != i)
            min2 = arr[i];
    }// for loop
    printf("\n2nd smallest no. is %d\n\n\n", min2);
}// main() ... the end

OutPut:

Enter the 5 integer No. to find 2nd smallest No.: 45
54
65
76
6

2nd smallest no. is 45 

Write a program that print the 2nd largest from 5 numbers by using array

INPUT:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
int main()
{
    int arr[5], i , save = 0 , lar = 0, lar2 = 0;
    printf("Enter the 5 integer value to find 2nd largest value: ");
    for(i=0; i<5; i++)
        scanf("%d",&arr[i]);
    lar=arr[0];
    for(i = 1; i < 5; i++) {
        if(lar<arr[i]) {
            lar = arr[i];
            save = i;
        }
    }// for loop
    lar2 = arr[1-1];
    for(i = 1; i < 5; i++) {
        if(lar2 < arr[i] && save != i)
            lar2 = arr[i];
    }// for loop
    printf("\n2nd largest no. is %d\n\n\n", lar2);
}// main() ... the end

OutPut:

Enter the 5 integer value to find 2nd largest value: 45
54
65
76
6

2nd largest no. is 65 


Saturday, November 08, 2014

Write a program that swap two integer by function call

Input:

//Write a program that swap two integer by function call (passing by reference/address)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include<stdio.h>

void swap(int *x, int *y)
{

    int temp;
    printf("x = %d, y = %d \n", x, y);
    temp = *x;
    x = *y;
    y = temp;
    printf("x = %d, y = %d\n\n", x, y);
}
void main()
{
    int x = 5;
    int y = 10;
    swap(x,y); //passing by reference/address
}

Output:

x = 5, y = 10
x = 10, y = 5

Write a program that swap two integer by function call

Input:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include<stdio.h>

void swap(int x, int y)
{

    int temp;
    printf("x = %d, y = %d \n", x, y);
    temp = x;
    x =y;
    y = temp;
    printf("x = %d, y = %d\n\n", x, y);
}
void main()
{
    int x = 5;
    int y = 10;
    swap(x,y); //passing by value
}

Output:

x = 5, y = 10
x = 10, y = 5

Sunday, November 02, 2014

Write a program that calculate simple calculation by function

INPUT:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
int sum(int a, int b)
{
    return (a + b);
}
int main()
{
    int a, b;
    char op;
    printf("Enter two integer and one operator(+,-,*,/):\t");
    scanf("%d %d %c", &a, &b, &op);
    switch(op) {
    case'+':
        printf("%d + %d = %d", a, b, a+b);
        break;
    case'-':
        printf("%d - %d = %d", a, b, a-b);
        break;
    case'*':
        printf("%d * %d = %d", a, b, a*b);
        break;
    case'/':
        printf("%d / %d = %d", a, b, a/b);
        break;
    default:
        printf("Incorrect Output.");
    }//switch
}//main()

OutPut:


Enter two integer and one operator(+,-,*,/): 15 3 /
15 / 3 = 5

Saturday, November 01, 2014

Shapes by loops

Shape:

123456
12345
1234
123
12
1

Input:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include<stdio.h>
int main()
{ int i,j;
   for( i=6;i>=1;i--){

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








Write a program that demonstrate the use of function

Input:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>
void pak();
void china();

void main()
{
    printf("Before fun call\n");

    china();
    printf("min fun call\n");
    pak();


    printf("After fun call\n");
}
// function call
void pak()
{
    printf("in pak()\n");
    china();
}
void china()
{
    printf("in china() \n");
}

OutPut:



Before fun call
in china()
min fun call
in pak()
in china
After fun call

Write a program that takes 10 integer from user and print the Maximum and minimum number

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
int main()
{
    int num, min, max, i;
    printf("Enter 10 Number: \n");
    scanf("%d", &num);
    min = num;
    max = num;
    for(i=1; i<=9; i++) {
        scanf("%d", &num);
        {
            if(num>max)
                max = num;
            else if(num<min)
                min = num;
        }
    }
    printf("Min = %d\n", min);
    printf("Max = %d\n", max);
    return 0;
}

OutPut:


Enter 10 Number:
1 
2 
3 
4 
5 
6 
7 
9 
6 
12
Min = 1
Max = 12

Diamond Shape

Write a program that print Diamond shape.


Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<stdio.h>
int main()
{
    int i=0,j=0,num =3;
    for(i = -num; i<= num; i++) {
        for(j= -num ; j<=num; j++) {
            if(abs(i)+ abs(j)<= num) {
                printf("*");
            } else
                printf(" ");
        }
        printf(" \n");
    }
    return 0;
}

OutPut:

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

Index

Function, Pointer and Array

Pointer

Write a program to determine the use of pointer.


Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include<stdio.h>
int main()
{
    int i = 5, *ip;
    float f = 5.2, *fp;
    char c = 'H', *cp;
    ip = &i;
    fp = &f;
    cp = &c;

    printf("%2d %2.1f %c\n \n", *ip, *fp, *cp );
    printf("%2d %2.1f %c\n \n", &i, &f, &c);
    printf("%2d %2.1f %c\n \n", *(&i), *(&f), *(&c));
    printf("%2d %2.1f %c \n\n", *(&ip), *(&fp), *(&cp));

    return 0;
}

Result:


5 5.2 H

2686748 0.0 H

5 5.2 H

2686748 0.0 H

Function

Write a program to Find the square of a number with the help of function

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<stdio.h>
void sq1();

void main()
{
    sq1();
}
sq1()
{
    int num, res = 0;
    printf("Enter a number: ");
    scanf("%d", &num );
    res = num * num;
    printf("The square of %d is %d", num, res);
}

Result:


1
2
Enter a number: 6
The square of 6 is 36

Sunday, February 09, 2014

For-Statement

Write a program to print digit from 1 to 10 


Input:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>
#include <conio.h>
void main(void)
{
   clrscr();
   int count;
   for (count = 1; count <= 10; count++)
 printf("%d \n", count);
   getch();

}

Result:

1
2
3
4
5
6
7
8
9
10

XY-coordinate

Write a program that input x- and y-coordinate of a point in the coordinate plane. Based on these values, it then determines where it lies, on x-axis or y-axis or in any of the four quadrant


Input:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <conio.h>
void main (void)
 {
  clrscr();
    int x, y;
  printf("Enter the value of x- and y-coordinate: ");
   scanf("%d %d", x, y);
   if(x == 0)
    {
     if(y == 0)
 printf("The point lie on origin.");
     else
 printf("The point lie on y-axis");
    }
   else if(x > 0)
     {
      if(y == 0)
 printf("The point lie on x-axis");
      else if(y > 0)
 printf("The point lies in 1st quadrant");
      else
 printf("The point lies in 4th quadrant");
     }
   else
   {
     if(y == 0)
 printf("The point lies on x-axis");
     else if(y > 0)
 printf("The points lies in 2nd quadrant");
     else
 printf("The points lies in 3rd quardant");
   }
     getch();
 }// main