You are on page 1of 20

Day 3-session 1

Print hello world using function:

#include<stdio.h>
void helloworld(); ​//function declaration

Int main()
{
helloworld(); ​//function call
Return 0;
}

helloworld() ​ //function prototype


{
printf(“hello world!”);
}

Doubling a number:

#include<stdio.h>
int doubling(int); ​//function declaration

int main()
{
int a,b;
printf(“Enter the number to be doubled”);
scanf(“%d”,&a);
b=doubling(a); ​//function call
return 0;
}

int doubling(x) ​//function prototype


{
return(2*x);
}

Square of a number:

#include<stdio.h>
int square(int); ​//function declaration
int main()
{
int a,b;
printf(“Enter the number to be squared”);
scanf(“%d”,&a);
b=square(a); ​//function call
return 0;
}

Int square(x) ​//function prototype


{
return(x*x);
}

Factors of a number:

#include <stdio.h>
Void factors(int);
int main()
{
int number;

printf("Enter a positive integer: ");


scanf("%d",&number);

printf("Factors of %d are: ", number);


factors(number);
return 0;
}
Void factors(x)
{
int i;
for(i=1; i <= x; ++i)
{
if (x%i == 0)
{
printf("%d ",i);
}
}
}
Session 2

Print N integers in reverse order:

#include <stdio.h>

int main() {
int array[10];
int loop,i;
printf(“enter the number of integers”);
scanf(“%d”,&loop); ​//get the number of elements
printf(“Enter number the %d numbers”, loop);

for(i=0;i<loop;i++)
scanf("%d ", array[i]); ​//get the elements in order

Printf(“the numbers in reverse order :”)


for(i=loop;i>=loop;i--) ​//condition to print in reverse order
printf(“%d “,array[i]);

return 0;
}

Print adjacent elements:

#include <stdio.h>

int main() {
int array[10];
int loop,i,n;
printf(“enter the number of integers”);
scanf(“%d”,&loop); ​//get the number of elements
printf(“Enter number the %d numbers”, loop);

for(i=0;i<loop;i++)
scanf("%d ", array[i]); ​//get the elements in order
Printf(“enter the index”);
scanf(“%d”,&n);
printf(“the adjacent numbers are %d and %d”,
array[n-1],array[n+1]);
​//printing adjacent numbers without performing check
Return 0;
}

Decimal to binary conversion:

#include <stdio.h>

void main()
{
long num, decimal_num, remainder, base = 1, binary = 0, no_of_1s
= 0;

printf("Enter a decimal integer \n");


scanf("%ld", &num);
decimal_num = num;
while (num > 0)
{
remainder = num % 2;
​// To count no.of 1s
if (remainder == 1)
{
no_of_1s++;
}
binary = binary + remainder * base;
num = num / 2;
base = base * 10;
}
printf("Input number is = %d\n", decimal_num);
printf("Its binary equivalent is = %ld\n", binary);
printf("No.of 1's in the binary number is = %d\n", no_of_1s);
}

Printing alternative elements of an array from first and last:

#include <stdio.h>

void main()

int array[10];
int i;

printf("enter the element of an array \n");

for (i = 0; i < 10; i++)

scanf("%d", &array[i]); ​//accepting the elements

printf("Alternate elements of a given array \n");

for (i = 0; i < 10; i += 2) ​//incrementing in 2’s for


alternative printing

printf( "%d\n", array[i]) ;

for(i=10; i > 0; i+=2) ​//incrementing in 2’s for alternative


printing from last

printf(“%d\n”,array[i]);

Printing the next smaller integer:

void printNSE(int arr[], int n) ​//function prototype

int next, i, j;

for (i=0; i<n; i++)

next = -1;

for (j = i+1; j<n; j++)

{
if (arr[i] > arr[j]) ​//condition for smallest
integer

next = arr[j];

break;

printf("%d -- %d\n", arr[i], next);

int main()

int arr[]= {11, 13, 21, 3};

int n ;

printNSE(arr, n); ​//function call for next smallest


integer

return 0;

Session 3

Sum of rows and columns of a matrix:

#include <stdio.h>

void main()
{
int i,j,k,arr1[10][10],rsum[10],csum[10],n;

printf("\nFind the sum of rows an columns of a


Matrix:\n");

printf("Input the size of the square matrix : ");


scanf("%d", &n);
printf("Input elements in the first matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("The matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n ;j++)
printf("% 4d",arr1[i][j]);
printf("\n");
}

​// Sum of rows


for(i=0;i<n;i++)
{
rsum[i]=0;
for(j=0;j<n;j++)
rsum[i]=rsum[i]+arr1[i][j];
}

​// Sum of Column


for(i=0;i<n;i++)
{
csum[i]=0;
for(j=0;j<n;j++)
csum[i]=csum[i]+arr1[j][i];
}

printf("The sum or rows and columns of the matrix is


:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("% 4d",arr1[i][j]);
printf("% 8d",rsum[i]);
printf("\n");
}
printf("\n");
for(j=0;j<n;j++)
{
printf("% 4d",csum[j]);
}
printf("\n\n");
}

Transpose of a matrix:

#include <stdio.h>

int main()

int m, n, c, d, matrix[10][10], transpose[10][10];

printf("Enter the number of rows and columns of matrix​\n​");

scanf("%d%d", &m, &n);

printf("Enter elements of the matrix​\n​");

for (c = 0; c < m; c++)

for(d = 0; d < n; d++)

scanf("%d", &matrix[c][d]);

for (c = 0; c < m; c++) ​//condition for transposing a


matrix

for( d = 0 ; d < n ; d++ )

transpose[d][c] = matrix[c][d];
printf("Transpose of the matrix:​\n​");

for (c = 0; c < n; c++) {

for (d = 0; d < m; d++)

printf("%d​\t​", transpose[c][d]);

printf("​\n​");

return 0;

Print the border elements of a matrix:

#include <stdio.h>

int main()

int m, n, c, d, matrix[10][10];

printf("Enter the number of rows and columns of matrix​\n​");

scanf("%d%d", &m, &n);

printf("Enter elements of the matrix​\n​");

for (c = 0; c < m; c++)

for(d = 0; d < n; d++)


scanf("%d", &matrix[c][d]); ​//accepting the matrix

printf(“Printing the border the elements\n”);

printf(“left and right border respectively\n”);

for(d=0;d<n;d++) ​//condition for printing the respective


border

printf(“%d”, matrix[0][d];

printf(“%d \n”, matrix[m][d];

printf(“the upper and lower border elements:\n”);

for(c=0;c<m;c++) ​//condition for printing the respective


border

printf(“%d”, matrix[c][0];

printf(“%d\n”, matrix[c][n];

Check if two matrices are identical:

#include<stdio.h>

int areSame(int A[][N], int B[][N])

int i, j;

for (i = 0; i < N; i++)

for (j = 0; j < N; j++)

if (A[i][j] != B[i][j]) ​//condition for identical


matrix
return 0;

return 1;

int main()

int A[N][N] = { {1, 1, 1, 1},

{2, 2, 2, 2},

{3, 3, 3, 3},

{4, 4, 4, 4}};

int B[N][N] = { {1, 1, 1, 1},

{2, 2, 2, 2},

{3, 3, 3, 3},

{4, 4, 4, 4}};

if (areSame(A, B))

printf("Matrices are identical");

else

printf("Matrices are not identical");

return 0;

}
Session-4

Print string length without built in functions:

#include <stdio.h>
int main()
{
char s[1000];
int i;

printf("Enter a string: ");


scanf("%s", s);

for(i = 0; s[i] != '\0'; ++i); ​//checking the length

printf("Length of string: %d", i);


return 0;
}

Print the word count in a string:


#include <stdio.h>

#include <string.h>

void main()

char s[200];

int count = 0, i;

printf("enter the string\n");

scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)

if (s[i] == ' ')

count++;

printf("number of words in given string are: %d\n", count +


1);

Print the count of vowels in a string:

#include <stdio.h>

int main()

int c = 0, count = 0;

char s[1000];

printf("Input a string​\n​");

gets(s);

while (s[c] != '​\0​') {

if (s[c] == 'a' || s[c] == 'A' || s[c] == 'e' || s[c] ==


'E' || s[c] == 'i' || s[c] == 'I' || s[c] =='o' || s[c]=='O' ||
s[c] == 'u' || s[c] == 'U') ​//Check for vowel

count++;

c++;
}

printf("Number of vowels in the string: %d", count);

return 0;

Print the common parts of 2 strings:

#include <stdio.h>

#include <conio.h>

#include<string.h>

void main()

char a[80],b[80];

int i,j,t;

clrscr();

printf("Enter 1st string\n");

gets(a);

fflush(stdin);

printf("Enter 2nd string\n");

gets(b);
printf("Common characters are :\n");

for(i=0;a[i]!='\0';i++)

for(j=i-1;j>=0;j--)

if(a[i]==a[j])

break;

if(j==-1)

for(t=0;b[t]!='\0';t++)

if(a[i]==b[t]) ​//condition check for same character

printf("%c",a[i]);

break;

getch();

Concatenate two strings without using strcat:

#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;
printf("Enter first string: ");
scanf("%s", s1);

printf("Enter second string: ");


scanf("%s", s2);

​// calculate the length of string s1


​// and store it in i
for(i = 0; s1[i] != '\0'; ++i)
}
for(j = 0; s2[j] != '\0'; ++j, ++i)
{
s1[i] = s2[j];
}}

s1[i] = '\0';
printf("After concatenation: %s", s1);

return 0;
}

Print the given strings in ascending order:

#include<stdio.h>
#include<conio.h>

int main()
{
char str[100],temp;
int i,j;
clrscr();
printf("Enter the string :");
gets(str);
printf("%s in ascending order is -> ",str);
for(i=0;str[i];i++)
{
for(j=i+1;str[j];j++)
{
if(str[j]<str[i])
{
temp=str[j]; ​//simple sorting algorithm
str[j]=str[i];
str[i]=temp;
}
}
}
printf("%s\n",str);
getch();
return 0;
}

Print string with maximum number of vowels:


#include<stdio.h>
#include<conio.h>

int main()
{
char s1[100],s2[100],temp;
int c=0;
clrscr();
printf("Enter the string 1:");
gets(s1);
printf(“enter the string 2”)
gets(s2);

while (s1[c] != '​\0​') {

if (s1[c] == 'a' || s1[c] == 'A' || s1[c] == 'e' || s1[c]


== 'E' || s1[c] == 'i' || s1[c] == 'I' || s1[c] =='o' ||
s1[c]=='O' || s1[c] == 'u' || s1[c] == 'U') ​//Check for vowel

count1++;

c++;

c=0;

while (s2[c] != '​\0​') {

if (s2[c] == 'a' || s2[c] == 'A' || s2[c] == 'e' || s2[c]


== 'E' || s2[c] == 'i' || s2[c] == 'I' || s2[c] =='o' ||
s2[c]=='O' || s2[c] == 'u' || s2[c] == 'U') ​//Check for vowel

count2++;

c++;
}

if(count1>count2)

printf(“string 1”);

Else

printf(“string 2”);

Return 0;

Printing the common alphabets of Two strings:

Print common characters in two strings:

#include<stdio.h>
#include<stdlib.h>
#define MAX 256

int *CountArray(char *str)


{
int *count=(int *)calloc(sizeof(int),MAX);
int i; ​// Count frequency of each character in first string
for(i=0;*(str+i);i++)
{
count[*(str+i)]++;
}
return count;
}

int CountCommonChar(char *str1, char *str2)


{
int *count=CountArray(str1);
int i,count_char=0;
for(i=0;*(str2+i);i++)
{
if(count[*(str2+i)]!=0)
{
count_char++;
count[*(str2+i)]--;
}
}

return count_char;
}
int main()
{
char str1[100],str2[100];

printf("Enter first string: ");


scanf("%s",str1);
printf("Enter second string: ");
scanf("%s",str2);

int n=CountCommonChar(str1,str2);
printf("Common char = %d",n);

return 0;
}

Capitalize each word in a string:

#include <stdio.h>

#include <string.h>

int main()

char string[1000];

printf("Input a string to convert to upper case​\n​");

gets(string);
printf("The string in upper case: %s​\n​", strupr(string));

return 0;

You might also like