You are on page 1of 13

1. 1, 2, 3, 4, ………….100.

#include
int main()
{
int i;
for (i=1;i<=100;i++)
{
printf("%d\n",i);
}
return 0;
}
2.1, 3, 5, 7, ……………..99.
3.2, 4, 6, 8, ………………100.
4.5, 10, 15, ………………….50.
#include
int main()
{
int i,s;
for (i=1;i<=10;i++)
{
s=5*i;
printf("%d\n",s);
}
return 0;
}
5.WAP to calculate sum of n-natural number.
#include
int main()
{
int n,i,s=0;
printf("Enter any number\t");
scanf("%ld",&n);
for (i=1;i<=n;i++)
{
s=s+i;
}
printf("Sum is %d\n",s);
return 0;
}
6.WAP to calculate product of n-natural number. (Factorial of a given
number.
#include
int main()
{
int n,i,p=1;
printf("Enter any number\t");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
p=p*i;
}
printf(“Product is %d\n”,p);
return 0;
}
7.WAP to generate fibonacci series. [ 0,1,1,2,3,5,8 …..10th term]
#include
int main()
{
int i,a=0,b=1,c;
printf(“%d%d””,a,b);
for (i=1;i<=10;i++)
{
c=a+b;
printf(“%d”,c);
a=b;
b=c;
}
return 0;
}
8.WAP to generate 1 2 4 7 11 ...............10th term
#include
int main()
{
int i,a=1,g=1;
for (i=1;i<=10;i++)
{
printf(“%d”,a);
a=a+g;
g=g+1;
}
return 0;
}
9.WAP to generate 1 2 5 10 17 ...............10th term
#include
int main()
{
int i,a=1,g=1;
for (i=1;i<=10;i++)
{
printf(“%d”,a);
a=a+g;
g=g+2;
}
return 0;
}
10.WAP to generate 3 12 27 48 ................10th term
#include
int main()
{
int a,i;
for (i=1;i<=10;i++)
{
a=3*I*I;
printf(“%d”,a);
}
return 0;

WHILE Loop (Entry Control loop)


Syntax:
while(condtion)
{
Block of statements;
}
11.WAP to calculate sum of n-natural number.
#include
int main()
{
int n,s=0,i=1;
printf("Enter any number\t");
scanf("%d",&n);
while (i<=n)
{
s = s+i;
i=i+1;
}
printf("Sum of digits is %ld\n",s);
return 0;
}
12.WAP to calculate product of digits of a given number.
#include
int main()
{
int n,p=1,i=1;
printf("Enter any number\t");
scanf("%d",&n);
while (i<=n)
{
p = p*i;
i=i+1;
}
printf(“Product of digits is %d\n”,p);
return 0;
}
13.WAP to generate 1 2 4 7 11 ...............10th term
#include
int main()
{
int i=1,a=1,g=1;
while (i<=10)
{
printf(“%d”,a);
a=a+g;
g=g+1;
i=i+1;
}
return 0;
}
14.WAP to calculate sum of digits of a given number.
#include
int main()
{
int n,s=0,r;
printf("Enter any number\t");
scanf("%d",&n);
while (n!=0)
{
r = n%10;
s = s+r;
n = n/10;
}
printf("Sum of digits is %d\n",s);
return 0;
}

15.WAP to calculate product of digits of a given number.


#include
int main()
{
int n,p=1,r;
printf("Enter any number\t");
scanf("%d",&n);
while (n!=0)
{
r = n%10;
p = p*r;
n = n/10;
}
printf(“Product of digits is %d\n”,p);
return 0;
}
16.WAP to reverse a given number.
#include
int main()
{
int n,p=0,r;
printf("Enter any number\t");
scanf("%d",&n);
while (n!=0)
{
r = n%10;
p = p*10+r;
n = n/10;
}
printf(“Reverse is %d\n”,p);
return 0;
}
17.WAP to check whether given number is palindrome or not.
#include
int main()
{
int n,p=0,r,z;
printf("Enter any number\t");
scanf("%d",&n);
z=n;
while (n!=0)
{
r = n%10;
p = p*10+r;
n = n/10;
}
if (z==p)
{
printf(“%d is palindrome \n”,z);
}
else
{
printf(“%d is not palindrome \n”,z);
}
return 0;
}

18.WAP to check whether given number is armstrong or not. [153 is Armstrong]


#include
int main()
{
int n,s=0,r,z;
printf("Enter any number\t");
scanf("%d",&n);
z=n;
while (n!=0)
{
r = n%10;
s = s+pow(r,3);
n = n/10;
}
if (z==s)
{
printf(“%d is armstrong \n”,z);
}
else
{
printf(“%d is not armstrong \n”,z);
}
return 0;
}

DO Loop

Syntax:
do
{
Block of statements;
}while(condition);
19.WAP to calculate sum of n-natural number.
#include
int main()
{
int n,s=0,i=1;
printf("Enter any number\t");
scanf("%d",&n);
do
{
s = s+i;
i=i+1;
}while (i<=n);
printf("Sum of digits is %ld\n",s);
return 0;
}
Array
An array is a collection of variables of same type that are represented by a
common name i.e. array is the collection of homogenous data type (Contains
similar datas). If we declare array as int then it must only contain all
integer data and same for floats and char. Each array element is specified
by array name followed by index or subscript enclosed in square bracket (eg,
a[10]). Hence, it is also called indexed or subscripted variables. In an
array index of array represent the size of an array.
Let us consider, int age[5]
This mean array name is age which is capable of storing 5 different data
under integer datatype.
age[0] = 15
age[1] = 25
age[2] = 13
age[3] = 22
age[4] = 17
Types of array
Depending upon the number of subscript or index number array are of 2 types:
1. 1-Dimension
2. N-Dimension (Multi-dimension)
1-Dimension:
This type of array has only one subscript.
Declaration:
Data_type array_name[size]
Example:
int salary[100]
Program example.
Q1. WAP to input 10 different number and print them all.
#include
int main()
{
int a[10], i;
printf ("Enter any 10 number");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
printf(“%d”,&a[i]);
}
return 0;
}
Q2. WAP to input salary of 1000 employees and calculate average salary.
#include
int main()
{
int sal[1000], i, s=0;
float av;
printf ("Enter any 10 number");
for(i=0;i<1000;i++)
{
scanf(“%d”,&sal[i]);
}
for(i=0;i<1000;i++)
{
s=s+sal[i];
}
av = s/1000;
printf(“Average salary of 1000 employee is %f”, av);
return 0;
}
Q3. WAP to input salary of 1000 employees and count total number of employee
getting salary between 30000 and 50000.
#include
int main()
{
int sal[1000], i, c=0;
for(i=0;i<1000;i++)
{
scanf(“%d”,&sal[i]);
}
for(i=0;i<1000;i++)
{
if(sal[i]>=30000 && sal[i]>=50000)
{
c=c+1;
}
}
printf(“Total number of employee is %d”, c);
return 0;
}
Q4. WAP to input 10 different number and find the greatest among them.
#include
int main()
{
int a[10], i, g;
printf ("Enter any 10 number");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
g = a[0];
for (i=0;i<10;i++)
{
if (g>a[i])
{
g=a[i];
}
}
printf("Greatest number is %d\n",g);
return 0;
}
Q5. WAP to input 10 different number and sort the in ascending order.
#include
int main()
{
int a[10], i, j, temp;
printf ("Enter any 10 number");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
If (a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf(“Ascending order is”);
for (i=0;i<10;i++)
{
Printf(“%d \t”,a[i]);
}
return 0;
}
Q6. WAP to input salary of n-employees and count total number of employee
getting salary between 30000 and 50000.
#include
int main()
{
int sal[1000], i, c=0, n;
printf("Enter the number of employees");
scanf("%d",&n);
for(i=0;i=30000 && sal[i]>=50000)
{
c=c+1;
}
}
printf(“Total number of employee is %d”, c);
return 0;
}
N-Dimension (Multidimension array)
This type of array has more than one subscript.
Declaration:
Data_type array_name[size][size]
Example:
int n[3][3]
Program example
Q1. WAP to input 3x3 matrix data and display the data of it.
#include
int main()
{ int a[3][3],i,j;
printf("Enter 9 number");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}
Q7. WAP to input 3x3 matrix and perform matrix addition
#include
int main()
{ int a[3][3], b[3][3], c[3][3],i ,j;
printf("Enter 9 number");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf(“%d%d”,&a[i][j],&b[i][j]);
}
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
printf("\n");
}
printf(“Added matrix is”);
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf(“%d \t”,c[i][j]);
}
printf(“\n”);
}
return 0;
}
Q8. WAP to input 3x3 matrix and perform matrix multiplication
#include
int main()
{ int a[3][3], b[3][3], p[3][3],i ,j, k;
printf("Enter 9 number");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf(“%d%d”,&a[i][j],b[I][j]);
}
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
p[i][j] = 0;
for(k=0;k<3;k++)
{
p[i][j] = p[i][j] + a[i][k]*b[k][j];
}
}
}
printf(“Multiplied matrix is”);
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf(“%d \t”,p[i][j]);
}
printf(“\n”);
}
return 0;
}
String: String is a group of characters that can be represented only by
character array. To represent a character/string we use ‘char’ datatype.
i.e. char a, char a=‘x’
Where, char is data type and a is string variable and ‘x’ is a character.
Similarly, we can initialize string as one-dimension character array as:
Char a[5] = “hello” which is stored in array as follow
a[0] a[1] a[2] a[3] a[4] a[5]

‘h’ ‘e’ ‘l’ ‘l’ ‘o’ \0


we can initialize array of string as two-dimension array as:
Char a[2][5] = {“ball” , “cat” , “apple”}, which is stored in array as
follow
a[2][5] a[0] a[1] a[2] a[3] a[4] a[5]

a[0] ‘b’ ‘a’ ‘l’ ‘l’ \0


a[1] ‘c’ ‘a’ ’t’ \0

a[2] ‘a’ ‘p’ ‘p’ ‘l’ ‘e’ \0


String handling function [Library function / Inbuilt function] V.imp
As we know, functions are the self contained program that performs certain
task. Similarly the function which are given by C-library itself or inbuilt
in C-library are known as library or inbuilt-function. String handling
function or string function are the library functions which are used to
manipulate strings in our program for simplicity and convenience. This
inbuilt functions are defined under the header file ’string.h’ . In C we
have several string handling functions which are as follows:
[Note: don’t miss ’string.h’ header file]
1) strlen( )
Function: This function returns total number of characters in a given
string.
Syntax: strlen(string);
Program example:
#include
#include
int main()
{
char a[10];
int l;
printf("Enter any string");
scanf("%s",a);
l=strlen(a);
printf("The length is %d",l);
return 0;
}
2) strrev( )
Function: This function returns reverse of given string i.e. if we enter
‘ball’ it will return ‘llab’
Syntax: strrev(string);
Program example:
#include
#include
int main()
{
char a[10];
printf("Enter any string");
scanf("%s",a);
strrev(a);
printf("The reverse is %s”,a);
return 0;
}
3) strlwr( )
Function: This function returns given string in lower case i.e. small
letters.
Syntax: strlwr(string);
Program example:
#include
#include
int main()
{
char a[10];
printf("Enter any string");
scanf("%s",a);
strlwr(a);
printf("The lower case is %s”,a);
return 0;
}
4) strupr( )
Function: This function returns given string in upper case i.e. capital
letters.
Syntax: strupr(string);
Program example:
#include
#include
int main()
{
char a[10];
printf("Enter any string");
scanf("%s",a);
strupr(a);
printf("The upper case is %s”,a);
return 0;
}
5) strcpy( )
Function: This function copies one string to another i.e. source_string to
destination_string
Syntax: strcpy(destination_string,source_string);
Program example:
#include
#include
int main()
{
char a[10],b[10];
printf("Enter any string");
scanf("%s",a);
strcpy(b,a);
printf("The copied string is %s”,b);
return 0;
}
6) strcat( )
Function: This function adds two strings into one.The process of adding
strings is known as string concatenation.
Syntax: strcat(string1,string2);
Program example:
#include
#include
int main()
{
char a[10],b[10];
printf("Enter first string");
scanf("%s",a);
printf("Enter second string");
scanf(“%s”,b);
strcat(a,b);
printf("The added string is %s”,a);
return 0;
}
7) strcmp( )
Function: This function compare two strings and return 0 if both string are
equal else return -1/1 if not equal.
Syntax: strcmp(string1,string2);
Program example:
#include
#include
int main()
{
char a[10],b[10];
Int x;
printf("Enter first string");
scanf("%s",a);
printf("Enter second string");
scanf(“%s”,b);
x = strcmp(a,b);
printf("The compared value is %d”,x);
return 0;
}

Example using string functions

WAP to check whether the given string is palindrome or not [MADAM is


palindrome]
#include
#include
int main()
{ char a[10],b[10];
printf("Enter string");
scanf("%s",a);
strcpy(b,a)
strrev(a);
if(strcmp(a,b)==0)
{
printf("%s is palindrome”,b);
}
else
{
printf("%s is not palindrome”,b);
}
return 0;
}

You might also like