Friday, September 5, 2014

check if the number is in the array or not

0 comments

Problem

Write a program that asks the user to input 10 integers and stored them in an array and then again asks user to input any single number that store in variable “Num” and program check whether the Num is in the array or not and then print "Number “Num” is in the array" or “Num” is not in the array".

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
 int arr[10];
 int i,num;
 for (i=0;i<10;i++)
 {
  printf ("Enter number : ");
  scanf ("%d",&arr[i]);
 }
 printf("\nEnter number to check: ");
 scanf ("%d",&num);
 for (i=0;i<10;i++)
 {
  if (num==arr[i])
  {
   printf ("Entered number is in the array\n");
   break;
  }   if (i == 9)
   printf ("Entered number is not in the array\n");
 }
 system("pause");
}

Sample Output

check if number exists in an array

Read More..

display the greatest, medium and smallest number using the notation a,b, and c.

0 comments

Problem

We will read in three integers: a, b and c. We want to construct the logic so that, no matter how these numbers were provided by the user, the output will always be the three numbers printed in order. For example, if we read in 40, 20, 60 into a, b and c respectively, then our logic would printb, a and c in that order. This will give an output of 20, 40 and 60. Or if we read in 70, 10 and 60, then the output would be b, c and a in that order. This will give an output of 10, 60 and 70.

Solution - Code

#include<stdio.h>
main()
{
 int a,b,c;
 printf("Enter 1st Value");
 scanf("%d",&a);

 printf("Enter 1st Value");
 scanf("%d",&b);

 printf("Enter 1st Value");
 scanf("%d",&c);


 if(a<b && a<c)
  printf("a");
 else if(b<a && b<c)
  printf("b");
 else
  printf("c");

 if(a<b && a>c || a>b && a<c)
  printf("a");
 else if(b<a && b>c ||b>a && b<c)
  printf("b");
 else
  printf("c");


 if(a>b && a>c)
  printf("a");
 else if(b>a && b>c)
  printf("b");
 else
  printf("c");

 system("pause");
}

Sample Output


Read More..

display all elements of an array, use pointers to find maximum and minimum element in an array.

0 comments

Problem

Write three user define functions:
  1. void display(int *p, int size);
    The first parameter is the base address of an array and the second parameter is the size of array
    (No of elements in an array). The function displays all the elements in the array. Call this
    function from main function.
  2. int max_value (int *p, size);
    this function returns the maximum element of the array pointed by the pointer p. second
    parameter specify the size of array.
  3. int min_value (int *p, size);
    this function returns the minimum element of the array pointed by the pointer p. second
    parameter specify the size of array.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void display(int *,int);
int max_value (int *,int);
int min_value (int *,int);
main()
{
 int max,min,size,i,x,sum=0,avg;
 int num[5];
 size = 5;
 display(num,size);
 max = max_value (num,size);
 printf ("Maximum element : %d\n",max);
 min = min_value (num,size);
 printf ("Minimum element : %d\n",min);
 system("pause");
}
void display(int *p,int size)
{
 int i,num[size],sum=0,avg;
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  sum = sum + num [i];
 }
 avg =(sum/5);
 printf ("sum = %d\n",sum);
 printf ("average = %d\n",avg);
}
int max_value (int *p,int size)
{
 int num[size];
 int i,max=num[0];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  if (max < num[i])
  max=num[i];
 }
 return max;
}
int min_value (int *p,int size)
{
 int num[size];
 int i,min=num[4];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=5;i>=0;i--)
 {
  if (min > num[i])
  min=num[i];
 }
 return min;
}

Sample Output

find maximum and minimum element c language

Read More..

find sum and average of subjects

0 comments

Problem

Write a program that asks the user to input marks in five different subjects. Then find the sum and average of those marks and print them.
Note: Solve the above problem by using array

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
 int i,x,sum=0,avg;
 int num[5];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  sum = sum + num [i];
 }
 avg =(sum/5);
 printf ("sum = %d\n",sum);
 printf ("average = %d\n",avg);
 system("pause");
}

Sample Output

sum and average array c language

Read More..

how much money will you receive on day 15 if you are given 0.20 paisa's on 1st day and double on each day

0 comments

Problem

Suppose you are given ten paisa on day 1 and on day 2 you are given twice as much. If each day you are given twice as much money as on the previous day, then on day 15, how much money you will receive? Build a C program to find the solution.
Total money for day 1 is Rs. 0.20
Total money for day 2 is Rs. 0.40
Total money for day 3 is Rs. 0.80
Total money for day 4 is Rs. 1.60
………….

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
 int i;
 float x=0.20;
 printf("Total money for day 1 = Rs.0.20\n");
 for(i=1;i<=15;i++)
 {
  x = (x*2);
  printf("Total money for day %d = %f\n",i,x);
 }
 system("pause");
}

Sample Output

calculate amount for day 15


Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact