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..

Sunday, August 31, 2014

Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter

0 comments
Hint: The area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter.
          Perimeter of rectangle = 2* (length + breadth)
          Area of rectangle = length * breadth

Code

#include<stdio.h>
#include<conio.h>
int main()
{
int length,breadth,perimeter,area;

printf("enter length and breadth: ");
scanf("%d%d",&length,&breadth);

perimeter=2*(length+breadth);
area=length*breadth;

if(area>perimeter)
printf("\narea is greater than perimeter");
else if(area<perimeter)
printf("area is lesser than perimeter");
else
printf("area and perimeter are equal");

printf ("\n\nPress any key to close.");

getch ();
return 0;
}

Output

area is greater than perimeter

Read More..

Write a program to output whether the person should be insured or not, his/her premium rate and maximum amount for which he/she can be insured.

0 comments
Description: An Insurance company follows following rules to calculate premium.
  1. If a person’s health is excellent and the person is between 25 and 35 years of age and lives in a city and is a male then the premium is Rs. 4 per thousand and his policy amount cannot exceed Rs. 2 lakhs.
  2. If a person satisfies all the above conditions except that the sex is female then the premium is Rs. 3 per thousand and her policy amount cannot exceed Rs. 1 lakh.
  3. If a person’s health is poor and the person is between 25 and 35 years of age and lives in a village and is a male then the premium is Rs. 6 per thousand and his policy cannot exceed Rs. 10,000.
  4. In all other cases the person is not insured.

Code

#include<stdio.h>
#include<conio.h>
int main()
{
char health,sex,area;
int age;

printf("Enter health condn(e/p),sex(m/f),area(c/v)&age\n");
scanf("%c %c %c %d",&health,&sex,&area,&age);

if(health=='e'&&sex=='m'&&area=='c'&&age>=25&&age<=35)
{
printf("\nInsured\n");
printf("\nPremium rate = Rs. 4 per 1,000\n");
printf("\nmaximum policy amount = Rs. 2,00,000");
}
else
if(health=='e'&&sex=='f'&&area=='c'&&age>=25&&age<=35)
{
printf("\nInsured");
printf("\nPremium Rate = Rs. 3 per 1000");
printf("\nMaximum policy amount = Rs. 1,00,000");
}
else
if(health=='p'&&sex=='m'&&area=='v'&&age>=25&&age<=35)
{
printf("\nInsured");
printf("\nPremium Rate = Rs. 6 per 1,000");
printf("\nMaximum policy amount = Rs. 10,000");
}
else
printf("\nYou cannot be insured\n");

printf ("\n\nPress any key to close.");

getch ();
return 0;
}

Output

insurance program c language

Read More..

A library charges a fine for every book returned late. For first 5 days the fine is 50 paise, for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after 30 days your membership will be cancelled. Write a program to accept the number of days the member is late to return the book and display the fine or the appropriate message.

4 comments

Code

#include<stdio.h>
#include<conio.h>
int main()
{
int days;
float fine;

printf("Number of days late: ");
scanf("%d",&days);

if(days<=30)
{
if(days<=5)
fine=0.5;
else if(days>5&&days<=10)
fine=1;
else if(days>10&&days<=30)
fine=5;
printf("you have to pay fine of Rs %f",fine);
}
else
printf("Your membership has been canceled");

printf ("\n\nPress any key to close.");

getch ();
return 0;
}

Output

library fine c language

Read More..

Write a program to receive marks in subject A and B and Output whether the student has passed, failed or is allowed to reappear in B.

0 comments
Description: A university has the following rules for a student to qualify for a degree with A as the
main subject and B as the subsidiary subject:
  1. He should get 55 percent or more in A and 45 percent or more in B.
  2. If he gets than 55 percent in A he should get 55 percent or more in B. However, he should get at least 45 percent in A.
  3. If he gets less than 45 percent in B and 65 percent or more in A he is allowed to reappear in an examination in B to qualify.
  4. In all other cases he is declared to have failed.

Code

#include<stdio.h>
#include<conio.h>
int main()
{
int pera,perb;

printf("Enter percentage of A and B: ");
scanf("%d%d",&pera,&perb);

if(pera>=55&&perb>=45)
printf("\n Student is passed");

else if(pera>=45&&pera<55&&perb>=55)
printf("\n Student is passed");

else if(perb<45&&pera>=65)
printf("Student is allowed to reappear in an exam");

else
printf("Student is failed");

printf ("\n\nPress any key to close.");

getch ();
return 0;
}

Output

if else in c language

Read More..

If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is isosceles, equilateral, scalene triangle.

0 comments

Description: An isosceles triangle is triangle with (at least) two equal sides.
                    An equilateral triangle is a triangle with all three sides of equal length
                    A scalene triangle is a triangle that has three unequal sides

Code

#include <stdio.h>
#include <conio.h>
int main()
{
int side1,side2,side3,h;

printf("Enter 3 sides of triangle: ");
scanf("%d%d%d",&side1,&side2,&side3);

if(side1==side2&&side1==side3)
{
printf("\nEquilateral triangle ");
}

else if(side1==side2||side2==side3||side1==side3)
{
printf("\ntriangle is isoscelses ");
}

else if(side1!=side2&&side2!=side3&&side1!=side3)
printf("\n triangle is scalene");
if(side1>side2&&side1>side3)
{
h=sqrt(side2*side2+side3*side3);

if(side1==h)
printf("\ntriangle is right angled triangle");

else
printf("\ntriangle is not right angled triangle");
}
else if(side2>side1&&side2>side3)
{
h=sqrt(side1*side1+side3*side3);

if(side2==h)
printf("\ntriangle is right angled triangle");

else
printf("\ntriangle is not right angled triangle");
}

else if(side3>side1&&side3>side2)
{
h=sqrt(side1*side1+side2*side2);

if(side3==h)
printf("\ntriangle is right angled triangle");
else
printf("\ntriangle is not right angled triangle");
}
printf ("\n\nPress any key to close.");

getch ();
return 0;
}

Output

triangle type c language

Read More..

Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degrees.

3 comments

Code

#include<stdio.h>
#include<conio.h>
int main()
{
int angle1,angle2,angle3,sum;
printf ("Enter three angles of triangle: ");
scanf("%d%d%d",&angle1,&angle2,&angle3);
sum=angle1+angle2+angle3;
if(sum==180)
{
printf("\nTriangle is valid");
}
else
printf("\nTriangle is invalid");

printf ("\n\nPress any key to close.");

getch ();
return 0;
}

Output

triangle validity c language

Read More..

If the ages of three students are input through the keyboard, write a program to determine the youpgest of the three.

0 comments

Code

#include<stdio.h>
int main()
{
int agea,ageb,agec;
printf ("Enter age of Ali, Shan and irfan: ");
scanf("%d%d%d",&agea,&ageb,&agec);
if(agea<ageb&&agea<agec)
{
printf("\nAli is youngest ");
}
else if(ageb<agea&&ageb<agec)
{
printf("\nShan is youngest");
}
else if(agec<agea&&agec<ageb)
{
printf("\nirfan is youngest");
}

printf ("\n\nPress any key to close.");

getch ();
return 0;
}

Output

find youngest person c language


Read More..

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