0% found this document useful (0 votes)
4 views81 pages

c Programs

The document provides a comprehensive overview of the C programming language, covering topics such as basic code structure, variables, data types, keywords, and input/output functions. It includes examples of arithmetic, relational, logical, and conditional operators, as well as control statements like if-else and switch-case, and looping constructs such as while, do-while, and for loops. Additionally, it discusses functions and arrays, both one-dimensional and two-dimensional, with practical code snippets for each topic.

Uploaded by

mahalaxmi0723
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views81 pages

c Programs

The document provides a comprehensive overview of the C programming language, covering topics such as basic code structure, variables, data types, keywords, and input/output functions. It includes examples of arithmetic, relational, logical, and conditional operators, as well as control statements like if-else and switch-case, and looping constructs such as while, do-while, and for loops. Additionally, it discusses functions and arrays, both one-dimensional and two-dimensional, with practical code snippets for each topic.

Uploaded by

mahalaxmi0723
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

C programming language

Day 1:
topic name : Basic code

1.
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
2.
#include <stdio.h>
int main()
{
printf("Hello C Programming Language\n");
return 0;
}

topic name : variable


1.
void main()
{
int x=10;
int y=20;
printf("x : %d",x);
printf("y : %d",y);

}
2.
void main()
{
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}

topic name : Data type


1.
int main()
{
int a= 10;
char ch= 'A';
float x= 25.896;
double z= 12.256222659;

return 0;
}

2.
int main()
{
int age = 25;
char grade = 'A';
float temp = 98.6;
double pi = 3.14159265359;

printf("Age : %d\n",age);
printf("Grade : %c\n",age);
printf("Temparature : %f\n",age);
printf("PI : %lf\n",age);

return 0;
}
topic name: keywords
int main()
{
const double pi = 3.14159265359;
printf("PI : %lf\n",age);

pi++;//error

return 0;
}
Day 2 :
topic name : printf() and scanf()

1.
#include<stdio.h>

int main()
{
int number;

printf("enter a number:");
scanf("%d",&number);

int cube = number*number*number;


printf("cube of number is : %d",cube);

return 0;
}
2.

#include<stdio.h>

int main()
{
int x=0,y=0,result=0;

printf("enter first number:");


scanf("%d",&x);

printf("enter second number:");


scanf("%d",&y);

result=x+y;
printf("sum of 2 numbers:%d ",result);

return 0;
}
topic name: Operartor

1. Arithmetic Operators

void main()
{
int a = 10;
int b = 20;

int add = a+b;


printf("Adddition : %d\n",add);

int sub = a-b;


printf("Sub : %d\n",sub);

int div = a/b;


printf("Div : %d\n",div);

int mult = a*b;


printf("Mult : %d\n",mult);
int mod = a%b;
printf("Mod : %d\n",mod);
}

2.Relational Operator

i)
#include<stdio.h>

void main()
{
int n1,n2;

printf("Enter first number : \n");


scanf("%d",&n1);

printf("Enter second number : \n");


scanf("%d",&n2);
if(n1 <= n2)
{
printf("Second number is greater or equal...");
}
else
{
printf("First number is greater or equal...");
}
}

ii)

#include<stdio.h>

void main()
{
int n1;
printf("Enter first value : \n");
scanf("%d",&n1);
int n2;
printf("Enter second value : \n");
scanf("%d",&n2);

if(n1 == n2)
{
printf("Numbers are equal...");
}
else
{
printf("Not equal...");
}
}
3. Increment and decrement

i)

#include<stdio.h>
main()
{
int a = 10;
printf("A = %d\n",a);

int b;
b = ++a;

printf("new A = %d\n",a);
printf("B = %d\n",b);

//int var = 7;
//printf("var value = %d\n",var);
//++var;
//printf("new value : %d\n",++var);
//printf("value of var : %d",var);

//var++;
//printf("new value : %d\n",var++);
//printf("incremented value of var = %d\n",var);

// var = var+1;
// printf("updated value of var = %d\n",var);

ii)
#include<stdio.h>

void main()
{
int a = 10;
printf("A = %d\n",a); // 10

int b;
b = a--;

printf("B = %d\n",b); // 9
printf("New A = %d",a); // 9

4.
Logical Operator

i)
/*
AND(&&)

((true) && (true)) = true


false true = false
true false = false
false false = false
*/

#include<stdio.h>

int main()
{

int a,b;

printf("Enter first number : \n");


scanf("%d",&a);

printf("Enter second number : \n");


scanf("%d",&b);

if((a > 10) && (b > 20))


{
printf("Both conditins are true...");
}
else
{
printf("conditions are false...");
}

return 0;

ii)

/*
OR(||)

true || true = true


true || false = true
false || true = true
false || false = false
*/

#include<stdio.h>

void main()
{
int x,y;

printf("Enter first value : \n");


scanf("%d",&x);

printf("Enter second value : \n");


scanf("%d",&y);

if((x == 5) || (y == 10))
{
printf("at least one conditions are true...");
}
else
{
printf("Both conditions are false...");
}
}

5. Conditional operator

#include<stdio.h>

void main()
{
int age;
printf("Enter your age : \n");
scanf("%d",&age);

(age>=18)?printf("You can vote..."):printf("you cannot


vote");

}
Day 3 :

topic name: Control Statement

A. Selection

1. if

void main()
{
int age = 15;
if(age >= 18)
{
printf("You can vote");
}
}
2. if else
i)
void main()
{
int age = 15;
if(age >= 18)
{
printf("You can vote");
}
else
{
printf("You cannot vote");
}
}

ii)

void main()
{
int a = 10;
int b = 20;
if(a > b)
{
printf("First number is greater the second");
}
else
{
printf("Second number is greater the First");
}
}

3. if else ladder
i)

#include<stdio.h>

int main()
{
int number=0;
printf("enter a number:");
scanf("%d",&number);

if(number==10)
{
printf("number is equals to 10");
}
else if(number==50)
{
printf("number is equal to 50");
}
else if(number==100)
{
printf("number is equal to 100");
}
else
{
printf("number is not equal to 10, 50 or 100");
}

return 0;
}

ii)

#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are fail ...");
}
}
2.Branching
i) Switch case

i)
#include<stdio.h>

void main()
{
char ch;
printf("Enter any character : \n");
scanf("%c",&ch);

switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("Vowel");
break;

default :
printf("Consonant");

ii)
#include<stdio.h>

void main()
{
int n;
printf("Enter any number: \n");
scanf("%d",&n);

int a,b;
printf("Enter two number :\n);
scanf("%d%d",&a,&b);

switch(n)
{
case 1:
printf("Addition : %d",a+b);
break;
case 2:
printf("Substraction : %d",a-b);
break;
case 3:
printf("Division : %d",a/b);
break;
case 5:
printf("multiplication : %d",a*b);
break;

default :
printf("wrong choice");

}
Day 4 :

3. Looping(Iteration)

i) While
1.
#include<stdio.h>

void main()
{
int t;
printf("Enter table number : \n");
scanf("%d",&t);

int i = 1;
while(i <= 10)
{
printf("%d\n",i*t);

i++;
}

/*int i = 5; // initialization
while(i >= 1) // condition checking
{
printf("Welcome\n");// task

i--; // increment
}*/

/*int i = 1; // initialization
while(i <= 10) // condition checking
{
printf("Welcome\n");// task

i++; // increment
}
*/
/*printf("Welcome\n");
printf("Welcome\n");
printf("Welcome\n");
printf("Welcome\n");
printf("Welcome\n");
printf("Welcome\n");
printf("Welcome\n");
printf("Welcome\n");
printf("Welcome\n");
printf("Welcome\n");
*/
}

2.

#include<stdio.h>

void main()
{
int i = 1; // initialization
while(i <= 10) // condition checking
{
printf("Welcome\n");// task

i++; // increment
}

ii. Do while
1)
#include<stdio.h>

void main()
{
int table;
printf("Enter the table number : \n");
scanf("%d",&table);

printf("Displaying tables :\n");

int k = 9;
do
{
int i = 1;
do
{
printf("%d\t",i*k);
i++;
} while(i <= 10);
printf("\n");

k++;
}while(k <= table);

}
2)
#include<stdio.h>

void main()
{

int i = 1;
do
{
printf("%d\t",i);
}
while(i <= 10);

}
Day 5 :

iii) For loop

1)
#include<stdio.h>

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

2)
#include<stdio.h>
void main()
{
int k;
for(k = 1; k <= 7; k++)
{
int i;
for(i = 1; i <= 10; i++)
{
printf("%d\t",i*k);
}
printf("\n");
}

}
Day 6 :

topic name: Function

1)

#include<stdio.h>
void printName();
void main ()
{
printf("Hello ");
printName();
}
void printName()
{
printf("Javatpoint");
}
2)
#include<stdio.h>
void sum();
void main()
{
printf("\nGoing to calculate the sum of two numbers:");
sum();
}
void sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
Day 7 :

3.
//without parameter without return type
#include<stdio.h>

void addition()
{
int a = 10, b = 20;
int c = a+b;
printf("Addition : %d\n",c);
}

int main()
{
addition();

return 0;
}
4.
//with paramter without return type
#include<stdio.h>

addition(int x, int y)
{
int c = x + y;
printf("Addition : %d\n",c);
}
void main()
{
int a = 10, b = 20;
addition(a,b);

int p = 100, q = 250;


addition(p,q);
}
5.
//without parameter with return type

#include<stdio.h>

int addition()
{
int a = 24, b = 30;
int c = a+b;

return c;
}

void main()
{
int res = addition();
printf("Addition : %d\n",res);
}

6.
//with paramter with return type
#include<stdio.h>

int addition(int n, int m)


{
int z = n + m;

return z;
}

void main()
{
int a = 45, b = 6;

int result = addition(a,b);

printf("Addition : %d\n",result);
}
Day 8 :
topic name: Arrays:

1. 1D array

i)

#include<stdio.h>

void main()
{
int age[5] = {21,45,67,24,24};

/*// initialization
age[0] = 15;
age[1] = 21;
age[2] = 17;
age[3] = 24;
age[4] = 21;
*/
printf("Displaying array values : \n");
int i;
for(i = 0; i < 5; i++)
{
printf("%d\t",age[i]);
}
}

ii)

#include<stdio.h>

void main()
{
int len;
printf("Enter the size of array : \n");
scanf("%d",&len);
int arr[len];
printf("Enter the value into array : \n");
int i;
for(i = 0; i < len; i++)
{
scanf("%d",&arr[i]);
}

printf("\nDisplaying array elements : \n");


int j;
for(j = 0; j < len; j++)
{
printf("%d\t",arr[j]);
}

iii)
#include<stdio.h>

int main()
{
int arr[5] = {10,30,20,40,80};

printf("Displaying array : \n");


int k;
for(k = 0; k < 5; k++)
{
printf("%d\t",arr[k]);
}

printf("\nDisplaying reverse array : \n");


int a;
for(a = 4; a >= 0; a--)
{
printf("%d\t",arr[a]);
}
return 0;
}
Day 9 :
2. 2D array

i)
#include<stdio.h>
int main()
{
int arr[3][3] = {{1,10,2} ,{20,56,4} ,{5,4,3}};
// {{00,01,02},{10,11,12},{20,21,22}}
int i,j;
// i=3 3<3
for(i = 0; i < 3; i++)
{
// j=3 3<3
for(j = 0; j < 3; j++)
{
printf("%d\t",arr[i][j]);//1 10 2
//20 56 4
//5 4 3
}
printf("\n");
}

return 0;
}

ii)
#include<stdio.h>
int main()
{
int arr[3][3];
printf("Enter the values into array : \n");

int i,j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d",&arr[i][j]);
}
}

printf("Displaying array elements : \n");


int a,b;
for(a = 0; a < 3; a++)
{
for(b = 0; b < 3; b++)
{
printf("%d\t",arr[a][b]);
}
printf("\n");
}

int sum = 0;
int x,y;
for(x = 0; x < 3; x++)
{
for(y = 0; y < 3; y++)
{
sum = sum + arr[x][y];
}
}

printf("\nAddition : %d\n",sum);

return 0;
}

iii)

/*
0 1 2

0 10 20 30
1 40 50 60
2 70 80 90
*/

#include<stdio.h>

void main()
{
int row,col;
printf("Enter the value of row and col : \n");
scanf("%d%d",&row,&col);

int arr[row][col];
printf("Enter the value into array : \n");

int a,b;
for(a = 0; a < row; a++)
{
for(b = 0; b < col; b++)
{
scanf("%d",&arr[a][b]);
}
}

printf("Displaying array values : \n");


int x,y;
for(x = 0; x < row; x++)
{
for(y = 0; y < col; y++)
{
printf("%d\t",arr[x][y]);
}
printf("\n");
}

/*
10 20 30
4 7 6
8 20 15
*/
printf("Addition of diagonal elements : \n"); //row = 3 col =
3
int sum = 0;//0
int i;//0 1 2 3
int j;//0 1 2 3 0 1 2 3 0 1 2 3
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
if(i + j == row-1)
{
sum = sum + arr[i][j];//32
}
}
}

printf("\nAddition : %d",sum);

}
Day 10 :
topic name: String:

1)
#include<stdio.h>
#include<string.h>

void main()
{
char ch[30];
printf("Enter your name : \n");
scanf("%[^\n]s",&ch);

printf("Upper case : %s",strlwr(ch));

/*
int i = 0;
while(ch[i] != NULL)
{
printf("%c",ch[i]-32);
i++;
}*/

2)
#include<stdio.h>
#include<string.h>

void main()
{
char str1[] = "virat";
char str2[] = "kohli";

int len1 = strlen(str1);


int len2 = strlen(str2);

int i = 0;
while(i < len2)
{
str1[len1] = str2[i];

i++;
len1++;
}

printf("%s",str1);

3)
#include<stdio.h>
#include<string.h>

void main()
{
char str[] = "virat";
char temp[20];

int len = strlen(str);


int i;
for(i = 0; i < len; i++)
{
temp[i] = str[i];
}

/*int i = 0;
while(str[i] != '\0')
{
temp[i] = str[i];
i++;
}
*/
printf("%s",temp);
}

4)
#include<stdio.h>
#include<string.h>
void main()
{
char ch[20] = {'h','e','l','l','o','\0'};
char temp[10] = {'j','a','v','a','\0'};

strcat(ch,temp);

printf("Value of first string is : %s",ch);

}
Day 11 :
5)
#include<stdio.h>
#include<string.h>

void main()
{
char str1[] = "gita";
char str2[] = "sita";

if(strcmp(str1,str2) == 0)
{
printf("Strings are equal...");
}
else
{
printf("Not equal...");
}

}
6)
#include<stdio.h>
#include<string.h>

void main()
{
char str[100];

printf("Enter any string : \n");


scanf("%[^\n]s",str);

printf("String : %s",str);

int cnt = 1,i = 0;

while(str[i] != '\0')
{
if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
{
cnt++;
}

i++;
}

printf("\nTotal number of words = %d",cnt);

7)

#include<stdio.h>
#include<string.h>

void main()
{
char ch[50] = {'v','i','r','a','t','\0'};
char ch2[50];
strcpy(ch2,ch);
printf("Values of second string : %s",ch2);

Day 12 :

topic name: Structure:


1)
#include<stdio.h>
#include<string.h>

struct student
{
int rNo; // 4
char name[20]; // 20 byte
float marks; // 4 byte ==> 18 byte
};

void main()
{
struct student s1 = {19,"ajay",69.5};

/*
s1.rNo = 21;
//s1.name = "ram";
strcpy(s1.name,"ram");

s1.marks = 75;
*/

printf("%d\t %s\t %f",s1.rNo,s1.name,s1.marks);

/*
printf("Roll No : %d\n",s1.rNo);
printf("Name = %s\n",s1.name);
printf("Marks : %f\n",s1.marks);
*/
}
2)

#include<stdio.h>
#include<string.h>

struct employee
{
int id;
char name[50];
float salary;
};

void main()
{
struct employee e1,e2;

e1.id = 101;
strcpy(e1.name,"sita");
e1.salary = 59000;
e2.id = 110;
strcpy(e2.name,"atul");
e2.salary = 45000;

printf("%d\t %s\t %f",e1.id,e1.name,e1.salary);


printf("\n%d\t %s\t %f",e2.id,e2.name,e2.salary);

3)
#include<stdio.h>
#include<string.h>

struct student
{
int rNo;
char name[50];
float marks;
}s1;

void main()
{
s1.rNo = 31;
strcpy(s1.name,"virat");
s1.marks = 57.48;

printf("Roll No : %d\n",s1.rNo);
printf("Name : %s\n",s1.name);
printf("Marks : %f\n",s1.marks);

4)
#include<stdio.h>
#include<string.h>

struct Car{
char brand[50];
char model[50];
int year;
};

void main()
{
struct Car c1 = {"BMW","X5",1999};

struct Car c2;


strcpy(c2.brand,"Ford");
strcpy(c2.model,"p5");
c2.year = 1990;

struct Car c3 = {"Toyota","pz",2005};

printf("Inforamation about cars : \n");


printf("\nBrand\t Model\t Year\n");
printf("\n%s\t %s\t %d",c1.brand,c1.model,c1.year);
printf("\n%s\t %s\t %d",c2.brand,c2.model,c2.year);
printf("\n%s\t %s\t %d",c3.brand,c3.model,c3.year);

5)
#include<stdio.h>
#include<string.h>

struct student{
int rollNo;
char name[50];
};

void main()
{
struct student st[5];

printf("Enter Record of 5 students : \n");


int i;
for(i = 0; i < 5; i++)
{
printf("Enter Roll No : \n");
scanf("%d",&st[i].rollNo);

printf("Enter the name : \n");


scanf("%s",&st[i].name);
}

printf("Student Information : \n");


for(i = 0; i < 5; i++)
{
printf("\n RollNo : %d\t Name :
%s",st[i].rollNo,st[i].name);
}
}

Day 13 :
topic name: union:

1)
#include<stdio.h>

union abc
{
int a;
char b;
}var;

void main()
{
var.a = 66;
var.b = 'z';

printf("\n a = %d",var.a);
printf("\n b = %c",var.b);

2)

#include<stdio.h>
#include<string.h>

union Student
{
int rNo;
char name[20];
float mks;
};

void main()
{
union Student st;
st.rNo = 11;
printf("Id : %d\t",st.rNo);

strcpy(st.name,"ram");
printf("Name : %s\t",st.name);

st.mks = 75;
printf("Mks : %f\n",st.mks);

}
topic name: sizeof()

1)
#include<stdio.h>
struct Student
{
int r;
char nm[20];
float mks;
};
void main()
{
printf("sizeof character : %d\n",sizeof(char));
printf("Size of integer : %d\n",sizeof(int));
printf("Sizeof float : %d\n",sizeof(float));
printf("Sizeof double : %d\n",sizeof(double));

struct Student st;


printf("Size of student structure : %d\n",sizeof(st));

int a = 10;
printf("size of a : %d\n",sizeof(a));
}
topic name: typedef:
1)
#include<stdio.h>

typedef struct Student


{
int rNo;
char name[20];
}stud;

void main()
{
stud st;

typedef unsigned long int ulint;

ulint a = 10;
printf("a : %d\n",a);
}
Day 14 :
topic name: pointer:

1)
#include<stdio.h>

void main()
{
int a = 10;
int *ptr = &a;

printf("Value of a : %d\n",a);
printf("address of a : %d\n",&a);
printf("value of ptr : %d\n",ptr);
printf("Address of ptr : %d\n",&ptr);
printf("value of a : %d\n",*ptr);

}
2)
#include<stdio.h>

void swap(int *a, int *b)


{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

int main()
{
int a = 10, b = 20;
printf("Before swapping :\n a = %d\tb = %d\n",a,b);

swap(&a,&b);
printf("After swapping :\n a = %d\tb = %d\n",a,b);
return 0;
}
topic name: File handling:

1)
#include<stdio.h>

int main()
{
FILE *fp;

fp = fopen("tejas.txt","a");

if(fp == NULL)
{
printf("File not created\n");
}
else{
printf("File open succesfully\n");
fprintf(fp,"virat kohli\n");

fclose(fp);
}

return 0;
}
Day 15 :
2)

#include<stdio.h>

int main()
{
FILE *fp;

char buff[200];

fp = fopen("tejas.txt","r");

if(fp == NULL)
{
printf("Error\n");
}
else
{
printf("File open succesfully\n");
while(fscanf(fp,"%s",buff) != EOF)
{
printf("%s ",buff);
}

fclose(fp);
}

return 0;
}

****************************************************
Thank you!!!!

You might also like