Sie sind auf Seite 1von 31

1.

write a program to Check Armstrong Number of three digits


#include <stdio.h>
#include<conio.h>
void main()
{
int number, originalNumber, remainder, result = 0;

printf("Enter a three digit integer: ");


scanf("%d", &number);

originalNumber = number;

while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}

if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);

getch ();
}

1
2.write a program to Switch

#include<stdio.h>
#include<conio.h>

intmain() {
charoperator;
floatnum1,num2;

printf("Enter two numbers as operands\n");


scanf("%f%f", &num1, &num2);
printf("Enter an arithemetic operator(+-*/)\n");
scanf("%*c%c",&operator);

switch(operator) {
case'+':
printf("%.2f + %.2f = %.2f",num1, num2, num1+num2);
break;
case'-':
printf("%.2f - %.2f = %.2f",num1, num2, num1-num2);
break;
case'*':
printf("%.2f * %.2f = %.2f",num1, num2, num1*num2);
break;
case'/':
printf("%.2f / %.2f = %.2f",num1, num2, num1/num2);
break;
default:
printf("ERROR: Unsupported Operation");
}

getch();
}

2
3. write a program toSource Code to Find Transpose of a Matrix
#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10], trans[10][10], r, c, i, j;
printf("Enter rows and column of matrix: ");
scanf("%d %d", &r, &c);

/* Storing element of matrix entered by user in array a[][]. */


printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ",a[i][j]);
if(j==c-1)
printf("\n\n");
}

/* Finding transpose of matrix a[][] and storing it in array trans[][]. */


for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
trans[j][i]=a[i][j];
}

/* Displaying the transpose,i.e, Displaying array trans[][]. */


printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",trans[i][j]);
if(j==r-1)
printf("\n\n");
}
getch();
}

3
4
4. write a Program to Check Even or Odd
#include<stdio.h>
#include<conio.h>
Void main()
{
int number;

printf("Enter an integer: ");


scanf("%d",&number);

// True if the number is perfectly divisible by 2


if(number%2==0)
printf("%d is even.", number);
else
printf("%d is odd.", number);

getch;
}

5
5. write a program to Structure program
#include <stdio.h>
#include <string.h>
#include<conio.h>
struct Books {
char title[50];
char author[50];
char subject[100];
intbook_id;
};

int main( ) {

struct Books Book1; /* Declare Book1 of type Book */


struct Books Book2; /* Declare Book2 of type Book */

/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;

/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;

/* print Book1 info */


printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);

/* print Book2 info */


printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);

getch();
}

6
7
6. write a program to union
#include <stdio.h>
#include <string.h>
#include<conio.h>
union Data {
int i;
float f;
charstr[20];
};

int main( ) {

union Data data;

printf( "Memory size occupied by data : %d\n", sizeof(data));

getch();
}

8
7. write a program to check perfect number
#include<stdio.h>
#include<conio.h>
void main(){
int n,i=1,sum=0;

printf("Enter a number: ");


scanf("%d",&n);

while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);

getch();
}

9
8. write a program to Farenhite to Celsius
#include<stdio.h>
#include<conio.h>
main()
{
float fh,cl;
printf("Enter temperature value in Fahrenheit: ");
scanf("%f", &fh);
cl = (fh - 32) / 1.8;
printf("Converted Celsius value: %f",cl);
getch();
return 0;
}

//CALL BY REFERENCE

voidcall_by_reference(int *y) {
printf("Inside call_by_reference y = %d before adding 10.\n", *y);
(*y) += 10;
printf("Inside call_by_reference y = %d after adding 10.\n", *y);
getch();
}

10
9. write a program toLeap Year
#include <stdio.h>
#include<conio.h>
void main()
{
int year;

printf("Enter a year: ");


scanf("%d",&year);

if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);

getch();
}

11
10. write a program to Fibanacci series

#include <stdio.h>
#include<conio.h>
void main()
{
int i, n, t1 = 0, t2 = 1, nextTerm = 0;

printf("Enter the number of terms: ");


scanf("%d",&n);

// displays the first two terms which is always 0 and 1


printf("Fibonacci Series: %d, %d, ", t1, t2);

// i = 3 because the first two terms are already dislpayed


for (i=3; i <= n; ++i)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf("%d, ",nextTerm);
}
getch();
}

12
11. write a program to Decimal to binary
#include<stdio.h>
#include<conio.h>

void main(){

long int decimalNumber,remainder,quotient;

int binaryNumber[100],i=1,j;

printf("Enter any decimal number: ");

scanf("%ld",&decimalNumber);

quotient = decimalNumber;

while(quotient!=0){

binaryNumber[i++]= quotient % 2;

quotient = quotient / 2;

printf("Equivalent binary value of decimal number %d: ",decimalNumber);

for(j = i -1 ;j> 0;j--)

printf("%d",binaryNumber[j]);
getch();}

13
14
12. write a program to Source Code to Calculate Average Using Arrays
#include <stdio.h>
#include<conio.h>
void main(){
int n, i;
floatnum[100], sum=0.0, average;
printf("Enter the numbers of data: ");
scanf("%d",&n);
while (n>100 || n<=0)
{
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d",&n);
}
for(i=0; i<n; ++i)
{
printf("%d. Enter number: ",i+1);
scanf("%f",&num[i]);
sum+=num[i];
}
average=sum/n;
printf("Average = %.2f",average);
getch();
}

15
13. write a program to Addition of All Elements of the Array
#include<stdio.h>
#include<conio.h>
void main() {
int i, arr[50], sum, num;

printf("\nEnter no of elements :");


scanf("%d", &num);

//Reading values into Array


printf("\nEnter the values :");
for (i = 0; i <num; i++)
scanf("%d", &arr[i]);

//Computation of total
sum = 0;
for (i = 0; i <num; i++)
sum = sum + arr[i];

//Printing of all elements of array


for (i = 0; i <num; i++)
printf("\na[%d]=%d", i, arr[i]);

//Printing of total
printf("\nSum=%d", sum);

getch();
}

16
14. write a program to Factorial using recursion
#include<stdio.h>
int fact(int);
int main(){
int num,f;
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
return 0;
}

int fact(int n){


if(n==1)
return 1;
else
return(n*fact(n-1));
}

17
15. write a program to Swap using Functions
#include <stdio.h>
#include<conio.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;
getch();
}

18
16. write a program to Matrix Multiplication
#include <stdio.h>
#include<conio.h>
void 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");
}
}

getch();
}

19
20
17. write a program to Sum of 5 subject marks

#include<stdio.h>
#include<conio.h>
void main()
{
inta,b,c,d,e,total;
clrscr();
printf("enter English marks");
scanf("%d",&a);
printf("enter Hindi marks");
scanf("%d",&b);
printf("enter SST marks");
scanf("%d",&c);
printf("enter Computer marks");
scanf("%d",&d);
printf("enter Science marks");
scanf("%d",&e);
total=a+b+c+d+e;
printf("sum =%d",total);
getch();
}

21
18. write a program to Give the value at run time to check Prime No.

#include<stdio.h>
#include<conio.h>
void main()
{
intn,i,c=0;
clrscr();
printf("enter a number");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{c++;
break;
}
}
if(c==0&& n!=1)
printf("%d is a prime number",n);
else
printf("%%d isnot a prime number",n);

getch();
}

22
19. write a program to Find the Greatest number using Array
#include<stdio.h>
#include<conio.h>
void main()
{
inti,a[10];l=0;
clrscr();
printf("enter the numbers: ");
for(i=1;i<=10; i++)
{
scanf("%d",& a[i]);
}
for(i=1;i<=10;i++)
{
if(a[i]>l)
{
l=a[i]];
}
printf("\n the largest number is=%d",l);
getch();
}

23
20. write a program to Call by Address in function
#include<stdio.h>
#include<conio.h>
int max(int num1,int num2);
void main()
{
inta,b,ret;
a=100;
b=200;
ret=max(a,b);
printf("max value is=%d \n",ret);
getch();
}
int max(int num1,int num2)
{
int result:
if(num1>num2)
result=num1;
else
result=num2;
return result;
}

24
21.write a program to Prime number using Function-
#include<stdio.h>
#include<conio.h>
main()
{
intn,result;
printf("enter a number to check whether it is prime or not.\n");
scanf("%d",&n);
result=check_prime(n);
if(result==1)
printf("%d is prime number. \n",n);
else
printf("%d is not a prime number. \n",n);
return 0;
}
intcheck_prime(int a)
{
int c;
for(c=2;c<=a-1;c++)
{ if(a%c==0)
return 0;
}
if(c==a)
return 1;
getch();
}

25
22-write a program to insert operation in binary search tree
#include<stdio.h>
#include<stdlib.h>

struct node
{
int key;
struct node *left, *right;
};

// A utility function to create a new BST node


struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// A utility function to do inorder traversal of BST


voidinorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d \n", root->key);
inorder(root->right);
}
}

/* A utility function to insert a new node with given key in BST */


struct node* insert(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL) return newNode(key);

/* Otherwise, recur down the tree */


if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);

/* return the (unchanged) node pointer */


return node;
}

// Driver Program to test above functions


int main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);

26
insert(root, 70);
insert(root, 60);
insert(root, 80);

// print inoder traversal of the BST


inorder(root);

return 0;
}

27
23- write a program to for Stack in C

#include <stdio.h>
#include <conio.h>
#define max 5
void main()
{
//... create stack
int stack[max],data;
inttop,option,reply;
//... init stack
top = -1;
clrscr();
do
{
printf("\n 1. push");
printf("\n 2. pop");
printf("\n 3. exit");
printf("\nSelect proper option : ");
scanf("%d",&option);
switch(option)
{
case 1 : // push
printf("\n Enter a value : ");
scanf("%d",&data);
reply = push(stack,&top,&data);
if( reply == -1 )
printf("\nStack is full");
else
printf("\n Pushed value");
break;
case 2 : // pop
reply = pop ( stack,&top,&data);
if( reply == - 1)
printf("\nStack is empty");
else
printf("\n Popped value is %d",data);

28
break;
case 3 : exit(0);
} // switch
}while(1);
} // main
int push( int stack[max],int *top, int *data)
{
if( *top == max -1 )
return(-1);
else
{
*top = *top + 1;
stack[*top] = *data;
return(1);
} // else
} // push
int pop( int stack[max], int *top, int *data)
{
if( *top == -1 )
return(-1);
else
{
*data = stack[*top];
*top = *top - 1;
return(1);
} //else

29
23- Write a program to pointer

#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}

30
24- Write a program to insertion short
#include <stdio.h>

int main()
{
int n, array[1000], c, d, t;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++) {


scanf("%d", &array[c]);
}

for (c = 1 ; c <= n - 1; c++) {


d = c;

while ( d > 0 && array[d-1] > array[d]) {


t = array[d];
array[d] = array[d-1];
array[d-1] = t;

d--;
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {


printf("%d\n", array[c]);
}

return 0;
}

31

Das könnte Ihnen auch gefallen