Source Code: C Program To Swap Two Numbers using Pointers and Function
#include
void swap(int*, int*);
int main()
{
int a, b;
printf("Enter values for a and b\n");
scanf("%d%d", &a, &b);
printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);
swap(&a, &b);
printf("\nAfter swapping: a = %d and b = %d\n", a, b);
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Output 1: Enter values for a and b 100 200
Before swapping: a = 100 and b = 200
After swapping: a = 200 and b = 100
Output 2: Enter values for a and b 30 20
Before swapping: a = 30 and b = 20
After swapping: a = 20 and b = 30
Logic To Swap Two Numbers using Pointers and Function
We ask the user to enter values for variable a and b. We pass the address of variable a and b to function swap().
Inside function swap() we take a local variable temp. Since address of variable a and b are passed to swap() method, we take 2 pointer variables *x and *y. Pointer variable x holds the address of a and pointer variable y holds the address of b. Using below logic we swap the values present at address a( or x ) and b( or y ).
temp = *x; *x = *y; *y = temp;
Since we are directly changing the value present at particular address, the value gets reflected throughout the program and not just inside particular function. That’s why we are able to print the result inside main function and the values of variable a and b are swapped, without getting any returned value from function swap().
Lets write a C program to print the multiplication table for a user input number, using function/method. The table should get displayed in the following format:
Example: Multiplication table of 5 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Coding Challenge on Numbers! Print the multiplication table which gives the following output:
Hint: Initialize count to 3. Increment the value of count by 3 for each iteration of for loop.
Answer: We’ve posted the source code to above coding challenge at the end of this blog post.
Source Code: C Program To Print Multiplication Table Using Function
#include
void tables(int);
int main()
{
int num;
printf("Enter a positive number\n");
scanf("%d", &num);
printf("\nMultiplication Table for %d is:\n", num);
tables(num);
return 0;
}
void tables(int num)
{
int count;
for(count = 1; count <= 10; count++)
{
printf("%d x %d = %d\n", num, count, num*count);
}
}
Output 1: Enter a positive number 5
Multiplication Table for 5 is: 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Output 2: Enter a positive number 2
Multiplication Table for 2 is: 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20
Output 3: Enter a positive number 7
Multiplication Table for 7 is: 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
Output 4: Enter a positive number 9
Multiplication Table for 9 is: 9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 9 x 10 = 90
Logic: Multiplication Table
User enters a positive number, we store it inside variable num. We pass this number entered by the user to a function called tables().
Inside tables() function we write a for loop. We initialize the for loop counter variable count to 1 and iterate through this for loop until count is less than or equal to 10 – because we want to display multiplication tables from 1 to 10. For each iteration of for loop we increment the value of count by 1.
Inside for loop we display the multiplication table.
Here I’m initializing 3 to count variable. For loop executes until count is less than or equal to 27. For each iteration of for loop count value increments by 3.
We assign 37 to num variable. Check the output.
Source Code: Some numerology!
#include
void tables(int);
int main()
{
int num = 37;
printf("\nMultiplication Table for %d is:\n", num);
tables(num);
return 0;
}
void tables(int num)
{
int count;
for(count = 3; count <= 27; count += 3)
{
printf("%d x %d = %d\n", num, count, num*count);
}
}
Output: Multiplication Table for 37 is:
37 x 3 = 111 37 x 6 = 222 37 x 9 = 333 37 x 12 = 444 37 x 15 = 555 37 x 18 = 666 37 x 21 = 777 37 x 24 = 888 37 x 27 = 999
Spurce Code: C Program To Convert Year To Roman Equivalent
#include
void roman(int num)
{
while(num)
{
if(num >= 1000)
{
printf("m");
num = num - 1000;
}
else if(num >= 500)
{
printf("d");
num = num - 500;
}
else if(num >= 100)
{
printf("c");
num = num - 100;
}
else if(num >= 50)
{
printf("l");
num = num - 50;
}
else if(num >= 10)
{
printf("x");
num = num - 10;
}
else if(num >= 5)
{
printf("v");
num = num - 5;
}
else if(num >= 1)
{
printf("i");
num = num - 1;
}
}
printf("\n");
}
int main()
{
int year;
printf("Input the year to get its Roman Equivalent\n");
scanf("%d", &year);
roman(year);
return 0;
}
Output 1: Input the year to get its Roman Equivalent 1988 mdcccclxxxviii
Output 2: Input the year to get its Roman Equivalent 1525 mdxxv
Output 3: Input the year to get its Roman Equivalent 2500 mmd
Output 4: Input the year to get its Roman Equivalent 2020 mmxx
Output 5: Input the year to get its Roman Equivalent 2021 mmxxi
Logic To Convert Year To Roman Equivalent
We ask the user to enter the year in decimal number format and store it inside variable year. Next we pass this user input year to function roman.
Inside function roman, we copy the value of year to a local variable num. We write a while loop and iterate until value of num is positive. Control exits while loop once num is equal to 0.
Inside while loop we check if the value of num is greater than or equal to 1000. If true, then we print roman equivalent of 1000, which is m. And then we decrement the value of num by 1000.
For each iteration of the while loop we check if num is greater than or equal to 1000 or 500 or 100 or 50 or 10 or 5 or 1. To whichever number the value of num matches we printout its corresponding representation in roman:
Lets write a C program to check whether two positive numbers entered by the user are Co-Prime numbers / Relative Prime Numbers or not using function / method.
Co-Prime numbers / Relative Prime Numbers: Two numbers are said to be co-prime or relative prime numbers if they do not have a common factor other than 1.
OR
Two numbers whose Greatest Common Divisor(GCD) is 1 are known as Co-Prime or Relative Prime Numbers.
Factors of a number: All the numbers which perfectly divide a given number are called as Factors of that number.
Note: User entered numbers(to check for co-prime) do not require to be prime numbers.
Source Code: C Program To Find If Two Numbers are Co-Prime or Not using Function
#include
int coprime(int num1, int num2)
{
int min, count, flag = 1;
min = num1 < num2 ? num1 : num2;
for(count = 2; count <= min; count++)
{
if( num1 % count == 0 && num2 % count == 0 )
{
flag = 0;
break;
}
}
return(flag);
}
int main()
{
int n1, n2;
printf("Enter 2 positive numbers\n");
scanf("%d%d", &n1, &n2);
if( coprime(n1, n2) )
{
printf("%d and %d are co-prime numbers.\n", n1, n2);
}
else
{
printf("%d and %d are not co-prime numbers.\n", n1, n2);
}
return 0;
}
Output 1: Enter 2 positive numbers 8 15 8 and 15 are co-prime numbers.
Output 2: Enter 2 positive numbers 12 15 12 and 15 are not co-prime numbers.
Note: coprime() method returns 1 or 0 value. If it returns 1, then the code inside if block gets executed. If it returns 0, then the code inside else block gets executed.
Source Code: C Program To Express A Number as Sum of Two Prime Numbers
#include
#include
int nxtPrime(int);
int isPrime(int);
int main()
{
int num, count, flag = 0;
printf("Enter a positive number\n");
scanf("%d", &num);
for(count = 2; count <=(num-count); count = nxtPrime(count))
{
if(isPrime(num-count))
{
flag = 1;
printf("%d + %d = %d\n", count, num-count, num);
}
}
if(flag == 0)
{
printf("%d cannot be expressed as the sum of 2 prime numbers.\n", num);
}
return 0;
}
int nxtPrime(int num)
{
do
{
num++;
}while(!isPrime(num));
return(num);
}
int isPrime(int num)
{
int count, inum, prime = 1;
inum = sqrt(num);
for(count = 2; count <= inum; count++)
{
if(num % count == 0)
{
prime = 0;
break;
}
}
return(prime);
}
Output 1: Enter a positive number 14 3 + 11 = 14 7 + 7 = 14
Output 2: Enter a positive number 5 2 + 3 = 5
Output 3: Enter a positive number 24 5 + 19 = 24 7 + 17 = 24 11 + 13 = 24
Output 4: Enter a positive number 34 3 + 31 = 34 5 + 29 = 34 11 + 23 = 34 17 + 17 = 34
Output 5: Enter a positive number 41 41 cannot be expressed as the sum of 2 prime numbers.
Note: Function nxtPrime() and isPrime() returns integer type data so its return type is int. And both these methods / functions accepts 1 integer type argument.
Logic To Express A Number as Sum of Two Prime Numbers
1. We ask the user to enter a positive number and store it inside variable num. Now we write the for loop. We initialize the loop counter variable count to 2 – because 2 is the first number in prime number series. We iterate through the for loop until count is less than or equal to (num-count). For each iteration of for loop we change the value of count to next prime number in the prime number series.
We use following simple expression to find and printout the result: count + (num – count) = num;
We already know that value of count is prime number. Next inside for loop we check the second part of above expression i.e., (num-count) for prime number or not. If both count and (num-count) are prime numbers, then we output the values, if not, then we change the value of count to next prime number and check if (num-count) is prime or not. We continue doing this until count is less than or equal to (num-count);
2. We need to get next prime number for loop count variable count. So we need to define (write logic for) nxtPrime() method.
int nxtPrime(int num)
{
do
{
num++;
}while(!isPrime(num));
return(num);
}
value of count is passed to nxtPrime() method, which is copied to local variable num. First we increment the value of num inside do block and then check the condition in while. If the number is prime then we exit the do-while loop and return the number orelse we keep incrementing the value of num and check if the new number is prime or not, by passing it to the function isPrime().
int isPrime(int num)
{
int count, inum, prime = 1;
inum = sqrt(num);
for(count = 2; count <= inum; count++)
{
if(num % count == 0)
{
prime = 0;
break;
}
}
return(prime);
}
This is the beauty of function. We call the function isPrime() 2 times. Once from main() function and once from nxtPrime() function. This avoids repeating our code. We write the code once in our function and call it any number of times in the program. This is called DRY concept in programming i.e., Do Not Repeat Yourself.