Program:
#include <stdio.h>
int main(){
int a, b, c;
printf("Enter a,b,c: \n");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c) {
printf("a is Greater than b and c");
}
else if (b > a && b > c) {
printf("b is Greater than a and c");
}
else if (c > a && c > b) {
printf("c is Greater than a and b");
}
else {
printf("all are equal or any two values are equal");
}
return 0;
}
Output:
Enter a,b,c: 3 5 8
c is Greater than a and b
Explanation with examples:
Consider three numbers a=5,b=4,c=8
if(a>b && a>c) then a is greater than b and c
now check this condition for the three numbers 5,4,8 i.e.
if(5>4 && 5>8) /* 5>4 is true but 5>8 fails */
so the control shifts to else if condition
else if(b>a && b>c) then b is greater than a and c
now checking this condition for 5,4,8 i.e.
else if(4>5 && 4>8) /* both the conditions fail */
now the control shifts to the next else if condition
else if(c>a && c>b) then c is greater than a and b
now checking this condition for 5,4,8 i.e.
else if(8>5 && 8>4) /* both conditions are satisfied */
Thus c is greater than a and b.
Program:
#include <stdio.h>
int main() {
int a[10];
int i;
int greatest;
printf("Enter ten values:");
//Store 10 numbers in an array
for (i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
//Assume that a[0] is greatest
greatest = a[0];
for (i = 0; i < 10; i++) {
if (a[i] > greatest) {
greatest = a[i];
}
}
printf("\nGreatest of ten numbers is %d", greatest);
return 0;
}
Output:
Enter ten values: 2 53 65 3 88 8 14 5 77 64 Greatest of ten numbers is 88
Explanation with example:
Entered values are 2, 53, 65, 3, 88, 8, 14, 5, 77, 64
They are stored in an array of size 10. let a[] be an array holding these values.
/* how the greatest among ten numbers is found */
Let us consider a variable 'greatest'. At the beginning of the loop, variable 'greatest' is assinged with the value of
first element in the array greatest=a[0]. Here variable 'greatest' is assigned 2 as a[0]=2.
Below loop is executed until end of the array 'a[]';.
for(i=0; i<10; i++)
{
if(a[i]>greatest)
{
greatest= a[i];
}
}
For each value of 'i', value of a[i] is compared with value of variable 'greatest'. If any value greater than the value
of 'greatest' is encountered, it would be replaced by a[i]. After completion of 'for' loop, the value of variable
'greatest' holds the greatest number in the array. In this case 88 is the greatest of all the numbers.
A prime number is a natural number that has only one and itself as factors. Examples: 2, 3, 13 are prime
numbers.
Program:
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number n: \n");
scanf("%d", &n);
/*logic*/
for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
if (c == 2) {
printf("n is a Prime number");
}
else {
printf("n is not a Prime number");
}
return 0;
}
Output:
Enter any number n: 7
n is Prime
Explanation with examples:
consider a number n=5
for(i=0;i<=n;i++) /* for loop is executed until the n value equals i */
i.e. for(i=0;i<=5;i++) /* here the for loop is executed until i is equal to n */
1st iteration: i=1;i<=5;i++
here i is incremented i.e. i value for next iteration is 2
now if(n%i==0) then c is incremented
i.e.if(5%1==0)then c is incremented, here 5%1=0 thus c is incremented.
now c=1;
2nd iteration: i=2;i<=5;i++
here i is incremented i.e. i value for next iteration is 3
now if(n%i==0) then c is incremented
i.e.if(5%2==0) then c is incremented, but 5%2!=0 and so c is not incremented, c remains 1
c=1;
3rd iteration: i=3;i<=5;i++
here i is incremented i.e. i value for next iteration is 4
now if(n%i==0) then c is incremented
i.e.if(5%3==0) then c ic incremented, but 5%3!=0 and so c is not incremented, c remains 1
c=1;
4th iteration: i=4;i<=5;i++
here i is incremented i.e. i value for next iteration is 5
now if(n%i==0) then c is incremented
i.e. if(5%4==0) then c is incremented, but 5%4!=0 and so c is not incremented, c remains 1
c=1;
5th iteration: i=5;i<=5;i++
here i is incremented i.e. i value for next iteration is 6
now if(n%i==0) then c is incremented
i.e. if(5%5==0) then c is incremented, 5%5=0 and so c is incremented.
i.e. c=2
6th iteration: i=6;i<=5;i++
here i value is 6 and 6<=5 is false thus the condition fails and control leaves the for loop.
now if(c==2) then n is a prime number
we have c=2 from the 5th iteration and thus n=5 is a Prime number.
If a number, which when read in both forward and backward way is same, then such a number is called a
palindrome number.
Program:
#include <stdio.h>
int main() {
int n, n1, rev = 0, rem;
printf("Enter any number: \n");
scanf("%d", &n);
n1 = n;
/* logic */
while (n > 0){
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
if (n1 == rev){
printf("Given number is a palindromic number");
}
else{
printf("Given number is not a palindromic number");
}
return 0;
}
Output:
Enter any number: 121
Given number is a palindrome
Explanation with an example:
Consider a number n=121, reverse=0, remainder;
number=121
now the while loop is executed /* the condition (n>0) is satisfied */
/* calculate remainder */
remainder of 121 divided by 10=(121%10)=1;
now reverse=(reverse*10)+remainder
=(0*10)+1 /* we have initialized reverse=0 */
=1
number=number/10
=121/10
=12
now the number is 12, greater than 0. The above process is repeated for number=12.
remainder=12%10=2;
reverse=(1*10)+2=12;
number=12/10=1;
now the number is 1, greater than 0. The above process is repeated for number=1.
remainder=1%10=1;
reverse=(12*10)+1=121;
number=1/10 /* the condition n>0 is not satisfied,control leaves the while loop */
Program stops here. The given number=121 equals the reverse of the number. Thus the given number is a
palindrome number.
Palindrome is a string, which when read in both forward and backward way is same.
Example: radar, madam, pop, lol, rubber, etc.,
Program:
#include <stdio.h>
#include <string.h>
int main() {
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string: \n");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
printf("%s is not a palindrome\n", string1);
}
else {
printf("%s is a palindrome\n", string1);
}
return 0;
}
Output:
Enter a string: radar
"radar" is a palindrome
Explanation with example:
To check if a string is a palindrome or not, a string needs to be compared with the reverse of itself.
Consider a palindrome string: "radar",
---------------------------
index: 0 1 2 3 4
value: r a d a r
---------------------------
To compare it with the reverse of itself, the following logic is used:
0th character in the char array, string1 is same as 4th character in the same string.
1st character is same as 3rd character.
2nd character is same as 2nd character.
. . . .
ith character is same as 'length-i-1'th character.
If any one of the above condition fails, flag is set to true(1), which implies that the string is not a palindrome.
By default, the value of flag is false(0). Hence, if all the conditions are satisfied, the string is a palindrome.
0 comments:
Post a Comment