Assignment
Assignment
1) Statement:
Example:
#include<stdio.h>
int main()
{
int age;
char name[10];
printf("Enter your Name: ");
scanf("%s",name);
printf("Enter Your Age: ");
scanf("%d",&age);
if(age>=18)
{
printf("Congratulations! %s. You can vote now.\n",name);
}
else if(age>=10 && age<18)
{
printf("Sorry! %s. You can not vote right now.\n",name);
}
else
{
printf("%s, You are very young for voting.",name);
}
return 0;
}
2) Logic:
Example:
#include<stdio.h>
int main ()
{
int n1,n2;
printf("Enter Two Numbers: ");
scanf("%d%d",&n1,&n2);
(n1==n2) ? printf("Two numbers are Equal\n"):(n1>n2) ? printf("%d s Greater
than %d\n",n1,n2):printf("%d is smaller than %d",n1,n2);
return 0;
}
3) Nested Logic:
Example:
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers\n");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("%d is the Highest Number.\n",a);
}
else
{
printf("%d is the Highest Number.\n",c);
}
}
else
{
if(b>c)
{
printf("%d is the Highest Number.\n",b);
}
else
{
printf("%d is the Highest Number.\n",c);
}
}
}
4) Nested Loop:
Example:
#include<stdio.h>
int main()
{
int i,j,r,c;
printf("Enter number of rows and column:");
scanf("%d%d",&r,&c);
for(i=r;i>=1;i--)
{
for(j=c;j>=i;j--)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
5) Array|2D|3D|1D:
Example: 2
#include<stdio.h>
int main()
{
int x[3][2] = {{0,1}, {2,3}, {4,5}};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
printf("Element at x[%i][%i]: ",i, j);
printf("%d\n",x[i][j]);
}
}
return (0);
}
Example: 3D
#include <stdio.h>
int main()
{
int x[2][3][2] = { { { 0, 1 }, { 2, 3 }, { 4, 5 } },{ { 6, 7 }, { 8, 9 }, {
10, 11 } } };
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 2; ++k) {
printf("Element at x[%i][%i][%i] = %d\n", i, j, k, x[i][j][k]);
}
}
}
return 0;
}
6) Function + Recursion:
Example:
#include<stdio.h>
int sumret(int);
int main()
{
int n=1,num,res;
scanf("%d",&num);
res=sumret(num);
printf("Sum of 1 to %d = %d\n",num,res);
return 0;
}
int sumret(int x)
{
int sum;
if(x==1)
return (1);
else
sum=x+sumret(x-1);
return (sum);
}
7) Pointer:
Example:
#include <stdio.h>
int summation(int *,int *);
int main()
{
int a,b,sum;
printf("Enter Two Numbers\n");
scanf("%d%d",&a,&b);
sum = summation(&a, &b);
printf("The sum of %d and %d is %d\n",a,b,sum);
return 0;
}
int summation( int *c,int *d)
{
int sum;
sum = *c+*d;
return sum;
}
8) Structure:
Example:
#include<stdio.h>
int main()
{
struct person
{
char name[10];
int age;
};
int i;
printf("Enter 5 person Name & age\n");
struct person a[5];
for(i=0;i<5;i++)
{
scanf("%s %d",a[i].name,&a[i].age);
}
for(i=0;i<5;i++)
{
printf("%s %d\n",a[i].name,a[i].age);
}
return 0;
}
9) String:
Example:
#include<stdio.h>
int main ()
{
int i;
char name1[10],name2[10],name3[10];
printf("Enter 3 Names\n");
scanf("%s",name1);
scanf("%s",name2);
scanf("%s",name3);
printf("\nYour Entered NAmes Are:\n1.%s\n2.%s\n3.%s\n",name1,name2,name3);
return 0;
}
10) File:
Example:
#include<stdio.h>
int main()
{
FILE *file;
char ch;
printf("Input Your Data\n");
file=fopen("Data","w");
while((ch=getchar())!=EOF)
{
putc(ch,file);
}
fclose(file);
printf("\nYour Data:\n");
file=fopen("Data","r");
while((ch=getc(file))!=EOF)
{
printf("%c",ch);
}
fclose(file);
}