Sie sind auf Seite 1von 20

1.

Write a C program to reverse a given number


This program reverses a no. entered by the user. For example if a user enters a no. "12" then
on reversing no. will be "21".

C Programming Code
#include<stdio.h>
int main()
{
int num,r,reverse=0;
printf("Enter any number: ");
scanf("%d",&num);
while(num){
r=num%10;
reverse=reverse*10+r;
num=num/10;
}
printf("Reversed of number: %d",reverse);
return 0;
}

Program Output:
Enter any number: 12
Reversed of number: 21
B. Addition Of Two Numbers
In this program addition of two numbers user enetred the numeric value of two no.and the is
shown in the output

C Programming Code
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int b=20;
int c=a+b;
printf("the sum of two no is%d",c);
}

Program Output:
The sum of two no is 30
2. Write a C program to concatenate two strings.
C Programming Code
#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);
for(i=0; s1[i]!='\0'; ++i); /* i contains length of string s1. */
for(j=0; s2[j]!='\0'; ++j, ++i)
{
s1[i]=s2[j];
}
s1[i]='\0';
printf("After concatenation: %s",s1);
return 0;
}

Program Output:
Enter first string: programming
Enter second string: lab
After concatenation: programminglab
3. Write a C program to take marks of a student as input and print the
his/her grade bases on following criteria using if – else statements
Marks <40 FAIL
40<= Marks <59 GOOD
59 <= Marks < 80 Excellent
80 <= Marks Outstanding

C Programming Code
#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
clrscr();
printf("\n\tenter the marks of the student");
scanf("%d",&marks);
if(marks<40)
{
printf("\n\t\tfail!....year back");
}
else if(40<=marks&&marks>59)
{
printf("\n\t\tgood!....need more hard work");
}
else if(59<marks&&marks<80)
{
printf("\n\t\texcellent!!.....keep it up");
}
else if(80<marks)
{
printf("\n\t\toutstanding!!!.....no comments");
}
getch();
}

Program Output:
Enter the marks of the student
79
excellent!!.....keep it up
4.Perform experiment 3 using switch case statement.
C Programming Code
#include <stdio.h>
#include <conio.h>
void main()
{
int marks;
printf("Enter your marks between 0 to 100\n");
scanf("%d", &marks);
switch(marks/10)
{
case 10 :
case 9 :
printf("Your Grade : A\n" );
break;
case 8 :
case 7 :
printf("Your Grade : B\n" );
break;
case 6 :
printf("Your Grade : C\n" );
break;
case 5 :
case 4 :
printf("Your Grade : D\n" );
break;
default :
printf("You failed\n" );
}
getch();
}

Program Output:
Enter your marks between 0 to 100
55
Your Grade : D
5.Write a C program to compute the length of a string using while loop.
C Programming Code
#include<stdio.h>
int main() {
char str[100];
int length;
printf("\nEnter the String : ");
gets(str);
length = 0; // Initial Length
while (str[length] != '\0')
length++;
printf("\nLength of the String is : %d", length);
return(0);
}

Program Output:
Enter the String : pritesh
Length of the given string pritesh is : 7
6.Write a C program to convert all the lowercase letter to uppercase letter
and all uppercase letters to lower case letter given a string as input.
ASCII value of 'A' is 65 while 'a' is 97. Difference between them is 97 – 65 = 32
So if we will add 32 in the ASCII value of 'A' then it will be 'a' and if will we subtract 32 in
ASCII value of 'a' it will be 'A'. It is true for all alphabets.
In general rule:
Upper case character = Lower case character – 32
Lower case character = Upper case character + 32

C Programming Code
#include<stdio.h>
#include<string.h>
void main(){
char str[20];
int i;
printf("Enter any string:");
gets(str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
else
str[i]=str[i]+32;
}
printf("Proper capitalisation is of great importance\n");
printf("%s\n",str);
getch();
}

Program Output:
Enter any string: abcdef
Output String : ABCDEF
7.Write a C program to compute the roots of a quadratic equation.
C Programming Code
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c;
float d,root1,root2;
printf("Enter quadratic equation value a, b & c of equation ax^2+bx+c: ");
scanf("%f%f%f",&a,&b,&c);
d = b * b - 4 * a * c;
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b - sqrt(d)) / (2* a);
printf("Roots of quadratic equation are: %f , %f",root1,root2);
return 0;
}

Program Output:
Enter quadratic equation value a, b & c of equation ax^2+bx+c: 2x^2+4x+-1
Roots of quadratic equation are: 0.000, -2.000
8.Write a C program to check whether a given number is prime or not, also check
whether it is divisible by a number k or not.

C Programming Code
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i;
clrscr();
printf("*****PRIME NUMBER*****\n\n");
printf("Enter the number to find it is prime or not : ");
scanf("%d",&num);
for(i=2;i<num;i++){
if(num%i==0){
printf("\n\n%d is Not a Prime Number\n",num);
printf("It is divisible by %d, ...",i);
gotoout;
}
}
printf("\n\n%d is a prime number",num);
out:
getch();
}
Program Output:
Enter a number: 5
5 is a prime number
9.Write a C program to check whether a given year is leap year or not.
C program to check leap year: c code to check leap year, year will be entered by the user.

C Programming Code

#include<stdio.h>
void main()
{
int year;
printf("Enter any year: ");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
return 0;
}

Program Output:
Enter any year: 2010
2010 is not a leap year
10.Write a C program to take two matrixes as input and print the sum of
two matrixes.
This c program add two matrices i.e. compute the sum of two matrices and then print it.
Firstly user will be asked to enter the order of matrix (number of rows and columns) and then
two matrices. For example if the user entered order as 2, 2 i.e. two rows and two columns and
matrices as
First Matrix :-
12
34
Second matrix :-
45
-1 5
then output of the program (sum of First and Second matrix) will be
57
29

C Programming Code
#include<stdio.h>
int main() {
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;
printf("\nEnter the number of Rows of Mat1 : ");
scanf("%d", &row1);
printf("\nEnter the number of Cols of Mat1 : ");
scanf("%d", &col1);
printf("\nEnter the number of Rows of Mat2 : ");
scanf("%d", &row2);
printf("\nEnter the number of Columns of Mat2 : ");
scanf("%d", &col2);
/* Before accepting the Elements Check if no of
rows and columns of both matrices is equal */
if (row1 != row2 || col1 != col2) {
printf("\nOrder of two matrices is not same ");
exit(0);
}
//Accept the Elements in Matrix 1
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("Enter the Element a[%d][%d] : ", i, j);
scanf("%d", &mat1[i][j]);
}
}
//Accept the Elements in Matrix 2
for (i = 0; i < row2; i++)
for (j = 0; j < col2; j++) {
printf("Enter the Element b[%d][%d] : ", i, j);
scanf("%d", &mat2[i][j]);
}
//Addition of two matrices
for (i = 0; i < row1; i++)
for (j = 0; j < col1; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
//Print out the Resultant Matrix
printf("\nThe Addition of two Matrices is : \n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("%d\t", mat3[i][j]);
}
printf("\n");
}
return (0);
}

Program Output:
Enter the number of Rows of Mat1 : 3
Enter the number of Columns of Mat1 : 3

Enter the number of Rows of Mat2 : 3


Enter the number of Columns of Mat2 : 3

Enter the Element a[0][0] : 1


Enter the Element a[0][1] : 2
Enter the Element a[0][2] : 3
Enter the Element a[1][0] : 2
Enter the Element a[1][1] : 1
Enter the Element a[1][2] : 1
Enter the Element a[2][0] : 1
Enter the Element a[2][1] : 2
Enter the Element a[2][2] : 1

Enter the Element b[0][0] : 1


Enter the Element b[0][1] : 2
Enter the Element b[0][2] : 3
Enter the Element b[1][0] : 2
Enter the Element b[1][1] : 1
Enter the Element b[1][2] : 1
Enter the Element b[2][0] : 1
Enter the Element b[2][1] : 2
Enter the Element b[2][2] : 1

The Addition of two Matrices is :


246
422
242
11.Write a C program to display the address of a variable using pointer.
C Programming Code

#include<stdio.h>
#include<conio.h>
void main()
{

int n,*pt;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
pt=&n;
printf("address of variable=%u\n",pt);
printf("Value of variable=%d",*pt);
getch();
}

Program Output:

Enter a number
23
Address of variable=65524
Value of variable=23
12.Write a C program to compute the length of a string using pointer.
C Programing Code
#include<stdio.h>
#include<conio.h>
void main()
{
char s[100];
char *p;
int length;
clrscr();
printf("Enter a string\n");
scanf("%s",s);
for(p=s;*p!='\0';p++);
length=p-s;
printf("Length of string is::%d",length);
getch();
}
Program Output:
Enter a string: Programiz
Length of string: 9
13.Create a structure called STUDENT having name, registration number,
class, session as its field. Compute the size of structure STUDENT.
A structure is a user defined data type. We know that arrays can be used to represent a group
of data items that belong to the same type, such as int or float. However we cannot use an
array if we want to represent a collection of data items of different types using a single name.
A structure is a convenient tool for handling a group of logically related data items.

The syntax of structure declaration is


struct structure_name
{

type element 1;
type element 2;

……………..
type element n;
};

In this program, a structure (student) is created which contains name, roll and marks as its
data member. Then, an array of structure of 10 elements is created. Then, data(name, roll and
marks) for 10 elements is asked to user and stored in array of structure. Finally, the data
entered by user is displayed.

C Programing Code
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
}
int main()
{
struct student s[10];
int i;
printf("Enter information of students:\n");
for(i=0;i<10;++i)
{
s[i].roll=i+1;
printf("\nFor roll number %d\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying information of students:\n\n");
for(i=0;i<10;++i)
{
printf("\nInformation for roll number %d:\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
}
struct student *ptr = 0;

ptr++;
printf("Size of the structure student: %d",ptr);

return 0;
}

Program Output:
Enter information of students:

For roll number 1


Enter name: Tom
Enter marks: 98

For roll number 2


Enter name: Jerry
Enter marks: 89
.
.
.
Displaying information of students:

Information for roll number 1:


Name: Tom
Marks: 98
.
.
.

B C Programming Code

#include <stdio.h>
struct student{
char name[50];
int roll;
float marks;
};
int main(){
struct student s;
printf("Enter information of students:\n\n");
printf("Enter name: ");
scanf("%s",s.name);
printf("Enter roll number: ");
scanf("%d",&s.roll);
printf("Enter marks: ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.f\n",s.marks);
return 0;
}

Program Output:
Enter information of students:

Enter name: Adele


Enter roll number: 21
Enter marks: 334.5

Displaying Information
name: Adele
Roll: 21
Marks: 334.50
14.Write a C program to check weather a given string is palindrome or not.
A palindrome number is a number such that if we reverse it, it will not change. For example
some palindrome numbers examples are 121, 212, 12321, -454. To check whether a number
is palindrome or not first we reverse it and then compare the number obtained with the
original, if both are same then number is palindrome otherwise not. C program for
palindrome number is given below.

Palindrome number algorithm

1. Get the number from user.


2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are same then print palindrome number
5. Else print not a palindrome number.

C Programming Code
#include<string.h>
#include<stdio.h>
int main(){
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrome");
else
printf("\nThe string is a palindrome");
return 0;
}

Program Output:
Enter a string:121
The string is a palindrome
15.Write a C program to generate following patterns.

2 2

3 3 3

4 4 4 4

B B

C C C

D D D D
These program prints various different patterns of numbers and alphabets. These codes
illustrate how to create various patterns using c programming. Most of these c programs
involve usage of nested loops and space. A pattern of numbers, star or characters is a way of
arranging these in some logical manner or they may form a sequence. Some of these patterns
are triangles which have special importance in mathematics. Some patterns are symmetrical
while other are not.

C Programming Code

#include<stdio.h>
#include<conio.h>
int main()
{
int r,c,sp,n=4;
for(r=1; r<=4; r++)
{
for(sp=1; sp<=n; sp++)
printf(" ");
for(c=1; c<=r; c++)
{
printf("%d",r);
printf(" ");
}
printf("\n");
n=n-1;
}
getch();
return 0;
}

C Programming Code
#include<stdio.h>
#include<conio.h>
int main()
{
char ch,r,c;
printf("Enter last pyramid character : ");
ch=getchar();
if(ch>='a' && ch<='z')
ch=ch-32;
for(r='A'; r<=ch; r++)
{
for(c='A'; c<=r; c++)
printf("%c",r);
printf("\n");
}
return 0;
}

Das könnte Ihnen auch gefallen