Showing posts with label Add. Show all posts
Showing posts with label Add. Show all posts

C Program To Add Digits Of Entered Number

How to write a C Program To Add Digits Of Entered Number in C Programming Language ?


Solution:

C Program To Add Digits Of Entered Number

#include<stdio.h>

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

   printf("Enter an integer\n");
   scanf("%d",&n);

   while( n != 0 )
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }

   printf("Sum of digits of entered number = %d\n",sum);

   return 0;
}

C Program To Add n Numbers

How to write a C Program To Add n Numbers in C Programming Language ?


Solution:

C Program To Add n Numbers

#include <stdio.h>

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

   printf("Enter the number of integers you want to add\n");
   scanf("%d", &n);

   printf("Enter %d integers\n",n);

   for ( c = 1 ; c <= n ; c++ )
   {
      scanf("%d",&value);
      sum = sum + value;
   }

   printf("Sum of entered integers = %d\n",sum);

   return 0;
}

C program to add first seven terms of the following series using a for loop: (1/1!) + (2/2!) + (3/3!) + .....

How to Write a C Program to add first seven terms of the following series using a for loop:

(1/1!) + (2/2!) + (3/3!) + .....

This C Program to add first seven terms of the following series using a for loop: (1/1!) + (2/2!) + (3/3!) + .....


  1. #include<stdio.h>
  2. main()
  3. {
  4. float result, temp_calc, num, temp, factorial;
  5. result=0;
  6. for (num=1; num<=7; num++)
  7. {
  8. for (temp=1, factorial=1; temp<=num; temp++)
  9. {
  10. factorial = factorial*temp;
  11. temp_calc = (num/factorial);
  12. }
  13. result=result+temp_calc;
  14. }
  15. printf("(1/1!) + (2/2!) + (3/3!) + (4/4!) + (5/5!) + (6/6!) + (7/7!) = %f", result);
  16. }

C Program to Adds literal to list of literals

How to write a C Program to Adds literal to list of literals in C Programming Language ?

Adds literal to list of literals.
Param token Token which contains literal value.
Return Pointer to the list of literals on current literal.

  1. /**
  2.  *
  3.  * Adds literal to list of literals.
  4.  *
  5.  * \param token Token which contains literal value.
  6.  *
  7.  * \return Pointer to the list of literals on current literal.
  8.  *
  9.  */
  10. static void* add_literal(token_t* token) {
  11.  
  12.         void* literal;
  13.  
  14.         if (token -> lexeme == T_DOUBLE)
  15.                 literal = get_literal_double(literals, token -> value -> double_value);
  16.  
  17.         else if (token -> lexeme == T_INT)
  18.                 literal = get_literal_int(literals, token -> value -> int_value);
  19.  
  20.         else if (token -> lexeme == T_STRING)
  21.                 literal = get_literal_string(literals, &token -> value -> string_value);
  22.  
  23.         else
  24.                 return NULL;
  25.  
  26. }

C Program to Add-Subtract Series 5-10+15

How to write a C Program to Add-Subtract Series 5-10+15 in C Programming Language ?


Solution:
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int n, sum=0,i,number=5,sign=1;
  5.     for(i=1; i<=9; i++) {
  6.         sum = sum + number*sign;
  7.         number = number + 5;
  8.         sign = sign*(-1);
  9.     }
  10.     printf("%d", sum);
  11. }

C Program to Add Series 1+3+5

How to write a C Program to Add Series 1+3+5 in C Programming Language ?


Solution:
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int n, sum=0,i;
  5.     for(i=1; i<=9; i=i+2) {
  6.         sum = sum+i;
  7.     }
  8.     printf("%d", sum);
  9. }

C program that lets the user choose if he wants to add a item to the shopping list or print it out

How to write a C program that lets the user choose if he wants to add a item to the shopping list or print it out in C Programming Language ?

Solution:

  1. /*C program that lets the user choose if he wants to add a item to the shopping list or print it out.*/

  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include <windows.h>
  9.  
  10. typedef struct{
  11.  
  12.     char name[20];
  13.     char unit[20];          // SKAPAR EN STRUCT OCH DÖPER DEN TILL SHOPPINGLIST MED HJÄLP AV TYPEDEF FÖR ATT ANVÄNDA DEN SMIDIGARE
  14.     float amount;
  15.     int uniqueid;
  16.  
  17.  
  18. }shoppingList;
  19.  
  20.  
  21. int error1(char*string)
  22. {
  23.     int i=0;                //SKAPAR FUNTIONEN ERROR 1 SOM TAR EN STRÄNG SOM ARGUMENT
  24.     int length=0;
  25.     length=strlen(string);
  26.  
  27.     for(i=0;i<length;i++)
  28.     {                                       //ANVÄNDER MIG AV ISALPHA OCH TITTAR OM DET INTE ÄR BOKSTÄVER A-Z RETURNAR VI -1 SOM ÄR VÃ…RAN FELHANTERING
  29.             if(!isalpha(string[i]))
  30.             {
  31.                 return -1;
  32.             }
  33.     }
  34.  
  35.     return 0;                    // ANNARS RETURNEAR VI 0 OCH DÃ… STÄMMER ALLT
  36. }
  37.  
  38. int error2(char*str)
  39. {
  40.     int i;
  41.     int length;                 // SKAPAR FUNKTIONEN ERROR 2 SOM OCKSÃ… TAR EN STRÄNG SOM ARGUMENT
  42.     int count = 0;
  43.     length=strlen(str);
  44.  
  45.     for(i=0;i<length;i++)
  46.     {
  47.         if (str[i] == '.')      // TITTAR OM DET FINNS EN PUNKT I STRÄNGEN, OM DET FINNS SÃ… PLUSSAR VI PÃ… COUNT MED 1 FÖR VARJE PUNKT SOM FINNS
  48.             {
  49.                 count++;
  50.  
  51.                 if (str[0] == '.' || str[length-1] == '.' || count > 1)
  52.                 {
  53.                                                                                     /* TITTAR OM FÖRSTA ELLER SISTA TECKNET I STRÄNGEN ÄR EN PUNKT ELLER OM DET FINNS MER ÄN EN PUNKT */
  54.                 {                                                                   /*  OCH DÃ… RETURNERAR VI -1 SOM ÄR VÃ…RAN FELHANTERING */
  55.                     return -1;
  56.                 }
  57.             }
  58.             else if(!isdigit(str[i]))   // ANVÄNDER MIG AV ISDIGIT OCH TITTAR OM DET INTE ÄR SIFFROR MELLAN 0-9 SÃ… RETURNERAR VI OCKSÃ… -1
  59.             {
  60.                 return -1;
  61.             }
  62.     }
  63.  
  64.     return 0;                           // ANNARS RETUNERAR VI O DÃ… ALLT STÄMMER
  65.  
  66. }
  67.  
  68.  
  69. shoppingList *additems(shoppingList*list,int count)
  70. {
  71.     char temparray[20];                             // SKAPAR FUNKTIONEN ADDITEMS OCH DEN TAR EN STRUCTPEKARE OCH EN INT SOM ARGUMENT
  72.     int i;
  73.     HANDLE hConsole;
  74.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  75.  
  76.     list = realloc(list, count*sizeof(shoppingList));       // SÄTTER STRUCT PEKAREN TILL ATT VARA LIKA MED REALLOC FÖR ATT ALLOKERA
  77.  
  78.     i=count;
  79.  
  80.     list[i-1].uniqueid=i;
  81.  
  82.         do
  83.         {
  84.  
  85.                 printf("\nName on item %d:",count);
  86.                 fflush(stdin);
  87.                 gets(list[i-1].name);
  88.  
  89.                 if(error1(list[i-1].name) == -1)
  90.                 {
  91.                     SetConsoleTextAttribute(hConsole,12);
  92.                     printf("-> Invalid input, try again! <-\n");
  93.                     SetConsoleTextAttribute(hConsole,7);
  94.  
  95.                 }
  96.  
  97.         }while(error1(list[i-1].name));
  98.  
  99.  
  100.                     do
  101.                     {
  102.  
  103.  
  104.                                 printf("Amount:");
  105.                                 fflush(stdin);
  106.                                 gets(temparray);
  107.  
  108.                                 if(error2(temparray) == -1)
  109.                                 {
  110.                                     SetConsoleTextAttribute(hConsole,12);
  111.                                     printf("-> Invalid input, try again! <-\n");
  112.                                     SetConsoleTextAttribute(hConsole,7);
  113.                                 }
  114.  
  115.                     }while(error2(temparray) == -1);
  116.  
  117.                     list[i-1].amount=atof(temparray);
  118.  
  119.  
  120.                                     do
  121.                                     {
  122.                                                 printf("Unit:");
  123.                                                 fflush(stdin);
  124.                                                 gets(list[i-1].unit);
  125.  
  126.                                                 if(error1(list[i-1].unit) == -1)
  127.                                                 {
  128.                                                     SetConsoleTextAttribute(hConsole,12);
  129.                                                     printf("-> Invalid input try again! <-\n");
  130.                                                     SetConsoleTextAttribute(hConsole,7);
  131.                                                 }
  132.  
  133.                                     }while(error1(list[i-1].unit) == -1);
  134.  
  135.     return list;
  136. }
  137.  
  138.  
  139. void printOut(shoppingList*list)
  140. {
  141.     int i=0;
  142.  
  143.  
  144.     printf("\n%-2d%-10s %-12.2g %-8s",list[i].uniqueid,list[i].name,list[i].amount,list[i].unit);
  145.  
  146.  
  147.  
  148. }
  149.  
  150.  
  151. void main(void)
  152.  
  153. {
  154.     int choice=0;
  155.     int count=0;
  156.     int i=0;
  157.     shoppingList*thelist;
  158.  
  159.     thelist=(shoppingList*)malloc(sizeof(shoppingList));
  160.  
  161.  
  162.     do
  163.     {
  164.             printf("\n");
  165.             printf("Menu\n1 - Add a item to the shopping list\n2 - Print out the shopping list\n3 - End the program\n");
  166.             scanf("%d",&choice);
  167.  
  168.             if(choice==1)
  169.             {
  170.                     count++;
  171.                     thelist = additems(thelist,count);
  172.  
  173.             }
  174.  
  175.  
  176.                     else if(choice==2)
  177.                     {
  178.  
  179.                             system("cls");
  180.                             printf("Shopping list:");
  181.  
  182.                             for(i=0;i<count;i++)
  183.                             {
  184.  
  185.                                 printOut(&thelist[i]);
  186.  
  187.                             }
  188.  
  189.                             printf("\n");
  190.                     }
  191.  
  192.  
  193.                             else if(choice == 3)
  194.                             {
  195.                                     break;
  196.  
  197.                             }
  198.  
  199.  
  200.  
  201.     }while(choice == 1 || 2);
  202.  
  203.  
  204.     free(thelist);
  205.     thelist=NULL;
  206.  
  207.  
  208. }