PROGRAMMING IN “C”
|
Computer Science Practical Notebook of Kalyani University
|
By Ankur Pandey.
|
Contents
- FIND LARGEST OF THREE NUMBER
- FIND FIBONACCI SERIES
- RATE THE FOLLOWING SERIES, s=1+2+3+4+5+6+….+n
- CALCULATION OF BONUS
- PROGRAM FOR THE FINDING THE SUM OF n NUMBER
- CALCULATION OF GROSS SALARY
- CALCULATION THE SERIES 0 1 1 2 3 5 8 13 21 34
- FIND FACTORIAL OF A GIVEN +VE NUMBER
- CONVERT THE TEMPERATURE FAHRENHEIT TO CELSIUS
- ADD, SUB, MUL, DIV ANY TWO NUMBERS
- TO CHACK A NUMBER WHILE IT IS PRIME OR NOT
- CALCULATE THE SIMPLE INTEREST
- TO FIND SUM, DIFFERENCE, PRODUUCT AS PER THE CHOICE
- FINDING BINARY EQUIVALENT OF THE DECIMAL NUMBER
- A NUMBER WHICH ARE DISABLE BY 7 BUT NOT DIVISIBLE BY 5 IN BETWEEN 1 TO 50
- TO PRINT PASCAL TRIANGLE
- TO FIND THE PERFECT NUMBER
- REVERSE ANY NUMBER
- PROGRAM FOR THE FINDING MULTIPLICATION TABLE.
------------'''''-----------
/*largest of three number */
#include<stdio.h>
Void main()
{
Inta, b, c, l;
Clrscr();
Printf(“enter three number:”);
Scanf(“%d%d%d, & a,&b,&c”);
l=a;
if(l<c)
l=c;
printf(“largest of %d,%d and %d , a,b,c,l”);
getch();
}
2.
/*fibonicci series*/
#include <stdio.h>
void main ()
{
int n, a=0, b=1, crt= 2,c;
clrscr();
printf(“enter the number”)
scanf(“%d”,&n);
printf(“%d\n”,a);
printf(“%d\n”,b);
for(ctr<n,ctr+++);
{
c=a+b;
printf(“%d\n”,c);
a=b;
b=c;
}
getch();
}
==================================================
3.
/*jana
rate the following series S=1+2+3+4+5+6+………………..+n*/
# include<stdio.h>
main()
{
int I,n, sum;
Sum=0;
i=1;
printf(“enter the value of the n”);
scanf(“%d”,n);
while(i<= n)
{
sum=sum+i
I=i+1;
}
printf(“Sum is = %d,”sum);
gertch();
}
==================================================
4.
/* calculation of bonus */
# include<stdio.h>
main()
{
int cy, yoj, yos, bonus;
clrscr();
printf (“enter the current year & year of joining”)
scanf(“%d%d”, & cy, & yoj);
yos=cy-yoj;
if(yos>3)
{
bonus=2500;
}
else
{
bonus=0;
}
printf(“bonus=%d”, bonus);
getch();
}
====================================================
5.
/*program for the finding the sum of n number*/
#include<stdio.h>
main()
{
Int s,n;
clrscr();
printf(“enter value for n:”);
scanf(“%d”,& n);
s=(n*(n+1)/2);
printf(“\nsum s=%d,s”);
getch()
}
====================================================
6.
/* calculation of gross salary */
# include<stdio.h>
main()
{
float bs, gs, hra, da;
printf (“enter the basic salary”);
scanf(%f, & bs);
if (bs<1500);
{
hra=bs* .1;
da=bs*.9;
}
else
{
hra =500;
da=bs* .98;
}
gs= bs+hra+da;
printf(“gross salary=%.2f”, gs);
getch();
}
7.
/* 0 1 1
2 3 5
8 13 21 34
*/
# include<stdio.h>
main()
{
int a, b, c, i,n;
a=0;
b=1;
clrscr();
printf(“enter the value of n”);
scanf(“%d”, &n);
printf(“\n%d”,a);
printf(“\n%d”,b);
for(i=2; i<n; i++)
{
c=a+b;
printf(“\n%d”,c);
a=b;
b=c;
}
getch();
}
8.
/*factorial of +ve number*/
# include<stdio.h>
main()
{
int n, i, f, num;
i=1;
f=1;
clrscr();
printf(“enter a number”);
scanf(“%d”, number);
while (i,<= num)
{
f=f*I;
i=i+1;
}
printf(“factorial is %d; f”);
getch();
}
9.
/*convert the temperature Fahrenheit
to Celsius*/
#include<stdio.h>
main()
{
int c,f;
clrscr();
printf(“enter the temperature in celcius”);
scanf(“%d”, & c);
f=(((9*c)/5)+32);
printf(“the value in Fahrenheit is %d”,f);
getch();
}
10.
/*add,sub,mul,div any two numbers*/
#include<stdio.h>
main()
{
float a,b,add,sub,mul,div;
clrscr();
printf(“enter the value of a\n”);
scanf(“%d”,&a);
printf(“enter the value of b\n”);
scanf(“%d”,&b);
add=a+b;
sub=a-b;
mul=a*b;
div=ab;
printf(“addition is =%df\n”,add);
printf(“subtractions is =%df\n”,sub);
printf(“multiplication is=%df\n”,mul);
printf(“division is = %df\n”, div);
getch();
}
11.
/*to chack a number while it is prime or not */
#include<stdio.h>
main()
{
int num,I,k;
printf(“enter a number”);
scanf(“%d”, num);
k=0;
for(i=2;i<num/2; i++);
{
k=1;
break;
}
if(k= =0)
{
printf(the number is prime);
}
else
{
printf(“number is not prime”);
}
getch();
}
12.
/*calculation of simple interest*/
#include<stdio.h>
void main()
{
float p,t,r,i;
clrscr();
printf(“enter principle:\n”);
scanf(“%f”, &p);
printf(“enter year:\n”);
scanf(“%f”, &t);
printf(“enter rate of interest:\n”);
scanf(“%f”, &r);
i=(p*t*r)/100;
printf(“simple interet:%2f”,i);
}
getch();
}
=================================================
13.
/*to find sum, difference, product or quotient as per the choice*/
#include<stdio.h>
main()
{
int ch;
float a,b,ans;
printf(“\n enter any two numbers:”);
scanf(“%f%f”, &a, &b);
printf(“\n1 to find sum\n”);
printf(“2 to find difference\n”);
printf(“3 to find product\n”);
printf(“4 to find quotient\n”);
printf(“enter your choice please:”);
scanf(“%d”, &
ch);
switch(ch)
{
case1:
ans=a+b;
break;
case2:
ans=a-b;
break;
case3:
ans=a*b;
break;
case 4;
ans=a/b;default:
printf(“\n bad choice entered”);
exit();
}
printf(“\n answer=%f”, ans);
/*finding binary equivalent of the decimal number */
#include<stdio.h>
#include<math.h>
main()
{
int a[10],n,I,j=0,k;
clrscr();
printf(“\n enter any decimal no:”);
scanf(“%d”,&n);
do
{
i=fmod(n,2);
n=n/2
a[j++]=I;
if (n==1)
a [j]=n;
}
while (n>1);
k=j;
printf(“\n the binary equivalent of the decimal number
is:”);
for(j=k;j>=0;j--);
{
printf(“%d”,a[j]);
}
}
getch();
15.
/*a
number which are disable by 7 but not divisible by 5 in between 1 to 50*/
#include<stdio.h>
main()
{
int I, ctr;
ctr=0;
for(i=1; i<=50; i++);
{
if(i%7= =0 &&i!=5)
{
printf(“%d/n”,i)
ctr++;
}
}
printf(“there are total %d number”, ctr);
getch();
}
==================================================
16.
/*to print Pascal triangle*/
or, 1. Pascal triangle in c without using array, 2. C code to print Pascal triangle,
3. Simple c program for Pascal triangle, 4. C program to generate Pascal triangle,
5. Pascal triangle program in c language, 6. C program to print Pascal triangle using for loop,
#include<stdio.h>
long fact(int);
int main(){
int line,i,j;
printf("Enter the no. of lines: ");
scanf("%d",&line);
for(i=0;i<line;i++){
for(j=0;j<line-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
return 0;
}
long fact(int num){
long f=1;
int i=1;
while(i<=num){
f=f*i;
i++;
}
return f;
}
Sample output:-
Enter the no. of lines: 8
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
====================================================
17.
/*to find the perfect number*/
Code 1:
1. C program to check perfect number
#include<stdio.h>
int 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);
return 0;
}
Sample output-
Enter a number: 6
6 is a perfect number
Code 2:
1. C program to find perfect numbers
2. C perfect number code
3. Perfect number program in c language
#include<stdio.h>
int main(){
int n,i,sum;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Perfect numbers in given range is: ");
for(n=min;n<=max;n++){
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;
}
Sample output:
Enter the minimum range: 1
Enter the maximum range: 20
Perfect numbers in given range is: 6
Code 3:
3. C program to print perfect numbers from 1 to 100
#include<stdio.h>
int main(){
int n,i,sum;
printf("Perfect numbers are: ");
for(n=1;n<=100;n++){
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;
}
Output:
Perfect numbers are: 6 28
-------------------------------------
# What is perfect number?
Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
Note: 6 is the smallest perfect number.
Next perfect number is 28 since 1+ 2 + 4 + 7 + 14 = 28
Some more perfect numbers: 496, 8128
-------------------------------
====================================================
18.
/*reverse any number */
Code 1:-
1. Write a c program to reverse a given number 2. C program to find reverse of a number
3. C program to reverse the digits of a number 4. Reverse of a number in c using while loop
#include<stdio.h>
int main(){
int num,r,reverse=0;
printf("Enter any number: ");
scanf("%d",&num);
while(num){
r=num%10;
reverse=reverse*10+r;
num=num/10;
}
printf("Reversed of number: %d",reverse);
return 0;
}
Sample output:-
Enter any number: 12
Reversed of number: 21
Code 2:-
1. Reverse very large or big numbers beyond the range of long int
2. Reverse five digit number c program.
Logic is we accept the number as string.
#include<stdio.h>
#define MAX 1000
int main(){
char num[MAX];
int i=0,j,flag=0;
printf("Enter any positive integer: ");
scanf("%s",num);
while(num[i]){
if(num[i] < 48 || num[i] > 57){
printf("Invalid integer number");
return 0;
}
i++;
}
printf("Reverse: ");
for(j=i-1;j>=0;j--)
if(flag==0 && num[j] ==48){
}
else{
printf("%c",num[j]);
flag =1;
}
return 0;
Sample output:-
Enter any positive integer: 234561000045645679001237800000000000
Reverse: 8732100976546540000165432
Code 3:-
1. C program to reverse a number using for loop
2. How to find reverse of a number in c 3. Wap to reverse a number in c
#include<stdio.h>
int main(){
int num,r,reverse=0;
printf("Enter any number: ");
scanf("%d",&num);
for(;num!=0;num=num/10){
r=num%10;
reverse=reverse*10+r;
}
printf("Reversed of number: %d",reverse);
return 0;
}
Sample output:-
Enter any number: 123
Reversed of number: 321
Code 4:- 1. C program to reverse a number using recursion
#include<stdio.h>
int main(){
int num,reverse;
printf("Enter any number: ");
scanf("%d",&num);
reverse=rev(num);
printf("Reverse of number: %d",reverse);
return 0;
}
int rev(int num){
static sum,r;
if(num){
r=num%10;
sum=sum*10+r;
rev(num/10);
}
else
return 0;
return sum;
}
Sample output:-
Enter any number: 456
Reverse of number: 654
=================================================
19.
/*to find multiplication table*/
1. Multiplication tables in c program 2. Write a c program to print multiplication table
3. Code for multiplication table in c 4. Multiplication table in c language
5. Write a c program to print multiplication table
#include<stdio.h>
int main(){
int r,i,j,k;
printf("Enter the number range: ");
scanf("%d",&r);
for(i=1;i<=r;i++){
for(j=1;j<=10;j++)
printf("%d*%d=%d ",i,j,i*j);
printf("\n");
}
return 0;
}
Sample Output:-
Enter the number range: 5
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 4*10=40
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50
====================================================
No comments:
Post a Comment