C Language Tutorial Beginners

C Language Tutorial Beginners

26.Write a program in C to find the sum of digit of a No entered by users.
//Enter No=52
//Output=7
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,a;
clrscr();
printf(“Enter any  Number :”);
scanf(“%d”,&n);
while(n>0)
{
a=n%10;
sum=sum+a;
n=n/10;
}
printf(“\n Sum of digit=%d”,sum);
getch();
}

27. Write a program to find in C to  design the report card of 5 subject according to the following condition if the total percentage are.
>=35   and <45     IIIrd Div
>=45   and <60     IInd Div
>=60                       Ist Div
If any students score <35 in any of the subject display fail
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c,d,e,avg;
clrscr();
printf(“Enter the five Number :”);
scanf(“%f%f%f%f%f”,&a,&b,&c,&d,&e);
avg=a+b+c+d+e/5;
if(avg>=60)
{
printf(“\n First Division”);
}
else if(avg<60 && avg>=45)
{
 printf(“\n Second Division”);
}
else if(avg<45 && avg>=35)
{
printf(“\n Third Division”);
}
else if(avg<35)
{
printf(“\n Fail” );
}
getch();
}

28. Write a Program to print the series 0,1,2,3,4,- – – – –  

#include<stdio.h>
#include<conio.h>
void main()
{
int a,i;
clrscr();
printf(“Enter any Number :”);
scanf(“%d”,&a);
for(i=0;i<=a;i++)
{
printf(“%d”,i);
printf(“,”);
}
getch();
}
29. Write a Program to print Even series.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,x;
clrscr();
printf(“Enter any Number :”);
scanf(“%d”,&n);
printf(“Even Series\n”);
for(x=1;x<=n;x++)
{
if(x%2==0)
printf(“%d, “,x);
}
getch();
}

30. Write a Program to print ODD series.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,x;
clrscr();
printf(“Enter any Number :”);
scanf(“%d”,&n);
printf(“Odd Series\n”);
for(x=1;x<=n;x++)
{
if(x%2!=0)
printf(“%d”,x);
}
getch();
}
31. Write a Program to create an array of size 10 and input element in an array and displayed it.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i;
clrscr();
for(i=0;i<10;i++)
{
printf(“Enter any Element of an Array :”);
scanf(“%d”,&a[i]);
}
for(i=0;i<10;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}

32. Write a Program to find sum of all Arrays elements.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,sum=0;
clrscr();
for(i=0;i<10;i++)
{
printf(“Enter any Element of an Array :”);
scanf(“%d”,&a[i]);
}
for(i=0;i<10;i++)
{
sum=sum+a[i];
}
printf(“\nSum=%d”,sum);
getch();
}

33. Write a Program to find multiplication of all input no. which present in Array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,mul=1;
clrscr();
for(i=1;i<=5;i++)
{
printf(“Enter any Element of an Array :”);
scanf(“%d”,&a[i]);
}
for(i=1;i<=5;i++)
{
mul=mul*a[i];
}
printf(“\nMul=%d”,mul);
getch();
}

34. Write a Program to find the minimum or maximum no. of an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,min,max;
clrscr();
for(i=0;i<10;i++)
{
printf(“Enter any Element of an Array :”);
scanf(“%d”,&a[i]);
max=a[0];
min=a[0];
}
for(i=0;i<10;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
printf(“\nMin=%d”,min);
printf(“\nMax=%d”,max);
getch();
}

35. Write a Program to find the total and average of a given no. an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
float sum=0,avg;
clrscr();
for(i=1;i<=5;i++)
{
printf(“Enter any Element of an Array :”);
scanf(“%d”,&a[i]);
}
for(i=1;i<=5;i++)
{
sum=sum+a[i];
}
avg=sum/5;
printf(“\nAverage=%.2f”,avg);
getch();
}

36. Write a Program to create 2-D array or order M*N and insert the element and display it.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3];
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“Enter any Element of an Array :”);
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”);
}
getch();
}

37. Write a Program to find the addition of two matrix of order M*N.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,x[3][3],y[3][3],z[3][3];
clrscr();
printf(“First Matrix\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&x[i][j]);
}
}
printf(“\n\n Next Matrix\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&y[i][j]);
}
}
 for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
z[i][j]=x[i][j]+y[i][j];
printf(“%d   “,z[i][j]);
}
printf(“\n”);
}
}

38. Write a Program to find multiplication of two number of order M*N.
/*Program For Multiply Matrix */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k,d=0;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“Type Elemet Ist Matrix “);
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“Type Elemet IInd Matrix “);
scanf(“%d”,&b[i][j]);
}
}
for(k=0;k<3;k++)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
d=d+(a[k][j]*b[j][i]);
}
printf(“%d\t”,d);
d=0;
}
printf(“\n”);
}
getch();
}

39. Write a Program to find the Transpose of the matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“Enter The Number :”);
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,a[j][i]);
}
printf(“\n”);
}
getch();
}

40.Write a Program to create 2-D array and calculate the sum of all digit.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,sum=0;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
 printf(“Enter The Number :”);
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+a[i][j];
}
}
printf(“\nSum=%d”,sum);
getch();
}

41.Write a Program in C that replace two or more conjunctive blank in a string a single space.
#include <stdio.h>
#include <conio.h>
void main()
{
int c,inspace=0;
clrscr();
printf(“Type Any Character\n”);
while((c = getchar()) != ‘\n’)
{
if(c == ‘ ‘)
{
if(inspace == 0)
{
inspace = 1;
putchar(c);
}
}
/* We haven’t met ‘else’ yet, so we have to be a little clumsy */
if(c != ‘ ‘)
{
inspace = 0;
putchar(c);
}
}
getch();
}

42. Write a Program to swap two number Call by Value.
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int a,b;
clrscr();
printf(“Enter Two Number :”);
scanf(“%d%d”,&a,&b);
swap(a,b);
}
void swap(int x, int y)
{
int c;
c=x;
x=y;
y=c;
printf(“a=%d\n”,x);
printf(“b=%d\n”,y);
}

43. Write a Program to swap two number using function pointers.
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a,b;
clrscr();
printf(“Enter Two Number :”);
scanf(“%d%d”,&a,&b);
swap(&a,&b);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
}
void swap(int *x, int *y)
{
int c;
c=*x;
*x=*y;
*y=c;
}

44.Write a Program to find length of using library function.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[100];
int len;
clrscr();
printf(“Enter String :”);
gets(str);
len=strlen(str);
printf(“\n Length of String %d”,str);
}

45. WAP for structure of player Name, batting average and then name.
#include<stdio.h>
#include<conio.h>
void main()
{
struct cricket
{
char playername[20];
char teamname[20];
int avg;
} information[10];
int j;
clrscr();
 for(j=0;j<3;j++)
{
printf(“Enter the record %d\n”,j+1);
printf(“Player Name “);
scanf(“%s”,information[j].playername);
printf(“Team Name “);
scanf(“%s”,information[j].teamname);
printf(“Average”);
scanf(“%d”,&information[j].avg);
}
printf(“Player Name\tTeam Name\tBatting Average\n”);
for(j=0;j<3;j++)
{
printf(“%s\t\t”,information[j].playername);
printf(“%s\t\t“,information[j].teamname);
printf(“%d\n”,information[j].avg);
}
}

46. WAP to find length of string without library function.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,count=0;
char a[50];
clrscr();
printf(“\n Enter The String “);
gets(a);
for(i=0;a[i]!=’\0′;i++)
{
count++;
}
printf(“\n Length of String %d”,count);
getch();
}

47. Write a Program to find reverse order without using library function.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str[50],revstr[50];
int i=0,j=0;
clrscr();
printf(“Enter the String to be reversed :”);
scanf(“%s”,str);
for(i=strlen(str)-1;i>=0;i–)
{
revstr[j]=str[i];
j++;
}
revstr[j]=’\0′;
printf(“Input String : %s”,str);
printf(“\nOutput String : %s”,revstr);
getch();
}

48. Write a program to reverse string using library function.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
clrscr();
printf(“\n Enter The String “);
gets(str);
strrev(str);
printf(“\n Reverse of String %s”,str);
getch();
}

49. Write a program to palindrome.
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char text[100];
int begin, middle, end, length = 0;
clrscr();
printf(“Enter Text “);
gets(text);
while ( text[length] != ‘\0’ )
length++;
end = length – 1;
middle = length/2;
for( begin = 0 ; begin < middle ; begin++ )
{
if ( text[begin] != text[end] )
{
printf(“Not a palindrome.\n”);
break;
}
end–;
}
if( begin == middle )
printf(“Palindrome.\n”);
}

50. Write a program to factorial no using function.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact;
clrscr();
printf(“\n Enter The Number “);
scanf(“%d”,&n);
fact=factorial(n);
printf(“%d”,fact);
}
int factorial(int n)
{
if(n==0)
return 1;
else
{
return(n*factorial(n-1));
}
}

51. Write a program to Fibonacci series using function.
#include<stdio.h>
#include<conio.h>
void main()
{
int count,n;
long int fibonacci(int count);
clrscr();
printf(“How Many Fibonacci number ? “);
scanf(“%d”,&n);
printf(“\n”);
for (count=1;count<=n;++count)
printf(“\ni=  =%2d  F=%ld”,count,fibonacci(count));
getch();
}
long int fibonacci(int count)
/*Calculate a Fibonacci number using the formulas
  F=1 for i<3, and F=f1+f2 for i>=3 */
{
static long int f1=1, f2=1;
long int f;
f=(count<3) ? 1 : f1 + f2;
f2=f1;
f1=f;
return(f);
}

52. Write a Program to Print month of name using switch case
#include<stdio.h>
void main()
{
int i ;
printf(“Enter the Month No.”);
scanf(“%d”,&i);
switch(i)
{
case 1:
printf(“January\n”);
break;
case 2:
printf(“Feburary\n”);
break;
case 3:
printf(“March\n”);
break;
case 4:
printf(“April\n”);
break;
case 5:
printf(“May\n”);
break;
case 6:
printf(“June\n”);
break;
case 7:
printf(“July\n”);
break;
case 8:
printf(“August\n”);
break;
case 9:
printf(“September\n”);
break;
case 10:
printf(“October\n”);
break;
case 11:
printf(“November\n”);
break;
case 12:
printf(“December\n”);
break;
default:
printf(“Invalid Month No.\n”);
}
}



C Language E-Book


Basic C Programming
Advance C Programming Part 1
Advance C Programming Part 2
CBNST
About me