0% found this document useful (0 votes)
20 views

C Program For 4 Codes - SolutionPDF

The document contains solutions and C code for 4 programming tasks. Task 1 prints numbers between upper and lower limits input by the user. Task 2 sums all integers input until the user enters 0. Task 3 sums the digits of a 3-digit number input by the user. Task 4 uses nested for loops to print a pattern of two-digit numbers incrementing in a specific way. C code with explanations of the algorithms is provided for each task.

Uploaded by

eslam ibrahim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C Program For 4 Codes - SolutionPDF

The document contains solutions and C code for 4 programming tasks. Task 1 prints numbers between upper and lower limits input by the user. Task 2 sums all integers input until the user enters 0. Task 3 sums the digits of a 3-digit number input by the user. Task 4 uses nested for loops to print a pattern of two-digit numbers incrementing in a specific way. C code with explanations of the algorithms is provided for each task.

Uploaded by

eslam ibrahim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Solutioninn Solutions

C Program for 4 codes


Task A:
Concept:
The first task requires a C-program to print values between Upper limit and Lower limit
specified by the user, using a loop.

Solution and Code:


The solution of this task can be summarized in the following steps:
1. Creating and Initializing 2 variables to hold the lower limit and upper limit.
2. Prompting the user to enter the lower and upper limits and save them into the created
variables.
3. Checking the input numbers, to specify which one is the lower limit and the upper limit.
4. Print the numbers between the lower limit and upper limit.

Code in Editor:

Page | 1
Solutioninn Solutions

Program Code:
#include <stdio.h>
#include <stdlib.h>

/* A Program code for asking the user to enter Lower limit and Upper limit then print
the numbers between them */
void printnumbers(int lower,int upper){
int count;
printf("The numbers between %i and %i are: ",lower,upper);

Page | 2
Solutioninn Solutions

for(count = lower ; count <= upper ; count++){


printf("%i ",count);
}
}
int main()
{
int num_1 = 0,num_2 = 0;// Creating 2 variables to hold input values
int lower = 0,upper = 0;// Lower will carry the lower limit number, and Upper will hold the upper limit
/* Prompting the user to enter the 2 limit numbers */
printf("\tWelcome to the Upper Lower limit Program\n");
printf("Please enter the lower limit number: ");
scanf("%i",&num_1);
printf("\nEnter the upper limit number: ");
scanf("%i",&num_2);
/* Checking which of the given 2 numbers is the lower limit and which is the upper limit */
if(num_1 > num_2){
lower = num_2;
upper = num_1;
printnumbers(lower,upper);
}
else if(num_1 < num_2){
lower = num_1;
upper = num_2;
printnumbers(lower,upper);
}
else{
lower = num_1;
upper = num_2;
printf("\nBoth numbers are equal, so there's no numbers in between\n");
}

Page | 3
Solutioninn Solutions

return 0;}

Task B:
Concept:
In this task, we need to write a C-code that keeps asking the user to specify integer values until a
zero value is entered, then print the sum of all numbers input by the user.

Solution and Code:


The solution algorithm can be shown as:
1. Creating 2 values/variables: sum variable to hold the sum of input numbers and a counter
to iterate through the loop.
2. Prompt the user to enter an integer value.
3. Using the loop, keep adding all numbers until a zero is entered by the user.
4. When zero is entered, print the sum on the screen.

Code in Simulator:

Page | 4
Solutioninn Solutions

Program Code:
#include <stdio.h>

#include <stdlib.h>

/* A program code to compute the sum of entered values, the last value must be a zero */

int main()

int sum = 0;

int val = 0;

// Check if the value is zero, then calculate and print the sum, otherwise take the input value

printf("Enter the numbers to sum, to exit enter zero 0: \n");

do{

scanf("%i",&val);

sum += val;

}while(val != 0);

printf("\nThe sum of numbers is: %i",sum);

return 0;

Page | 5
Solutioninn Solutions

Task C:
Concept:
For this task, we need to write a C-code to sum the digits of a 3-digit number entered by the user,
then print the sum on a new line.

Solution and Code:


The algorithm used to implement this code is summarized as follows:
1. Creating 4 empty variables: sum variable to carry the sum, remd variable for holding the
remainder of division operation, numb to hold the quotient of division and temp is a
temporary variable.
2. Prompt the user to enter the required 3-digit integer number.
3. Start by dividing the given number by 10, take the quotient and save it to numb.
4. Keep the remainder in remd and then sum it to sum.
5. Repeat the last step for 2 more times using a for loop, till we get the sum of the 3 digits.

Code in Simulator:

Page | 6
Solutioninn Solutions

Program Code:
#include <stdio.h>

#include <stdlib.h>

/* Code to calculate the sum of the 3 digits of a number entered by user */

int main()

// Create 2 variables, one to hold the remainder and the other to hold the sum of digits

int sum = 0, remd = 0;

int numb , temp;// Variable to hold the number to sum its digits

int count; // counter for the loop

printf("Please enter the 3-digit number to calculate their sum: \n");

scanf("%i" , &numb);

temp = numb;

for(count = 0; count < 3 ; count++){

remd = numb % 10;

sum += remd;

numb = numb / 10;

Page | 7
Solutioninn Solutions

printf("\nThe sum of the 3 digits of %i is: %i",temp , sum);

return 0;

Task D:
Concept:
For the 4th task, we need to use 2 for loops to print the following pattern: {01, 03, 05, 07, 09, 11,
13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, …}

Solution and Code:


The algorithm behind the given code is shown below.
1. Create 2 empty character variables: digit_1 and digit_2 representing the first and second
digits of the respectively.
2. Create the first for loop of the first digit (digit_1) and iterate through it.
3. Create another for loop inside the first loop for the second digit (digit_2) and start to
iterate from digit = 1.
4. Print the resulting 2 digits each time, then increment and repeat.

Code in Simulator:

Page | 8
Solutioninn Solutions

Program Code:
#include <stdio.h>

#include <stdlib.h>

/* This program will print numbers with 2 digits, where the first digit will increment by 1 every 10
numbers

while the 2nd digit will increment by 1 every number */

int main()

char digit_1 = '0',digit_2 = '1';

for(; digit_1 <= '9' ;digit_1=digit_1+1){

for(digit_2 = '1'; digit_2 <= '9' ;digit_2=digit_2+2){

printf("%c%c",digit_1,digit_2);

printf(" ");

return 0;

Page | 9
Solutioninn Solutions

Program Code with do-while loop:


#include <stdio.h>

#include <stdlib.h>

/* This program will print numbers with 2 digits, where the first digit will increment by 1 every 10
numbers

while the 2nd digit will increment by 1 every number */

int main()

char digit_1 = '0',digit_2 = '1';

do{

do{

printf("%c%c",digit_1,digit_2);

digit_2=digit_2+2

}while(digit_2 <= '9');

digit_1=digit_1+1

}while(digit_1 <= '9'); return 0;

Page | 10

You might also like