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

C Programs Answer For 13 Q

The document contains 10 C programming examples: 1. A Fibonacci series program that prints the Fibonacci sequence up to a given number of terms. 2. A program that calculates the sum of digits of a number. 3. A program that checks if a number is an Armstrong number. 4. Four programs that demonstrate different ways to swap two numbers in C. 5. A matrix multiplication program. 6. A program to find the transpose of a matrix. 7. Three programs to calculate the factorial of a number using different approaches. 8. Three palindrome programs using strings, numbers, and without string functions. 9. Two programs to find the largest of three numbers using if and

Uploaded by

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

C Programs Answer For 13 Q

The document contains 10 C programming examples: 1. A Fibonacci series program that prints the Fibonacci sequence up to a given number of terms. 2. A program that calculates the sum of digits of a number. 3. A program that checks if a number is an Armstrong number. 4. Four programs that demonstrate different ways to swap two numbers in C. 5. A matrix multiplication program. 6. A program to find the transpose of a matrix. 7. Three programs to calculate the factorial of a number using different approaches. 8. Three palindrome programs using strings, numbers, and without string functions. 9. Two programs to find the largest of three numbers using if and

Uploaded by

AbanishAmp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Program 1

/* Fibonacci Series c language */


Numbers of Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1,
1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms,
For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and
Computer Science.
#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("Fibonacci series\n");
printf(" %d \n%d\n",first,second);
for ( c = 0 ; c < n ; c++ )
{
next = first + second;
first = second;
second = next;
printf("%d\n",next);
}
return 0;
}
Program 2
/*Sum of Digits of a Number*/
98%10 = 8 (% is modulus operator which gives us remainder when 98 is divided by 10).
sum = sum + remainder
so sum = 8 now.
98/10 = 9 because in c whenever we divide integer by another integer we get an integer.
9%10 = 9
sum = 8(previous value) + 9
sum = 17

9/10 = 0.
So finally n = 0, loop ends we get the required sum.
#include <stdio.h>
int main()
{
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
return 0;
}
/*One Line code for sum of digits of a number*/
#include <stdio.h>
int main()
{
int n, sum = 0;
printf("Enter an integer\n");
scanf("%d",&n);
sum = n%9;
if(sum==0)
sum=9;//
}
printf("Sum of digits of entered number = %d\n",sum);

return 0;
}
Program 3
/*ARMSTRONG NUMBER */
A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself.
For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1,
153, 370, 407.
#include <stdio.h>
int main()
{
int number, sum = 0, temp, remainder;
printf("Enter an integer\n");
scanf("%d",&number);
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
printf("Entered number is an armstrong number.\n");
else
printf("Entered number is not an armstrong number.\n");
return 0;
}
Program 4
Swapping of two numbers in c
#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;

= temp;

printf("After Swapping\nx = %d\ny = %d\n",x,y);


return 0;
}
Swapping of two numbers without 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;
}

Swap two numbers using pointers

#include <stdio.h>
int main()
{
int x, y, *a, *b, 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);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
Swapping numbers using call by reference
#include <stdio.h>
void swap(int*, int*);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}

void swap(int *a, int *b)


{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
C programming code to swap using bitwise XOR
#include <stdio.h>
int main()
{
int x, y;
scanf("%d%d", &x, &y);
printf("x = %d\ny = %d\n", x, y);
x = x ^ y;
y = x ^ y;
x = x ^ y;
printf("x = %d\ny = %d\n", x, y);
return 0;
}
Program 5
Matrix multiplication in c language
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);

if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}

}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
Program 6
Transpose of a Matrix
#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix ");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix \n");
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&matrix[c][d]);
}
}
for( c = 0 ; c < m ; c++ )

{
for( d = 0 ; d < n ; d++ )
{
transpose[d][c] = matrix[c][d];
}
}
printf("Transpose of entered matrix :-\n");
for( c = 0 ; c < n ; c++ )
{
for( d = 0 ; d < m ; d++ )
{
printf("%d\t",transpose[c][d]);
}
printf("\n");
}
return 0;
}
Program 7
Factorial program in c using for loop
#include <stdio.h>
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);

return 0;
}
Factorial program in c using function
#include <stdio.h>
long factorial(int);
int main()
{
int number;
long fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &number);
printf("%d! = %ld\n", number, factorial(number));

return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return result;
}
Factorial program in c using recursion
#include<stdio.h>
long factorial(int);
int main()
{

int n;
long f;
printf("Enter an integer to find factorial\n");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
Program 8
C program for palindrome
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the string to check if it is a palindrome\n");

gets(a);
strcpy(b,a);
strrev(b);
if( strcmp(a,b) == 0 )
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");
return 0;
}
Palindrome number in c
#include <stdio.h>
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;

}
C program for palindrome without using string functions
#include <stdio.h>
#include <string.h>
int main()
{
char text[100];
int begin, middle, end, length = 0;
gets(text);
while ( text[length] != '\0' )
length++;
end = length - 1;
middle = length/2;
for( begin = 0 ; begin < middle ; begin++ )
{
if ( text[begin] != text[end] )
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if( begin == middle )
printf("Palindrome.\n");
return 0;
}
C program check palindrome
#include <stdio.h>

int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);
int main()
{
char string[100];
int result;
printf("Enter a string\n");
gets(string);
result = is_palindrome(string);
if ( result == 1 )
printf("\"%s\" is a palindrome string.\n", string);
else
printf("\"%s\" is not a palindrome string.\n", string);
return 0;
}
int is_palindrome(char *string)
{
int check, length;
char *reverse;
length = string_length(string);
reverse = (char*)malloc(length+1);
copy_string(reverse, string);
reverse_string(reverse);
check = compare_string(string, reverse);
free(reverse);

if ( check == 0 )
return 1;
else
return 0;
}
int string_length(char *string)
{
int length = 0;
while(*string)
{
length++;
string++;
}
return length;
}

void copy_string(char *target, char *source)


{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '\0';
}
void reverse_string(char *string)
{

int length, c;
char *begin, *end, temp;
length = string_length(string);
begin = string;
end = string;
for ( c = 0 ; c < ( length - 1 ) ; c++ )
end++;
for ( c = 0 ; c < length/2 ; c++ )
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}

int compare_string(char *first, char *second)


{
while(*first==*second)
{
if ( *first == '\0' || *second == '\0' )
break;
first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;

else
return -1;
}
Program 9
/* C program to find largest number using if statement only */
#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
if(b>=a && b>=c)
printf("Largest number = %.2f", b);
if(c>=a && c>=b)
printf("Largest number = %.2f", c);
return 0;
}
/* C program to find largest number using if...else statement */

#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)

printf("Largest number = %.2f",a);


else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else
printf("Largest number = %.2f",c);
}
return 0;
}
greatest number in given three numbers

#include<stdio.h>
int main(){
int a,b,c,big;
printf("\nEnter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf("\nThe biggest number is:%d",big);
return 0;
}
Program 10
Prime number program in c language
#include<stdio.h>
int main()

{
int n, i = 3, count, c;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
return 0;
}
C program for prime number or not
#include<stdio.h>
main()

{
int n, c = 2;
printf("Enter a number to check if it is prime\n");
scanf("%d",&n);
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);
return 0;
}
C program for prime number using function
#include<stdio.h>
int check_prime(int);
main()
{
int n, result;
printf("Enter an integer to check whether it is prime or not.\n");
scanf("%d",&n);
result = check_prime(n);
if ( result == 1 )
printf("%d is prime.\n", n);
else

printf("%d is not prime.\n", n);


return 0;
}
int check_prime(int a)
{
int c;
for ( c = 2 ; c <= a - 1 ; c++ )
{
if ( a%c == 0 )
return 0;
}
if ( c == a )
return 1;
}
Program 11
LCM of Two numbers
#include <stdio.h>
int main()
{
int num1, num2, max;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
max=(num1>num2) ? num1 : num2; /* maximum value is stored in variable max */
while(1)

/* Always true. */

{
if(max%num1==0 && max%num2==0)
{
printf("LCM of %d and %d is %d", num1, num2,max);

break;

/* while loop terminates. */

}
++max;
}
return 0;
}
Program 12
/* C Program to Find Highest Common Factor. */
#include <stdio.h>
int main()
{
int num1, num2, i, hcf;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
for(i=1; i<=num1 || i<=num2; ++i)
{
if(num1%i==0 && num2%i==0) /* Checking whether i is a factor of both number */
hcf=i;
}
printf("H.C.F of %d and %d is %d", num1, num2, hcf);
return 0;
}
#include <stdio.h>
int main()
{
int num1, num2, min,i;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);

min=(num1>num2)?num2:num1; /* minimum value is stored in variable min */


for(i=min;i>=1;--i)
{
if(num1%i==0 && num2%i==0)
{
printf("HCF of %d and %d is %d", num1, num2,i);
break;
}
}
return 0;
}
Program 13
/*c program for calculate simple interest*/
#include<stdio.h>
#include<conio.h>
int main()
{
float p,rate,time,si;
printf("Enter principal amount : ");
scanf("%f", &p);
printf("Enter rate of interest : ");
scanf("%f", &rate);
printf("Enter time period in year : ");
scanf("%f", &time);
/*calculating simple interest*/
si=(p*time*rate)/100;
printf("\nSimple Interest = %2f",si);
getch();

return 0;
}
Program 14
/*c program for calculate compound interest*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
float p,rate,time,ci;
printf("Enter principal amount : ");
scanf("%f", &p);
printf("Enter interest rate : ");
scanf("%f", &rate);
printf("Enter time period in year : ");
scanf("%f", &time);
//calculate ci
ci=p*(pow((1+rate/100),time)-1);
printf("\nCompound interest = %f",ci);
return 0;
}

You might also like