Codes For C&CPP Tutorials For Linux
Codes For C&CPP Tutorials For Linux
#include <stdio.h>
int main()
{
printf("Talk To a Teacher\n");
return 0;
}
#include <stdio.h>
int main()
{
int a = 2;
double const b = 4;
float c = 1.5;
char d = 'A';
printf("The value of a is %d\n",a);
printf("The value of b is %lf\n",b);
printf("The value of c is %.2f\n",c);
printf("The value of d is %c\n",d);
return 0;
}
#include <stdio.h>
int main()
{
int a = 2;
double const b = 4;
float c = 1.5;
char d = 'A';
printf("The value of a is %d\n",a);
printf("The value of b is %lf\n",b);
printf("The value of c is %.2f\n",c);
printf("The value of d is %c\n",d);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a = 2;
double const b = 4;
float c = 1.5;
char d = 'A';
cout<<"The value of a is "<<a<<"\n";
cout<<"The value of b is "<<b<<"\n";
cout<<"The value of c is "<<c<<"\n";
cout<<"The value of d is "<<d<<"\n";
return 0;
}
#include <stdio.h>
int add(int a, int b)
{
int c = a + b;
printf("Sum of a and b is %d\n",c);
}
int main()
{
int sum;
sum = add(5,4);
return 0;
#include <iostream>
using namespace std;
int add(int a, int b)
{
int c = a + b;
cout<<"Sum of a and b is "<<c<<"\n";
}
int main()
{
int sum;
sum = add(5,4);
return 0;
#include <stdio.h>
int a=5;
int b=2;
void add()
{
int sum;
sum = a + b;
printf("Sum of a and b is %d\n",sum);
}
int main()
{
add();
return 0;
}
#include <iostream>
using namespace std;
int a=5;
int b=2;
void add()
{
int sum;
sum = a + b;
cout <<"Sum of a and b is "<<sum<<"\n";
}
int main()
{
add();
return 0;
}
#include <stdio.h>
int main()
{
int a,b,sum;
printf("Enter the value of a and b \n");
scanf("%d%d",&a,&b);
sum = a + b;
printf("Sum of a and b is %d\n", sum);
if(sum > 50)
{
printf("Sum is greater than 50 \n");
}
/* else if(sum > 40)
{
printf("Sum is greater than 40\n");
}
else if(sum > 30)
{
printf("Sum is greater than 30\n");
}
#include <iostream>
using namespace std;
int main()
{
int a,b,sum;
cout <<"Enter the value of a and b \n";
cin >> a >>b;
sum = a + b;
cout <<"Sum of a and b is "<<sum<<"\n";
if(sum > 20)
{
cout <<"Sum is greater than 20 \n";
}
else if(sum > 10)
{
cout <<"Sum is greater than 10 and less
than 20\n";
}
else
{
cout <<"Sum is less than 10 \n";
}
return 0;
}
#include <stdio.h>
int main()
{
int x, y;
printf("Enter a number between 0 to 39: ");
scanf("%d",&y);
if(y/10==0)
{
printf("you have entered the number in the range
of 0 to 9\n");
}
else if(y/10==1)
{
printf("you have entered the number in the range
of 10 to 19\n");
}
else if (y/10==2)
{
printf("you have entered number in the range of
20-29\n");
}
else if (y/10==3)
{
printf("you have entered number in the range of
30-39\n");
}
else
{
printf("The number not in range \n");
}
return 0;
}
#include <stdio.h>
int main()
{
int x, y;
printf("enter a number between 0 to 39: ");
scanf("%d",&y);
x=y/10;
switch(x)
{
case 0:
printf("you have entered the number in the range
of 0 to 9\n");
break;
case 1:
printf("you have entered the number in the range
of 10 to 19\n");
break;
#include <iostream>
using namespace std;
int main()
{
int x, y;
cout<<"Enter a number between 0 to 39: ";
cin>>y;
if(y/10==0)
{
cout<<"you have entered the number in the
range of 0 to 9\n";
}
else if(y/10==1)
{
cout<<"you have entered the number in the
range of 10 to 19\n";
}
else if (y/10==2)
{
cout<<"you have entered number in the range of
20-29\n";
}
else if (y/10==3)
{
cout<<"you have entered number in the range of
30-39\n";
}
else
{
cout<<"number not in range \n";
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int x, y;
cout<<"Enter a number between 0 to 39: ";
cin>>y;
x=y/10;
switch(x)
{
case 0:
cout<<"you have entered the number in the
range of 0 to 9\n";
break;
case 1:
cout<<"you have entered the number in the
range of 10 to 19\n";
case 2:
printf("you have entered number in the range of
20-29\n");
break;
case 3:
printf("you have entered number in the range of
30-39\n");
break;
default:
printf("number not in range \n");
}
return 0;
}
break;
case 2:
cout<<"you have entered number in the range of
20-29\n";
break;
case 3:
cout<<"you have entered number in the range of
30-39\n";
break;
default:
cout<<"number not in range \n";
}
return 0;
}
// Arithmetic Operators in C
#include <stdio.h>
int main()
{
int a,b;
float c;
a = 5;
b = 2;
c - a + b;
printf("Sum of %d and %d is %.2f\n",a,b,c);
c = a * b;
printf("Product of %d and %d is
%.2f\n",a,b,c);
c = a / b;
printf("Integer Division of %d and %d is
%.2f\n",a,b,c);
c = (float)a / b;
printf("Real Division of %d and %d is
%.2f\n",a,b,c);
return 0;
}
c = a / b;
cout <<"Integer Division of " <<a <<" and "
<<b <<" is " <<c <<"\n";
c = (float)a / b;
cout <<"Real Division of " <<a <<" and " <<b
<<" is " <<c <<"\n";
return 0;
}
// Relational Operators in C
#include <stdio.h>
int main()
{
int a,b;
printf("Enter the values of a and b \n");
scanf("%d %d", &a,&b);
if(a > b)
printf("%d is greater than %d\n",a,b);
else if(a < b)
printf("%d is less than %d\n",a,b);
if(a <= b)
printf("%d is less than or equal to %d\n",a,b);
else if(a >= b)
printf("%d is greater than or equal to
%d\n",a,b);
if(a == b)
printf("%d is equal to %d\n",a,b);
else if (a != b)
printf("%d is not equal to %d\n",a,b);
return 0;
}
//Logical operators in C
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter the values of a,b, and c\n");
scanf("%d %d %d", &a, &b, &c);
if((a > b) && (a > c))
printf("a is greatest \n");
else if(b > c)
printf("b is greatest \n");
else
printf("c is greatest \n");
if((a == 0) || (b == 0) || (c == 0))
printf("The product of a, b and c is zero \n");
return 0;
return 0;
}
#include<stdio.h>
int main()
{
int x=0;
int y=0;
do
{
y=y+x;
printf("%d\n",y);
x++;
}
while(x<=10);
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int x=0;
int y=0;
do
{
y=y+x;
cout<<y<<"\n";
x++;
}
while(x<=10);
return 0;
}
#include<stdio.h>
int main()
{
int x=0;
int y=0;
while(x<=10)
{
y=y+x;
printf("%d\n",y);
x++;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int x=0;
int y=0;
while(x<=10)
{
y=y+x;
cout<<y<<"\n";
x++;
}
return 0;
}
#include<stdio.h>
int main()
{
int x=0;
int y=0;
do
{
y=y+x;
printf("%d\n",y);
x++;
}
while(x<=10);
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int x=0;
int y=0;
do
{
y=y+x;
cout<<y<<"\n";
x++;
}
while(x<=10);
return 0;
}
#include<stdio.h>
#include<iostream>
int main()
{
int x=0;
int y=0;
while(x<=10)
{
y=y+x;
printf("%d\n",y);
x++;
}
return 0;
}
#include <stdio.h>
int main()
{
int star[3]={4,5,6};
int sum;
sum = star[0] + star[1] + star[2];
printf("The sum is %d\n",sum);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int star[3]={4,5,6};
int sum;
sum = star[0] + star[1] + star[2];
cout<<"The sum is "<<sum<<"\n";
return 0;
}
//Two-dimensional arays in C
#include <stdio.h>
int main()
{
int i,j;
int num1[3][4],num2[3][4];
printf("Enter the elements of 3X4 array
num1\n");
for(i=0; i<3; i++)
for(j=0; j<4; j++)
scanf("%d", &num1[i][j]);
}
cout<<"The 3X4 array num2 is\n";
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
cout <<num2[i][j]<<"\t";
cout <<"\n";
}
cout<<"The sum of num1 and num2 is\n";
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
cout<<(num1[i][j] + num2[i][j])<<"\t";
cout<<"\n";
}
return 0;
}
}
#include<stdio.h>
#include<string.h>
int main()
{
char strname[30];
printf("Enter the string\n");
scanf("%[^\n]s", strname);
printf("The string is %s\n", strname);
return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string strname;
cout<<"Enter the string\n";
getline(cin, strname);
cout<<"The string is "<<strname<<"\n";
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char strname[30]="Spoken-Tutorial";
printf("The string is %s\n", strname);
return 0;
}
//String Compare
//String Copy
#include<stdio.h>
#include<string.h>
int main()
{
char str1[] = "Ice";
char str2[] = "Cream";
int i,j;
i = strcmp(str1,"Hello");
#include<stdio.h>
#include<string.h>
int main()
{
char source[] = "Ice";
char target[10];
strcpy(target, source);
printf("source string = %s\n",source);
j = strcmp(str2,"Cream");
printf("%d, %d\n",i,j);
return 0;
}
//String Length
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = "Ashwini";
int len1 = strlen(arr);
printf("string = %s length = %d\n",arr, len1);
return 0;
}
#include <stdio.h>
struct student
{
int eng;
int maths;
int science;
};
int main()
{
int total;
struct student stud;
stud.eng = 75;
stud.maths = 70;
stud.science = 65;
total = stud.eng + stud.maths +
stud.science;
printf("Total is %d\n",total);
return 0;
}
#include <iostream>
using namespace std;
struct student
{
int eng;
int maths;
int science;
};
int main()
{
int total;
struct student stud;
stud.eng = 75;
stud.maths = 70;
stud.science = 65;
total = stud.eng + stud.maths +
stud.science;
cout<<"Total is "<<total<<"\n";
return 0;
}
#include <stdio.h>
void main()
{
long int num = 10;
long int *ptr;
printf("num's address: %p\n", &num);
ptr = #
#include <iostream>
using namespace std;
int main()
{
long int num = 10;
long int *ptr;
cout<<"num's address :"<< &num<<"\n";
ptr = #
sizeof(ptr)<<"\n";
printf("pointer's value: %p\n", ptr);
cout<<"pointer's value: "<< ptr<<"\n";
printf("value pointed to: %ld\n", *ptr);
}
#include<stdio.h>
int swap(int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
#include <iostream>
using namespace std;
int main()
{
int i, j;
printf("Enter the values ");
scanf("%d%d",&i,&j);
swap(a, b);
cout << "After swapping a and b : " << a <<"
and " <<b <<"\n";
return 0;
}
#include<stdio.h>
int cube(int x)
{
x=x*x*x;
return(x);
}
int main()
{
int n=8;
printf("Cube of %d is %d\n",n,cube(n));
return 0;
}
#include <stdio.h>
int main()
#include <stdio.h>
int main()
{
{
FILE *fp;
FILE *fp;
fp = fopen("/home/ttt/Desktop/sample.txt","w"); char c;
fprintf(fp,"Welcome to the spoken-tutorial
fp =
\n");
fopen("/home/ttt/Desktop/sample.txt","r");
fprintf(fp,"This is an test example\n");
if (fp == NULL)
fclose(fp);
printf("File doesn't exist\n");
return 0;
else
}
{
while (c != EOF)
{
c = getc(fp);
putchar(c);
}
fclose(fp);
}
return 0;
}