Académique Documents
Professionnel Documents
Culture Documents
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;
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;
scanf("%d",&n);
if(n%i==0)
flag=0;
break;
if (flag!=1)
else
getch();
}
Output
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
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;
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
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];
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];
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
multiply[c][d] = sum;
sum = 0;
}
}
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;
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.
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()
{
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
Output
Input a character
I
I is a vowel.
Input a character
S
S is a consonant.