c Programs
c Programs
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;
}
}
2.
void main()
{
x=x+1;
y=y+1;
printf("%d,%d",x,y);
}
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);
return 0;
}
2.
#include<stdio.h>
int main()
{
int x=0,y=0,result=0;
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;
2.Relational Operator
i)
#include<stdio.h>
void main()
{
int n1,n2;
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(&&)
#include<stdio.h>
int main()
{
int a,b;
return 0;
ii)
/*
OR(||)
#include<stdio.h>
void main()
{
int x,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);
}
Day 3 :
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);
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 :
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 :
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);
#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>
return z;
}
void main()
{
int a = 45, b = 6;
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]);
}
iii)
#include<stdio.h>
int main()
{
int arr[5] = {10,30,20,40,80};
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]);
}
}
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]);
}
}
/*
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);
/*
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 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 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);
}
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("String : %s",str);
while(str[i] != '\0')
{
if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
{
cnt++;
}
i++;
}
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 :
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("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;
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};
5)
#include<stdio.h>
#include<string.h>
struct student{
int rollNo;
char name[50];
};
void main()
{
struct student st[5];
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));
int a = 10;
printf("size of a : %d\n",sizeof(a));
}
topic name: typedef:
1)
#include<stdio.h>
void main()
{
stud st;
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>
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!!!!