Computer Codes
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;
}
__________________________________________
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);
}
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
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");
}
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)
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
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
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;
}
________________________
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
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
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
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;
}
}
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]);
}
}