Record Programs 3
Record Programs 3
2.To find the GCD (greatest common divisor) of two given integers.
#include <stdio.h>
int recgcd(int x, int y);
int nonrecgcd(int x, int y);
void main()
{
int a, b, c, d;
printf("Enter two numbers a and b");
scanf("%d%d", &a, &b);
c = recgcd(a, b);
printf("The gcd of two numbers using recursion is %d\n", c);
d = nonrecgcd(a, b);
printf("The gcd of two numbers using nonrecursion is %d", d);
}
int recgcd(int x, int y){
if(y == 0){
return(x);
}
else{
return(recgcd(y, x % y));
}
}
int nonrecgcd(int x, int y){
int z;
while(x % y != 0){
z = x % y;
x = y;
y = z;
}
return(y);
}
#include <stdio.h>
int rpower(int , int);
void power(int,int)
int main() {
int base, p, result;
printf("Enter base number:\n ");
scanf("%d", &base);
printf("Enter power number(positive integer):\n ");
scanf("%d", &p);
result = rpower(base, p);
printf("%d^%d = %d\n", base, a, result);
power(base,p);
return 0;
}
void power(int base,int p)
{
int i,re=1;
for(i=1;i<=p;i++)
re=re*base;
printf("%d^%d = %d\n", base, a, re);
}
int power(int base, int p) {
if (p != 0)
return (base * power(base, p - 1));
else
return 1;
}
Files:
#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("* ”);
}
printf("\n");
}
Output:
*
**
***
#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("%d ",i);
}
printf("\n");
}
}
Output:
1
22
333
#include <stdio.h>
void main()
{
int i, j, rows,t=1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ",t++);
}
printf("\n");
}
}
Output:
1
23
456
#include <stdio.h>
void main()
{
int i, j, rows,t=1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
for (i = rows-1; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}}
Output:
*
**
***
**
*