Sunday, June 14, 2020

sum of closest float values in c

Three floating point values are passed as the input to the program. The program must print the sum of two numbers which are closest to each other with the precision up to 2 decimal places.

Example Input/Output 1:
Input:
2.45 6.78 2.35

Output:
4.80

solution:

int main()

{

float a,b,c;

scanf("%f %f %f",&a,&b,&c);

if(abs(a-b)<abs(b-c) && abs(a-b)<abs(a-c)){

    printf("%.2f",a+b);

}

else if(abs(b-c)<abs(a-b) && abs(b-c)<abs(a-c)){

    printf("%.2f",b+c);

}

else{

    printf("%.2f",a+c);

}


}

largest floating point value in c

Three floating point numbers are passed as the input to the program. The program must print the number which has the largest floating point value in it.

Example Input/Output 1:
Input:
12.45 7.89 30.56

Output:
7.89


solution:

int main()

{

float a,b,c;

scanf("%f %f %f",&a,&b,&c);

float m=a-(int)a;

float n=b-(int)b;

float o=c-(int)c;

if(m>n && m>o){

    printf("%.2f",a);

}

else if(n>m && n>o){

    printf("%.2f",b);

}

else{

    printf("%.2f",c);

}


}

palindrome missing alphabet in c

String S which is a palindrome is passed as the input. But just one alphabet A is missing in S. The program must print the missing alphabet A.
Note: The FIRST alphabet of S will always be present.

Input Format:
The first line contains S.

Output Format:
The first line contains the missing alphabet A.

Example Input/Output 1:
Input:
malayaam

Output:
l

Solution:
int main()
{
char s[100];
scanf("%s",s);
int l=strlen(s)-1;
for(int i=0;i<strlen(s);i++){
    if(s[i]!=s[l]){
        printf("%c",s[i]);
        break;
    }
    else{
        l--;
    }
}
}

print string till character in c

input:
manager
e
output:
manag 

For clear explanation of this program check  https://youtu.be/GQzWQzFts8k

solution:
int main()
{
char s[100],c;
scanf("%s\n%c",s,&c);
for(int i=0;i<strlen(s);i++){
    if(s[i]!=c){
        printf("%c",s[i])}
    else{
        break;}    
}
}


Saturday, June 13, 2020

both vowel or consonant alphabets in c

The program must accept two lower case alphabets A and B as the input. The program must print Yes as the output if both the alphabets are either vowels or consonants. Else the program must print No as the output.

Example Input/Output 1:
Input:
a m

Output:
No



solution:


int main(){
char a,b;
scanf("%c %c,&a,&b);
if(  (  (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')&&(b=='a'||b=='e'||b=='i'||b=='o'||b=='u')  ) || ( (a!='a'&&a!='e'&&a!='i'&&a!='o'&&a!='u')&&(b!='a'&&b!='e'&&b!='i'&&b!='o'&&b!='u') ) )
{
    pritnf("Yes");    
}
else{
printf("No");
}
}

four alphabets alphabetical order in c

The program must accept four alphabets as the input. If the four alphabets are in alphabetical order print YES as the output. Else print NO as the output.

Example Input/Output 1:
Input:
a c e r

Output:
YES


solution:

int main()
{
char a,b,c,d;
scanf("%c %c %c %c",&a,&b,&c,&d);
if(a<b&&b<c&&c<d){
    printf("yes");
}
else{
    printf("no");
}
}

least significant bit is 0 or 1


The program must accept an integer N as the input. The program must print YES if the least significant bit of N is set as 1. Else the program must print NO as the output.

Example Input/Output 1:
Input:
11

Output:
YES

solution:
int main()
{
int a;
scanf("%d",&a);
if(a&1==1){
    printf("Yes");}
else{
    printf("no");
}
}

concatenate unit digits are equal in c

The program must accept an integer M and a digit N as the input. The program must print the concatenated value of M and N as the output if the unit digit of M is not equal to N. Else the program must print M without it’s unit digit as the output.

Example Input/Output 1:
Input:
981 2

Output:
9812



solution:
int main()
{
int a,b;
scanf("%d %d",&a,&b);
if(abs(a)%10!=abs(b)%10){
    printf("%d%d",a,b);
}
else{
    printf("%d",a/10);
}
}

Table marked price in c

input:
5225 5
output:
5500

int main()
{
int a,b;
scanf("%d %d",&a,&b);
float c=1-(float)b/100;
printf("%.f",a/c);
}

Interchanged unit digits in c

Hello Guys in this post we will be solving a program in c to interchange the unit digits of two numbers.

input:
234 456
output:
236 454

Understanding the Program Structure:
In this program we need to get two input from the console, which is of type int seperated by Space.
The output should be the same format with the interchanged unit digits of two numbers with space in between.

Key Concept of the Program:
Unit Digit: Unit digit is the last digit of any number, It is the digit on the One's Place of a number. In program we obtain the unit digit by using modulo operator.

Procedure:
First we are getting two inputs from the user which are a and b, which stores the two integer values using "scanf" function.

                                                    scanf("%d %d",&a,&b);

Next there may be two conditions for our program, the first one is what if the numbers are less than 10?
So we check them by the if condition,
                
                                                    if(a<10 && b<10){
                                                        printf("%d %d",b,a);}

Here we are checking whether the numbers are less then 10,if they are less then we are just printing the numbers in the other order.
Next logic will be for the numbers greater than 10,so if they are greater than 10,we will be finding their unit digits by the following ways,
n%10- gives the last number
n%100 - gives the last before number and so on.
a%10-unit digit of a
b%10-unit digit of b
Then we are printing the number except the unit digit. This can be done by dividing the number by 10.
n/10 - gives value except last number
a/10 - gives value except last number of a
b/10 - gives value except last number of b

                                    else{
                                    printf("%d%d %d%d",a/10,b%10,b/10,a%10);
                                    }
                                            
Program:

int main(){
int a,b;
scanf("%d %d",&a,&b);
if(a<10 && b<10){
    printf("%d %d",b,a);}
else{
    printf("%d%d %d%d",a/10,b%10,b/10,a%10);}
}

Hope you understood the program. If any queries please reach out to me or just comment in this post .

Friday, June 12, 2020

matrix sum of corner elements in c


Hello guys in this Post we will be seeing a program to calculate the sum of the corner elements using c language. 

First we declare two variables n and sum, where n is the number of rows and columns for our matrix and sum is the variable which will Store the added values. Initially we will assign it to 0.

                          int n,sum=0;


We will be getting input from the user in the console. By using the "scanf" function in C.
The scanf function accepts various Parameters in C.
Like we have %d - for getting integer input
                      %c - for getting character input
                      %s - for getting String input
these are some commonly used functions in scanf.
In this Program we will be using the %d for getting the input from the user a integer

                                                   scanf("%d",&n);

We will declare an array in c named arr which will store the value of the matrix. It contains two fields one is for the number of rows and other is for the number of columns.

                                                    int arr[n][n];

After Creating the array we will get the array element from the user. This can be achieved by using the for loop. Two for loops are used for this process. One is for the Number of rows and other is for the number of columns. We use Scanf for storing the value.

                                                    for(int  i=0;i<n;i++){

                                                        for(int j=0;j<n;j++){

                                                        scanf("%d",arr[i][j]);
                                                          }
                                                         }

Finally we move to the Logic part to add the corner elements.
In a matrix,
                   First row elements have the index of 0 in the array [i=0].
                   First column element have the index of 0 in the array[[j=0].
                   Last row elements have the index of n-1 in the array [i=n-1].
                   Last column elements have the index of n-1 in the array [j=n-1].
So if we map the values of these indexes we will get the corner element. So we create another for loop and by using the if statement and by combining these indexes by using or operator. We are finding that the element is the corner element and finally we are adding those values by using the += operator.

           for(int i=0;i<n;i++){
                                   for(int j=0;j<n;j++){
                                   if((i==0 && j==0)||(i==0 && j==n-1)||(i==n-1&&j==0)||(i==n-1&&j==n-1)){
                                          sum+=arr[i][j];
                                    }
                                    }}

At Last, We are printing the value of sum to the console.

Program: 

int main()
{
int n,sum=0;

scanf("%d",&n);

int arr[n][n];

for(int  i=0;i<n;i++){

    for(int j=0;j<n;j++){
        scanf("%d",arr[i][j]);}
}
for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        if((i==0 && j==0)||(i==0 && j==n-1)||(i==n-1&&j==0)||(i==n-1&&j==n-1)){
                sum+=arr[i][j];}
}
}
printf("%d",sum);
}

Please feel free to comment below if you have any queries. You can also Reach me out  anytime.

MFIB Comma Separated Integers

Fill in the blanks with code so that the program must accept three integers separated by a space and print them separated by a comma as the ...