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

Main N First Second Next C: Int Int

The document contains C code for several programs: 1) A Fibonacci series program that prints the first n terms using a for loop. 2) A palindrome checker that reverses a number and compares it to the original to check for palindromes. 3) Two programs to swap the values of two variables, one using a third variable and one without. 4) Programs to print patterns like ascending numbers, stars pyramids, and number pyramids using for loops. 5) A program to calculate the highest common factor and lowest common multiple of two numbers.

Uploaded by

Akanksha Juneja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Main N First Second Next C: Int Int

The document contains C code for several programs: 1) A Fibonacci series program that prints the first n terms using a for loop. 2) A palindrome checker that reverses a number and compares it to the original to check for palindromes. 3) Two programs to swap the values of two variables, one using a third variable and one without. 4) Programs to print patterns like ascending numbers, stars pyramids, and number pyramids using for loops. 5) A program to calculate the highest common factor and lowest common multiple of two numbers.

Uploaded by

Akanksha Juneja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

/* Fibonacci Series */

#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
return 0;
}
/* Palindrome */
#include <stdio.h>
int main()
{
int n, reverse = 0, temp;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}

/* Swap two numbers */


#include <stdio.h>
int main()
{
int x, y, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x
= y;
y
= temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
return 0;
}

/* Swap two numbers without using third variable */


#include <stdio.h>
int main()
{
int a, b;
printf("Enter two integers to swap\n");
scanf("%d%d", &a, &b);
a = a + b;
b = a - b;
a = a - b;

printf("a = %d\nb = %d\n",a,b);


return 0;

/* Print Pattern */
1
12
123
1234
12345
#include <stdio.h>
int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}

/* Print Start Pyramid */


*
*

*
*
*

*
*

#include <stdio.h>
int main()
{
int row, c, n, temp;
printf("Enter the number of rows in pyramid of stars you wish to see ");
scanf("%d",&n);
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
printf(" ");
temp--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
printf("*");
}

printf("\n");

return 0;
}

/* Print Inverted Start Pyramid */


*

*
*

*
*
*

*
*

#include <stdio.h>
int main()
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("*");
}
printf("\n");
}
return 0;
}

/* Print Number Pyramid */


2
4

#include<stdio.h>
int main()
{
int i,j,k;
k=1;
for(i=1;i<=5;i+=2)
{
for(j=5;j>=1;j--)
{
if(j>i)
printf(" ");
else
printf("%d ",k++);
}
printf("\n");
}
return 0;
}

1
5

3
6

/* HCF and LCM */


#include <stdio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while
t =
b =
a =
}

(b != 0) {
b;
a % b;
t;

gcd = a;
lcm = (x*y)/gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
return 0;
}

You might also like