Showing posts with label First. Show all posts
Showing posts with label First. Show all posts

C Program To Calculate First Numbers Using Recursion

How to write a C Program To Calculate First Numbers Using Recursion in C Programming Language ?

Solution For C Program :

/*C Program To Calculate First Numbers Using Recursion*/

#include<stdio.h>
void main()
{
int sum(int);
int n;
printf("\nEnter Any Number : ");
scanf("%d", &n);
printf("\nSum of N values in the Series is : %d.", sum(n));
}
int sum(int x)
{
int a, b;
if(x == 0) return (0);
if(x == 1) return (1);
a = x - 1;
b = x + sum(a);
return(b);
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program to Sum of The First and Last Digit Of 'n' Digit Number

How to write a C Program to Sum of The First and Last Digit Of  'n' Digit Number  in C Programming Language ?


  1. int main()
  2. {
  3. int num,len,last;
  4. printf("\nEnter n digit number \n");
  5. scanf("%d",&num);
  6. last=num%10;
  7. while(num>0)
  8. {
  9. num=num/10;
  10. len=len+1;
  11. }
  12. num=num/(pow(10,len-1));
  13. num=num%10;
  14. printf("the result is %d",last+num);
  15. getch();
  16. return 0;
  17.  
  18. }

C Program to Sum of First and Last Digits of a Four-Digit number

How to write a C Program to Sum of First and Last Digits of a Four-Digit number in C Programming Language ?


Solution:

This program is based on the "sum of digits" program discussed previously.
This program also has some usage of the Modulus Operator.

If a four-digit number is input through the keyboard,
write a program to obtain the sum of the first and the last digit of this number.
/*HINT: If a number is divided using % , then the number to the right side of the decimal point is the result. (This applies only to integers.) */

  1. #include<stdio.h>
  2.  
  3. main ()
  4.  
  5. {
  6.  
  7. int number, last_digit, first_digit, total;
  8.  
  9. printf (" Enter the number which is to be operated on: ");
  10.  
  11. scanf ("%d", &number);
  12.  
  13.  
  14.  
  15. last_digit = number % 10;
  16.  
  17. total = last_digit;
  18.  
  19.  
  20.  
  21. first_digit = (number / 1000) % 10;
  22.  
  23. total = total + first_digit;
  24.  
  25.  
  26.  
  27. printf ("The total of the first and the last digit of the entered number is: %d", total);
  28.  
  29.  
  30.  
  31. }

C Program that first prompts you for the number of customers you would like to enter in

How to Write a program that first prompts you for the number of customers you would like to enter in.


The program will then allow you to enter the customer information, namely:
names(first and last),
address,
phone numbers(home and cell).

Solution:
/*Write a program that first prompts you for the number of customers you would like to enter in.

The program will then allow you to enter the customer information, namely:
names(first and last),
address,
phone numbers(home and cell).

Your program should then print out the customers, sorted in order by lastname, and then phone number.
You should store the customers as a dynamic array of structures.
The strings for the names etc. should be dynamically allocated also.Use of pointers absolutely
extensively and throughout your solution is required*/

#include <string.h>
#include <stdio.h>

typedef struct {
char * firstName;
char * lastName;
char * address;
int homePhone;
int cellPhone;
} customer;

void newCustomer(customer * cust);
void printCustomer(customer * cust);
char *getString();
int getNumber();

int main()
{


// Ask for number of customers to input
printf("How many customers would you like to input?   ");
int numCust = getNumber();
printf("You have %i customers to input\n\n", numCust);

// Create array of customers
customer *customerList = (customer *)malloc(numCust * sizeof(customer));

// Ask for information for the selected number of customers
for (int i = 0; i < numCust; i++)
{
printf("Customer %i of %i", i, numCust);
newCustomer(&customerList[i]);
}

// Sort the list of customers by lastname then by (home) phone number

// Print the list of customers
for (int i = 0; i < numCust; i++)
{
printf("Customer %i of %i", i, numCust);
printCustomer(&customerList[i]);
}
}

/* Enter Customer Data*/
void newCustomer(customer *cust) {
printf("First Name:   ");
cust->firstName = getString();
printf("Last Name:   ");
cust->lastName = getString();
printf("Address:   ");
cust->address = getString();
printf("Home Phone:   ");
cust->homePhone = getNumber();
printf("Mobile Phone:   ");
cust->cellPhone = getNumber();
}

void printCustomer(customer *cust) {
printf("First Name:   %s\n", cust->firstName);
printf("Last Name:   %s\n", cust->lastName);
printf("Address:   %s\n", cust->address);
printf("Home Phone:   %i\n", cust->homePhone);
printf("Mobile Phone:   %i\n\n", cust->cellPhone);
}

/* Get an integer number from the user*/
int getNumber() {

int num = 0;

while (num == 0) {

//Ask the user for a number
if (scanf_s("%i", &num) != 1) {

//If there were spaces or Non-numeric characters
printf("That is not an integer number.\n");
printf("Please enter an integer number:   ");
num = 0;

}

//Clear buffer
while (getchar() != '\n');
}

return num;
}

/* Get a string from the user*/
char *getString() {
char input[100];
scanf_s("%s", input);
char * result = (char *)malloc(sizeof(char)*(strlen(input) + 1));

//If there isn't enough memory
if (!result) {
printf("Out of Memory");
exit(1);
}

strcpy_s(result, input, "");
return *result;
}