C programming
1.Structure declaring type 1
#include<stdio.h>
#include<string.h>
struct Value{
int id;
int age;
char name[20];
}s1;//declaring after struct data type
int main()
{
s1.id=6;
s1.age=55;
strcpy(s1.name,"roman");
printf("id of student no. 1 is %d\n",s1.id);
printf("age of student no. 1 is %d\n",s1.age);
printf("name of student no. 1 is :%s",s1.name);
return 0;
}
2.Structure declaring type 2
#include<stdio.h>
#include<string.h>
struct Value{
int id;
int age;
char name[20];
};
int main()
{
//declaring in main function
struct Value s1;
s1.id=6;
s1.age=55;
strcpy(s1.name,"roman");
printf("id of student no. 1 is %d\n",s1.id);
printf("age of student no. 1 is %d\n",s1.age);
printf("name of student no. 1 is :%s",s1.name);
return 0;
}
3.String printing methods
#include<stdio.h>
void func(char name[]){
int i=0;
while (name[i]!='\0'){
printf("%c",name[i]);
i++;
}
}
int main()
{
//methods for printing strings.
char name[15];
gets(name);
printf("using puts:\n");
puts(name);
printf("using printf:\n");
printf("%s\n",name);
printf("using function:\n");
func(name);
return 0;
}
4.String Functions
#include<stdio.h>
#include<string.h>
int main()
{
char f[10]="nabin";
char l[30]=" rimal";
//strcat() function
printf("full name be=\n");
//puts(strcat(f,l));
//strlen() function;
printf("the length be=%d\n",strlen(l));
//strrev function
printf("the reverse be:\n");
// puts(strrev(f));
//does not work on phone.
//strcpy() function
printf("the strcpy() function copy second to first:");
puts(strcpy(l,f));
printf("\nthe strcmp() of l and f be =%d",strcmp(f,l));
return 0;
}
5.Array Arithmetic
#include<stdio.h>
int main()
{
int arr[]={7,6,7};
int*arry=&arr;
//for address
printf("%p\n",arr+1);
printf("%p\n",&arr[1]);
printf("%p\n",*&arry+1);
//for value
printf("%d\n",*(arr+1));
printf("%d\n",arr[1]);
printf("%d\n",*(*&arry+1));
return 0;
}
6.Grade showing program.
#include<stdio.h>
int main(){
int a;
printf("Enter the percentage=");
scanf("%d",&a);
if(a<20)
{
printf("your grade is E");
}
else if((a<=29 && a>=20))
{
printf("your grade is D");
}
else if((a<=39 && a>=30))
{
printf("your grade is D+");
}
else if((a<=49 && a>=40))
{
printf("your grade is C");
}
else if((a<=59 && a>=50))
{
printf("your grade is C+");
}
else if((a<=69 && a>=60))
{
printf("your grade is B");
}
else if((a<=79 && a>=70))
{
printf("your grade is B+");
}
else if((a<=89 && a>=80))
{
printf("your grade is A");
}
else if((a<=100 && a>=90))
{
printf("your grade is A+");
}
else{
printf("you have entered wrong format");
}
return 0;
}
7.Star pattern program
#include<stdio.h>
int main()
{
int a,b,c;
printf("enter the number:");
scanf("%d",&a);
for(;b<=a;b++){
for(c=1;c<=b;c++)
{
printf("*");
}
printf("\n");
}
return 0;
}
8.Infinite count program
#include<stdio.h>
int main()
{
int a;
for( a=1;a++;){
printf("%d \n",a);
}
return 0;
}
This program could harm your computer.
9.Sum Of the Series/first and last term are given
#include<stdio.h>
int fnum(int num1,int num2){
int proc1=(num2-num1+1);
int proc2=(proc1*(proc1+1))/2;
return proc2;
}
int main()
{int a,b;
printf("sum of the series when last and first terms are given\n");
printf("\nEnter First Number=");
scanf("%d",&a);
printf("Enter Last Number==");
scanf("%d",&b);
printf("Sum Of The Series be=%d",fnum(a,b));
return 0;
}
10.Practice/grade showing program+goto function
#include<stdio.h>
int main(){
int a;
lable:
printf("\nEnter the value=");
scanf("%d",&a);
if(a<20)
{
printf("your grade is E");
goto lable;
}
else if((a<=29 && a>=20))
{
printf("your grade is D");
goto lable;
}
else if((a<=39 && a>=30))
{
printf("your grade is D+");
goto lable;
}
else if((a<=49 && a>=40))
{
printf("your grade is C");
goto lable;
}
else if((a<=59 && a>=50))
{
printf("your grade is C+");
goto lable;
}
else if((a<=69 && a>=60))
{
printf("your grade is B");
goto lable;
}
else if((a<=79 && a>=70))
{
printf("your grade is B+");
goto lable;
}
else if((a<=89 && a>=80))
{
printf("your grade is A");
goto lable;
}
else if((a<=100 && a>=90))
{
printf("your grade is A+");
goto lable;
}
else{
printf("you have entered wrong format");
goto lable;
}
return 0;
}
11.multiplying two values using functions(declaring function).
#include<stdio.h>
int mynum(int a,int b);
int main()
{
int a,b,c;
printf("Enter Any Number=");
scanf("%d",&a);
printf("Enter Any Number=");
scanf("%d",&b);
c= mynum(a,b);
printf("multiplication be=%d",c);
}
int mynum(int a,int b)
{
return a*b;
}
12.Call By Value
#include<stdio.h>
int chng(int b){
b=6;
return b;}f
int main()
{
//call by value
int a=5;
printf("the value of a is %d\n",a);
printf("the value be %d\n",chng(a));
//main function's value of 'a' didn't change
printf("the changed value is %d",a);
return 0;
}
13.Call By Reference
#include<stdio.h>
int chng(int*b){
//address is taken.
*b=6;
return *b;}
int main()
{
//call by reference
int a=5;
printf("the value of a is %d\n",a);
printf("the value be %d\n",chng(&a));
//function changes the initializing value
printf("the changed value is %d",a);
return 0;
}
above we changed
value by using function
14.2D Array With Loop
#include<stdio.h>
int main()
{ // put one number more than the initialization value count
int age[][5]={{1,2,3,4,5},
{2,3,4,5,6},
{6,7,8,9,10}};
for(int i=0;i<=2;i++){
for(int j=0;j<5;j++){
printf("the value of %d,%d is %d\n",i,j,age[i][j]);
}}
return 0;
}
15.Array Initializing Using loop
#include<stdio.h>
//array initialing using loop
int main()
{
int arr[13];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;
arr[5]=6;
arr[6]=7;
arr[7]=8;
arr[8]=9;
arr[9]=10;
arr[10]=11;
arr[11]=12;
arr[12]=13;
for(int i=0;i<13;i++)
{ printf("the value of element %d is %d\n",i,arr[i]);}
return 0;
}
16.Multiplication On C
#include<stdio.h>
int main()
{int a,b;
printf("enter the number=");
scanf("%d",&a);
for(;b<=10;b++){
printf("%d x %d=%d\n",a,b,a*b);
}
return 0;
}
Output:17.Factorial on C.
int fac(int fnum){
if(fnum==1){
return 1;
}
else if(fnum==0){
return 1;
}
else{
return fnum*fac(fnum-1);
}
}
int main()
{int number;
printf("enter the number=");
scanf("%d",&number);
printf("the factorial of %d is %d",number,fac(number));
return 0;
}
output:
