(Go original page? click here)
c++ programming examples |
7. Write a C++ program to accept a number and check the number is even or not. Prints 1 if the number is even or 0 if the number is odd
#include<stdio.h>
#include<conio.h>
void main ()
{
int a, b;
printf (“ Enter a number”);
scanf (“%d”, &a);
b= a % 2;
if (b==0)
printf (“1’);
else
printf (“0”);
getch ();
}
8. Write a C++ program that accepts three integers from the user and return true if the second number is greater than first number and third number is greater than second number. If "abc" is true second number does not need to be greater than first number.
#include<stdio.h>
#include<conio.h>
int small ( int a, int b, int c)
{
int true = 1;
if (b>a && c>b)
return true;
else;
}
void main ()
{
int a, b, c, d;
printf (“ Enter first number= ”);
scanf (“%d”, &a);
printf (“ \n Enter second number=”);
scanf (“%d”, &b);
printf (“ \n Enter third number=”);
scanf (“%d”, &c);
d = small (a, b, c);
getch ();
}
9. Write a C++ program that accepts three integers from the user and return true if two or more of them (integers) have the same rightmost digit. The integers are non-negative.
#include<stdio.h>
#include<conio.h>
int right ( int d, int e, int f)
{
int true = 1;
if (d==e || d==f ||e==f)
return true;
else;
}
void main ()
{
int a, b, c, d, e, f, g;
printf (“ Enter first integer= ”);
scanf (“%d”, &a);
printf (“ \n Enter second integer=”);
scanf (“%d”, &b);
printf (“ \n Enter third integer=”);
scanf (“%d”, &c);
d = a % 10;
e = b % 10;
f = C % 10;
g = right (d, e, f);
getch ();
}
10. Write a C++ program to find the number of integers within the range of two specified numbers and that are divisible by another number.
#include<stdio.h>
#include<conio.h>
void main ()
{
int a, b, c, d;
printf (“ Enter range lower number= ”);
scanf (“%d”, &a);
printf (“ \n Enter range higher number=”);
scanf (“%d”, &b);
printf (“ \n Enter third divisor=”);
scanf (“%d”, &c);
for (i= a; i<=b; i=i+1)
{
d = a % c;
If (d = 0);
Printf (“%d”, a);
else;
}
Printf (“ \n The following numbers are the number which is divisible by given divisor from the given range”);
getch ();
}
11. Write a C++ program that accepts two integer values from the user and return the larger values. However if the two values are the same, return 0 and return the smaller value if the two values have the same remainder when divided by 6
#include<stdio.h>
#include<conio.h>
int fun ( int a, int b )
{
int c, d, e;
if (a > b)
c = a;
else if (b>a)
c=b;
else if (a==b)
return 0;
else;
d = a % 6;
e = b % 6;
if (d==e)
{
If (a < b)
c == a;
else
c ==b;
}
return c;
}
void main ()
{
int a, b, r;
printf (“ Enter first number= ”);
scanf (“%d”, &a);
printf (“ \n Enter second number=”);
scanf (“%d”, &b);
r = fun (a, b) ;
printf(“ return value is %d”, r);
getch ();
}
12. Write a C++ program that accepts two integer values between 25 to 75 and return true if there is a common digit in both numbers.
#include<stdio.h>
#include<conio.h>
int common ( int a, int b )
{
int c, d, e, f, true=1 ;
c = a % 10;
d = a / 10;
e = b % 10;
f = b / 10;
if (c==d || c==e ||c==f || d==e || d==f || e==f)
return true;
else;
}
void main ()
{
int a, b, r;
printf (“ Enter first integer= ”);
scanf (“%d”, &a);
printf (“ \n Enter second integer=”);
scanf (“%d”, &b);
if ((a>= 25 && a<=75) &&(b>=25 && b<=75))
r = common (a, b);
else
printf (“your given number is either smaller than 25 or greater than 75”);
getch ();
}
13. Write a C++ program to calculate the modules of two numbers without using any built in modulus operator.
#include<stdio.h>
#include<conio.h>
void main ()
{
int a, b, c, d, e, f;
printf (“ Enter dividend number=”);
scanf (“%d”, &a);
printf (“enter the divisor=”);
scanf(“%d”, &b);
c = a/b;
d= c * b;
e =a – d;
printf (“ without using any built in modules the module of given number is =%d”, e);
getch ();
}
0 comments:
Post a Comment
Please do not enter any spam link in the comment box