Ani suru mai euta template banayera dincha tyo website le jun chai hamro C Program ko basic structure ho. Yaha baata sabai kura ke ho bistarai sikdai jaamla pahila yo part ma heram.
int main() {
printf("Hello World!");
return 0;
}
Yo Chai hamro Program ko main part ho. Tapai le yaha je je code lekhnu huncha tei tei execute huncha. Tyo int ke ho hami pachi bujhamla. Pahila ta tyo printf ma jaam.
printf vaneko C ko sabse vanda basic statement ho. Yo statement le basically hamro screen ma word print garcha. Aba print ko matlab Printer baata print garney haina screen ma kei kura dekhauney ho khas. Tapai le tyaha "" vitra je lekhnu huncha tei right patti ko screen ma dekhauncha. For example tyaha "Hello [Tapai ko Naam]" lekhera ani Run ma dabaunu ta. Ani tyei kura tyaha print huncha.
Aba tyo return 0; vaneko ke ho vaney C Program le program error free cha ki chaina check garney euta tarika ho. Tyo mathi ko kunai sentence bigrya cha vaney 0 return hudaina yo main() baata ani Compiler lai thaha huncha ki tapai le lekheko program bigreko cha vanera. Ani 0 vaneko euta integer ho ni ta tyei vayera mathi int main() lekheko ho.
Yo tyeti important chaina so tyeti dhyan na diikana alli important kura ma jaam.
Variables
Math ma hamle dherai x y z lekhirako huncham ni? Yeslai nai tayar huna lekhirako ho. Yaha variable vaneko euta kunai pani storage space ho jaha chai euta specific value store huncha. Jstai maile euta variable x banauna sakchu ani tyo x ma euta user le euta number haalna sakcha. User le tyo variable ma number kasari haalney pachi heramla ahile basic variable haru heram. Variable ko pani dherai type haru hunchan tara ahile laai main chahiney vaneko:
int
float
NOTE: Variable ko type haru lai Data Types pani vanincha.
Yo duita ho ahile main chainey. int vaneko chai integer ho mtlb positive ra negative decimal point na vaako number laai matra yelle store garna sakcha. Jstai yelle -127, 56, -900 jsto number lai store garna sakcha tara -4.644 laai chai garna sakdaina. Aba yo -4.644 laai chai kasari store garney ta?
float le. Float pani tyestai ho tara decimal point haru ko number pani store garna sakcha. Aba euta program heram jaha yestai sabai variable haru use gareko cham.
A Program to Calculate the Area of Circle
#include <stdio.h>
int main() {
int r = 5;
float pi = 3.14;
float area = pi * r * r;
}
NOTE: Harek sentence ko end ma ; lekhna na chuttaunu hola!!
Yaha chai hamle euta variable r banako cham jaha chai hamle euta circle ko radius rakheko cham. Tyo chai usually integer huney vayera int use gareko. Ani euta pi ko variable pani banako chu jasma pi ko value rakheko chu. Tyo chai obviously point haru use garney vayeko vayera float use gareko. Ani Area point ma pani aauna sakney vayera float use gareko. Aba Area Calculate the vayo. Tara tyo aba dekhauney chai kasari?
Printing Variables
Variable print garna chai ekdamai sajilo cha. Euta Example tyei mathi ko program anusar:
printf("The Radius is: %d \n", r);
printf("The Value of Pi used is: %f \n", pi);
printf("The Area of Circle is: %f \n", area);
Yo Code aagadi ko code ko last ma add garnu. Aba yaha ke vaako ta? Basically, suru ko line ma %d ko thaau ma r ko value replace vayera aauncha. Mtlb ki %d lai hatayera r ko value jaancha. Ani %d nai kina lekhya ta? Integer ko lai tei nai lekhney ho. Ani Float ko lagi %f tala lekheko jstai. Yo kura chai bistarai kantha hudai jaancha. Test garna laai yo pani try garam:
printf("%d is the Radius.", r);
Yaha pani %d ma r replace vayera aauncha. So Variable Print garna laai hamle yo garna parchaaa.
Ani tyo \n vaneko C ko Enter ho basically. Euta line chodcha.
User Input
Mathi ko program ramro cha haina. Tara tyo program lai ajhai ramro banauna milcha k. What if tyo program run garda kheri user le nai radius input garney ani tyo radius anusar Area print garney. Well tyo program tala dekhaiyeko cha:
#include <stdio.h>
int main() {
int r;
float pi = 3.1415;
printf("Enter the radius of circle: ");
scanf("%d", &r);
float area = pi * r * r;
printf("The Area of the Circle you entered is: %f", area);
return 0;
}
Aba tapai ko dimag ma euta question aako hola. Tyo & Kinaa???? Tyo vanda pahila aru kura heram. Hamle user baata input maagna ko laagi scanf() vanney function use garcham. Ani tyo kun variable ho specify garna laai variable print gareko jstai %d integer laai ra %f float laai use garchamm. Ani tyo & chai kina ta? Tyo & vaneko address specify garney ho. Like variable haru store huney vaneko RAM ma ho ra RAM ma store garney thaau haru dheraiii hunchan, tii thaau haru laai address vanincha. Ani tyo r variable kaha store vaako cha vanera chai &r le vaneko huncha. Euta example laai yo pani print garam ta:
printf("%d", &r);
Yesle chai r ko address ke ho vanera dekhauncha. Tei vayera chai hamle & use gareko ho scanf ma. Yesari samjhim kii scanf aauney bittikai & use garney variable ma.
Operations
Variables haru ma math pani use garna milcha. Jstai:
#include <stdio.h>
int main() {
int a = 5, b = 6;
int add = a + b;
int diff = a - b;
int mul = a * b;
int div = a / b;
printf("Adding a and b: %d \n", add);
printf("Subtracting a and b: %d \n", diff);
printf("Multiplying a and b: %d \n", mul);
printf("Dividing a and b: %d \n", div);
return 0;
}
Yaha sabai self-explanatory cha. * vaneko multiply ani / vaneko divide ho Computer ko sense ma chai. Aba yaha aarko kura aauncha. Jstai ki malai euta program lekhna mann laagyo jaha chai euta rectangle ko perimeter ko output chaiyeko cha. Aba maile:
int perimeter = 2(l+b);
Yesto maile lekhna paaudina. Tyo 2 ra (l ko bich ma ke cha vanera computer laai specify garna parcha. Yesto huncha:
int perimeter = 2 * (l + b);
Tyestai Aru:
Simple Interest: (p * t * r) / 100
Perimeter of Square: 4 * l
Example Programs
WAP to enter marks of five different subjects and find the average of those marks.
#include <stdio.h>
int main() {
int m1, m2, m3, m4, m5;
float avg;
printf("Enter marks of five subjects: ");
scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5);
avg = (m1 + m2 + m3 + m4 + m5) / 5
printf("The Average is: %f", avg);
return 0;
}
WAP to input length and breadth of a rectangle and print it's perimeter and area.
#include <stdio.h>
int main() {
int l, b;
printf("Enter length and breadth: ");
scanf("%d %d", &l, &b);
int p = 2 * (l + b);
int a = l * b;
printf("\n The Perimeter is %d \n The Area is %d", p, a);
return 0;
}
WAP to input radius of a circle and print it's circumference.
#include <stdio.h>
int main() {
int r;
float pi = 3.1415;
printf("Enter the radius: ");
scanf("%d", &r);
float area = pi * r * r;
printf("The Area of the circle is: %f", area);
return 0;
}
WAP to input distance between two houses in km and print it in m.
#include <stdio.h>
int main() {
int km;
printf("Enter distance in km: ");
scanf("%d", &km);
float m = km / 1000;
printf("The Distance in m is: %f", m);
return 0;
}