Vous êtes sur la page 1sur 27

Computer

Programming

MAYANK SHARMA
40516100418
BTech BCE
INDEX
S NO. TOPIC DATE OF
SUBMISSION SIGNATURE
/* Write a program to swap two numbers without using a third variable */
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
int a,b;
printf("Enter 1st number");
scanf("%d",&a); //entering first value
printf("Enter 2nd number");
scanf("%d",&b); //entering second value
a=a+b;
b=a-b; //CODE FOR SWAPPING
a=a-b;

printf("Values of 'a' and 'b' after swapping are %d and %d ",a,b);


getch();
}

Output
Enter 1st number 9
Enter 2nd number 81
Values of 'a' and 'b' after swapping are 81 and 9
/* Write a program to swap two numbers using a third variable */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
printf("Enter 1st variable");
scanf("%d",&a); //entering 1st value
printf("Enter 2nd variable");
scanf("%d",&b); //entering 2nd value
temp=a;
a=b;
b=temp; //code to swap
printf("Values of 'a' and 'b' after swapping are %d and %d respectively",a,b);
getch();
}

Output
Enter 1st variable 72
Enter 2nd variable 12
Values of 'a' and 'b' after swapping are 12 and 72 respectively
/* Write a program to check that given numbers are prime or not */
#include<stdio.h>

#include<conio.h>

void main()

int n, i, flag = 1;

printf("Enter a positive integer: ");

scanf("%d",&n);

for(i=2; i<=n/2; ++i)

if(n%i==0)

flag=0;

break;

if (flag!=1)

printf("%d Is a prime number.",n);

else

printf("%d Is not a prime number.",n);

getch();

}
Output

Enter a positive integer: 11

11 Is a prime number.
/*Write a Program to find factorial of given number*/
#include <stdio.h>
#include<conio.h>

void main()
{
int i, n, fact = 1; //declaring variable

printf("Enter a number: ");


scanf("%d", &n);
for (i=1;i<=n;i++)
fact = fact*i;
printf("Factorial of %d = %d\n", n, fact);
getch();
}

Output
Enter a number: 8

Factorial of 8 =40320
/*Write a program to check given year is a leap year or not */
#include<stdio.h>
#include<conio.h>
int main()
{
int year;

printf("Enter a year to check if it is a leap year\n");


scanf("%d", &year); //accepting year

if ( year%400 == 0)
printf("%d is a leap year.\n", year);

else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);

else
printf("%d isn't a leap year.\n", year);
getch();
}

Output
Enter a year to check if it is a leap year
2016
2016 is a leap year
/* Write a program to print the Fibonacci series */
#include<stdio.h>
int main()
{
int n, first=0, second=1, next, c;
printf("enter no. of terms\n");
scanf("%d", &n);
printf("first %d terms of 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;
}

Output

enter no. of terms


5
first 5 terms of series are:
0
1
1
2
3
/* Write a program to make a calculator*/
#include<conio.h>
#include<stdio.h>
int main()
{ char operator;
float f1,f2;
printf("enter the operator(+,-,*,/):");
scanf("%c",&operator);
printf("enter the numbers:");
scanf("%f %f",&f1,&f2);

switch(operator)
{ case '+':
printf("%f + %f= %d", f1,f2,f1+f2);
break;
case '-':
printf("%f - %f= %f", f1,f2,f1-f2);
break;
case '*':
printf("%f * %f= %f", f1,f2,f1*f2);
break;
case '/':
printf("%f / %f= %f", f1,f2,f1/f2);
break;

default:
printf(" error! wrong operator input ");
}
return 0;
}

Output
enter the operator(+,-,*,/): +
enter the numbers: 5 6
5 +6 = 11
/* Write a program to perform matrix multiplication*/
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of 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 elements of second matrix\n");

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");

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


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}

return 0;
}
Output:
enter the number of rows and columns
2
2
Enter the elements of first matrix
12
34
Enter the elements of second matrix
56
21
Sum of entered matrices:
68
55
/* Write a program to perform matrix multiplication*/
#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 number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

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


scanf("%d%d", &p, &q);

if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter 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 the matrices:\n");

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


for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
return 0;
}

Output:
enter the number of rows and columns of first matrix
3
3
Enter the elements of first matrix
120
011
201
enter the number of rows and columns of second matrix
3
3
Enter the elements of second matrix
112
211
121
Product of entered matrices:
534
332
345
/* Write a program to find factorial of a number using recursion*/
#include <stdio.h>
long int multiplyNumbers(int n);

int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Output
enter a positive integer
6

Factorial of 6 = 720
/* Write a program to check that whether a given number is Armstrong number
or not.*/
#include <stdio.h>
int 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);

return 0;
}

Output
Enter a three digit integer: 153
153 is an Armstrong number.

Enter a three digit integer: 300


300 is not an Armstrong number.
/* Write a Program to print the pyramid*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j; //Declaration of variables
clrscr(); //To clear the output screen
printf("Enter a number till which you want to print the pyramid:"); //To print on output screen
scanf("%d",&n); //To take input from user
for(i=1;i<=n;i++) //For loop header
{
for(j=1;j<=i;j++)
{
printf("%d\t",i);
}
printf("\n");
}
getch();
}

Output
Enter the number till you want to print the pyramid:
1
22
333
4444
55555
/* Write a program to print Floyd’s triangle*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a=1; //Declaration of variables
clrscr(); //To clear the output screen
printf("Enter number upto which you want to print the floyd's triangle:"); //To print output on screen
scanf("%d",&n); //To take input from user
for(i=1;i<=n;i++) //For loop header
{
for(j=1;j<=i;j++)
{
printf("%d\t",a);
a++;
}
printf("\n");
}
getch();
}

Output
Enter the number upto which you want to print floyd’s triangle:
1
23
456
7 8 9 10
11 12 13 14 15
/* Write a program to find the sum of all elements in array using pointer*/
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10],sum=0,i; //Declaration of variables
int *ptr; //Declaration of pointer
clrscr(); //To clear the output screen
printf("Enter the elements of array:"); //To print on the output screen
for(i=0;i<10;i++) //For loop header
{
scanf("%d",&A[i]); //To take input from user
}
ptr=A; //Assigning value to pointer
for(i=0;i<10;i++)
{
sum=sum+*ptr;
ptr++; //pointer incrementation
}
printf("\nSum of the elements:%d",sum);
getch();
}

Output
Enter the elements of array
2345124
Sum of elements: 16
/* Write a program to delete an element from array by asking user the location
of deletion*/
#include<stdio.h>
#include<conio.h>
void main()
{

char ch; //Declaration of characters


int A[100],i,n,pos; //Declaration of variables
clrscr(); //To clear previous output screen
printf("Enter the number of elements of array:"); //To print on output screen
scanf("%d",&n); //To take input from user
printf("\nEnter the elements of array:");

for(i=0;i<n;i++) //For loop header


{
scanf("%d",&A[i]);
}
clrscr();
printf("\nArray:");
for(i=0;i<n;i++)
{
printf("%d,",A[i]);
}
printf("\nChoose the element you want to delete:");
printf("\n1.Delete the element entered");
printf("\n2.Delete the element before the entered element");
printf("\n3.Delete the element after the entered element");
printf("\nEnter your choice:");
scanf("%d",&ch);

switch(ch) //To provide menu


{
case 1: //Options provided in menu
printf("\nEnter the location:");
scanf("%d",&pos);
if(pos>=n+1)
printf("\nElement cannot be deleted");
else
{
for(i=pos-1;i<n-1;i++)
A[i]=A[i+1];
printf("\nResultant array:");

for(i=0;i<n-1;i++)
printf("%d,",A[i-1]);
break; //To end the case
}
case 2:
printf("\nEnter the location:");
scanf("%d",&pos);
if(pos>=n+1)
printf("\nElement cannot be deleted");
else
{

for(i=pos-2;i<n-1;i++)
A[i]=A[i+1];
printf("\nResultant array:");
for(i=0;i<n-1;i++)
printf("%d,",A[i-1]);
break;
}
case 3:
printf("\nEnter the location:");
scanf("%d",&pos);
if(pos>=n+1)
printf("\nElement cannot be deleted");
else
{

for(i=pos;i<n-1;i++)
A[i]=A[i+1];
printf("\nResultant array:");
for(i=0;i<n-1;i++)
printf("%d,",A[i]);
break;
}
default:
printf("\nWrong choice!");
}
getch();
}

Output
Array :1,2,3,4,5,6,7,8,9,0
Choose the element you want to delete:
1. Delete the element entered.
2. Delete the element before the entered element.
3. Delete the element after the entered element.
Enter your choice: 3
Enter the location: 4
1,2,3,4,6,7,8,9,0
/* Write a program to check whether the number is automorphic or not.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,rem1=0,c,rem2=0; //Declaration of variables
clrscr(); //To clear previous output screen
printf("\nEnter a number:"); //To print on output screen
scanf("%d",&a); //To take input from user
c=pow(a,2);
rem1=a%10;
rem2=c%10;
if(rem1==rem2)
printf("\nNumber is automorphic");
else
printf("\nNumber is not automorphic");
getch();
}

Output
Enter a number:
5

Number is automorphic
/* Write a program to print Fibonacci series using recursion*/
#include<stdio.h>
#include<conio.h>
int fib(int);
void main()
{
int i,x,n; //Declaration of variables
clrscr(); //To clear previous output screen
printf("Enter the number of elements you want: "); //To print on the output screen
scanf("%d",&x); //To take input from user
printf("\n\n");
for(i=1;i<=x;i++) //For loop header
{
n=fib(i);
printf("%d ",n);
}
getch();
}
int fib(int n)
{
if(n==1)
return(0);
if(n==2)
return(1);
return(fib(n-1)+fib(n-2)); //Recursion
}

Output
Enter the number of elements you want : 10
0 1 1 2 3 5 8 13 21 34
/* Write a program to store information of students using structures.*/
#include<stdio.h>
#include<conio.h>
struct stud //Structure body
{
char name[10];
int roll;
float per;
}s[2]; //Structure object
void main()
{
int i; //Declaration of variables
clrscr(); //To clear the previous output screen
printf("Enter the information:"); //To print on the output screen
for(i=0;i<2;i++) //For loop header
{
s[i].roll=i+1;
printf("\nEnter for student %d\n",s[i].roll);
printf("\nEnter name: ");
scanf("%s",&s[i].name); //To take input from user
printf("\nEnter percentage: ");
scanf("%f",&s[i].per);
}
printf("\n\n\n");
printf("Roll no.\tName\tPercentage");
for(i=0;i<2;i++)
{
printf("\n%d\t\t%s\t\t%f",s[i].roll,s[i].name,s[i].per);
}
getch();
}
Output
Enter for student 1
Enter name: Mayank
Enter percentage: 98
Enter for student 2
Enter name: Salman
Enter percentage:69
Enter for student 3
Enter name: Aamir
Enter percentage: 64
Enter for student 4
Enter name: John
Enter percentage: 70
Enter for student 5
Enter name: Roman
Enter percentage: 96

Roll no. Name Percentage


1 Mayank 98
2 Salman 69
3 Aamir 64
4 John 70
5 Roman 96
/* Write a program to find the largest and smallest element of array and
interchange it*/
#include<stdio.h>
int main()
{
int a[20],b[20],n,sml=0,lar=0,i,spos,lpos,temp;
printf("Enter the Numbers of terms: ");
scanf("%d",&n);
printf("\nEnter the terms : \n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sml=a[1];
for(i=1;i<=n;i++)
{
if(a[i]<=sml)
{
sml=a[i];
spos=i;
}
if(lar<=a[i])
{
lar=a[i];
lpos=i;
}
}
temp=a[spos];
a[spos]=a[lpos];
a[lpos]=temp;

printf("\nThe Array enter by user are :\n");


for(i=1;i<=n;i++)
printf("%d\t",b[i]);
printf("\nThe Array after interchanging the largenst and smallest element :\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
return 0;
}
Output
Enter the Numbers of terms: 5
Enter the terms :
74615
The Array enter by user are :
7 4 6 1 5
The Array after interchanging the largest and smallest element :
1 4 6 7 5
/* Write a program to find whether a character is consonant or vowel*/
#include <stdio.h>
void main()
{
char ch;
printf("Input a character\n");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is a consonant.\n", ch);
}
}

Output
Input a character
I
I is a vowel.

Input a character
S

S is a consonant.

Vous aimerez peut-être aussi