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

Computer Codes

The document contains several code snippets showing the use of basic C programming concepts like functions, arrays, pointers, loops etc. to solve problems related to calculations, input/output, conditional statements. Different code examples demonstrate concepts like defining and calling functions, passing arguments by value and reference, using arrays and loops to iterate through elements, performing basic math operations and type conversions.

Uploaded by

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

Computer Codes

The document contains several code snippets showing the use of basic C programming concepts like functions, arrays, pointers, loops etc. to solve problems related to calculations, input/output, conditional statements. Different code examples demonstrate concepts like defining and calling functions, passing arguments by value and reference, using arrays and loops to iterate through elements, performing basic math operations and type conversions.

Uploaded by

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

Computer codes

1 homework
int main() {//home work to get sum etc..
printf("enter 3 numbers\n");
float x,y,z,sum,sub,multi,dv;
scanf("%f\n%f\n",&x,&y,&z);
sum=x+y+z;
sub=x-y-z;
multi=x*y*z;
dv=(x/y)/y;
printf(" sum is %f\n sub is %f\n multi is %f\n dv is %f",sum,sub,multi,dv);
return 0;
}
__________________________________________

2 getting volume with functions


int get_rec_volume(int a, int b, int c);

int main() {
int h,w,l;
printf("enter h w and l bruv\n");
scanf("%d\n%d\n%d",&h,&w,&l);
int vol;
vol = get_rec_volume(h,w,l);
printf("Volume equals %d",vol);
}

int get_rec_volume(int a, int b, int c){

return a*b*c;
}
_________________________________________
3 Money exchange

#include <stdio.h>
float USD_to_NIS(float a,float r);
float EUR_to_NIS(float a,float r);

int main() {
float usdr,eurr,amount1,amount2,results;
printf("enter USD/NIS Rate.");
scanf("%f",&usdr);
printf("enter EUR/NIS rate");
scanf("%f",&eurr);
printf("enter amount in USD.");
scanf("%f",&amount1);
printf("enter amount in EUR.");
scanf("%f",&amount2);
results = USD_to_NIS(amount1,usdr);
printf("the result is %f",results);
results= EUR_to_NIS(amount2,eurr);
printf("the result is %f",results);
return 0;
}
float USD_to_NIS(float a,float r)
{
float results;
results=a*r;
return results;
}
float EUR_to_NIS(float a,float r)
{
float results;
results=a*r;
return results;
}
_________________________________________
4 Volume

// Online C compiler to run C program online


#include <stdio.h>
float get_v(float R, float H);
float get_A(float R);

int main() {
float R,H,V,A;
printf("enter R,H for a cylinder\n");
scanf("%f\n%f",&R,&H);
V = get_v(R,H);
printf(" the volume is %f ^3\n",V);
A = get_A(R);
printf(" the area is %f m^2",A);
return 0;
}
float get_v(float R, float H)
{
float v;
return v;
}
float get_A(float R)
{
float A;
A= 3.14*R*R;
return A;
}
_________________________________________
5 XY plane

#include <stdio.h>
#include <math.h>
double get_d(int x1, int y1, int x2, int y2);
int main() {
int x1,x2,y1,y2;
printf("enter x1, y1, x2, y2.\n");
scanf("%d\r%d\r%d\r%d",&x1,&y1,&x2,&y2);
get_d(x1,y1,x2,y2);

return 0;
}
double get_d(int x1, int y1, int x2, int y2){
double d;
d=sqrt(pow((x2-x1),2) +pow((y2-y1),2));

printf("%lf",d);

}
_________________________________________
6 surface area and volume

#include <stdio.h>
#include <math.h>
#define pi 3.14
float SA(float r);
float V(float r,float h);
int main() {
float r,h,sa,v;
printf("enter r and h\n");
scanf("%f\n%f",&r,&h);
sa= SA(r);
v= V(r,h);
printf("the surface area is %f \r the volume is %f",sa,v);
return 0;
}
float SA(float r){
return (2*pi*r);
}
float V(float r,float h){
return pow (r,2)*pi*h;
}
_________________________________________
7 area in inches and cm

#include <stdio.h>
#include <math.h>
float AI (float x);
float AC (float x);
int main() {
float x,Ai,Ac;
printf("enter the length of the square\n"),
scanf("%f",&x),
Ai= AI(x);
Ac= AC(Ai);
printf ( "the are in inches is %f in^2 \n the area in cm is %f cm^2",Ai,Ac);
}
float AI (float x){
return pow(x,2);
}
float AC (float x){
return x*2.54;
}
_________________________________________
8 estimation

#include <stdio.h>

int main() {
float x;
printf("enter number\n");
scanf("%f",&x);
printf("%0.2f",x);
return 0;
}
_________________________________________
9 Area of a triangle
#include <stdio.h>
#include <math.h>
float A(float a,float b, float c);

int main() {
float a,b,c,area;
scanf("%f\n%f\n%f\n",&a,&b,&c);
if((a+b)>c && (b+c)>a && (a+c)>b)
{
area= A(a,b,c);
printf("%f",area);
}
else
{
printf("try again mate");
}

float A(float a,float b, float c){


float s;
s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
_________________________________
Basic calculator
// Online C compiler to run C program online
#include <stdio.h>
#include <math.h>

int main()
{
float a,b,x;
char o;
scanf("%f\n%c\n%f",&a,&o,&b);
switch(o)
{
case '+':
{
x=a+b;
printf("%f",x);
break;
}
case '-':
{
x=a-b;
printf("%f",x);
break;
}
case '*':
{
x=a*b;
printf("%f",x);
break;
}
case '/':
{
if (b!=0)
{
x=a/b;
printf("%f",x);
}
else
{
printf("no can do");
}
break;
}
case '%':
{
x=fmod(a,b);
printf("%f",x);
break;
}
default: {printf("try again bruv");}
}
return 0;
}
_____________________________
Loop (needs fixing)

// Online C compiler to run C program online


#include <stdio.h>

int main() {
int g;
int avg;
int c=1;
int sum=0;
while (c<=10)
{
printf("enter g");
scanf ("%d",&g);
sum = sum+g;
c++;

}
avg= sum/10;
printf("avg is %d",avg);
switch (avg)
{
case (avg>8):
{
printf ("wa7sh");
break;

}
case (avg>5):
{
printf("less wahsh");
break;

}
default:
{
printf("hemar");}
}
return 0;
}
_________________
Printing stars

// Online C compiler to run C program online


#include <stdio.h>

int main() {
int n=0;
int l=1;
while (l<=6){
while (n<l)
{
printf ("*");
n++;
}
n=0;
printf("\n");
l++;
}

return 0;
}
__________________
First 10 numbers that are divisible by 5
// Online C compiler to run C program online
#include <stdio.h>
int main() {
int c,t,r;
c=0;
t=0;

while (t<=10)
{
c=c+1;
r=c%5;
if (r==0){
t=t+1;
printf("%d\n",c);
}
}
return 0;
}
____________________
Number check
// Online C compiler to run C program online
#include <stdio.h>

int main()
{
int a,b;
int r=1;
while (r>0)
{
printf("enter the numbers\n");
scanf("%d\n%d",&a,&b);
r=b%a;
if (r!=0)
printf("again ");
}
printf("u done it %d is divible by %d",b,a);
return 0;
}
___________________
Prime numbers by loop

// Online C compiler to run C program online


#include <stdio.h>

int main() {
int x,c,r,t;
c=1;
printf("enter number\n");
scanf("%d",&x);
while (x>=c){
r=x%c;
c++;
if (r==0)
t=t+1;
}
if (t==2)
printf(" %d is primary",x);
else
printf(" %d is not primary",x);
return 0;
}
________________________

Printing letters in a box shape


// Online C compiler to run C program online
#include <stdio.h>

int main() {
char x;
int a=0;
for(x='a';x<='z';x++){
printf ("%c ",x);
a++;
if(a==5){
printf("\n");
a=0;
}
}
}
____________________________
Quiz 1 changing from C to F

// Online C compiler to run C program online


#include <stdio.h>
void F(float);
void C(float);
int main() {
float t;
char x;
printf("enter the tempreture u want to convert with F for fahrengeit and C for
Celsius\n");
scanf("%f %c",&t,&x);
switch (x)
{
case 'F':
{
C(t);
break;
}
case 'C':
{
F(t);
break;
}
default :
printf("F for fahrengeit and C for Celsius only");
}
}

void C(float t){


float c;
c= 5*(t-32)/9;
;
printf("%f",c);
}
void F(float t){
float f;
f= (9*t/5) + 32;
printf("%f",f);
}
______________________________
Pointers
// Online C compiler to run C program online
#include <stdio.h>

int main() {
int q=2;
int *p;
p=&q;
*p=100;
printf("%d\n",q);
printf("%p\n",p);
printf("%d\n",*p);
printf("%p\n",&q);
printf("%p\n",&p);
return 0;
}
__________________
Pointers 2
// Online C compiler to run C program online
#include <stdio.h>

int main() {
int x=3,y=4,z=6;
int *p1, *p2,*p3;
p1=&x;
p2=&y;
p3=&z;
*p1=*p2+*p3;
(*p1)++;
(*p2)--;
*p1=(*p2)*(*p3);
*p2=(*p2)*(*p1);
x=y+z;
printf("%d\n",x);
printf("%d\n",y);
printf("%d\n",z);
printf("%d\n", *p1);
printf("%d\n",*p2);
printf("%d\n",*p3);
return 0;
}
______________________
Calc with functions

// Online C compiler to run C program online


#include <stdio.h>
#include <math.h>
void sum(float,float);
void sub(float,float);
void mult(float,float);
void divs(float,float);
void remm(float,float);

int main()
{
float a,b,x;
char o;
scanf("%f\n%c\n%f",&a,&o,&b);
switch(o)
{
case '+':
{
sum(a,b);
break;
}
case '-':
{
sub(a,b);
break;
}
case '*':
{
mult(a,b);
break;
}
case '/':
{
divs(a,b);
break;
}
case '%':
{
remm(a,b);
break;
}
default: {printf("try again bruv");}
}
return 0;
}
void sum(float a,float b){
float x;
x=a+b;
printf("sum is: %f",x);
}
void sub(float a,float b){
float x;
x=a-b;
printf("sub is: %f",x);
}
void mult(float a,float b){
float x;
x=a*b;
printf("sum is: %f",x);
}
void divs(float a,float b){
float x;
if (x!=0)
{
x=a/b;
printf("div is: %f",x);
}
else
{
printf("no can do");
}
void remm(float a,float b){
float x;
x=fmod(a,b);
printf("rem is: %f",x);
}
}
________________
Calc with pointers

// Online C compiler to run C program online


#include <stdio.h>
float fun(float,float,float*,float*,float*,float*);
int main() {
float a,b,sum,sub,multi,divs;
printf("enter 2 numbers\n");
scanf("%f\n%f",&a,&b);
fun(a,b,&sum,&sub,&multi,&divs);
printf("%f\n%f\n%f\n%f",sum,sub,multi,divs);
return 0;
}
float fun(float a,float b,float *sum,float *sub,float *multi,float *divs){
*sum=a+b;
*sub=a-b;
*multi=a*b;
*divs=a/b;
}
_______________________
For pointers u can’t just use a pointer alone it should be linked with smthn also
u don’t use & with them
________________
Arrays (u can use them when having smthn like a date sheet
U define it like
Int grade[6];
Then go in to fill the 6 locations like
Grade (0) = 90 etc..
And u use the array to give a value i.e. (my grade= grade(0))
\⸻⸻
Int g[10]
For(I=0,i<0;I++)
{scanf(“%d”.&g[I];]
_______________________
// Online C compiler to run C program online
#include <stdio.h>

int main() {
int i, f[5],x,z;
printf("enter a num\n");
for(i=0;i<=4;i++){
scanf("%d",&f[i]);
if (f[i]<=f[0]){
x=f[i];
z=i;
}
}

printf("smallest is %d and its the %d",x,z);


return 0;
}
__________________________
// Online C compiler to run C program online
#include <stdio.h>
void a(int r[]);
void b(int x[], int y[], int sum[]);
int main() {
int x[3],y[3],sum[3];
printf("enter first set of values\n");
a(x);
printf("enter second set of values\n");
a(y);
b(x,y,sum);

return 0;
}
void a(int r[]){
int i;
for(i=0;i<=2;i++){
scanf("%d",&r[i]);
}
}
void b(int x[], int y[],int sum[]){
int i;
for (i=0;i<=2;i++){
sum[i]=x[i]+y[i];
}
for (i=0;i<=2;i++){
printf("%d\n",sum[i]);
}
}

You might also like