Você está na página 1de 4

//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]);

printf("Enter the elements of 3X4
array num2\n");
for(i=0; i<3; i++)
for(j=0; j<4; j++)
scanf("%d", &num2[i][j]);

printf("The 3X4 array num1 is\n");
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
printf("%3d ", num1[i][j]);
printf("\n");
}
printf("The 3X4 array num2 is\n");
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
printf("%5d ", num2[i][j]);
printf("\n");
}
printf("The sum of num1 and num2
is\n");
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
printf("%3d ", (num1[i][j] +
num2[i][j]));
printf("\n");
}
return 0;
}

//array.c
#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;
}

// call by reference[callbyref.c]
#include<stdio.h>
int swap(int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}

int main()
{
int i, j;

printf("Enter the values ");
scanf("%d%d",&i,&j);

printf("Before swapping %d and
%d\n",i,j);

swap(&i,&j);

printf("After swapping %d and
%d\n",i,j);

return 0;
}

//call by value[callbyval.c]
#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;
}

//de - while
#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;
}


//file.c
#include <stdio.h>
int main()
{
FILE *fp;
fp =
fopen("/home/ttt/Desktop/sample.txt",
"w");
fprintf(fp,"Welcome to the spoken-
tutorial \n");
fprintf(fp,"This is an test example\n");
fclose(fp);
return 0;
}


//function.c
#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;
}

//ifstatement.c

#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");
}

else if(sum > 20)
{
printf("Sum is greater than 20\n");
}
else
{
printf("Sum is less than 10 \n");
}*/
return 0;
}

//Increment and Decrement Operators
in C
#include <stdio.h>
int main()
{
int a=1;
printf("a's value is now %d\n", a++);
printf("a's value is now %d\n", a);
a=1;
printf("a's value is now %d\n", ++a);
printf("a's value is now %d\n", a);
a=1;
printf("a's value is now %d\n", a--);
printf("a's value is now %d\n", a);
a=1;
printf("a's value is now %d\n", --a);
printf("a's value is now %d\n", a);
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;
}

//nestedif
#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;
}

//pointerdemo.c

#include <stdio.h>
void main()
{
long int num = 10;
long int *ptr;
printf("num's address: %p\n", &num);
ptr = &num;

printf("pointer's address: %p\n",
&ptr);

printf("pointer's size: %ld bytes\n",
sizeof(ptr));

printf("pointer's value: %p\n", ptr);

printf("value pointed to: %ld\n", *ptr);
}


// 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;
}

//scope.c

#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;
}

//String Compare

#include<stdio.h>
#include<string.h>
int main()
{
char str1[] = "Ice";
char str2[] = "Cream";
int i,j;
i = strcmp(str1,"Hello");
j = strcmp(str2,"Cream");
printf("%d, %d\n",i,j);

return 0;
}

//String Copy

#include<stdio.h>
#include<string.h>
int main()
{
char source[] = "Ice";
char target[10];
strcpy(target, source);
printf("source string = %s\n",source);
printf("target string = %s\n",target);
return 0;
}

//string.c

#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;
}

//stringinitialize.c
#include<stdio.h>
#include<string.h>
int main()
{
char strname[30]="Spoken-Tutorial";
printf("The string is %s\n", strname);
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;
}

//structure.c

#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;
}

//switch.c

#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;
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;
}

//tokens.c
#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;
}

//while.c
#include<stdio.h>
int main()
{
int x=0;
int y=0;
while(x<=10)
{
y=y+x;
printf("%d\n",y);
x++;
}
return 0;
}


Assignments
1) Write a program to calculate the
difference of the elements stored in an
array.
2) Write a program to create a file TEST
Write your name and address in the file
TEST Then display it on the console
using C program.
3) Write a similar program to calculate
the cube of a number. Using call by
value in C
4) Write a program to calculate the
square of a number.
5) Write a program to check 'a' is
greater than 'b' or less than 'b'
Hint: use if statement.
6) Write another program to check
which value is greater 'a', 'b' or 'c'.
Hint: use else-if statement.
(Take values of a, b, c as user inputs)
7) Write a program to solve the
following expression:
(a/b) + (c/d)
The values of a, b, c and d are taken as
input from the user.
Use typecasting to perform real division.
8) Write a program that takes two
numbers as input from the user.
Check whether the two numbers are
equal or not using NOT operator.
Hint: (a!=b)
9) Write a program to print the
following using for loop.
0 1 2 3 4 5 6 7 8 9
Hint: Syntax
for(var initialization; condition; var
incre-decr)
{
body
}
10) Write a program to check whether
the age of the employee is between 20-
60.
11) Write a program that takes marks of
three students as input.
Compare the marks to see which
student has scored the highest.
Check also if two or more students have
scored equal marks.
12) Write a program to print the
difference of two numbers.
13) Write a C progr5am to concatenate
String 'best' and String 'bus'.
Hint: strcat(char str1, char str2);
14) Explore the other functions in string
library.
15) Write a program to print a string
using the 2nd syantax.
char var_name[] = {'S','t','r','i','n','g'};
16) Write a C program to calculate the
simple interest.
Hint: principal*rate*time/100
17) Write a C and C++ program, declare
a variable and a pointer. Store the
address of the variable in the pointer.
Print the value of the pointer.
18) Write a program that takes two 2D
arrays as input from the user. Subtract
them and find the result.
19) Write a program to display records
of an employee. Like name, address,
designation, salary.

Você também pode gostar