0% found this document useful (0 votes)
13 views33 pages

Gautam

Uploaded by

noday48768
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views33 pages

Gautam

Uploaded by

noday48768
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

/*Source code to convert temperature from Celsius to Fahrenheit

Name – Gautam Bisht


Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
int main()
{
float f,c;
printf("\n Enter the temp. in Celsius:");
scanf("%f",&c);
f=c*1.8+32;
printf("\n the temperature in Fahrenheit is:%.2f",f);
return 0;
}

/*OUTPUT

Enter the temp. in Celsius:26

the temperature in Fahrenheit is:78.80 */


/*Source code to calculate Simple Interest and Compound interest.

Name – Gautam Bisht


Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
#include<math.h>
int main()
{
float p,r,t;
float si,A,ci;
printf("Enter the value of p,r and t");
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
A=p*pow(1+(r/100),t);
ci=A-p;
printf("\n The Simple interest is %f", si);
printf("\n The Compound interest is %f", ci);
return 0;
}

/*OUTPUT

Enter the value of p,r and t


500
2
1
The Simple interest is 10.000000
The Compound interest is 10.000000 */
/*Source Code- to calculate total salary.

Name – Gautam Bisht


Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
int main()
{
float hra, ta, da;
float bas_sal, total_sal;
printf("Enter Basic Salary");
scanf("%f",& bas_sal);
hra= bas_sal *3/100;
ta= bas_sal *5/100;
da= bas_sal *3/100;
total_sal= bas_sal +hra+ta+da;
printf("Total Salary is %f", total_sal);
return 0;
}

/*OUTPUT

Enter Basic Salary


200
Total Salary is 222.00 */
/*Source Code- to find whether a given number is even or odd

Name – Gautam Bisht


Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
int main()
{
int num;
printf("\n Enter the number:");
scanf("%d",&num);
if(num %2==0)
printf("\n The number is even ");
else
printf("\n The number is odd ");
return 0;
}

/*OUTPUT

Enter the number:4

The number is even

Enter the number:5

The number is odd */


/*Source Code- To find whether number is positive, negative or
zero (if else-if else
structure)
Name – Gautam Bisht
Section - E
Roll No - 2461105
Branch – B.Tech CSE */

#include<stdio.h>
int main()
{
int num;
printf("\n Enter the number:");
scanf("%d",&num);
if(num>0)
printf("\n The number is positive");
else if(num <0)
printf("\n The number is negative ");
else
printf("\n The number is zero ");
return 0;
}

/*OUTPUT

Enter the number:1


The number is positive
Enter the number:-2
The number is negative
Enter the number:0
The number is zero */
/*Source Code- to find biggest of three numbers (IS STRUCTURE)

Name – Gautam Bisht


Section – E
Roll No.- 2461105
Branch – B.Tech CSE */

#include <stdio.h>
int main()
{
int large, num1, num2,num3;
printf("Enter the value of num1, num2,num3\n");
scanf("%d%d%d", &num1,& num2,&num3);
large=num1;
if(num2>large)

large=num2;
if (num3>large)

large=num3;
printf("Largest no. is %d",large);
return 0;
}

/*OUTPUT

Enter the value of num1, num2,num3


34
67
44
Largest no. Is 67 */
/*Source Code- to calculate electricity bill

Name – Gautam Bisht


Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
int main()
{float unit, bill,rate;
printf("enter the units ");
scanf("%f",&unit);
if(unit>2000)
rate=7;
else if((unit<=2000)&&(unit>1000))
rate=6;
else
rate=5;
bill=rate*unit;
printf("\nBILL :%f",bill);
return 0;
}

/* output

enter the units :3000


BILL :21000.000000

enter the units :2000


BILL :12000.000000

enter the units :500


BILL :2500.000000

*/
*Source code to Calculate percentage and based upon the condition
print the grade

Name – Gautam Bisht


Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
int main()
{
float s1, s2,s3,s4,percent;
printf("Enter the value of s1, s2,s3,s4\n");
scanf("%f%f%f%f", &s1,& s2,&s3, &s4);
percent=(s1+s2+s3+s4)*100/400;
if (percent>=85)

printf("Grade A");
else if(percent>=70)

printf("Grade B");
else if(percent>=55)

printf("Grade C");
else

printf("Grade D");
return 0;
}

/*OUTPUT

Enter the value of s1, s2,s3,s4


97
89
88
95
Grade A
Enter the value of s1, s2,s3,s4
33
23
27
20
Grade D */

/*Source code to calculate bill for a shopkeeper


Name – Gautam Bisht
Section -E
Roll No.- 2461105
Branch – B.Tech CSE */

#include <stdio.h>
int main()
{
int m_qty,bill;
printf("Enter the number of mango");
scanf("%d", &m_qty);
bill=5*m_qty;
if(bill>500)
bill=bill-bill*10/100;
printf("%d", bill);
return 0;
}

/*OUTPUT

Enter the number of mango


50
250.000000

Enter the number of mango


200
900.000000 */

/*Source code to check whether entered year is leap year or not


Name – Gautam Bisht
Section - E
Roll No.- 2461105
Branch- B.Tech CSE */

#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}

*Source code to calculate area by applying validations


Name – Gautam Bisht
Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
float s,A;
printf("\n Enter the value of the sides of the triangle:");
scanf("%d%d%d",&a,&b,&c);
if((a+b>c) && (b+c>a) && (a+c>b))
{
s=(float)(a+b+c)/2;
A=sqrt(s*(s-a)(s-b)(s-c));
printf("\n The area of the triangle is:%f",A);
}
else
printf("\n The triangle is not valid");
return 0;
}

/*OUTPUT

Enter the value of the sides of the triangle:


1 1 1
The area of the triangle is:0.433013

Enter the value of the sides of the triangle:


5 1 1
The triangle is not valid*/

/*Source code to find roots of a quadratic equation. Find roots


only when
discriminant is positive.

Name – Gautam Bisht


Section - E
Roll No.- 2461105
Branch – B>Tch CSE */

#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,r1,r2,d;
printf("Enter the values for equation:");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
r1=(-b+sqrt(d))/2*a;
r2=(-b-sqrt(d))/2*a;
printf("roots are real and unequal\n");
printf("%f\n%f\n",r1,r2);
}
else
printf("roots are imaginary");
return 0;
}

/*OUTPUT
Enter the values for equation:
1 3 1
roots are real and unequal
-1.881966
-4.118034

Enter the values for equation:


2 3 4
roots are imaginary */

/*Source code to find whether entered character is small case,


capital case, a digit or a special symbol.
Name – Gautam Bisht
Section- E
Roll No.- 2461105
Branch-B.Tech CSE */

#include<stdio.h>
int main()
{
char c;
printf("Enter a character:");
scanf("%c",&c);
if((c>='a' && c<='z'))
printf("Small case letter");
else if((c>='A' && c<='Z'))
printf("Capital case letter");
else if((c>='0' && c<='9'))
printf("Digit");
else
printf("Special symbol");
return 0;
}

/*OUTPUT
Enter a character:a
Small case letter

Enter a character:S
Capital case letter

Enter a character:5
Digit

Enter a character:;
Special symbol */

/*Source Code to find the sum of even and odd numbers between x
and y.
Name – Gautam Bisht
Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
#include<conio.h>
int main()
{
int i,x,y,sume=0,sumo=0;
printf("Enter range");
scanf("%d%d",&x,&y);
i=x;
while(i<=y)
{
if(i%2==0)
sume=sume+i;
else
sumo=sumo+i;
i++;
}
printf("\nSum of even numbers is: %d",sume);
printf("\nSum of odd numbers is: %d",sumo);
getch();
return 0;
}

/*OUTPUT

Enter range
1 4
Sum of even numbers is: 6
Sum of odd numbers is: 4 */

/*Source code to print square of n numbers, where value of n is


inputted by the
user.
Name – Gautam Bisht
Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
int main()
{
int i,n,num;
printf("enter the number of terms");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter the number");
scanf("%d",&num);
printf("The square is %d \n",num*num);
}
return 0;
}

/*OUTPUT

enter the number of terms 7


enter the number 5

The square is 25

*/

/*Source code to print Fibonacci series.


0 1 1 2 3 5 8 13…………………
Name – Gautam Bisht
Section – E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<conio.h>
#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
c=0;
while(c<=n-1)
{
if ( c <= 1 )
next = c;
else
{
next=first + second;
first=second;
second=next;
}
printf(" %d",next);
c++;
}
getch();
return 0;
}

/*OUTPUT

Enter the number of terms


4
First 4 terms of Fibonacci series are :-
0 1 1 2 */

/*Source code to find the sum of digits of a number.

Name – Gautam Bisht


Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
#include<conio.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);
getch();
return 0;
}

/*OUTPUT

Enter an integer
123
Sum of digits of entered number = 6 */

/*Source code to find whether a given number is Armstrong number


or not

Name – Gautam Bisht


Section – E
Roll No.- 2461105
Branch – B.Tech CSE */

#include<stdio.h>
#include<conio.h>
int main()
{
int num, sum = 0, temp, rem;
printf("Enter an integer\n");
scanf("%d",&num);
temp=num;
while( num != 0 )
{
remainder=num%10;
sum=sum+rem*rem*rem;
num=num/10;
}
if(temp==sum )
printf("Entered number is an armstrong number.\n");
else
printf("Entered number is not an armstrong number.\n");
getch();
return 0;
}

/*OUTPUT

Enter an integer
153
Entered number is an armstrong number.
Enter an integer
675
Entered number is not an armstrong number. */

/*Source code to find whether a given number is palindrome or not.

Name – Gautam Bisht


Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include <stdio.h>
#include<conio.h>
int main()
{
int num,rem,temp,rev=0;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&num);
temp=num;
while(num!=0)
{
rem=num%10;
rev=(rev*10)+rem;
num=num/10;
}
if(rev==temp)
printf("%d is a palindrome number.\n", num);
else
printf("%d is not a palindrome number.\n", num);
getch();
return 0;
}

/*OUTPUT

Enter a number to check if it is a palindrome or not


12321
12321 is a palindrome number.

Enter a number to check if it is a palindrome or not


654
654 is not a palindrome number. */

*Source Code to print the sum of following series:


1-1/3+1/5-1/7+...............upto n terms

Name – Gautam Bisht


Section - E
Roll No.- 2461105
Branch – B>Tech CSE */

#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,sign;
float sum=0;
printf("Enter a number: ");
scanf("%d",&n);
i=1,sign=1;
while(i<=2*n-1)
{
sum=sum+(1/(float)i)*sign;
sign=sign*(-1);
i=i+2;
}
printf("Sum is %f",sum);
getch();
return 0;
}

/*OUTPUT

Enter a number: 4
Sum is 0.990476 */

/*Source code to print the following pattern according to number


of rows entered
*****
****
***
**
*
Name – Gautam Bisht
Section - E
Roll No.- 2461105
Branch – B.Tech CSE */

#include <stdio.h>
int main()
{
int n,j,c=0,k;
printf("Enter number of rows\n");
scanf("%d",&n);
while(c<=n-1)
{
j=1;
while(j<=c)
{
printf(" ");
j++;
}
k=1;
while(k<=n-c)
{
printf("*");
k++;
}
printf("\n");
c++;
}
return 0;
}

/*Output

Enter Number of rows


5
*****
****
***
**
*

*/
/*Source code- Write a c Program to search an element in a 1D
array/

Name- Gautam Bisht


Section- E
Roll no.- 2461105
Branch- B.Tech CSE */

#include<stdio.h>
int main()
{
int arr[50],i,n,key,c=0;
printf("Enter size of array : ");
scanf("%d",&n);
printf("Enter elements of array : \n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);

printf("Enter Element to Be Searched : ");


scanf("%d",&key);
for(i=0;i<n;i++)
{
if(arr[i]==key)
{
printf("Element found at %d index\n",i);
c++;
}
}
if(c==0)
printf("Element Not Found");
return 0;
}

*/OUTPUT

Enter size of array : 5


Enter elements of array :
5
4
6
3
2
Enter Element to Be Searched : 4
Element found at 1 index
*/
/* Source code -Write a C program to find maximum and minimum
element in 1-D array

Name - Gautam Bisht


Section - E
Roll No.- 2461105
Branch - B.tech C.S.E */

#include<stdio.h>
int main()
{
int arr[50],n,i,min,max;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("Enter %d elements in array",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

min=max=arr[0];
for(i=0;i<n;i++)
{
if(arr[i]>max)
max=arr[i];
else if(arr[i]<min)
min=arr[i];
}

printf("\nThe max element is:%d",min);


printf("\nThe min element is:%d",max);
return 0;
}

/*output
Enter no. of elements:10
Enter 10 elements in array9
8
7
4
5
6
3
2
1
0
The max element is:0
The min element is:9
*/

/* Source code - Write a C program to perform sorting on 1-D array

Name - Gautam Bisht


Section - E
Roll No.- 2461105
Branch - B.tech C.S.E */
include<stdio.h>
void sort(int [],int );
int main()
{
int arr[50],n,i,j,temp;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("Enter %d elements in array",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
sort(arr,n);
printf("Array Elements after Sorting");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]);
}
return 0;
}
void sort(int arr[50],int n)
{
int temp,i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
return 0; }

/*output
Enter no. of elements:10
Enter 10 elements in array17
12
13
45
56
78
89
65
64
32 Array Elements after
Sorting 12 13 17 32 45 56
64 65 78 89 */

/* Source code - Write a C Program to add two 1d array of unequal


sizes

Name - Gautam Bisht


Section - E
Roll No.- 2461105
Branch - B.tech C.S.E */

#include<stdio.h>
int main()
{
int arr1[50]={0},arr2[50]={0},sum[50],i,n1,n2,large;
printf("Enter size of 1st and 2nd array : ");
scanf("%d%d",&n1,&n2);
printf("Enter elements of 1st array : \n");
for(i=0;i<n1;i++)
{
scanf("%d",&arr1[i]);
}
printf("Enter elements of 1st array : \n");
for(i=0;i<n2;i++)
{
scanf("%d",&arr2[i]);
}
if(n2>n1)
large=n2; 20:
else
large=n1;
for(i=0;i<large;i++)
{
sum[i]=arr1[i]+arr2[i];
}
printf("Array Sum : ");
for(i=0;i<large;i++)
{
printf("%d\t",sum[i]);
}
return 0;
}

/* output

Enter size of 1st and 2nd array : 10 5


Enter elements of 1st array : 9 6 3 2 1 4 5 7 8 0
Enter elements of 2nd array : 5 9 7 6 4
Array Sum : 14 15 */

/* Source code - Write a C Program to print the upper and lower


triangle in a matrix

Name - Gautam Bisht


Section - E
Roll No.- 2461105
Branch - B.tech C.S.E */

#include<conio.h>
#include<stdio.h>
#define row 3
#define col 3
int main()
{
int ar[row][col],i,j,n; 11:
printf("Enter the elements of array\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&ar[i][j]);
}
}
printf("The elements of array\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(" %d ",ar[i][j]);
}
printf("\n");
}
if(row==col)
{
printf("The lower triangular matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(j<=i)
{
printf(" %d ",ar[i][j]);
}
}
printf("\n");
}
printf("The upper triangular matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(j>=i)
{
printf(" %d ",ar[i][j]);
}
else
{
printf(" ");
}
}
printf("\n");
}
}
else
printf("Can't calculate the upper/lower triangle in matrix"); 67:

getch();
}
return 0;
}

/* output

Enter the elements of array 12 13 32


14 25 6 3
36 35 The elements
of array
12 13 32
14 25 6
3 36 35
The lower triangular matrix
12
14 25
3 36 35
The upper triangular matrix
12 13 32
25 6
35

*/
/* Source code - Write a C program having a function which
calculates and prints the sum of n integers

Name - Gautam Bisht


Section - E
Roll No.- 2461105
Branch - B.tech C.S.E */

#include<stdio.h>
int sum(int);
int main()
{
int n,s;
printf("Enter no. of integers:");
scanf("%d",&n);
s=sum(n);
printf("The sum of Entered integers is %d",s);
return 0;
}
int sum(int n)
{
int i,num,result=0;
printf("Enter integers:");
for(i=1;i<=n;i++)
{
scanf("%d",&num);
result=result+num;
}
return result;
}

/* output

Enter no. of integers:05


Enter integers:3 6 4 5 1 The sum
of Entered integers is 19 */

/* Source code - Write a C program having function find_prime that


returns 1 if its argument is a prime number and returns 0
otherwise

Name - Gautam Bisht


Section - E
Roll No.- 2461105
Branch - B.tech C.S.E */

#include<stdio.h>
int find_prime(int);
int main()
{
int num,p;
printf("Enter any no.:");
scanf("%d",&num);
p=find_prime(num);
if(p==1)
printf("%d is a Prime Number",num);
else
printf("%d is not a Prime Number",num);
return 0;
}
int find_prime(int num)
{
int i;
if(num==1)
return 0;
else{
for(i=2;i<=num/2;i++)
{
if(num%i==0)
return 0;
}
}
return 1;
}

/* output

Enter any no.:19


19 is a Prime Number
Enter any no.:20
20 is not a Prime Number
*/

/* Source code - Write a C program to calculate sum of digit of a


given number using recursion

Name - Gautam Bisht


Section - E
Roll No.- 2461105
Branch - B.tech C.S.E */

int sum(int);
#include<stdio.h>
int main()
{
int n,ans;
printf("Enter no. \n");
scanf("%d",&n);
ans=sum(n);
printf("sum of digit of given no. is %d",ans);
return 0;
}
int sum(int n)
{
if(n>0)
return n%10+sum(n/10);
else
return 0;
}

/*output

Enter no.
456
sum of digit of given no. Is 15
*/

/* Source code - Write a C program to calculate nCr using


recursion

Name - Gautam Bisht


Section - E
Roll No.- 2461105
Branch - B.tech C.S.E */

float ncr(int,int);
#include<stdio.h>
int main()
{
int n,r;
float ans;
printf("enter n of nCr\n"); 9: scanf("%d",&n);
printf("enter r of nCr\n");
scanf("%d",&r);
if(n<r)
{
printf("Invalid Input");
return 0; 16: }
ans=ncr(n,r);
printf("nCr=%f",ans);
return 0;
}
float ncr(int n, int r)
{
float ans;
ans=(float)fact(n)/ (fact(r)*fact(n-r));
return ans;
}
int fact(int n)
{
if(n>1)
{
return n*fact(n-1);
}
else
{
return 1;
}
}

/*OUTPUT:

enter n of nCr
5
enter r of nCr
3
nCr=10.000000
*/

You might also like