0% found this document useful (0 votes)
2 views14 pages

C SamplePgms

others

Uploaded by

j24.cyn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

C SamplePgms

others

Uploaded by

j24.cyn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Print cube of given


number:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
printf("Enter the number");
scanf("%d",&number);
printf("cube of number is:
%d",number*number*number);
return 0;
}

2. Sum of 2 number:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,y,result;
printf("Enter the value x:");
scanf("%d",&x);
printf("Enter the value y:");
scanf("%d",&y);
printf("sum of two number is:
%d",x+y);
(or)
result=x+y;
printf("sum of the result is:
%d",result);
return 0;
}
3. To define constant
variable in C

int main()
{
const float PI=3.14;
printf("value is: %f\n",PI);
return 0;
}

4. To calculate area of square:


(side)

#include <stdio.h>
#include <stdlib.h>
int main()
{
int side;
printf("Enter side:");
scanf("%d",&side);

printf("area is:%d\n\
n",side*side);
return 0;
}

5. To calculate are of a circle:


(radius)

#include <stdio.h>
#include <stdlib.h>
int main()
{
float radius;
printf("Enter radius:");
scanf("%f",&radius);

printf("Radius %f:\n\n\
n",3.14*radius*radius);
return 0;
}
6. To calculate perimeter of
rectangle:

#include <stdio.h>
#include <stdlib.h>
int main()
{
float
length,width,perimeter;
printf("Enter the value of
length:");
scanf("%f",&length);
printf("Enter the value of
width:");
scanf("%f",&width);

perimeter=2*(length+width);
printf("Perimeter of
rectangle is: %f\
n",perimeter);

(or) printf("Perimeter of
rectangle is: %f\n",2*(length+width));
return 0;
}

7. Take a number(n) from user


& output its cube(n*n*n*)
#include <stdio.h>
#include <stdlib.h>
int main()
{
float n;
printf("Enter the value n:");
scanf("%f",&n);
printf("cube value of the n
is:%f",n*n*n);
return 0;
}

8. Write comments for program


a&b:

#include<stdio.h>
#include<stdlib.h>
int main()
{
// comments for a&b(single
line comment)
/* welcome to
C Programming*/
(multi line
programming)
printf(“Welcome to C
program”);
return 0;
}

9. A program to check if a
number is divisible by 2 or
not: (or) chk even or odd

#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
printf("Enter the value:");
scanf("%d",&x);
printf("resulting value is:%d\
n",x%2==0);
return 0;
}
10. Print the average of 3
numbers:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, average;
printf("Enter value a:");
scanf("%d", &a);
printf("Enter value b:");
scanf("%d",&b);
printf("Enter value c:");
scanf("%d",&c);

printf("Average is:%d\
n",a+b+c/3);
return 0;
}

11. Print 1(True) or 0(False for


following statement:
a. If its Sunday & its
snowing -> True
int main()
{
int isSunday=1;
int isSnowing=1;
printf("Result is:%d\
n",isSunday &&
isSnowing);
return 0;
}
b. If its Monday or its
raining-> True
int main()
{
int isMonday=0;
int isRaining=1;
printf("Result is:%d\
n",isMonday || isRaining);
return 0;
}
c. If a number is greater
than 9 & less than 100-
>True (2 digit number)
int main()
{
int x;
printf("Enter the value
x:");
scanf("%d",&x);
printf("Result is:%d\
n",x>9&&x<100);
return 0;
}
12. Program to check if given
character is digit or not:

int main()
{
char Ch;
printf("Enter the value:");
scanf("%c",&Ch);
if(isdigit(Ch))
{
printf("Entered character
is digit");
}
else
{
printf("Entered character
is not a digit");
}
return 0;
}
13. If-else condition:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int age;
printf("Enter the age:");
scanf("%d",&age);

if(age>18)
{printf("Major..!\n");}
else
{printf("Minor...!\n");}
printf("Thank you..");
return 0;
}

14. Sample pgm(Marks)


#include <stdio.h>
#include <stdlib.h>
int main()
{
int mark;
printf("Enter the mark(0-
100):");
scanf("%d",&mark);

if(mark>=0 && mark<=30)


{
printf("Fail\n");
}
else if(mark>=30 &&
mark<=100)
{
printf("Pass\n");
}
else
{
printf("Invalid\n");
}
return 0;
}

15. Write a program to give


Grades to a student
Marks < 30 is C
30<= marks <70 is B
70<= marks <90 is A
90<= marks <100 is A++

#include <stdio.h>
#include <stdlib.h>
int main()
{
int mark;
printf("Enter the
mark:");
scanf("%d",&mark);

if(mark>=0 &&
mark<=30)
{
printf("Grade 'C'\n\
n");
}
else if(mark>=30 &&
mark<=70)
{
printf("Grade 'B'\n\
n");
}
else if(mark>=70 &&
mark<=90)
{
printf("Grade 'A'\n");
}
else if(mark>=90 &&
mark<=100)
{
printf("Grade 'A+'");
}
else
{
printf("Invalid");
}
return 0;
}

16. Sample: will this code


Int x=2;
If(x=1)
{printf(“x is equal to 1”);}
Else
{printf(“x is equal to 1”);} a)
give error, b) print x is equal
to 1, c) print x is not equal to
1
Answer: B

17. Write a program to find if a


character entered by user is
uppercase or not:

#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
printf("Enter the
character:");
scanf("%c",&ch);

if(ch >= 'A' && ch <= 'Z')


{
printf("Its uppercase..!\
n\n");
}
else if(ch >= 'a' && ch <=
'z')
{
printf("Its Lowercase..!\
n\n");
}
else
{
printf("Invalid\n");
}
return 0;
}

18. Write a program to check if


a given number is
Armstrong number or not:

Armstrong number, means a


number which equal to the
sum of the cubes of its
individual digits.
Ex: 153= (1)3 +(5)3+(3)3;
153=1+125+27 153=153. &
More 0,1,153,370,371,407
19. Write a program to check if
the given number is a
Natural number:
(Natural number start from
1)

Natural number are the


number of sequences of
positive integers from 1 to
infinity used to count and
order.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
printf("The first 10 natural
numbers are:\n\n");
for(i=1; i<=10;i++)
{
printf("%d \t", i);
}
return 0;
}

20. For Loop Statement:


#include <stdio.h>
#include <stdlib.h>
int main()
{
for(int i=1;i<=5;i=i+1)
{
printf("Hello world\n\n");
}
return 0;
}
Ex: print 1 to 5 increment
order:
#include <stdio.h>
#include <stdlib.h>
int main()
{
for(int i=1;i<=5;i=i+1)
{
printf("%d\n",i);
}
return 0;
}
Ex: print 1 to 5 decrement
order:
for(int i=5;i>=0;i--)

21. Print the number from 0 to


10:

#include <stdio.h>
#include <stdlib.h>
int main()
{
for(int i=0;i<=10;i=i+1)
{
printf("%d \n",i);
}
return 0;
}

22. Increment operator:


Types :++i (Pre increment),
i++ (Post increment)

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1;
{
printf("%d \n",i++);//
use, then increase
printf("%d\n", i);

printf("%d \n",++i);//
increase, then use
printf("%d\n", i);

}
return 0;
}

23. Decrement operator:


Type: --i (Pre decrement),
i-- (Post decrement)

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1;
{
printf("%d \n",i--);// use,
then decrease
printf("%d\n", i);

printf("%d \n",--i);//
decrease, then use
printf("%d\n", i);

}
return 0;
}

24. While loop:


#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1;
while(i<=5)
{
//printf("%d\n",i);
printf("Hello world\n");
i++;
}
return 0;
}

25. Print the number from 0 to n,


if n is given by user: (n=4)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter the
number:");
scanf("%d",&n);
int i=0;
while(i <= n)
{

printf("%d\n",i);
i++;
}
return 0;
}

For loop:

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


{
printf("%d\n",i);
}

26. Do while loop


#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1;
do
{
printf("%d \n",i);
i++;
}while(i<=5);
return 0;
}
Print 1 to 5: upside down
(decrement)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=5;
do
{
printf("%d \n",i);
i--;
}while(i>=1);
return 0;
}
27.

You might also like