0% found this document useful (0 votes)
3 views

Programs

The document contains multiple C programming examples demonstrating various concepts such as calculating freezer temperature, logical structures, student division based on average marks, recursion versus loops, and basic arithmetic operations. Each code snippet illustrates different programming techniques, including conditional statements, functions, and user input handling. The document serves as a practical guide for learning fundamental programming concepts in C.

Uploaded by

chikbiswal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Programs

The document contains multiple C programming examples demonstrating various concepts such as calculating freezer temperature, logical structures, student division based on average marks, recursion versus loops, and basic arithmetic operations. Each code snippet illustrates different programming techniques, including conditional statements, functions, and user input handling. The document serves as a practical guide for learning fundamental programming concepts in C.

Uploaded by

chikbiswal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Programs
/*calculation freezer temperature */

#include<stdio.h>
//#define degree 3.8 fahrenheit //macro constant
void main(){
double hr,mnt,t;
double T;
printf("enter elapse time(hr,mnt) of freezer from power failure \n");
scanf("%lf%lf",&hr,&mnt);
printf("power elaqpse time is hour=%.2lf and minute =%.2lf",hr,mnt);
t=hr+(mnt/60.0);
printf("time is %.2lf",t);
T=((4*t*t)/(t+2))-20;
printf("Freezer temperature in degree celsius = %.2lf \n",T);
}

2. Q2.
3. //loical structure
4. #include <stdio.h>;
5. void main()
6. {
7. int k;
8. int m=5;
9. k=22;
10. int t=m&k;//bitwise or operator
11. printf("t=%d",t);
12. //output=4
13. }

Question.3
14. /*WAP in c to find out division of a student as per followind data
15. if avg>=60 =>1st division
16. avg>=50 =>2nd division
17. avg>=40 =>3rd division
18. other wise FAIL.*/
19. #include <stdio.h>
20. void main(){
21. double avg;
22. printf("enter average mark \n");
23. scanf("%lf",&avg);
24. if(avg>=60)
25. printf("1st\n");
26. else if(avg>=50)
27. printf("2nd\n");
28. else if(avg>=40)
29. printf("3rd\n");
30. else
31. pri }
28.printf("FAIL\n");
32.

4.
/*function recursion vs loop
Example : factorial of a number.*/
/*#include<stdio.h>

void main(){
int num;

printf("Ntr any number(positive)\n");


scanf("%d",&num);
int r=rev(num);
printf("reverse is %d\n",r);
printf("\n");
}
int rev(int n)
{
int r=0;
while (n>0)
{
r=r*10 + n%10;
n=n/10;
}
return r;

}*/
#include<stdio.h>

void main(){
int num;

printf("Ntr any number(positive)\n");


scanf("%d",&num);
int r=rev(num);
printf("reverse is %d\n",r);
printf("\n");
}
int rev(int n)
{
int static r=0;
if(n>0)
{
r=r*10 + n%10;
n=n/10;
rev(n);
}
return r;
}

5. /*Write a program to convert a temperature in degrees Fahrenheit to


degrees

Celsius. */

#include<stdio.h>
void main(){
int far; //far=fahrenheit
double cel;//cel=celsius
printf("Enter the degree fahrenheit temperature : ");
scanf("%d",&far);
cel=((far-32)*5)/9;
printf(" degree fahrenheit to degree celsius is %.2lf \n",cel);
}

6.
/* WAP in c to find division of any student for following set of data
if avg>=60 =>1st
if avg>=50 =>2nd
if avg>=40 =>3rd
other wise fail*/
#include <stdio.h>
void main()
{
int avg;
printf("Enter avg mark\n");
scanf("%d",&avg);
if(avg>=60)
printf("you got 1st division\n");
else if(avg>=50)
printf("2nd\n");
else if(avg>=40)
printf("3rd\n");
else
printf("fail\n");
}

7.
//jump control structure
void main()
{
goto delhi;
int i=10;

while(i<5)
{delhi: //colision
printf("hello \n");
i++;
printf("i=%d\n",i);
}
}

8.
/*1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.DIVISION*/
void main(){
char z;
printf("A Addition\n");
printf("B subtraction\n");
printf("C Multiplication\n");
printf("D Division\n");
printf("enter your choice \n");
scanf("%c",&z);

switch(z)
{
case 'A':
case 'a':
add();
break;
case 'B':
case 'b':
sub();
break;
case 'C':
case 'c':
mul();
break;
case 'D':
case 'd':
div();
break;
default:
printf("You enter wrong choice\n");

}
void add()
{
int a,b,r;
printf("enter the values of a and b\n");
scanf("%d%d",&a,&b);
r=a+b;
printf("summation = %d",r);
}

void sub()
{
int a,b,r;
printf("enter the values of a and b\n");
scanf("%d%d",&a,&b);
r=a-b;
printf("subtraction = %d",r);
}

void mul()
{
int a,b,r;
printf("enter the values of a and b\n");
scanf("%d%d",&a,&b);
r=a*b;
printf("multiplication= %d",r);
}
void div()
{
float a,b,r;
printf("enter the values of a and b\n");
scanf("%f%f",&a,&b);
r=a/b;
printf("division = %f",r);
}

9.
//WAP in c to compute and display absolute difference of two variables x&y
usinng predefined function.
#include <stdio.h>
void main()
{
int x,y,diff,abs_diff;
printf("ntr values of x and y");
scanf("%i%i",&x,&y);
diff=x-y;
abs_diff=abs(x-y);
printf("difference=%d and absolute diff=%d\n",diff,abs_diff);
}

10.
#include <stdio.h>
void main(){

int num;
printf("Ntr a number :\n");
scanf("%d",&num);
int c=prime(num);
if(c==2||c==1)
printf("prime");
else
printf(" not prime");

}
int prime(int n)
{
int cnt=0;
for(int i=0;i>n;i++){
if(n%i==0){
cnt++;
}

}
return cnt;

11.
/*function recursion vs loop
Example : Check a number is prime or not
#include<stdio.h>

void main(){
int num;

printf("Ntr any number(positive)\n");


scanf("%d",&num);
int cnt=prime(num);
if (cnt==2)
printf("%d is prime\n",num);
else
printf("%d is not prime \n",num);
printf("\n");
}
int prime(int n)
{
int i,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
return c;

}
*/
/*function recursion vs loop
Example : Check a number is prime or not(recursion)*/
#include<stdio.h>

void main(){
int num;

printf("Ntr any number(positive)\n");


scanf("%d",&num);
int cnt=prime(num);
if (cnt==2)
printf("%d is prime\n",num);
else
printf("%d is not prime \n",num);
printf("\n");
}
int prime(int n)
{
int static i=1;
int static c=0;
if(i<=n)
{
if(n%i==0)
{
c++;
}
i++;
prime(n);
}
return c;

12.
/*function recursion vs loop
Example : 1 to 10.
#include<stdio.h>

void main(){
int i=1;
for(i=1;i<=10;i++){
printf("%3d",i);
}
printf("\n");

}*/
/*# include <stdio.h>

void main()
{
int static i=1;//initialize the variable only once.
if(i<=10)
{
printf("%3d",i);
i++;
main();//function recursion

}*/
/*function recursion vs loop
Example : factorial of a number.*/
#include<stdio.h>

void main(){
int num;

printf("Ntr any number\n");


scanf("%d",&num);
int r=fact(num);
printf("%d!=%d\n",num,r);
printf("\n");
}
int fact(int n){
int static f=1;
if(n>0)
{
f=f*n;
n--;
fact(n);//function recursion
}
return f;

}
/*function recursion vs loop
Example : factorial of a number.
#include<stdio.h>

void main(){
int num;

printf("Ntr any number\n");


scanf("%d",&num);
int r=fact(num);
printf("%d!=%d\n",num,r);
printf("\n");
}
int fact(int n){
int f=1;
while(n>0)
{
f=f*n;
n--;

}
return f;

}*/

13.
/*function recursion vs loop
Example : 1 to 10 */
void main(){
int i=1;
for(i=1;i<=10;i++){
printf("%3d",i);
}
printf("\n");

You might also like