C Program to Design Love Calculator

Posted in

C Program to Design Love Calculator
vinaykhatri

Vinay Khatri
Last updated on December 3, 2024

    You probably have calculated love percentage for yourself or your friend in your school and/or college. This article details a C program to design a love calculator.

    The C program asks the user to enter his/her and his/her partner's name. Then the program will use logic and calculate the love percentage depending on the entered names. The logic of the love calculator is basically taken from the pen and paper games , and this program is only for fun.

    Although nothing serious, it will help you understand how do and for loops work.

    Steps to Create the Program

    1. Ask the user to enter his or her name. (Sam)
    2. Next, ask the user to enter his or her partner's name. (Sofi)
    3. According to the alphabets, take the sum of user, his partner name, and LOVE. (Sam = 19 + 1 +13 = 33 ), (Sofi = 19 + 15 + 6 + 9 = 49 ), and (Love = 12 + 15 + 22 + 5 =  54)
    4. Add each digit of the sums individually. (Sam = 33 = 3+3= 6 ) and (Sofi = 49 = 4+9= 13 )
    5. Use the addition operation to Sum of Digits. ( 6 + 13 = 19 )
    6. Add the Sum of Digit and Love. ( 19 + 54 = 73 )
    7. Love percentage would be the sum of Love and Sum of Digits. (73% for Sam and Sofi)

    C Program to Design a Love Calculator

    #include<ctype.h>
    #include<stdio.h>
    #include <conio.h>
    #include<string.h>
    
    int Sum_of_Digits(int digit)
        {
           int sum =0;
           while(digit > 0)
              {
                  sum +=(digit%10);
                  digit/=10;
              }
            return sum;
        }
    
    void main()
     {
          char name[100], partner[100];
          int p,ns = 0, ps =0,love =54,i,j,love_percentage, opt;
          clrscr();
    
         do
         {
             printf("Enter Your Name: ");
             gets(name);
    
             printf("Enter Your Partner Name: ");
             gets(partner);
    
             for(i =0; i<strlen(name); i++)
                {
                   ns += (tolower(name[i])-96);
                }
    
             for(j = 0; partner[j] != '\0'; j++)
                {
                    ps+=(tolower(partner[j])-96);
                }
              p= Sum_of_Digits(ns);
              love_percentage = (Sum_of_Digits(ns)+Sum_of_Digits(ps)+love);
              printf("The Love Percentage is %d", love_percentage);
              printf("\nPress 1 To continue or 0 to Exit: ");
              scanf("%d",&opt);
    
        }while(opt!=0);
        getch();
    }

    Output:

    Enter your Name: Sam
    Enter your Partner Name: Sofi
    The Love Percentage is 73
    Press 1 To continue or 0 to Exit: 0

    Conclusion

    That concludes the simple C program to design a love calculator. It is a fun programming exercise that will help you to play with the loops - in this case, do and for - and understand them better.

    People are also reading:

    Leave a Comment on this Post

    0 Comments