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

1./ C Program To Find The Given Number Is Perfect Number or Not

The document contains 16 C program code examples demonstrating various programming concepts like: - Checking if a number is perfect, Armstrong or finding digit sum - Generating Fibonacci series, prime numbers - Linear search, matrix addition, multiplication - Sorting arrays, recursion, structures, passing arrays to functions - Swapping values using pointers The code examples show the use of basic C constructs like loops, functions, pointers, arrays and structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
348 views

1./ C Program To Find The Given Number Is Perfect Number or Not

The document contains 16 C program code examples demonstrating various programming concepts like: - Checking if a number is perfect, Armstrong or finding digit sum - Generating Fibonacci series, prime numbers - Linear search, matrix addition, multiplication - Sorting arrays, recursion, structures, passing arrays to functions - Swapping values using pointers The code examples show the use of basic C constructs like loops, functions, pointers, arrays and structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

/* C Program to find the given number is Perfect Number or not*/


#include<stdio.h>
#include<conio.h>
main() {
int num, i = 1, sum = 0;
clrscr();
printf("\nEnter a number: ");
scanf("%d", &num);
while (i < num) {
if (num % i == 0) {
sum = sum + i;
}
i++;
}
if (sum == num)
printf("%d is a Perfect Number", i);
else
printf("%d is Non Perfect Number", i);
getch(); }
2. /* C Program to Check whether the given number is Armstrong Number or not*/
#include<stdio.h>
#include<conio.h>
main() {
int num, temp, sum = 0, rem;
clrscr();
printf("\nEnter number for checking Armstrong : ");
scanf("%d", &num);
temp = num;
while (num != 0) {
rem = num % 10;
sum = sum + (rem * rem * rem);
num = num / 10;
}
if (temp == sum)
printf("%d is Amstrong Number", temp);
else
printf("%d is Amstrong Number", temp);
getch(); }
3. /*C Program to find the sum of digits in a given number*/
#include<stdio.h>
#include<conio.h>
main(){
int num,sum=0,r;
clrscr();

Page 1 of 10
printf("\nEnter a number: ");
scanf("%d",&num);
while(num){
r=num%10;
num=num/10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
getch();}
4. /* C program to Generate fibonacci series for n numbers*/
#include<stdio.h>
#include<conio.h>
main() {
int first, second, sum, num, counter = 0;
clrscr();
printf("Enter the term : ");
scanf("%d", &num);
printf("\nEnter First Number : ");
scanf("%d", &first);
printf("\nEnter Second Number : ");
scanf("%d", &second);
printf("\nFibonacci Series : %d %d ", first, second);
while (counter < num) {
sum = first + second;
printf("%d ", sum);
first = second;
second = sum;
counter++;
}
getch(); }
5./* C program to Generate prime numbers */
#include<stdio.h>
#include<conio.h>
main(){
int n, i = 3, count, c;
clrscr();
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 ; )

Page 2 of 10
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
getch(); }
6. /* C Program to Find largest and smallest number in a list of numbers*/
#include<stdio.h>
#include<conio.h>
main(){
int i, n, lar,sm, elem;
clrscr();
printf ("Enter total number of elements \n");
scanf ("%d", &elem);
printf ("Enter first number \n");
scanf ("%d", &n);
lar = n;
sm=n;
for (i=1; i<= elem -1 ; i++)
{
printf ("\n Enter another number \n");
scanf ("%d",&n);
if (n>lar)
lar=n;
if (n<sm)
sm=n;
}
printf ("\n The largest number is %d", lar);
printf ("\n The smallest number is %d", sm);
getch();}
7. /* C program to perform Addition of two matrices*/
#include<stdio.h>
#include<conio.h>
main() {
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;

Page 3 of 10
clrscr();
printf("\nEnter the number of Rows of Mat1 : ");
scanf("%d", &row1);
printf("\nEnter the number of Cols of Mat1 : ");
scanf("%d", &col1);
printf("\nEnter the number of Rows of Mat2 : ");
scanf("%d", &row2);
printf("\nEnter the number of Columns of Mat2 : ");
scanf("%d", &col2);
if (row1 != row2 || col1 != col2) {
printf("\nOrder of two matrices is not same ");
exit(0);
}
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("Enter the Element a[%d][%d] : ", i, j);
scanf("%d", &mat1[i][j]);
}
}
for (i = 0; i < row2; i++)
for (j = 0; j < col2; j++) {
printf("Enter the Element b[%d][%d] : ", i, j);
scanf("%d", &mat2[i][j]);
}
for (i = 0; i < row1; i++)
for (j = 0; j < col1; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
printf("\nThe Addition of two Matrices is : \n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("%d\t", mat3[i][j]);
}
printf("\n");
}
getch(); }
8. /* C program to perform Multiplication of two matrices*/
# include <stdio.h>
# include <conio.h>
main(){
int mata[10][10], matb[10][10], matc[10][10] ;
int i, j, k, row1, col1, row2, col2 ;
clrscr() ;
printf("Enter the order of first matrix : ") ;

Page 4 of 10
scanf("%d %d", &row1, &col1) ;
printf("\nEnter the order of second matrix : ") ;
scanf("%d %d", &row2, &col2) ;
if(col1 == row2)
{
printf("\nEnter the elements of first matrix : \n\n") ;
for(i = 0 ; i < row1 ; i++)
for(j = 0 ; j < col1 ; j++)
scanf("%d", &mata[i][j]) ;
printf("\nEnter the elements of second matrix : \n\n") ;
for(i = 0 ; i < row2 ; i++)
for(j = 0 ; j < col2 ; j++)
scanf("%d", &matb[i][j]) ;
for(i = 0 ; i < row1 ; i++)
{
for(j = 0 ; j < col2 ; j++)
{
matc[i][j] = 0 ;
for(k = 0 ; k < col1 ; k++)
matc[i][j] = matc[i][j] + mata[i][k] * matb[k][j] ;
}
}
printf("\nThe resultant matrix is : \n\n") ;
for(i = 0 ; i < row1 ; i++)
{
for(j = 0 ; j < col2 ; j++)
{
printf("%d \t", matc[i][j]) ;
}
printf("\n") ;
}
}
else
printf("\nMatrix Multiplication is not possible ...") ;
getch() ;}
9. /*C program implements Linear search*/
#include<stdio.h>
#include<conio.h>
int main() {
int a[30], ele, num, i;
clrscr();
printf("\nEnter no of elements :");
scanf("%d", &num);
printf("\nEnter the values :");

Page 5 of 10
for (i = 0; i < num; i++) {
scanf("%d", &a[i]);
}
printf("\nEnter the elements to be searched :");
scanf("%d", &ele);
i = 0;
while (i < num && ele != a[i]) {
i++;
}
if (i < num) {
printf("Number found at the location = %d", i + 1);
} else {
printf("Number not found");
}
getch(); }
10. /* C program to Sort elements in an array*/
#include <stdio.h>
#include<conio.h>
main(){
int arr[100];
int size, i, j, temp;
clrscr();
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[j] < arr[i])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
} } }
printf("\nElements of array in sorted ascending order: ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
} getch(); }

Page 6 of 10
11./* C program to print n fibonacci numbers using recursion*/
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
12. /* C program to find sum of n numbers using recursion*/
#include <stdio.h>
int addNumbers(int n);
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d",addNumbers(num));
return 0;
}
int addNumbers(int n)
{

Page 7 of 10
if(n != 0)
return n + addNumbers(n-1);
else
return n;
}
13/* C Program to find Sum of N Numbers using Recursion */
#include <stdio.h>
void display(int);
int main()
{
int num, result;
printf("Enter the Nth number: ");
scanf("%d", &num);
display(num);
return 0;
}
void display(int num)
{
static int i = 1;
if (num == i)
{
printf("%d \n", num);
return;
}
else
{
printf("%d ", i);
i++;
display(num);
}
}
14. * C program to demonstrate structure*/
#include <stdio.h>
struct student
{
char name[50];
int roll;

Page 8 of 10
float marks;
} s[10];
int main()
{
int i;
printf("Enter information of students:\n");
// storing information
for(i=0; i<10; ++i)
{
s[i].roll = i+1;
printf("\nFor roll number%d,\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<10; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
return 0;
}
15. /* C program demonstrating passing arrays to functions*/
#include <stdio.h>
/* function declaration */
double getAverage(int arr[], int size);
int main () {
/* an int array with 5 elements */
int balance[5] = {1000, 2, 3, 17, 50};
double avg;

Page 9 of 10
/* pass pointer to the array as an argument */
avg = getAverage( balance, 5 ) ;
/* output the returned value */
printf( "Average value is: %f ", avg );
return 0;
}
double getAverage(int arr[], int size) {
int i;
double avg;
double sum = 0;
for (i = 0; i < size; ++i) {
sum += arr[i];
}
avg = sum / size;
return avg;
}
16./* C Program to swap two numbers using pointers and function. */
#include <stdio.h>
void swap(int *n1, int *n2);
int main()
{
int num1 = 5, num2 = 10;
// address of num1 and num2 is passed to the swap function
swap( &num1, &num2);
printf("Number1 = %d\n", num1);
printf("Number2 = %d", num2);
return 0;
}
void swap(int * n1, int * n2)
{
// pointer n1 and n2 points to the address of num1 and num2 respectively
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}

Page 10 of 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy